-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathservice.go
761 lines (634 loc) · 21.1 KB
/
service.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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
package server
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/dghubble/gologin/v2"
"github.com/dghubble/gologin/v2/github"
oauth2login "github.com/dghubble/gologin/v2/oauth2"
"github.com/go-playground/validator/v10"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
_ "github.com/lib/pq"
"github.com/rs/zerolog"
uuid "github.com/satori/go.uuid"
"golang.org/x/oauth2"
githuboauth2 "golang.org/x/oauth2/github"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
"github.com/cosmos/atlas/config"
"github.com/cosmos/atlas/server/models"
)
const (
methodGET = "GET"
methodPOST = "POST"
methodPUT = "PUT"
methodDELETE = "DELETE"
sessionName = "atlas_session"
sessionGithubID = "github_id"
sessionUserID = "user_Id"
bearerSchema = "Bearer "
)
var (
// MaxTokens defines the maximum number of API tokens a user can create.
MaxTokens int64 = 100
)
// Service defines the encapsulating Atlas service. It wraps a router which is
// responsible for handling API requests with a given controller that interacts
// with Atlas models. The Service is responsible for establishing a database
// connection and managing session cookies.
type Service struct {
logger zerolog.Logger
cfg config.Config
db *gorm.DB
cookieCfg gologin.CookieConfig
sessionStore *sessions.CookieStore
oauth2Cfg *oauth2.Config
validate *validator.Validate
router *mux.Router
server *http.Server
}
func NewService(logger zerolog.Logger, cfg config.Config) (*Service, error) {
dbLogger := NewDBLogger(logger).LogMode(gormlogger.Silent)
cookieCfg := gologin.DefaultCookieConfig
sessionStore := sessions.NewCookieStore([]byte(cfg.String(config.FlagSessionKey)), nil)
sessionStore.Options.HttpOnly = true
sessionStore.Options.Secure = true
sessionStore.Options.MaxAge = 3600 * 24 * 7 // 1 week
if cfg.Bool(config.FlagDev) {
dbLogger = dbLogger.LogMode(gormlogger.Info)
cookieCfg = gologin.DebugOnlyCookieConfig
sessionStore.Options.Secure = false
}
db, err := gorm.Open(postgres.Open(cfg.String(config.FlagDatabaseURL)), &gorm.Config{Logger: dbLogger})
if err != nil {
return nil, err
}
service := &Service{
logger: logger.With().Str("module", "server").Logger(),
cfg: cfg,
db: db,
cookieCfg: cookieCfg,
sessionStore: sessionStore,
validate: validator.New(),
router: mux.NewRouter(),
oauth2Cfg: &oauth2.Config{
ClientID: cfg.String(config.FlagGHClientID),
ClientSecret: cfg.String(config.FlagGHClientSecret),
RedirectURL: cfg.String(config.FlagGHRedirectURL),
Endpoint: githuboauth2.Endpoint,
},
}
service.registerV1Routes()
return service, nil
}
// Start starts the atlas service as a blocking process.
func (s *Service) Start() error {
s.server = &http.Server{
Handler: s.router,
Addr: s.cfg.String(config.FlagListenAddr),
WriteTimeout: s.cfg.Duration(config.FlagHTTPReadTimeout),
ReadTimeout: s.cfg.Duration(config.FlagHTTPWriteTimeout),
}
s.logger.Info().Str("address", s.server.Addr).Msg("starting atlas server...")
return s.server.ListenAndServe()
}
// Cleanup performs server cleanup. If the internal HTTP server is non-nil, the
// server will be shutdown after a grace period deadline.
func (s *Service) Cleanup() {
if s.server != nil {
// create a deadline to wait for all existing requests to finish
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// Do not block if no connections exist, but otherwise, we will wait until
// the timeout deadline.
_ = s.server.Shutdown(ctx)
}
}
func (s *Service) registerV1Routes() {
// Create a versioned sub-router. All API routes will be mounted under this
// sub-router.
v1Router := s.router.PathPrefix("/api/v1").Subrouter()
// build middleware chain
mChain := buildMiddleware(s.logger)
// unauthenticated routes
v1Router.Handle(
"/modules/search",
mChain.ThenFunc(s.SearchModules()),
).Queries("cursor", "{cursor:[0-9]+}", "limit", "{limit:[0-9]+}", "q", "{q}").Methods(methodGET)
v1Router.Handle(
"/modules",
mChain.ThenFunc(s.GetAllModules()),
).Queries("cursor", "{cursor:[0-9]+}", "limit", "{limit:[0-9]+}").Methods(methodGET)
v1Router.Handle(
"/modules/{id:[0-9]+}",
mChain.ThenFunc(s.GetModuleByID()),
).Methods(methodGET)
v1Router.Handle(
"/modules/{id:[0-9]+}/versions",
mChain.ThenFunc(s.GetModuleVersions()),
).Methods(methodGET)
v1Router.Handle(
"/modules/{id:[0-9]+}/authors",
mChain.ThenFunc(s.GetModuleAuthors()),
).Methods(methodGET)
v1Router.Handle(
"/modules/{id:[0-9]+}/keywords",
mChain.ThenFunc(s.GetModuleKeywords()),
).Methods(methodGET)
v1Router.Handle(
"/users/{id:[0-9]+}",
mChain.ThenFunc(s.GetUserByID()),
).Methods(methodGET)
v1Router.Handle(
"/users/{id:[0-9]+}/modules",
mChain.ThenFunc(s.GetUserModules()),
).Methods(methodGET)
v1Router.Handle(
"/users",
mChain.ThenFunc(s.GetAllUsers()),
).Queries("cursor", "{cursor:[0-9]+}", "limit", "{limit:[0-9]+}").Methods(methodGET)
v1Router.Handle(
"/keywords",
mChain.ThenFunc(s.GetAllKeywords()),
).Queries("cursor", "{cursor:[0-9]+}", "limit", "{limit:[0-9]+}").Methods(methodGET)
// authenticated routes
v1Router.Handle(
"/modules",
mChain.ThenFunc(s.UpsertModule()),
).Methods(methodPUT)
v1Router.Handle(
"/user/tokens",
mChain.ThenFunc(s.CreateUserToken()),
).Methods(methodPUT)
v1Router.Handle(
"/user/tokens",
mChain.ThenFunc(s.GetUserTokens()),
).Methods(methodGET)
v1Router.Handle(
"/user/tokens/{id:[0-9]+}",
mChain.ThenFunc(s.RevokeUserToken()),
).Methods(methodDELETE)
// session routes
v1Router.Handle(
"/session/start",
mChain.Then(s.StartSession()),
).Methods(methodGET)
v1Router.Handle(
"/session/authorize",
mChain.Then(s.AuthorizeSession()),
).Methods(methodGET)
v1Router.Handle(
"/session/logout",
mChain.ThenFunc(s.LogoutSession()),
).Methods(methodPOST)
}
// UpsertModule implements a request handler to create or update a Cosmos SDK
// module.
func (s *Service) UpsertModule() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
authUser, ok, err := s.authorize(req)
if err != nil || !ok {
respondWithError(w, http.StatusUnauthorized, err)
return
}
var request ModuleRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Errorf("failed to read request: %w", err))
return
}
if err := s.validate.Struct(request); err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Errorf("invalid request: %w", transformValidationError(err)))
return
}
module := ModuleFromRequest(request)
// The publisher must already be an existing owner or must have accepted an
// invitation by an existing owner.
//
// TODO: Handle invitations to allow other users to update existing modules.
record, err := models.QueryModule(s.db, map[string]interface{}{"name": module.Name, "team": module.Team})
if err == nil {
// the module already exists so we check if the publisher is an owner
var isOwner bool
for i := 0; i < len(record.Owners) && !isOwner; i++ {
if record.Owners[i].ID == authUser.ID {
isOwner = true
}
}
if !isOwner {
respondWithError(w, http.StatusBadRequest, errors.New("publisher must be an owner of the module"))
return
}
module.Owners = record.Owners
} else {
module.Owners = []models.User{authUser}
}
module, err = module.Upsert(s.db)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
respondWithJSON(w, http.StatusOK, module)
}
}
// GetModuleByID implements a request handler to retrieve a module by ID.
func (s *Service) GetModuleByID() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
idStr := params["id"]
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Errorf("invalid module ID: %w", err))
return
}
module, err := models.GetModuleByID(s.db, uint(id))
if err != nil {
code := http.StatusInternalServerError
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
}
respondWithError(w, code, err)
return
}
respondWithJSON(w, http.StatusOK, module)
}
}
// SearchModules implements a request handler to retrieve a set of module objects
// by search criteria.
func (s *Service) SearchModules() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
cursor, limit, err := parsePagination(req)
if err != nil {
respondWithError(w, http.StatusBadRequest, err)
return
}
query := req.URL.Query().Get("q")
modules, err := models.SearchModules(s.db, query, cursor, limit)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
paginated := NewPaginationResponse(len(modules), limit, cursor, modules)
respondWithJSON(w, http.StatusOK, paginated)
}
}
// GetAllModules implements a request handler returning a paginated set of
// modules.
func (s *Service) GetAllModules() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
cursor, limit, err := parsePagination(req)
if err != nil {
respondWithError(w, http.StatusBadRequest, err)
return
}
modules, err := models.GetAllModules(s.db, cursor, limit)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
paginated := NewPaginationResponse(len(modules), limit, cursor, modules)
respondWithJSON(w, http.StatusOK, paginated)
}
}
// GetModuleVersions implements a request handler to retrieve a module's set of
// versions by ID.
func (s *Service) GetModuleVersions() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
idStr := params["id"]
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Errorf("invalid module ID: %w", err))
return
}
module, err := models.GetModuleByID(s.db, uint(id))
if err != nil {
code := http.StatusInternalServerError
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
}
respondWithError(w, code, err)
return
}
respondWithJSON(w, http.StatusOK, module.Versions)
}
}
// GetModuleAuthors implements a request handler to retrieve a module's set of
// authors by ID.
func (s *Service) GetModuleAuthors() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
idStr := params["id"]
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Errorf("invalid module ID: %w", err))
return
}
module, err := models.GetModuleByID(s.db, uint(id))
if err != nil {
code := http.StatusInternalServerError
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
}
respondWithError(w, code, err)
return
}
respondWithJSON(w, http.StatusOK, module.Authors)
}
}
// GetModuleKeywords implements a request handler to retrieve a module's set of
// keywords by ID.
func (s *Service) GetModuleKeywords() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
idStr := params["id"]
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Errorf("invalid module ID: %w", err))
return
}
module, err := models.GetModuleByID(s.db, uint(id))
if err != nil {
code := http.StatusInternalServerError
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
}
respondWithError(w, code, err)
return
}
respondWithJSON(w, http.StatusOK, module.Keywords)
}
}
// GetUserByID implements a request handler to retrieve a user by ID.
func (s *Service) GetUserByID() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
idStr := params["id"]
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Errorf("invalid user ID: %w", err))
return
}
user, err := models.GetUserByID(s.db, uint(id))
if err != nil {
code := http.StatusInternalServerError
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
}
respondWithError(w, code, err)
return
}
respondWithJSON(w, http.StatusOK, user)
}
}
// GetAllUsers implements a request handler returning a paginated set of
// users.
func (s *Service) GetAllUsers() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
cursor, limit, err := parsePagination(req)
if err != nil {
respondWithError(w, http.StatusBadRequest, err)
return
}
users, err := models.GetAllUsers(s.db, cursor, limit)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
paginated := NewPaginationResponse(len(users), limit, cursor, users)
respondWithJSON(w, http.StatusOK, paginated)
}
}
// GetUserModules implements a request handler to retrieve a set of modules
// authored by a given user by ID.
func (s *Service) GetUserModules() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
idStr := params["id"]
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Errorf("invalid user ID: %w", err))
return
}
modules, err := models.GetUserModules(s.db, uint(id))
if err != nil {
code := http.StatusInternalServerError
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
}
respondWithError(w, code, err)
return
}
respondWithJSON(w, http.StatusOK, modules)
}
}
// GetAllKeywords implements a request handler returning a paginated set of
// keywords.
func (s *Service) GetAllKeywords() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
cursor, limit, err := parsePagination(req)
if err != nil {
respondWithError(w, http.StatusBadRequest, err)
return
}
keywords, err := models.GetAllKeywords(s.db, cursor, limit)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
paginated := NewPaginationResponse(len(keywords), limit, cursor, keywords)
respondWithJSON(w, http.StatusOK, paginated)
}
}
// StartSession returns a request handler to begin a user session via Github
// OAuth authentication. The user must either grant or reject access. Upon
// granting access, Github will perform a callback where we create a session
// and obtain a token.
func (s *Service) StartSession() http.Handler {
return github.StateHandler(s.cookieCfg, github.LoginHandler(s.oauth2Cfg, nil))
}
// AuthorizeSession returns a callback request handler for Github OAuth user
// authentication. After a user grants access, this callback handler will be
// executed. A session cookie will be saved and sent to the client. A user record
// will also be upserted.
func (s *Service) AuthorizeSession() http.Handler {
return github.StateHandler(s.cookieCfg, github.CallbackHandler(s.oauth2Cfg, s.authorizeHandler(), nil))
}
func (s *Service) authorizeHandler() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
token, err := oauth2login.TokenFromContext(ctx)
if err != nil {
respondWithError(w, http.StatusInternalServerError, fmt.Errorf("failed to get github token: %w", err))
return
}
githubUser, err := github.UserFromContext(ctx)
if err != nil {
respondWithError(w, http.StatusInternalServerError, fmt.Errorf("failed to get github user: %w", err))
return
}
user := models.User{
Name: githubUser.GetLogin(),
GithubUserID: sql.NullInt64{Int64: githubUser.GetID(), Valid: true},
GravatarID: githubUser.GetGravatarID(),
AvatarURL: githubUser.GetAvatarURL(),
GithubAccessToken: sql.NullString{String: token.AccessToken, Valid: true},
}
record, err := user.Upsert(s.db)
if err != nil {
respondWithError(w, http.StatusInternalServerError, fmt.Errorf("failed to upsert user: %w", err))
return
}
session, err := s.sessionStore.Get(req, sessionName)
if err != nil {
respondWithError(w, http.StatusInternalServerError, fmt.Errorf("failed to get session: %w", err))
return
}
session.Values[sessionGithubID] = githubUser.GetID()
session.Values[sessionUserID] = record.ID
if err = session.Save(req, w); err != nil {
respondWithError(w, http.StatusInternalServerError, fmt.Errorf("failed to save session: %w", err))
return
}
http.Redirect(w, req, "/", http.StatusFound)
}
}
// LogoutSession implements a request handler to terminate and logout of an
// existing session.
func (s *Service) LogoutSession() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
session, err := s.sessionStore.Get(req, sessionName)
if err != nil {
respondWithError(w, http.StatusInternalServerError, fmt.Errorf("failed to get session: %w", err))
return
}
// Remove session keys and set max age to -1 to trigger the deletion of the
// cookie.
delete(session.Values, sessionGithubID)
delete(session.Values, sessionUserID)
session.Options.MaxAge = -1
if err = session.Save(req, w); err != nil {
respondWithError(w, http.StatusInternalServerError, fmt.Errorf("failed to save session: %w", err))
return
}
http.Redirect(w, req, "/", http.StatusFound)
}
}
// CreateUserToken implements a request handler that creates a new API token for
// the authenticated user.
func (s *Service) CreateUserToken() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
authUser, ok, err := s.authorize(req)
if err != nil || !ok {
respondWithError(w, http.StatusUnauthorized, err)
return
}
numTokens := authUser.CountTokens(s.db)
if numTokens >= MaxTokens {
respondWithError(w, http.StatusBadRequest, errors.New("maximum number of user API tokens reached"))
return
}
token, err := authUser.CreateToken(s.db)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
respondWithJSON(w, http.StatusOK, token)
}
}
// GetUserTokens implements a request handler returning all of an authenticated
// user's tokens.
func (s *Service) GetUserTokens() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
authUser, ok, err := s.authorize(req)
if err != nil || !ok {
respondWithError(w, http.StatusUnauthorized, err)
return
}
tokens, err := authUser.GetTokens(s.db)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
respondWithJSON(w, http.StatusOK, tokens)
}
}
// RevokeUserToken implements a request handler revoking a specific token from
// the authorized user.
func (s *Service) RevokeUserToken() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
authUser, ok, err := s.authorize(req)
if err != nil || !ok {
respondWithError(w, http.StatusUnauthorized, err)
return
}
params := mux.Vars(req)
idStr := params["id"]
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
respondWithError(w, http.StatusBadRequest, fmt.Errorf("invalid module ID: %w", err))
return
}
token, err := models.QueryUserToken(s.db, map[string]interface{}{"id": id, "user_id": authUser.ID, "revoked": false})
if err != nil {
code := http.StatusInternalServerError
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
}
respondWithError(w, code, err)
return
}
token, err = token.Revoke(s.db)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err)
return
}
respondWithJSON(w, http.StatusOK, token)
}
}
// authorize attempts to authorize the given request against the session cookie
// store or a bearer authorization header. If the session cookie does not exist,
// or the session has been deleted, or the supplied bearer authorization header
// is invalid, we treat the request as unauthorized and return false. Otherwise,
// we return the user record ID and true with no error to indicate successful
// authorization.
func (s *Service) authorize(req *http.Request) (models.User, bool, error) {
session, err := s.sessionStore.Get(req, sessionName)
if err != nil {
return models.User{}, false, fmt.Errorf("failed to get session: %w", err)
}
var userID uint
// check for a valid session cookie or bearer authorization header
if v, ok := session.Values[sessionUserID]; ok {
userID = v.(uint)
} else if h := req.Header.Get("Authorization"); strings.HasPrefix(h, bearerSchema) {
tokenStr := h[len(bearerSchema):]
tokenUUID, err := uuid.FromString(tokenStr)
if err != nil {
return models.User{}, false, fmt.Errorf("failed to get parse token: %w", err)
}
token, err := models.QueryUserToken(s.db, map[string]interface{}{"token": tokenUUID.String(), "revoked": false})
if err != nil {
return models.User{}, false, err
}
token, err = token.IncrCount(s.db)
if err != nil {
return models.User{}, false, err
}
userID = token.UserID
} else {
return models.User{}, false, errors.New("unauthorized")
}
user, err := models.GetUserByID(s.db, userID)
if err != nil {
return models.User{}, false, err
}
return user, true, nil
}