This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
hero.go
710 lines (611 loc) · 16.2 KB
/
hero.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// Package hero is a heroic oauth2 provider.
package hero
import (
"errors"
"net/http"
"net/url"
// load mysql driver.
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
// loag postgres driver
_ "github.com/lib/pq"
)
var (
// requestType contains oauth2 request_type details.
// that is code, and token.
requestType = struct {
Code, Token string
}{"code", "token"}
// gratType contains fields that stores oauth2 grant types.
grantType = struct {
AuthorizationCode, RefreshToken string
Password, ClientCredentials string
Assertion, Implicit string
}{
"authorization_code", "refresh_token",
"password", "client_credentials",
"assertion", "__implicit",
}
// params contains varions keys used by hero
params = struct {
error string
errDesc string
errURI string
state string
grantType string
location string
clientID string
clientSecret string
accessToken string
tokenType string
expiresIn string
refreshToken string
scope string
redirectURL string
code string
assertion string
assertionType string
responseType string
}{
"error",
"error_description",
"error_uri",
"state",
"grant_type",
"Location",
"client_id",
"client_secret",
"access_token",
"token_type",
"expires_in",
"refresh_token",
"scope",
"redirect_url",
"code",
"assertion",
"assertion_type",
"response_type",
}
// registerParams contains registration parameters
registerParams = struct {
username string
password string
confirm string
email string
}{
"register_username",
"register_password",
"register_confirm",
"register_email",
}
//loginParams conains login parameters
loginParams = struct {
username string
password string
}{
"login_username",
"login_password",
}
// contextParams contains keys for context data sent to templates.
contextParams = struct {
Config string
Message string
StatusCode string
}{
"Config",
"Message",
"StatusCode",
}
)
const (
//LoginPath is the route for login.
LoginPath = "/login"
// LogoutPath is the route for logout.
LogoutPath = "/logout"
//RegisterPath is the route for registration
RegisterPath = "/register"
//ProfilePath is the route for user profile
ProfilePath = "/profile"
//ProfileUpdatePath is the route for updating user profile
ProfileUpdatePath = "/profile/update"
//ClientsPath is the route for user clients
ClientsPath = "/clients"
//HomePath is the home page route
HomePath = "/"
//StaticPath is the path for static assets.
StaticPath = "/static/"
// FlashKey is the key used to store flash messages in seesion
FlashKey = "_flash"
csrfTokenLength = 32
)
// Server is the oauth 2.0 provider.
//
// It implements http.Server interface. This uses *http.ServeMux to register
// and serve its routes.
//
// This provide both resource owner, resource server and authorization server.
type Server struct {
q *query
cfg *Config
gen TokenGenerator
view View
log Logger
store *Store
mux *mux.Router
}
//NewServer creates a new *Server.
//
//It panics if database connection cannot be established.If view is nil the default
// view is used instead, it panics when the defalut view can not be created.
//
//*Server.Init is called, meaning the returned *Server has all the http endpoints registered,
// the returned instance is ready to be hooked on *http.ListenAndServe.
func NewServer(cfg *Config, gen TokenGenerator, view View) *Server {
db, err := gorm.Open(cfg.DatabaseDialect, cfg.DatabaseConnection)
if err != nil {
panic(err)
}
q := &query{}
q.DB = db
if view == nil {
view, err = NewDefaultView(cfg.TemplatesDir, false)
if err != nil {
panic(err)
}
}
s := &Server{
q: q,
cfg: cfg,
gen: gen,
view: view,
log: NewLogger(),
mux: mux.NewRouter(),
store: DefaultStore(q.DB),
}
return s.Init()
}
// Init registers the url routes. This uses *http.ServerMux as its router.
func (s *Server) Init() *Server {
// normal stuffs
s.mux.HandleFunc(HomePath, s.Home)
s.mux.HandleFunc(RegisterPath, s.Register)
s.mux.HandleFunc(LoginPath, s.Login)
s.mux.HandleFunc(LogoutPath, s.Logout)
s.mux.HandleFunc(ProfilePath, s.Profile)
s.mux.HandleFunc(ProfileUpdatePath, s.ProfileUpdate).Methods("GET", "POST")
s.mux.HandleFunc(ClientsPath, s.Client)
// oauth stuffs
s.mux.HandleFunc(s.cfg.AuthEndpoint, s.Authorize)
s.mux.HandleFunc(s.cfg.TokenEndpoint, s.Access)
s.mux.HandleFunc(s.cfg.InfoEndpoint, s.Info)
// static stuffs
s.mux.PathPrefix(StaticPath).
Handler(http.StripPrefix(StaticPath, http.FileServer(http.Dir(s.cfg.StaticDir))))
return s
}
// Authorize provide oauth2 authorization.
func (s *Server) Authorize(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
ctx := newContext(w)
redirectURI, err := url.QueryUnescape(r.Form.Get(params.redirectURL))
if err != nil {
ctx.SetErrorState(errorsKeys.InvalidRequest, "", "")
ctx.InternalError = err
_ = ctx.CommitJSON()
return
}
state := r.Form.Get(params.state)
scope := r.Form.Get(params.scope)
clientID := r.Form.Get(params.clientID)
client, err := s.q.ClientByCode(clientID)
if err != nil {
if err.Error() == gorm.ErrRecordNotFound.Error() {
ctx.SetErrorState(errorsKeys.UnauthorizedClient, "", state)
} else {
ctx.SetErrorState(errorsKeys.ServerError, "", state)
}
ctx.InternalError = err
_ = ctx.CommitJSON()
return
}
if client.RedirectURL == "" {
ctx.SetErrorState(errorsKeys.UnauthorizedClient, "", state)
_ = ctx.CommitJSON()
return
}
if redirectURI == "" && firstURI(client.RedirectURL, s.cfg.RedirSeparator) == client.RedirectURL {
redirectURI = firstURI(client.RedirectURL, s.cfg.RedirSeparator)
}
if err = validateURIList(client.RedirectURL, redirectURI, s.cfg.RedirSeparator); err != nil {
ctx.SetErrorState(errorsKeys.InvalidRequest, "", state)
ctx.InternalError = err
_ = ctx.CommitJSON()
return
}
ctx.SetRedirect(redirectURI)
reqTyp := r.Form.Get(params.responseType)
data := make(map[string]interface{})
data["Config"] = s.cfg
var usr *User
if r.Method == "POST" {
username := r.Form.Get(loginParams.username)
password := r.Form.Get(loginParams.password)
usr = s.validUser(r, username, password)
}
// Case we can't find the user. The user-agent is served with login template
// that is used to authenticate the user. All re original query paametersare
// retained by passing them through a template context variable Action.
if usr == nil {
data["Action"] = r.URL.String()
data["Title"] = "login"
err = s.view.Render(w, s.cfg.LoginTemplate, data)
if err != nil {
s.log.Println(err)
}
return
}
switch reqTyp {
case requestType.Code:
grant := newGrant(s.gen.Generate())
grant.ExpiresIn = s.cfg.AuthorizationExpire
grant.Scope = scope
grant.State = state
grant.ClientID = client.ID
usr.Grants = append(usr.Grants, grant)
err = s.q.SaveModel(usr)
if err != nil {
ctx.SetErrorState(errorsKeys.ServerError, "", state)
ctx.InternalError = err
break
}
client.Grants = append(client.Grants, grant)
err = s.q.SaveModel(client)
if err != nil {
ctx.SetErrorState(errorsKeys.ServerError, "", state)
ctx.InternalError = err
break
}
ctx.SetData(params.code, grant.Code)
ctx.SetData(params.state, state)
case requestType.Token:
ctx.SetRedirectFragment(true)
grant := newGrant(s.gen.Generate())
grant.Type = grantType.Implicit
grant.Scope = scope
grant.State = state
grant.RedirectURL = redirectURI
grant.ClientID = client.ID
grant.UserID = usr.ID
_, err = s.finalizeAccess(&grant, ctx)
if err != nil {
ctx.SetError(errorsKeys.ServerError, "")
ctx.InternalError = err
break
}
if state != "" {
ctx.SetData(params.state, state)
}
default:
ctx.SetErrorState(errorsKeys.UnsupportedResponseType, "", state)
}
_ = ctx.CommitJSON()
}
// Access provide oauth 2.0 access. This support all grant rypes specified by RFC 6976 namely
// * Authorization code
// * Implicit
// * Resource owner password credentials
// * Client credentials
func (s Server) Access(w http.ResponseWriter, r *http.Request) {
ctx := newContext(w)
if r.Method == "GET" {
if !s.cfg.AllowGetAccess {
ctx.SetError(errorsKeys.InvalidRequest, "")
ctx.InternalError = errors.New("request must be POSt")
_ = ctx.CommitJSON()
return
}
}
if r.Method != "POST" {
ctx.SetError(errorsKeys.InvalidRequest, "")
ctx.InternalError = errors.New("request must be POSt")
_ = ctx.CommitJSON()
return
}
_ = r.ParseForm()
accessGrant := r.Form.Get(params.grantType)
redirectURI := r.Form.Get(params.redirectURL)
scope := r.Form.Get(params.scope)
code := r.Form.Get(params.code)
auth, err := getCLientAuth(r, true)
if err != nil {
ctx.SetError(errorsKeys.InvalidClient, "")
ctx.InternalError = err
_ = ctx.CommitJSON()
return
}
if s.cfg.AccessAllowed(accessGrant) {
switch accessGrant {
case grantType.AuthorizationCode:
if code == "" {
ctx.SetError(errorsKeys.InvalidGrant, "")
break
}
client := s.getClient(auth)
if client == nil {
break
}
grant, err := s.q.GrantByCLient(client, code)
if err != nil {
ctx.SetError(errorsKeys.UnauthorizedClient, "")
ctx.InternalError = err
break
}
if grant.IsExpired() {
ctx.SetError(errorsKeys.InvalidGrant, "")
break
}
if redirectURI == "" {
redirectURI = firstURI(client.RedirectURL, s.cfg.RedirSeparator)
}
if err = validateURIList(client.RedirectURL, redirectURI, s.cfg.RedirSeparator); err != nil {
ctx.SetError(errorsKeys.InvalidRequest, "")
ctx.InternalError = err
break
}
_, err = s.finalizeAccess(grant, ctx)
if err != nil {
ctx.SetError(errorsKeys.ServerError, "")
ctx.InternalError = err
break
}
case grantType.RefreshToken:
refreshToken := r.Form.Get(params.refreshToken)
if refreshToken == "" {
ctx.SetError(errorsKeys.InvalidGrant, "")
break
}
client := s.getClient(auth)
if client == nil {
break
}
grant, err := s.q.GrantByRefreshToken(refreshToken)
if err != nil {
ctx.SetError(errorsKeys.InvalidGrant, "")
ctx.InternalError = err
break
}
if grant.ClientID != client.ID {
ctx.SetError(errorsKeys.UnauthorizedClient, "")
ctx.InternalError = err
break
}
authGrant := &Grant{
Scope: scope,
RedirectURL: grant.RedirectURL,
}
if authGrant.Scope == "" {
authGrant.Scope = grant.Scope
}
if extraScopes(grant.Scope, authGrant.Scope) {
ctx.SetError(errorsKeys.AccessDenied, "")
break
}
_, err = s.finalizeAccess(grant, ctx)
if err != nil {
ctx.SetError(errorsKeys.ServerError, "")
ctx.InternalError = err
break
}
case grantType.Password:
// handle
username := r.Form.Get("username")
password := r.Form.Get("password")
if username == "" || password == "" {
ctx.SetError(errorsKeys.InvalidGrant, "")
break
}
usr := s.validUser(r, username, password)
if usr == nil {
ctx.SetError(errorsKeys.InvalidGrant, "")
break
}
client := s.getClient(auth)
if client == nil {
break
}
grant := &Grant{
Scope: scope,
UserID: usr.ID,
ClientID: client.ID,
}
_, err = s.finalizeAccess(grant, ctx)
if err != nil {
ctx.SetError(errorsKeys.ServerError, "")
ctx.InternalError = err
break
}
case grantType.ClientCredentials:
// handle
client := s.getClient(auth)
if client == nil {
break
}
grant := &Grant{
Scope: scope,
ClientID: client.ID, UserID: client.UserID,
}
_, err = s.finalizeAccess(grant, ctx)
if err != nil {
ctx.SetError(errorsKeys.ServerError, "")
ctx.InternalError = err
break
}
case grantType.Assertion:
assertionTyp := r.Form.Get(params.assertionType)
assertion := r.Form.Get(params.assertion)
if assertionTyp == "" || assertion == "" {
ctx.SetError(errorsKeys.InvalidGrant, "")
break
}
client := s.getClient(auth)
if client == nil {
break
}
redirectURI = firstURI(client.RedirectURL, s.cfg.RedirSeparator)
grant := &Grant{
Scope: scope,
RedirectURL: redirectURI,
}
_, err = s.finalizeAccess(grant, ctx)
if err != nil {
ctx.SetError(errorsKeys.ServerError, "")
ctx.InternalError = err
break
}
}
} else {
ctx.SetError(errorsKeys.UnsupportedGrantType, "")
}
_ = ctx.CommitJSON()
}
func (s *Server) getClient(auth interface{}) *Client {
switch auth.(type) {
case *basicAuth:
cAuth := auth.(*basicAuth)
client, err := s.q.ClientByCode(cAuth.UserName)
if err != nil {
return nil
}
err = compareHashedString(client.Secret, cAuth.Password)
if err != nil {
return nil
}
return client
case *bearerAuth:
// handle bearer auth
bAuth := auth.(*bearerAuth)
access, err := s.q.TokenByCode(bAuth.Code)
if err != nil {
s.log.Println(err)
return nil
}
client := &Client{}
dd := s.q.Where(&Client{ID: access.ClientID}).First(client)
if dd.Error != nil {
s.log.Println(dd.Error)
return nil
}
return client
}
return nil
}
// finalizeAccess finalizess access request by generating access token and refresh token for the access grant.
// When the access grant is saved to the database, the authorize grant is deleted.
func (s *Server) finalizeAccess(authGrant *Grant, ctx *context) (accessGrant *Grant, err error) {
accessGrant = &Grant{}
accessGrant.ClientID = authGrant.ClientID
accessGrant.UserID = authGrant.UserID
accessGrant.RedirectURL = authGrant.RedirectURL
accessGrant.Scope = authGrant.Scope
accessGrant.State = authGrant.State
accessGrant.ExpiresIn = s.cfg.AccessExpire
genAccessToken := Token{
Code: s.gen.Generate(),
ClientID: authGrant.ClientID,
UserID: authGrant.UserID,
}
if err = s.q.SaveModel(&genAccessToken); err != nil {
return nil, err
}
genRefreshToken := Token{
Code: s.gen.Generate(),
ClientID: authGrant.ClientID,
UserID: authGrant.UserID,
}
if err = s.q.SaveModel(&genRefreshToken); err != nil {
return nil, err
}
accessGrant.AccessToken = genAccessToken
accessGrant.RefreshToken = genRefreshToken
if err = s.q.SaveModel(accessGrant); err != nil {
return nil, err
}
ctx.SetData(params.accessToken, accessGrant.AccessToken.Code)
ctx.SetData(params.tokenType, s.cfg.TokenType)
ctx.SetData(params.expiresIn, accessGrant.ExpiresIn)
if accessGrant.RefreshTokenID != 0 {
ctx.SetData(params.refreshToken, accessGrant.RefreshToken.Code)
}
if accessGrant.Scope != "" {
ctx.SetData(params.scope, accessGrant.Scope)
}
if authGrant.ID != 0 {
// delete authorization
if aerr := s.q.DeleteModel(authGrant); aerr != nil {
//TODO ??
}
// // delete access tokens and refresh tokens
// if aerr := s.q.DeleteModel(authGrant.AccessToken); aerr != nil {
// //TODO ???
// }
// if aerr := s.q.DeleteModel(authGrant.RefreshToken); aerr != nil {
// //TODO ???
// }
}
return
}
// Info provide user information using Bearer token. The information served is user email, name and
// avatar_url(this is the link to the user's profile picture a.k.a avatar.
func (s *Server) Info(w http.ResponseWriter, r *http.Request) {
ctx := newContext(w)
if err := r.ParseForm(); err != nil {
ctx.SetError(errorsKeys.InvalidRequest, "")
ctx.InternalError = err
_ = ctx.CommitJSON()
return
}
bearer := checkBearerAuth(r)
if bearer == nil {
ctx.SetError(errorsKeys.InvalidRequest, "")
_ = ctx.CommitJSON()
return
}
if bearer.Code == "" {
ctx.SetError(errorsKeys.InvalidRequest, "")
_ = ctx.CommitJSON()
return
}
client := s.getClient(bearer)
if client == nil {
ctx.SetError(errorsKeys.UnauthorizedClient, "")
_ = ctx.CommitJSON()
return
}
grant, err := s.q.GrantByBearer(bearer.Code)
if err != nil {
ctx.SetError(errorsKeys.InvalidGrant, "")
_ = ctx.CommitJSON()
return
}
if grant.IsExpired() {
ctx.SetError(errorsKeys.InvalidGrant, "")
_ = ctx.CommitJSON()
return
}
user, err := s.q.UserByID(grant.UserID)
if err != nil {
ctx.SetError(errorsKeys.InvalidGrant, "")
_ = ctx.CommitJSON()
return
}
switch grant.Scope {
case "user":
ctx.SetData("email", user.Email)
ctx.SetData("avatar_url", "avatar")
ctx.SetData("name", user.UserName)
default:
ctx.SetError(errorsKeys.InvalidGrant, "")
}
_ = ctx.CommitJSON()
}