-
Notifications
You must be signed in to change notification settings - Fork 36
/
auth.go
314 lines (263 loc) · 8.38 KB
/
auth.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package web
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
jwt "github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request"
"github.com/gorilla/sessions"
"github.com/src-d/lookout/util/ctxlog"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
)
// TODO: move/rewrite it when we have other endpoints
type successResp struct {
Data interface{} `json:"data"`
}
type errorResp struct {
Errors []error `json:"errors"`
}
func successJSON(w http.ResponseWriter, r *http.Request, data interface{}) {
b, err := json.Marshal(successResp{data})
if err != nil {
ctxlog.Get(r.Context()).Warningf(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Write(b)
}
func errorJSON(w http.ResponseWriter, r *http.Request, code int, errors ...error) {
b, err := json.Marshal(errorResp{errors})
if err != nil {
ctxlog.Get(r.Context()).Warningf(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(code)
w.Write(b)
}
// Auth is http service to authorize users, it uses oAuth and JWT underneath
type Auth struct {
config *oauth2.Config
store *sessions.CookieStore
signingKey []byte
userGetter func(client *http.Client) (*User, error)
}
// NewAuth create new Auth service
func NewAuth(clientID, clientSecret string, signingKey string) *Auth {
config := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Scopes: []string{"read:user", "read:org"},
Endpoint: github.Endpoint,
}
return &Auth{
config: config,
store: sessions.NewCookieStore([]byte(clientSecret)),
signingKey: []byte(signingKey),
userGetter: getGithubUser,
}
}
// User represents the user response returned by provider
type User struct {
ID int `json:"id"`
Login string `json:"login"`
Username string `json:"name"`
AvatarURL string `json:"avatar_url"`
}
// Login handler redirects user to oauth provider
func (a *Auth) Login(w http.ResponseWriter, r *http.Request) {
url, err := a.makeAuthURL(w, r)
if err != nil {
ctxlog.Get(r.Context()).Warningf(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
// callbackResp defines successful response of Callback endpoint
type callbackResp struct {
Token string `json:"token"`
}
// Callback makes exchange with oauth provider and redirects to index page with JWT token
func (a *Auth) Callback(w http.ResponseWriter, r *http.Request) {
state := r.URL.Query().Get("state")
if err := a.validateState(r, state); err != nil {
ctxlog.Get(r.Context()).Warningf(err.Error())
http.Error(w, "The state passed by github is incorrect or expired", http.StatusPreconditionFailed)
return
}
code := r.URL.Query().Get("code")
if code == "" {
errorText := r.URL.Query().Get("error_description")
if errorText == "" {
errorText = "OAuth provider didn't send code in callback"
}
http.Error(w, errorText, http.StatusBadRequest)
return
}
oauthToken, err := a.config.Exchange(r.Context(), code)
if err != nil {
http.Error(w, fmt.Sprintf("oauth exchange error: %s", err), http.StatusBadRequest)
return
}
user, err := a.getUser(r.Context(), oauthToken)
if err != nil {
ctxlog.Get(r.Context()).Warningf("oauth get user error: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
token, err := a.makeToken(*oauthToken, user)
if err != nil {
ctxlog.Get(r.Context()).Warningf("make jwt token error: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
successJSON(w, r, callbackResp{token})
}
// Me endpoint make request to provider and returns user details
func (a *Auth) Me(w http.ResponseWriter, r *http.Request) {
t, err := getOAuthToken(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
u, err := a.getUser(r.Context(), t)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
successJSON(w, r, u)
}
// Middleware return http.Handler which validates token and set user id in context
func (a *Auth) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var claims jwtClaim
_, err := request.ParseFromRequestWithClaims(r, extractor, &claims, func(token *jwt.Token) (interface{}, error) {
return a.signingKey, nil
})
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
ctx := context.WithValue(r.Context(), userIDKey, claims.ID)
ctx = context.WithValue(ctx, userLoginKey, claims.Login)
ctx = context.WithValue(ctx, userOAuthToken, &claims.OAuthToken)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// makeAuthURL returns string for redirect to provider
func (a *Auth) makeAuthURL(w http.ResponseWriter, r *http.Request) (string, error) {
b := make([]byte, 16)
rand.Read(b)
state := base64.URLEncoding.EncodeToString(b)
session, err := a.store.Get(r, "sess")
if err != nil {
return "", fmt.Errorf("could not save state under the current session: %s", err)
}
session.Values["state"] = state
if err := session.Save(r, w); err != nil {
return "", fmt.Errorf("could not save state under the current session: %s", err)
}
return a.config.AuthCodeURL(state), nil
}
// validateState protects the user from CSRF attacks
func (a *Auth) validateState(r *http.Request, state string) error {
session, err := a.store.Get(r, "sess")
if err != nil {
return fmt.Errorf("can't get session: %s", err)
}
expectedState := session.Values["state"]
if state != expectedState {
return fmt.Errorf("incorrect state: %s; expected: %s", state, expectedState)
}
return nil
}
// getUser gets user from provider and return user model
func (a *Auth) getUser(ctx context.Context, token *oauth2.Token) (*User, error) {
return a.userGetter(a.config.Client(ctx, token))
}
func getGithubUser(client *http.Client) (*User, error) {
resp, err := client.Get("https://api.github.com/user")
if err != nil {
return nil, fmt.Errorf("can't get user from github: %s", err)
}
defer resp.Body.Close()
var user User
err = json.NewDecoder(resp.Body).Decode(&user)
if err != nil {
return nil, fmt.Errorf("can't parse github user response: %s", err)
}
return &user, nil
}
type jwtClaim struct {
ID int
Login string
OAuthToken oauth2.Token
jwt.StandardClaims
}
// makeToken generates token string for a user
func (a *Auth) makeToken(ot oauth2.Token, user *User) (string, error) {
claims := &jwtClaim{ID: user.ID, Login: user.Login, OAuthToken: ot}
t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := t.SignedString(a.signingKey)
if err != nil {
return "", fmt.Errorf("can't sign jwt token: %s", err)
}
return ss, nil
}
// Strips 'Bearer ' prefix from bearer token string
func stripBearerPrefixFromTokenString(tok string) (string, error) {
// Should be a bearer token
if len(tok) > 6 && strings.ToUpper(tok[0:7]) == "BEARER " {
return tok[7:], nil
}
return tok, nil
}
var extractor = &request.PostExtractionFilter{
Extractor: &request.MultiExtractor{
request.HeaderExtractor{"Authorization"},
request.ArgumentExtractor{"jwt_token"},
},
Filter: stripBearerPrefixFromTokenString,
}
type userContext int
const (
_ userContext = iota
userIDKey
userLoginKey
userOAuthToken
)
// getUserInt gets the value stored in the Context for the key userIDKey, bool
// is true on success
func getUserInt(ctx context.Context) (int, bool) {
i, ok := ctx.Value(userIDKey).(int)
return i, ok
}
// GetUserID gets the user ID set by the JWT middleware in the Context
func GetUserID(ctx context.Context) (int, error) {
id, ok := getUserInt(ctx)
if !ok {
return 0, fmt.Errorf("User ID is not set in the context")
}
return id, nil
}
// GetUserLogin gets the user login set by the JWT middleware in the Context
func GetUserLogin(ctx context.Context) (string, error) {
login, ok := ctx.Value(userLoginKey).(string)
if !ok || login == "" {
return "", fmt.Errorf("user login is not set in the context")
}
return login, nil
}
func getOAuthToken(ctx context.Context) (*oauth2.Token, error) {
t, ok := ctx.Value(userOAuthToken).(*oauth2.Token)
if !ok {
return nil, fmt.Errorf("OAuth token is not set in the context")
}
return t, nil
}