/
login.go
executable file
·75 lines (61 loc) · 1.65 KB
/
login.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
74
75
package githubexample
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/nu7hatch/gouuid"
"google.golang.org/appengine"
)
// change redirectURI for deployment; eg, http://<yourAppId>.appspot.com/oauth2callback
const redirectURI = "http://localhost:8080/oauth2callback"
const githubAPIURL = "https://api.github.com"
func init() {
http.HandleFunc("/", handleIndex)
http.HandleFunc("/github-login", handleGithubLogin)
http.HandleFunc("/oauth2callback", handleOauth2Callback)
}
func handleIndex(res http.ResponseWriter, req *http.Request) {
io.WriteString(res, `<!DOCTYPE html>
<html>
<head></head>
<body>
<a href="/github-login">LOGIN WITH GITHUB</a>
</body>
</html>`)
}
var githubScopes = []string{
"user:email",
"read:org",
}
func handleGithubLogin(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
// get the session
session := getSession(ctx, req)
id, _ := uuid.NewV4()
values := make(url.Values)
values.Add("client_id", "fbbaa8ce5c394b7c3198")
values.Add("redirect_uri", redirectURI)
values.Add("scope", strings.Join(githubScopes, ","))
values.Add("state", id.String())
// save the session
session.State = id.String()
putSession(ctx, res, session)
http.Redirect(res, req, fmt.Sprintf(
"https://github.com/login/oauth/authorize?%s",
values.Encode(),
), 302)
}
func handleOauth2Callback(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
// get the session
session := getSession(ctx, req)
state := req.FormValue("state")
code := req.FormValue("code")
if state != session.State {
http.Error(res, "invalid state", 401)
return
}
fmt.Fprintln(res, code)
}