github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/modules/oauth/store.go (about)

     1  package oauth
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  
     7  	"github.com/covergates/covergates/core"
     8  	"github.com/go-oauth2/oauth2/v4"
     9  	"github.com/go-oauth2/oauth2/v4/models"
    10  )
    11  
    12  type tokenStore struct {
    13  	oauth2.TokenStore
    14  	store core.OAuthStore
    15  }
    16  
    17  func (s *tokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error {
    18  	data, err := json.Marshal(info)
    19  	if err != nil {
    20  		return err
    21  	}
    22  	user, ok := getUser(ctx)
    23  	if !ok {
    24  		return ErrTokenOwnerNotFound
    25  	}
    26  	name, ok := getTokenName(ctx)
    27  	if !ok {
    28  		name = user.Login
    29  	}
    30  	token := &core.OAuthToken{
    31  		Name:  name,
    32  		Owner: user,
    33  		Data:  data,
    34  	}
    35  	if code := info.GetCode(); code != "" {
    36  		token.Code = code
    37  		token.Expires = info.GetCodeCreateAt().Add(info.GetCodeExpiresIn())
    38  	} else {
    39  		token.Access = info.GetAccess()
    40  		token.Expires = info.GetAccessCreateAt().Add(info.GetAccessExpiresIn())
    41  		if refresh := info.GetRefresh(); refresh != "" {
    42  			token.Refresh = refresh
    43  			token.Expires = info.GetRefreshCreateAt().Add(info.GetRefreshExpiresIn())
    44  		}
    45  	}
    46  	return s.store.Create(token)
    47  }
    48  
    49  func (s *tokenStore) RemoveByCode(ctx context.Context, code string) error {
    50  	return s.store.Delete(&core.OAuthToken{Code: code})
    51  }
    52  
    53  func (s *tokenStore) RemoveByAccess(ctx context.Context, access string) error {
    54  	return s.store.Delete(&core.OAuthToken{Access: access})
    55  }
    56  
    57  func (s *tokenStore) RemoveByRefresh(ctx context.Context, refresh string) error {
    58  	return s.store.Delete(&core.OAuthToken{Refresh: refresh})
    59  }
    60  
    61  func (s *tokenStore) toTokenInfo(token *core.OAuthToken) oauth2.TokenInfo {
    62  	var item models.Token
    63  	if err := json.Unmarshal(token.Data, &item); err != nil {
    64  		return nil
    65  	}
    66  	return &item
    67  }
    68  
    69  func (s *tokenStore) GetByCode(ctx context.Context, code string) (oauth2.TokenInfo, error) {
    70  	token, err := s.store.Find(&core.OAuthToken{Code: code})
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return s.toTokenInfo(token), nil
    75  }
    76  
    77  func (s *tokenStore) GetByAccess(ctx context.Context, access string) (oauth2.TokenInfo, error) {
    78  	token, err := s.store.Find(&core.OAuthToken{Access: access})
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	return s.toTokenInfo(token), nil
    83  }
    84  
    85  func (s *tokenStore) GetByRefresh(ctx context.Context, refresh string) (oauth2.TokenInfo, error) {
    86  	token, err := s.store.Find(&core.OAuthToken{Refresh: refresh})
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	return s.toTokenInfo(token), nil
    91  }