linting + organizing

This commit is contained in:
tsmethurst
2021-04-20 18:14:23 +02:00
parent 32c5fd987a
commit dafc3b5b92
60 changed files with 746 additions and 390 deletions

View File

@@ -64,6 +64,7 @@ func (cs *clientStore) Delete(ctx context.Context, id string) error {
return cs.db.DeleteByID(id, poc)
}
// Client is a handy little wrapper for typical oauth client details
type Client struct {
ID string
Secret string

View File

@@ -46,7 +46,7 @@ const (
// of a User who has successfully passed Bearer token authorization.
// The interface returned from grabbing this key should be parsed as a *gtsmodel.Account
SessionAuthorizedAccount = "authorized_account"
// SessionAuthorizedAccount is the key set in the gin context for the Application
// SessionAuthorizedApplication is the key set in the gin context for the Application
// of a Client who has successfully passed Bearer token authorization.
// The interface returned from grabbing this key should be parsed as a *gtsmodel.Application
SessionAuthorizedApplication = "authorized_app"
@@ -66,6 +66,10 @@ type s struct {
log *logrus.Logger
}
// Authed wraps an authorized token, application, user, and account.
// It is used in the functions GetAuthed and MustAuth.
// Because the user might *not* be authed, any of the fields in this struct
// might be nil, so make sure to check that when you're using this struct anywhere.
type Authed struct {
Token oauth2.TokenInfo
Application *gtsmodel.Application
@@ -208,6 +212,7 @@ func (s *s) GenerateUserAccessToken(ti oauth2.TokenInfo, clientSecret string, us
return accessToken, nil
}
// New returns a new oauth server that implements the Server interface
func New(database db.DB, log *logrus.Logger) Server {
ts := newTokenStore(context.Background(), database, log)
cs := newClientStore(database)

View File

@@ -98,7 +98,7 @@ func (pts *tokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error
if !ok {
return errors.New("info param was not a models.Token")
}
if err := pts.db.Put(OAuthTokenToPGToken(t)); err != nil {
if err := pts.db.Put(TokenToPGToken(t)); err != nil {
return fmt.Errorf("error in tokenstore create: %s", err)
}
return nil
@@ -130,7 +130,7 @@ func (pts *tokenStore) GetByCode(ctx context.Context, code string) (oauth2.Token
if err := pts.db.GetWhere("code", code, pgt); err != nil {
return nil, err
}
return PGTokenToOauthToken(pgt), nil
return TokenToOauthToken(pgt), nil
}
// GetByAccess selects a token from the DB based on the Access field
@@ -144,7 +144,7 @@ func (pts *tokenStore) GetByAccess(ctx context.Context, access string) (oauth2.T
if err := pts.db.GetWhere("access", access, pgt); err != nil {
return nil, err
}
return PGTokenToOauthToken(pgt), nil
return TokenToOauthToken(pgt), nil
}
// GetByRefresh selects a token from the DB based on the Refresh field
@@ -158,7 +158,7 @@ func (pts *tokenStore) GetByRefresh(ctx context.Context, refresh string) (oauth2
if err := pts.db.GetWhere("refresh", refresh, pgt); err != nil {
return nil, err
}
return PGTokenToOauthToken(pgt), nil
return TokenToOauthToken(pgt), nil
}
/*
@@ -194,8 +194,8 @@ type Token struct {
RefreshExpiresAt time.Time `pg:"type:timestamp"`
}
// OAuthTokenToPGToken is a lil util function that takes a gotosocial token and gives back a token for inserting into postgres
func OAuthTokenToPGToken(tkn *models.Token) *Token {
// TokenToPGToken is a lil util function that takes a gotosocial token and gives back a token for inserting into postgres
func TokenToPGToken(tkn *models.Token) *Token {
now := time.Now()
// For the following, we want to make sure we're not adding a time.Now() to an *empty* ExpiresIn, otherwise that's
@@ -236,8 +236,8 @@ func OAuthTokenToPGToken(tkn *models.Token) *Token {
}
}
// PGTokenToOauthToken is a lil util function that takes a postgres token and gives back a gotosocial token
func PGTokenToOauthToken(pgt *Token) *models.Token {
// TokenToOauthToken is a lil util function that takes a postgres token and gives back a gotosocial token
func TokenToOauthToken(pgt *Token) *models.Token {
now := time.Now()
return &models.Token{