diff options
Diffstat (limited to 'internal/oauth')
-rw-r--r-- | internal/oauth/clientstore.go | 25 | ||||
-rw-r--r-- | internal/oauth/errors.go | 6 | ||||
-rw-r--r-- | internal/oauth/server.go | 2 | ||||
-rw-r--r-- | internal/oauth/tokenstore.go | 57 |
4 files changed, 35 insertions, 55 deletions
diff --git a/internal/oauth/clientstore.go b/internal/oauth/clientstore.go index 5bb600e70..bddb30b1b 100644 --- a/internal/oauth/clientstore.go +++ b/internal/oauth/clientstore.go @@ -27,11 +27,11 @@ import ( ) type clientStore struct { - db db.Basic + db db.DB } // NewClientStore returns an implementation of the oauth2 ClientStore interface, using the given db as a storage backend. -func NewClientStore(db db.Basic) oauth2.ClientStore { +func NewClientStore(db db.DB) oauth2.ClientStore { pts := &clientStore{ db: db, } @@ -39,26 +39,27 @@ func NewClientStore(db db.Basic) oauth2.ClientStore { } func (cs *clientStore) GetByID(ctx context.Context, clientID string) (oauth2.ClientInfo, error) { - poc := >smodel.Client{} - if err := cs.db.GetByID(ctx, clientID, poc); err != nil { + client, err := cs.db.GetClientByID(ctx, clientID) + if err != nil { return nil, err } - return models.New(poc.ID, poc.Secret, poc.Domain, poc.UserID), nil + return models.New( + client.ID, + client.Secret, + client.Domain, + client.UserID, + ), nil } func (cs *clientStore) Set(ctx context.Context, id string, cli oauth2.ClientInfo) error { - poc := >smodel.Client{ + return cs.db.PutClient(ctx, >smodel.Client{ ID: cli.GetID(), Secret: cli.GetSecret(), Domain: cli.GetDomain(), UserID: cli.GetUserID(), - } - return cs.db.Put(ctx, poc) + }) } func (cs *clientStore) Delete(ctx context.Context, id string) error { - poc := >smodel.Client{ - ID: id, - } - return cs.db.DeleteByID(ctx, id, poc) + return cs.db.DeleteClientByID(ctx, id) } diff --git a/internal/oauth/errors.go b/internal/oauth/errors.go index dd61be28c..b16143e5c 100644 --- a/internal/oauth/errors.go +++ b/internal/oauth/errors.go @@ -19,7 +19,5 @@ package oauth import "github.com/superseriousbusiness/oauth2/v4/errors" -// InvalidRequest returns an oauth spec compliant 'invalid_request' error. -func InvalidRequest() error { - return errors.New("invalid_request") -} +// ErrInvalidRequest is an oauth spec compliant 'invalid_request' error. +var ErrInvalidRequest = errors.New("invalid_request") diff --git a/internal/oauth/server.go b/internal/oauth/server.go index 3e4519479..4f2ed509b 100644 --- a/internal/oauth/server.go +++ b/internal/oauth/server.go @@ -75,7 +75,7 @@ type s struct { } // New returns a new oauth server that implements the Server interface -func New(ctx context.Context, database db.Basic) Server { +func New(ctx context.Context, database db.DB) Server { ts := newTokenStore(ctx, database) cs := NewClientStore(database) diff --git a/internal/oauth/tokenstore.go b/internal/oauth/tokenstore.go index 3658f0aa9..14b91fa06 100644 --- a/internal/oauth/tokenstore.go +++ b/internal/oauth/tokenstore.go @@ -20,7 +20,6 @@ package oauth import ( "context" "errors" - "fmt" "time" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -34,14 +33,14 @@ import ( // tokenStore is an implementation of oauth2.TokenStore, which uses our db interface as a storage backend. type tokenStore struct { oauth2.TokenStore - db db.Basic + db db.DB } // newTokenStore returns a token store that satisfies the oauth2.TokenStore interface. // // In order to allow tokens to 'expire', it will also set off a goroutine that iterates through // the tokens in the DB once per minute and deletes any that have expired. -func newTokenStore(ctx context.Context, db db.Basic) oauth2.TokenStore { +func newTokenStore(ctx context.Context, db db.DB) oauth2.TokenStore { ts := &tokenStore{ db: db, } @@ -69,19 +68,19 @@ func newTokenStore(ctx context.Context, db db.Basic) oauth2.TokenStore { func (ts *tokenStore) sweep(ctx context.Context) error { // select *all* tokens from the db // todo: if this becomes expensive (ie., there are fucking LOADS of tokens) then figure out a better way. - tokens := new([]*gtsmodel.Token) - if err := ts.db.GetAll(ctx, tokens); err != nil { + tokens, err := ts.db.GetAllTokens(ctx) + if err != nil { return err } // iterate through and remove expired tokens now := time.Now() - for _, dbt := range *tokens { + for _, dbt := range tokens { // The zero value of a time.Time is 00:00 january 1 1970, which will always be before now. So: // we only want to check if a token expired before now if the expiry time is *not zero*; // ie., if it's been explicity set. if !dbt.CodeExpiresAt.IsZero() && dbt.CodeExpiresAt.Before(now) || !dbt.RefreshExpiresAt.IsZero() && dbt.RefreshExpiresAt.Before(now) || !dbt.AccessExpiresAt.IsZero() && dbt.AccessExpiresAt.Before(now) { - if err := ts.db.DeleteByID(ctx, dbt.ID, dbt); err != nil { + if err := ts.db.DeleteTokenByID(ctx, dbt.ID); err != nil { return err } } @@ -107,67 +106,49 @@ func (ts *tokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error { dbt.ID = dbtID } - if err := ts.db.Put(ctx, dbt); err != nil { - return fmt.Errorf("error in tokenstore create: %s", err) - } - return nil + return ts.db.PutToken(ctx, dbt) } // RemoveByCode deletes a token from the DB based on the Code field func (ts *tokenStore) RemoveByCode(ctx context.Context, code string) error { - return ts.db.DeleteWhere(ctx, []db.Where{{Key: "code", Value: code}}, >smodel.Token{}) + return ts.db.DeleteTokenByCode(ctx, code) } // RemoveByAccess deletes a token from the DB based on the Access field func (ts *tokenStore) RemoveByAccess(ctx context.Context, access string) error { - return ts.db.DeleteWhere(ctx, []db.Where{{Key: "access", Value: access}}, >smodel.Token{}) + return ts.db.DeleteTokenByAccess(ctx, access) } // RemoveByRefresh deletes a token from the DB based on the Refresh field func (ts *tokenStore) RemoveByRefresh(ctx context.Context, refresh string) error { - return ts.db.DeleteWhere(ctx, []db.Where{{Key: "refresh", Value: refresh}}, >smodel.Token{}) + return ts.db.DeleteTokenByRefresh(ctx, refresh) } // GetByCode selects a token from the DB based on the Code field func (ts *tokenStore) GetByCode(ctx context.Context, code string) (oauth2.TokenInfo, error) { - if code == "" { - return nil, nil - } - dbt := >smodel.Token{ - Code: code, - } - if err := ts.db.GetWhere(ctx, []db.Where{{Key: "code", Value: code}}, dbt); err != nil { + token, err := ts.db.GetTokenByCode(ctx, code) + if err != nil { return nil, err } - return DBTokenToToken(dbt), nil + return DBTokenToToken(token), nil } // GetByAccess selects a token from the DB based on the Access field func (ts *tokenStore) GetByAccess(ctx context.Context, access string) (oauth2.TokenInfo, error) { - if access == "" { - return nil, nil - } - dbt := >smodel.Token{ - Access: access, - } - if err := ts.db.GetWhere(ctx, []db.Where{{Key: "access", Value: access}}, dbt); err != nil { + token, err := ts.db.GetTokenByAccess(ctx, access) + if err != nil { return nil, err } - return DBTokenToToken(dbt), nil + return DBTokenToToken(token), nil } // GetByRefresh selects a token from the DB based on the Refresh field func (ts *tokenStore) GetByRefresh(ctx context.Context, refresh string) (oauth2.TokenInfo, error) { - if refresh == "" { - return nil, nil - } - dbt := >smodel.Token{ - Refresh: refresh, - } - if err := ts.db.GetWhere(ctx, []db.Where{{Key: "refresh", Value: refresh}}, dbt); err != nil { + token, err := ts.db.GetTokenByRefresh(ctx, refresh) + if err != nil { return nil, err } - return DBTokenToToken(dbt), nil + return DBTokenToToken(token), nil } /* |