-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhandlers.go
128 lines (98 loc) · 2.79 KB
/
handlers.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/gob"
"fmt"
"net/http"
"github.com/google/go-github/github"
"github.com/mitchellh/mapstructure"
"golang.org/x/oauth2"
)
const sessionStoreKey = "sess"
func init() {
gob.Register(&oauth2.Token{})
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, sessionStoreKey)
if err != nil {
fmt.Fprintln(w, err)
return
}
renderData := map[string]interface{}{}
if accessToken, ok := session.Values["githubAccessToken"].(*oauth2.Token); ok {
client := github.NewClient(oauthCfg.Client(oauth2.NoContext, accessToken))
user, _, err := client.Users.Get(context.Background(), "")
if err != nil {
fmt.Fprintln(w, err)
return
}
renderData["github_user"] = user
var userMap map[string]interface{}
mapstructure.Decode(user, &userMap)
renderData["github_user_map"] = userMap
}
tmpls["home.html"].ExecuteTemplate(w, "base", renderData)
}
func StartHandler(w http.ResponseWriter, r *http.Request) {
b := make([]byte, 16)
rand.Read(b)
state := base64.URLEncoding.EncodeToString(b)
session, _ := store.Get(r, sessionStoreKey)
session.Values["state"] = state
session.Save(r, w)
url := oauthCfg.AuthCodeURL(state)
http.Redirect(w, r, url, 302)
}
func AuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, sessionStoreKey)
if err != nil {
fmt.Fprintln(w, "aborted")
return
}
if r.URL.Query().Get("state") != session.Values["state"] {
fmt.Fprintln(w, "no state match; possible csrf OR cookies not enabled")
return
}
token, err := oauthCfg.Exchange(oauth2.NoContext, r.URL.Query().Get("code"))
if err != nil {
fmt.Fprintln(w, "there was an issue getting your token")
return
}
if !token.Valid() {
fmt.Fprintln(w, "retreived invalid token")
return
}
client := github.NewClient(oauthCfg.Client(oauth2.NoContext, token))
user, _, err := client.Users.Get(context.Background(), "")
if err != nil {
fmt.Println(w, "error getting name")
return
}
session.Values["githubUserName"] = user.Name
session.Values["githubAccessToken"] = token
session.Save(r, w)
http.Redirect(w, r, "/", 302)
}
// http://www.gorillatoolkit.org/pkg/sessions#CookieStore.MaxAge
func SessionDestroyHandler(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, sessionStoreKey)
if err != nil {
fmt.Fprintln(w, "aborted")
return
}
session.Options.MaxAge = -1
session.Save(r, w)
http.Redirect(w, r, "/", 302)
}
// func sessionGithubUser(r *http.Request) error {
// session, err := store.Get(r, "sess")
// if err != nil {
// return err
// }
// accessToken, ok := session.Values["githubAccessToken"].(*oauth2.Token); ok {
// client := github.NewClient(oauthCfg.Client(oauth2.NoContext, accessToken))
// }
// return nil
// }