/
verify.go
70 lines (63 loc) · 1.61 KB
/
verify.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
package main
import (
"log"
"net/http"
"github.com/go-martini/martini"
"golang.org/x/oauth2"
)
func initVerify() {
endpoints := map[string]oauth2.Endpoint{
"github": {
AuthURL: "https://github.com/login/oauth/authorize",
TokenURL: "https://github.com/login/oauth/access_token",
},
"google": {
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
},
}
authConfig = map[string]oauth2.Config{}
for name, ep := range endpoints {
client, ok := gConf.OAuth[name]
if ok {
authConfig[name] = oauth2.Config{
ClientID: client.ClientID,
ClientSecret: client.ClientSecret,
Endpoint: ep,
RedirectURL: "http://" + gConf.Hostname + "/verify/" + name + "/redirect",
Scopes: []string{},
}
}
}
}
var authConfig map[string]oauth2.Config
func verify(w http.ResponseWriter, r *http.Request, params martini.Params) {
name := params["type"]
cfg, ok := authConfig[name]
if !ok {
http.NotFound(w, r)
return
}
url := cfg.AuthCodeURL(randomState())
http.Redirect(w, r, url, http.StatusFound)
}
func verifyRedirect(w http.ResponseWriter, r *http.Request, params martini.Params) {
name := params["type"]
cfg, ok := authConfig[name]
if !ok {
http.NotFound(w, r)
return
}
query := r.URL.Query()
log.Println(query)
if "" != query.Get("error") {
http.Error(w, query.Get("error_description"), http.StatusBadRequest)
return
}
token, _ := cfg.Exchange(oauth2.NoContext, query.Get("code"))
http.Error(w, token.AccessToken, http.StatusAccepted)
//more todo
}
func randomState() string {
return "thecakeisalie"
}