2019-09-11 09:36:44 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package authorization
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrService is the default error class for the authorization service.
|
2021-04-28 09:06:17 +01:00
|
|
|
var ErrService = errs.Class("authorization service")
|
2019-09-11 09:36:44 +01:00
|
|
|
|
|
|
|
// Service is the authorization service.
|
|
|
|
type Service struct {
|
|
|
|
log *zap.Logger
|
|
|
|
db *DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new authorization service.
|
|
|
|
func NewService(log *zap.Logger, db *DB) *Service {
|
|
|
|
return &Service{
|
|
|
|
log: log,
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrCreate will return an authorization for the given user ID.
|
|
|
|
func (service *Service) GetOrCreate(ctx context.Context, userID string) (_ *Token, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if userID == "" {
|
|
|
|
msg := "missing user ID"
|
|
|
|
err = ErrService.New(msg)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
existingGroup, err := service.db.Get(ctx, userID)
|
2019-11-13 10:07:21 +00:00
|
|
|
if err != nil && !ErrNotFound.Has(err) {
|
2019-09-11 09:36:44 +01:00
|
|
|
msg := "error getting authorizations"
|
|
|
|
err = ErrService.Wrap(err)
|
|
|
|
service.log.Error(msg, zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingGroup != nil && len(existingGroup) > 0 {
|
2019-10-30 21:36:19 +00:00
|
|
|
for _, authorization := range existingGroup {
|
|
|
|
if authorization.Claim == nil {
|
|
|
|
return &authorization.Token, nil
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 09:36:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
createdGroup, err := service.db.Create(ctx, userID, 1)
|
|
|
|
if err != nil {
|
|
|
|
msg := "error creating authorization"
|
|
|
|
err = ErrService.Wrap(err)
|
|
|
|
service.log.Error(msg, zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
groupLen := len(createdGroup)
|
|
|
|
if groupLen != 1 {
|
|
|
|
clientMsg := "error creating authorization"
|
|
|
|
internalMsg := clientMsg + fmt.Sprintf("; expected 1, got %d", groupLen)
|
|
|
|
|
|
|
|
service.log.Error(internalMsg)
|
|
|
|
return nil, ErrEndpoint.New("%s", clientMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
authorization := createdGroup[0]
|
|
|
|
return &authorization.Token, nil
|
|
|
|
}
|