-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.go
73 lines (58 loc) · 2.04 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"html/template"
"io"
"log"
"net/http"
"net/url"
"time"
gosteamauth "github.com/TeddiO/GoSteamAuth/src"
)
func main() {
serverRouter := http.NewServeMux()
server := &http.Server{
Addr: ":8080",
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Handler: serverRouter,
}
serverRouter.HandleFunc("/", ExamplePage)
serverRouter.HandleFunc("/process", ProcessSteamLogin)
log.Fatal(server.ListenAndServe())
}
// ExamplePage is just your average default page handler. In this example
// We're just using the one liner to redirect the client and at the same time notify
// the openid provider (Steam) where to return us.
func ExamplePage(resp http.ResponseWriter, req *http.Request) {
queryString := req.URL.Query()
if queryString.Get("login") == "true" {
gosteamauth.RedirectClient(resp, req, gosteamauth.BuildQueryString("http://localhost:8080/process"))
return
}
loadingTemplate := template.New("index.html")
loadingTemplate, _ = template.ParseFiles("index.html")
if err := loadingTemplate.Execute(resp, nil); err != nil {
fmt.Println(err)
}
}
// ProcessSteamLogin is where the real magic happens in terms of validation.
// As long as isValid is true we should always be able to trust the SteamID64 returned.
func ProcessSteamLogin(resp http.ResponseWriter, req *http.Request) {
queryString, _ := url.ParseQuery(req.URL.RawQuery)
// Due to ParseQuery() returning a url.Values in form map[string][]string we're going to
// convert that data structure to map[string]string so we can validate.
queryMap := gosteamauth.ValuesToMap(queryString)
steamID64, isValid, err := gosteamauth.ValidateResponse(queryMap)
if err != nil {
fmt.Fprintf(resp, "Failed to log in\nError: %s", err)
return
}
// The below is purely for demonstrative purposes, typically you would move the
// client on away from this page, set cookies / sessions and so on.
if isValid {
fmt.Fprintf(resp, "Successfully logged in!\nSteamID: %s", steamID64)
} else {
io.WriteString(resp, "Failed to log in.")
}
}