summaryrefslogtreecommitdiff
path: root/internal/oauth/tokenstore.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-04-15 14:22:21 +0100
committerLibravatar GitHub <noreply@github.com>2024-04-15 14:22:21 +0100
commitf79d50b9b26590b6d2468aeb41f0846272e08d1a (patch)
tree196b9bfaf8ad6fce782c2130633cfb5c97cbd31a /internal/oauth/tokenstore.go
parent[chore] Turn `accounts-registration-open` false by default (#2839) (diff)
downloadgotosocial-f79d50b9b26590b6d2468aeb41f0846272e08d1a.tar.xz
[performance] cached oauth database types (#2838)
* update token + client code to use struct caches * add code comments * slight tweak to default mem ratios * fix envparsing * add appropriate invalidate hooks * update the tokenstore sweeping function to rely on caches * update to use PutClient() * add ClientID to list of token struct indices
Diffstat (limited to 'internal/oauth/tokenstore.go')
-rw-r--r--internal/oauth/tokenstore.go57
1 files changed, 19 insertions, 38 deletions
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}}, &gtsmodel.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}}, &gtsmodel.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}}, &gtsmodel.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 := &gtsmodel.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 := &gtsmodel.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 := &gtsmodel.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
}
/*