/
oauth2provider.go
462 lines (391 loc) · 12.6 KB
/
oauth2provider.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package oauth2provider
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
"github.com/credli/osin"
"github.com/gorilla/mux"
"golang.org/x/crypto/bcrypt"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
)
type AEServer struct {
sconfig *osin.ServerConfig
server *osin.Server
storage osin.Storage
}
func NewServer() *AEServer {
s := &AEServer{sconfig: osin.NewServerConfig()}
s.sconfig.AllowedAuthorizeTypes = osin.AllowedAuthorizeType{osin.CODE, osin.TOKEN}
s.sconfig.AllowedAccessTypes = osin.AllowedAccessType{
osin.AUTHORIZATION_CODE,
osin.REFRESH_TOKEN,
osin.PASSWORD,
osin.CLIENT_CREDENTIALS}
s.sconfig.AllowGetAccessRequest = true
s.sconfig.AllowClientSecretInParams = true
s.storage = NewAEStorage()
s.server = osin.NewServer(s.sconfig, s.storage)
s.server.AccessTokenGen = &AccessTokenGenJWT{privatekey, publickey}
return s
}
func init() {
server := NewServer()
r := mux.NewRouter()
r.HandleFunc("/init", server.HandleInitClient)
r.HandleFunc("/authorize", server.HandleAuthorize)
r.HandleFunc("/token", server.HandleToken)
// r.HandleFunc("/app", HandleApp)
// r.HandleFunc("/appauth/code", HandleAppAuthCode)
// r.HandleFunc("/appauth/client_credentials", HandleAppAuthClientCredentials)
r.HandleFunc("/appauth/password", server.HandleAppAuthPassword)
r.HandleFunc("/appauth/info", server.HandleInfo)
r.HandleFunc("/appauth/refresh", server.HandleAppAuthRefresh)
r.HandleFunc("/appauth/tokenInfo", server.HandleAppAuthTokenInfo)
http.Handle("/", r)
}
//HandleAuthorize handles implicit authorization
//USAGE:http://localhost:8080/authorize?response_type=token&client_id=47bef51d-1b4a-4028-b4a1-07dfe3116a9b&state=xyz&scope=everything
func (s *AEServer) HandleAuthorize(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
resp := s.server.NewResponse()
defer resp.Close()
if ar := s.server.HandleAuthorizeRequest(c, resp, r); ar != nil {
ar.Authorized = true
s.server.FinishAuthorizeRequest(c, resp, r, ar)
}
if resp.IsError && resp.InternalError != nil {
log.Errorf(c, "%v", resp.InternalError)
}
if !resp.IsError {
resp.Output["custom_parameter"] = 4567890567
}
log.Infof(c, "AUTHORIZED")
osin.OutputJSON(resp, w, r)
}
func (s *AEServer) HandleInitClient(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
clientID := "47bef51d-1b4a-4028-b4a1-07dfe3116a9b"
clientSecret := "aabbccdd"
redirectUri := "http://localhost:14000/appauth"
name := "Default Client"
active := true
s.insertClient(c, clientID, clientSecret, redirectUri, name, active)
}
func (s *AEServer) HandleToken(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
log.Infof(c, "HANDLE TOKEN")
resp := s.server.NewResponse()
defer resp.Close()
if ar := s.server.HandleAccessRequest(c, resp, r); ar != nil {
switch ar.Type {
case osin.IMPLICIT:
log.Infof(c, "Passing through IMPLICIT %v", ar)
ar.Authorized = true
case osin.AUTHORIZATION_CODE:
log.Infof(c, "Passing through CODE %v", ar)
ar.Authorized = true
case osin.REFRESH_TOKEN:
ar.Authorized = true
case osin.PASSWORD:
log.Infof(c, "VERIFYING PASSWORD")
if s.verifyPassword(c, ar.Username, ar.Password) {
ar.Authorized = true
}
case osin.CLIENT_CREDENTIALS:
ar.Authorized = true
}
s.server.FinishAccessRequest(c, resp, r, ar)
}
if resp.IsError && resp.InternalError != nil {
log.Errorf(c, "%v", resp.InternalError)
}
if !resp.IsError {
resp.Output["custom_parameter"] = 236663
}
osin.OutputJSON(resp, w, r)
}
func (s *AEServer) verifyPassword(c context.Context, username, password string) bool {
user, err := s.GetUser(c, username)
if err != nil {
log.Infof(c, "USER NOT FOUND: %v", err)
return false
}
err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password))
if err != nil {
return false
}
return true
}
//HandleInfo handles password authentication
//USAGE: http://localhost:8080/appauth/info?access_token=<access_token>
func (s *AEServer) HandleInfo(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
resp := s.server.NewResponse()
defer resp.Close()
if ir := s.server.HandleInfoRequest(c, resp, r); ir != nil {
s.server.FinishInfoRequest(c, resp, r, ir)
}
osin.OutputJSON(resp, w, r)
}
type TokenInfoRequest struct {
AccessToken string
AccessData *osin.AccessData
}
func (s *AEServer) HandleTokenInfoRequest(c context.Context, w *osin.Response, r *http.Request) *TokenInfoRequest {
r.ParseForm()
token := r.Form.Get("access_token")
if token == "" {
w.SetError(osin.E_INVALID_REQUEST, "")
return nil
}
ret := &TokenInfoRequest{
AccessToken: token,
}
var err error
ret.AccessData, err = s.storage.LoadAccess(c, ret.AccessToken)
if err != nil {
w.SetError(osin.E_INVALID_REQUEST, "")
w.InternalError = err
return nil
}
if ret.AccessData == nil {
w.SetError(osin.E_INVALID_REQUEST, "")
return nil
}
if ret.AccessData.Client.GetRedirectUri() == "" {
w.SetError(osin.E_UNAUTHORIZED_CLIENT, "")
return nil
}
if ret.AccessData.IsExpiredAt(s.server.Now()) {
w.SetError(osin.E_UNAUTHORIZED_CLIENT, "")
return nil
}
return ret
}
func (s *AEServer) FinishTokenInfoRequest(c context.Context, w *osin.Response, r *TokenInfoRequest) {
if w.IsError {
w.Output["error_description"] = w.InternalError.Error()
return
}
if aestorage, ok := s.storage.(*AEStorage); ok {
//decode from claims
u, err := aestorage.LoadUserFromAccessData(c, r.AccessData)
if err != nil {
w.IsError = true
w.InternalError = err
w.Output["error"] = err.Error()
w.Output["error_description"] = err.Error()
} else if u == nil {
w.IsError = true
w.InternalError = errors.New("User not found")
w.Output["error"] = err.Error()
w.Output["error_description"] = err.Error()
} else {
w.Output["user_id"] = u.ID
w.Output["email"] = u.Email
w.Output["verified_email"] = !u.VerifiedAt.IsZero()
w.Output["issued_to"] = r.AccessData.Client.GetId()
w.Output["audience"] = r.AccessData.Client.GetId() //TODO: works temporarily but needs to be updated for Android devices
w.Output["scope"] = r.AccessData.Scope
w.Output["expires_in"] = r.AccessData.CreatedAt.Add(time.Duration(r.AccessData.ExpiresIn)*time.Second).Sub(s.server.Now()) / time.Second
w.Output["access_type"] = "online"
}
} else {
w.IsError = true
w.InternalError = errors.New("Storage is not available")
w.Output["error"] = w.InternalError.Error()
w.Output["error_description"] = w.InternalError.Error()
}
}
//HandleAppAuthTokenInfo Called implicitly by Endpoints API to gather information about the user
//USAGE: /appauth/tokenInfo?access_token=<access_token>
func (s *AEServer) HandleAppAuthTokenInfo(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
resp := s.server.NewResponse()
defer resp.Close()
if ir := s.HandleTokenInfoRequest(c, resp, r); ir != nil {
s.FinishTokenInfoRequest(c, resp, ir)
}
osin.OutputJSON(resp, w, r)
}
func HandleAppAuthToken(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
w.Write([]byte("<html><body>"))
w.Write([]byte("APP AUTH - TOKEN<br/>"))
w.Write([]byte("Response data in fragment - not acessible via server - Nothing to do"))
w.Write([]byte("</body></html>"))
}
//HandleAppAuthPassword handles password authentication
//USAGE: http://localhost:8080/appauth?grant_type=password&scope=everything&username=test&password=test
func (s *AEServer) HandleAppAuthPassword(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
c := appengine.NewContext(r)
switch r.Method {
case "POST":
//provide tls encryption over connection
u := r.FormValue("username")
p := r.FormValue("password")
log.Infof(c, "HANDLE PASSWORD")
jr := make(map[string]interface{})
// build access code url
aurl := fmt.Sprintf("/token?grant_type=password&scope=everything&username=%s&password=%s", u, p)
err := downloadAccessToken(c, aurl, &osin.BasicAuth{
Username: "47bef51d-1b4a-4028-b4a1-07dfe3116a9b", Password: "aabbccdd"}, jr) //Password must be encrypted and stored in GCS
if err != nil {
b, err := json.Marshal(jr)
if err == nil {
fmt.Fprintf(w, "%s", b)
}
}
// show json error
if _, ok := jr["error"]; ok {
b, err := json.Marshal(jr)
if err == nil {
fmt.Fprintf(w, "%s", b)
}
}
// show json access token
if _, ok := jr["access_token"]; ok {
b, err := json.Marshal(jr)
if err == nil {
fmt.Fprintf(w, "%s", b)
}
}
default:
fmt.Fprintf(w, "{%q:%q}", "error", "Bad Request")
}
}
func HandleAppAuthClientCredentials(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
w.Write([]byte("<html><body>"))
w.Write([]byte("APP AUTH - CLIENT CREDENTIALS<br/>"))
jr := make(map[string]interface{})
// build access code url
aurl := fmt.Sprintf("http://%s/token?grant_type=client_credentials", r.Host)
// download token
c := appengine.NewContext(r)
err := downloadAccessToken(c, aurl, &osin.BasicAuth{Username: "1234", Password: "aabbccdd"}, jr)
if err != nil {
w.Write([]byte(err.Error()))
w.Write([]byte("<br/>"))
}
// show json error
if erd, ok := jr["error"]; ok {
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
}
// show json access token
if at, ok := jr["access_token"]; ok {
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
}
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
if rt, ok := jr["refresh_token"]; ok {
rurl := fmt.Sprintf("/appauth/refresh?code=%s", rt)
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Refresh Token</a><br/>", rurl)))
}
if at, ok := jr["access_token"]; ok {
rurl := fmt.Sprintf("/appauth/info?code=%s", at)
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Info</a><br/>", rurl)))
}
w.Write([]byte("</body></html>"))
}
//HandleAppAuthRefresh handles refresh
//USAGE: http://localhost:8080/appauth/refresh?grant_type=refresh_token&code=EdO7wVOtQCWIe117DiIPgg
func (s *AEServer) HandleAppAuthRefresh(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
code := r.Form.Get("code")
if code != "" {
jr := make(map[string]interface{})
// build access code url
aurl := fmt.Sprintf("/token?grant_type=refresh_token&refresh_token=%s", url.QueryEscape(code))
c := appengine.NewContext(r)
log.Infof(c, "Getting Refresh")
err := downloadAccessToken(c, aurl,
&osin.BasicAuth{Username: "47bef51d-1b4a-4028-b4a1-07dfe3116a9b", Password: "aabbccdd"}, jr)
if err != nil {
fmt.Fprintf(w, "{%q:%q}", "error", "Bad Request")
}
// show json error
if _, ok := jr["error"]; ok {
b, err := json.Marshal(jr)
if err == nil {
fmt.Fprintf(w, "%s", b)
}
}
// show json access token
if _, ok := jr["access_token"]; ok {
b, err := json.Marshal(jr)
if err == nil {
fmt.Fprintf(w, "%s", b)
}
}
} else {
fmt.Fprintf(w, "{%q:%q}", "error", "Bad Request")
}
}
func (s *AEServer) HandleAppAuthInfo(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
w.Write([]byte("<html><body>"))
w.Write([]byte("APP AUTH - INFO<br/>"))
defer w.Write([]byte("</body></html>"))
code := r.Form.Get("code")
if code == "" {
w.Write([]byte("Nothing to do"))
return
}
jr := make(map[string]interface{})
// build access code url
aurl := fmt.Sprintf("http://%s/info?code=%s", r.Host, url.QueryEscape(code))
// download token
c := appengine.NewContext(r)
//err := downloadAccessToken(c, aurl, &osin.BasicAuth{Username: "1234", Password: "aabbccdd"}, jr)
err := downloadAccessToken(c, aurl, nil, jr)
if err != nil {
w.Write([]byte(err.Error()))
w.Write([]byte("<br/>"))
}
// show json error
if erd, ok := jr["error"]; ok {
w.Write([]byte(fmt.Sprintf("ERROR: %s<br/>\n", erd)))
}
// show json access token
if at, ok := jr["access_token"]; ok {
w.Write([]byte(fmt.Sprintf("ACCESS TOKEN: %s<br/>\n", at)))
}
w.Write([]byte(fmt.Sprintf("FULL RESULT: %+v<br/>\n", jr)))
if rt, ok := jr["refresh_token"]; ok {
rurl := fmt.Sprintf("/appauth/refresh?code=%s", rt)
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Refresh Token</a><br/>", rurl)))
}
}
func downloadAccessToken(c context.Context, url string, auth *osin.BasicAuth, output map[string]interface{}) error {
if auth == nil || url == "" {
return errors.New(osin.E_INVALID_REQUEST)
}
log.Infof(c, "URL: %s", url)
// download access token
preq, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
if auth != nil {
preq.SetBasicAuth(auth.Username, auth.Password)
}
log.Infof(c, "HANDLE DOWNLOAD TOKEN")
pclient := urlfetch.Client(c)
presp, err := pclient.Do(preq)
if err != nil {
return err
}
if presp.StatusCode != 200 {
return errors.New("Invalid status code")
}
jdec := json.NewDecoder(presp.Body)
err = jdec.Decode(&output)
return err
}