-
Notifications
You must be signed in to change notification settings - Fork 26
/
token.go
124 lines (106 loc) · 2.34 KB
/
token.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
package beyond
import (
"encoding/json"
"flag"
"net/http"
"strings"
"time"
cache "github.com/patrickmn/go-cache"
)
var (
tokenBase = flag.String("token-base", "", "token server URL prefix (eg. https://api.github.com/user)")
tokenGQL = flag.String("token-graphql", "", "GraphQL URL for auth (eg. https://api.github.com/graphql)")
tokenGQLQ = flag.String("token-graphql-query", `{"query": "query { viewer { login }}"}`, "")
tokenCache = cache.New(10*time.Minute, 10*time.Minute)
tokenTypes = map[string]bool{
"bearer": true,
"token": true,
}
)
// {"data":{"viewer":{"login":"github[bot]"}}}
func tokenAuth(r *http.Request) string {
if *tokenBase == "" && *tokenGQL == "" {
return ""
}
u, token, ok := r.BasicAuth()
if ok && (token == "x-oauth-basic" || token == "") {
token = u
}
if token == "" {
token = r.URL.Query().Get("access_token")
}
if token == "" {
parts := strings.Split(r.Header.Get("Authorization"), " ")
if len(parts) > 1 && tokenTypes[strings.ToLower(parts[0])] {
token = parts[1]
}
}
if token == "" {
return ""
}
if v, ex := tokenCache.Get(token); ex {
if v, ok := v.(string); ok {
return v
}
}
var (
req *http.Request
err error
)
switch {
case *tokenGQL != "":
req, err = http.NewRequest("POST", *tokenGQL, strings.NewReader(*tokenGQLQ))
if err != nil {
Error(err)
return ""
}
default:
req, err = http.NewRequest("GET", *tokenBase, nil)
if err != nil {
Error(err)
return ""
}
}
req.Header.Set("Authorization", "Bearer "+token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
Error(err)
return ""
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
tokenCache.Set(token, "", cache.DefaultExpiration)
return ""
}
switch {
case *tokenGQL != "":
v := new(gqlResponse)
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
Error(err)
return ""
}
tokenCache.Set(token, v.Data.Viewer.Login, cache.DefaultExpiration)
return v.Data.Viewer.Login
default:
v := &tokenUser{}
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
Error(err)
return ""
}
tokenCache.Set(token, v.Login, cache.DefaultExpiration)
return v.Login
}
}
type tokenUser struct {
Login string
Email string
}
type gqlResponse struct {
Data struct {
Viewer struct {
Login string `json:"login"`
} `json:"viewer"`
} `json:"data"`
}