mirror of
https://github.com/usememos/memos.git
synced 2026-11-19 10:29:11 +01:00
fix(auth): surface PAT store failures instead of a silent 401 (#6461)
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
@@ -335,7 +338,7 @@ func TestAuthenticatorPAT(t *testing.T) {
|
||||
|
||||
authenticator := auth.NewAuthenticator(ts.Store, ts.Secret)
|
||||
_, _, err := authenticator.AuthenticateByPAT(ctx, "invalid-token-without-prefix")
|
||||
assert.Error(t, err)
|
||||
assert.ErrorIs(t, err, auth.ErrInvalidPAT)
|
||||
assert.Contains(t, err.Error(), "invalid PAT format")
|
||||
})
|
||||
|
||||
@@ -348,7 +351,13 @@ func TestAuthenticatorPAT(t *testing.T) {
|
||||
|
||||
authenticator := auth.NewAuthenticator(ts.Store, ts.Secret)
|
||||
_, _, err := authenticator.AuthenticateByPAT(ctx, token)
|
||||
assert.Error(t, err)
|
||||
assert.ErrorIs(t, err, auth.ErrInvalidPAT)
|
||||
|
||||
// A rejected token is anonymous, not an error, for every entry point.
|
||||
user, err := authenticator.AuthenticateToUser(ctx, "Bearer "+token, "")
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, user)
|
||||
assert.Nil(t, authenticator.Authenticate(ctx, "Bearer "+token))
|
||||
})
|
||||
|
||||
t.Run("fails with expired PAT", func(t *testing.T) {
|
||||
@@ -376,7 +385,7 @@ func TestAuthenticatorPAT(t *testing.T) {
|
||||
// Try to authenticate
|
||||
authenticator := auth.NewAuthenticator(ts.Store, ts.Secret)
|
||||
_, _, err = authenticator.AuthenticateByPAT(ctx, token)
|
||||
assert.Error(t, err)
|
||||
assert.ErrorIs(t, err, auth.ErrInvalidPAT)
|
||||
assert.Contains(t, err.Error(), "expired")
|
||||
})
|
||||
|
||||
@@ -443,9 +452,46 @@ func TestAuthenticatorPAT(t *testing.T) {
|
||||
// Try to authenticate
|
||||
authenticator := auth.NewAuthenticator(ts.Store, ts.Secret)
|
||||
_, _, err = authenticator.AuthenticateByPAT(ctx, token)
|
||||
assert.Error(t, err)
|
||||
assert.ErrorIs(t, err, auth.ErrInvalidPAT)
|
||||
assert.Contains(t, err.Error(), "archived")
|
||||
})
|
||||
|
||||
t.Run("reports a store failure instead of a rejected token", func(t *testing.T) {
|
||||
ts := NewTestService(t)
|
||||
defer ts.Cleanup()
|
||||
|
||||
user, err := ts.CreateRegularUser(ctx, "pat-corrupt")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Store a PAT row the server cannot decode: lastUsedAt is not a timestamp.
|
||||
token := auth.GeneratePersonalAccessToken()
|
||||
tokenHash := auth.HashPersonalAccessToken(token)
|
||||
_, err = ts.Store.GetDriver().UpsertUserSetting(ctx, &store.UserSetting{
|
||||
UserID: user.ID,
|
||||
Key: storepb.UserSetting_PERSONAL_ACCESS_TOKENS,
|
||||
Value: `{"tokens":[{"tokenId":"pat-corrupt","tokenHash":"` + tokenHash + `","lastUsedAt":""}]}`,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
authenticator := auth.NewAuthenticator(ts.Store, ts.Secret)
|
||||
|
||||
_, _, err = authenticator.AuthenticateByPAT(ctx, token)
|
||||
require.Error(t, err)
|
||||
assert.False(t, errors.Is(err, auth.ErrInvalidPAT), "a store failure must not look like a rejected token: %v", err)
|
||||
|
||||
// AuthenticateToUser propagates the failure to its caller.
|
||||
_, err = authenticator.AuthenticateToUser(ctx, "Bearer "+token, "")
|
||||
require.Error(t, err)
|
||||
|
||||
// Authenticate treats the caller as anonymous but leaves a log entry.
|
||||
var logs bytes.Buffer
|
||||
previous := slog.Default()
|
||||
slog.SetDefault(slog.New(slog.NewTextHandler(&logs, nil)))
|
||||
defer slog.SetDefault(previous)
|
||||
|
||||
assert.Nil(t, authenticator.Authenticate(ctx, "Bearer "+token))
|
||||
assert.Contains(t, logs.String(), "failed to resolve bearer token")
|
||||
})
|
||||
}
|
||||
|
||||
func TestStoreRefreshTokenMethods(t *testing.T) {
|
||||
|
||||
@@ -124,26 +124,33 @@ func (a *Authenticator) AuthenticateByRefreshToken(ctx context.Context, refreshT
|
||||
return user, claims.TokenID, nil
|
||||
}
|
||||
|
||||
// AuthenticateByPAT validates a Personal Access Token.
|
||||
// ErrInvalidPAT marks a Personal Access Token that cannot authenticate: it is
|
||||
// malformed, unknown, expired, or belongs to a missing or archived user. Any
|
||||
// other error from AuthenticateByPAT means the lookup itself failed.
|
||||
var ErrInvalidPAT = errors.New("invalid PAT")
|
||||
|
||||
// AuthenticateByPAT validates a Personal Access Token. A rejected token yields an
|
||||
// error wrapping ErrInvalidPAT; a store failure yields any other error.
|
||||
func (a *Authenticator) AuthenticateByPAT(ctx context.Context, token string) (*store.User, *storepb.PersonalAccessTokensUserSetting_PersonalAccessToken, error) {
|
||||
if !strings.HasPrefix(token, PersonalAccessTokenPrefix) {
|
||||
return nil, nil, errors.New("invalid PAT format")
|
||||
return nil, nil, errors.Wrap(ErrInvalidPAT, "invalid PAT format")
|
||||
}
|
||||
|
||||
tokenHash := HashPersonalAccessToken(token)
|
||||
result, err := a.store.GetUserByPATHash(ctx, tokenHash)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "invalid PAT")
|
||||
return nil, nil, errors.Wrap(err, "failed to look up PAT")
|
||||
}
|
||||
if result == nil {
|
||||
return nil, nil, errors.Wrap(ErrInvalidPAT, "PAT not found")
|
||||
}
|
||||
|
||||
// Check expiry
|
||||
if result.PAT.ExpiresAt != nil && result.PAT.ExpiresAt.AsTime().Before(time.Now()) {
|
||||
return nil, nil, errors.New("PAT expired")
|
||||
return nil, nil, errors.Wrap(ErrInvalidPAT, "PAT expired")
|
||||
}
|
||||
|
||||
// Check user status
|
||||
if result.User.RowStatus == store.Archived {
|
||||
return nil, nil, errors.New("user is archived")
|
||||
return nil, nil, errors.Wrap(ErrInvalidPAT, "user is archived")
|
||||
}
|
||||
|
||||
return result.User, result.PAT, nil
|
||||
@@ -182,12 +189,17 @@ func (a *Authenticator) resolveBearer(ctx context.Context, token string) (*beare
|
||||
return a.resolveAccessToken(ctx, token)
|
||||
}
|
||||
|
||||
// Personal Access Token.
|
||||
if user, pat, err := a.AuthenticateByPAT(ctx, token); err == nil && user != nil {
|
||||
a.recordPATUsage(user.ID, pat.TokenId)
|
||||
return &bearerAuth{user: user, pat: pat}, nil
|
||||
// Personal Access Token. A rejected token yields no identity and no error; a
|
||||
// store failure propagates so it is never mistaken for bad credentials.
|
||||
user, pat, err := a.AuthenticateByPAT(ctx, token)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrInvalidPAT) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
a.recordPATUsage(user.ID, pat.TokenId)
|
||||
return &bearerAuth{user: user, pat: pat}, nil
|
||||
}
|
||||
|
||||
// recordPATUsage updates a personal access token's last-used timestamp
|
||||
@@ -225,11 +237,17 @@ func (a *Authenticator) AuthenticateToUser(ctx context.Context, authHeader, cook
|
||||
|
||||
// Authenticate resolves a Bearer token (Access Token V2 or PAT) into an AuthResult,
|
||||
// returning nil when no valid credentials are present. Unlike AuthenticateToUser it
|
||||
// ignores the refresh cookie.
|
||||
// ignores the refresh cookie. A store failure also yields nil, so the caller is
|
||||
// treated as anonymous, but it is logged so operators can tell it apart from a
|
||||
// rejected token.
|
||||
func (a *Authenticator) Authenticate(ctx context.Context, authHeader string) *AuthResult {
|
||||
token := ExtractBearerToken(authHeader)
|
||||
bearer, err := a.resolveBearer(ctx, token)
|
||||
if err != nil || bearer == nil {
|
||||
if err != nil {
|
||||
slog.Error("failed to resolve bearer token", "error", err)
|
||||
return nil
|
||||
}
|
||||
if bearer == nil {
|
||||
return nil
|
||||
}
|
||||
if bearer.pat != nil {
|
||||
|
||||
@@ -2,6 +2,7 @@ package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -88,6 +89,9 @@ func (d *DB) GetUserByPATHash(ctx context.Context, tokenHash string) (*store.PAT
|
||||
|
||||
err := d.db.QueryRowContext(ctx, query, tokenHash).Scan(&userID, &tokensJSON)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -105,5 +109,5 @@ func (d *DB) GetUserByPATHash(ctx context.Context, tokenHash string) (*store.PAT
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("PAT not found")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
storepb "github.com/usememos/memos/proto/gen/store"
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
@@ -87,8 +85,9 @@ func (d *DB) DeleteUserSettings(ctx context.Context, delete *store.DeleteUserSet
|
||||
}
|
||||
|
||||
func (d *DB) GetUserByPATHash(ctx context.Context, tokenHash string) (*store.PATQueryResult, error) {
|
||||
// Simplified query: fetch all PERSONAL_ACCESS_TOKENS rows and search in Go
|
||||
// This matches SQLite/MySQL behavior and avoids PostgreSQL's strict JSONB errors
|
||||
// Fetch every PERSONAL_ACCESS_TOKENS row and match in Go. Filtering with
|
||||
// PostgreSQL's JSON operators raises errors on rows whose value is not the
|
||||
// expected shape, which would block every user's token at once.
|
||||
query := `
|
||||
SELECT
|
||||
user_id,
|
||||
@@ -109,13 +108,20 @@ func (d *DB) GetUserByPATHash(ctx context.Context, tokenHash string) (*store.PAT
|
||||
var tokensJSON string
|
||||
|
||||
if err := rows.Scan(&userID, &tokensJSON); err != nil {
|
||||
continue // Skip malformed rows
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Only a row whose raw text carries the hash can match. Skipping the rest
|
||||
// before decoding keeps one user's corrupt setting from blocking every
|
||||
// other user's token, while a corrupt row that does carry the hash surfaces
|
||||
// as an error instead of a miss.
|
||||
if !strings.Contains(tokensJSON, tokenHash) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Try to unmarshal - skip if invalid JSON
|
||||
patsUserSetting := &storepb.PersonalAccessTokensUserSetting{}
|
||||
if err := protojsonUnmarshaler.Unmarshal([]byte(tokensJSON), patsUserSetting); err != nil {
|
||||
continue // Skip invalid JSON
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Search for matching token hash
|
||||
@@ -133,5 +139,5 @@ func (d *DB) GetUserByPATHash(ctx context.Context, tokenHash string) (*store.PAT
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, errors.New("PAT not found")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -13,8 +13,9 @@ import (
|
||||
)
|
||||
|
||||
// TestGetUserByPATHashWithMissingData tests the fix for #5611 and #5612.
|
||||
// Verifies that GetUserByPATHash handles missing/malformed data gracefully
|
||||
// instead of throwing PostgreSQL JSONB errors.
|
||||
// Verifies that GetUserByPATHash treats missing data as a miss and only fails on
|
||||
// a row that carries the hash but cannot be decoded, instead of throwing
|
||||
// PostgreSQL JSONB errors.
|
||||
func TestGetUserByPATHashWithMissingData(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping PostgreSQL integration test in short mode")
|
||||
@@ -55,9 +56,8 @@ func TestGetUserByPATHashWithMissingData(t *testing.T) {
|
||||
// Test case: User has no PERSONAL_ACCESS_TOKENS key
|
||||
// This simulates fresh users or users upgraded from v0.25.3
|
||||
result, err := driver.GetUserByPATHash(ctx, "any-hash")
|
||||
assert.Error(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Contains(t, err.Error(), "PAT not found")
|
||||
})
|
||||
|
||||
t.Run("EmptyTokensArray", func(t *testing.T) {
|
||||
@@ -70,9 +70,8 @@ func TestGetUserByPATHashWithMissingData(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := driver.GetUserByPATHash(ctx, "any-hash")
|
||||
assert.Error(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Contains(t, err.Error(), "PAT not found")
|
||||
})
|
||||
|
||||
t.Run("MalformedJSON", func(t *testing.T) {
|
||||
@@ -84,11 +83,24 @@ func TestGetUserByPATHashWithMissingData(t *testing.T) {
|
||||
`, 1002, "PERSONAL_ACCESS_TOKENS", `{invalid json}`)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Should handle gracefully without crashing
|
||||
// A corrupt row that cannot carry the hash is skipped.
|
||||
result, err := driver.GetUserByPATHash(ctx, "any-hash")
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
|
||||
t.Run("CorruptRowCarryingHash", func(t *testing.T) {
|
||||
_, err := db.ExecContext(ctx, `
|
||||
INSERT INTO user_setting (user_id, key, value)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (user_id, key) DO UPDATE SET value = EXCLUDED.value
|
||||
`, 1002, "PERSONAL_ACCESS_TOKENS", `{"tokens":[{"tokenId":"pat-corrupt","tokenHash":"hash-corrupt","lastUsedAt":""}]}`)
|
||||
require.NoError(t, err)
|
||||
|
||||
// A corrupt row that does carry the hash is a lookup failure, not a miss.
|
||||
result, err := driver.GetUserByPATHash(ctx, "hash-corrupt")
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, result)
|
||||
assert.Contains(t, err.Error(), "PAT not found")
|
||||
})
|
||||
|
||||
t.Run("MissingTokensField", func(t *testing.T) {
|
||||
@@ -102,7 +114,7 @@ func TestGetUserByPATHashWithMissingData(t *testing.T) {
|
||||
|
||||
// Should handle gracefully
|
||||
result, err := driver.GetUserByPATHash(ctx, "any-hash")
|
||||
assert.Error(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, result)
|
||||
})
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -104,6 +105,9 @@ func (d *DB) GetUserByPATHash(ctx context.Context, tokenHash string) (*store.PAT
|
||||
|
||||
err := d.db.QueryRowContext(ctx, query, tokenHash).Scan(&userID, &tokensJSON)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -121,5 +125,5 @@ func (d *DB) GetUserByPATHash(ctx context.Context, tokenHash string) (*store.PAT
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("PAT not found")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -77,6 +77,9 @@ type Driver interface {
|
||||
UpsertUserSetting(ctx context.Context, upsert *UserSetting) (*UserSetting, error)
|
||||
ListUserSettings(ctx context.Context, find *FindUserSetting) ([]*UserSetting, error)
|
||||
DeleteUserSettings(ctx context.Context, delete *DeleteUserSetting) error
|
||||
// GetUserByPATHash returns the token matching tokenHash with its owner ID, or
|
||||
// (nil, nil) when no stored token matches. An error means the lookup itself
|
||||
// failed, including a stored row that cannot be decoded.
|
||||
GetUserByPATHash(ctx context.Context, tokenHash string) (*PATQueryResult, error)
|
||||
|
||||
// IdentityProvider model related methods.
|
||||
|
||||
@@ -397,7 +397,7 @@ func TestUserSettingGetUserByPATHashNotFound(t *testing.T) {
|
||||
|
||||
// Lookup non-existent PAT hash
|
||||
result, err := ts.GetUserByPATHash(ctx, "non-existent-hash")
|
||||
require.Error(t, err)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, result)
|
||||
|
||||
ts.Close()
|
||||
@@ -413,12 +413,8 @@ func TestUserSettingGetUserByPATHashNoTokensKey(t *testing.T) {
|
||||
// User exists but has no PERSONAL_ACCESS_TOKENS key at all
|
||||
// This simulates fresh users or users upgraded from v0.25.3
|
||||
result, err := ts.GetUserByPATHash(ctx, "any-hash")
|
||||
require.Error(t, err)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, result)
|
||||
// Error could be "PAT not found" (Postgres) or "sql: no rows in result set" (SQLite/MySQL)
|
||||
require.True(t,
|
||||
strings.Contains(err.Error(), "PAT not found") || strings.Contains(err.Error(), "no rows"),
|
||||
"expected PAT not found or no rows error, got: %v", err)
|
||||
|
||||
// Now add a PAT for the user
|
||||
pat := &storepb.PersonalAccessTokensUserSetting_PersonalAccessToken{
|
||||
@@ -459,12 +455,8 @@ func TestUserSettingGetUserByPATHashEmptyTokensArray(t *testing.T) {
|
||||
|
||||
// Lookup should fail gracefully, not crash
|
||||
result, err := ts.GetUserByPATHash(ctx, "any-hash")
|
||||
require.Error(t, err)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, result)
|
||||
// Error could be "PAT not found" (Postgres) or "sql: no rows in result set" (SQLite/MySQL)
|
||||
require.True(t,
|
||||
strings.Contains(err.Error(), "PAT not found") || strings.Contains(err.Error(), "no rows"),
|
||||
"expected PAT not found or no rows error, got: %v", err)
|
||||
|
||||
ts.Close()
|
||||
}
|
||||
@@ -515,7 +507,7 @@ func TestUserSettingGetUserByPATHashWithOtherUsers(t *testing.T) {
|
||||
|
||||
// Should not find non-existent hash even with mixed user states
|
||||
result, err = ts.GetUserByPATHash(ctx, "non-existent")
|
||||
require.Error(t, err)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, result)
|
||||
|
||||
ts.Close()
|
||||
@@ -694,7 +686,7 @@ func TestUserSettingGetUserByPATHashAfterRemoval(t *testing.T) {
|
||||
|
||||
// Should no longer be found
|
||||
result, err = ts.GetUserByPATHash(ctx, patHash)
|
||||
require.Error(t, err)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, result)
|
||||
|
||||
ts.Close()
|
||||
@@ -767,6 +759,49 @@ func TestUserSettingGetUserByPATHashLargeTokenCount(t *testing.T) {
|
||||
ts.Close()
|
||||
}
|
||||
|
||||
func TestUserSettingGetUserByPATHashCorruptRow(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
ts := NewTestingStore(ctx, t)
|
||||
user, err := createTestingHostUser(ctx, ts)
|
||||
require.NoError(t, err)
|
||||
other, err := createTestingUserWithRole(ctx, ts, "pat-corrupt-other", store.RoleUser)
|
||||
require.NoError(t, err)
|
||||
|
||||
// A stored row the server cannot decode (lastUsedAt is not a timestamp).
|
||||
const corruptHash = "pat-hash-corrupt"
|
||||
_, err = ts.GetDriver().UpsertUserSetting(ctx, &store.UserSetting{
|
||||
UserID: user.ID,
|
||||
Key: storepb.UserSetting_PERSONAL_ACCESS_TOKENS,
|
||||
Value: `{"tokens":[{"tokenId":"pat-corrupt","tokenHash":"` + corruptHash + `","lastUsedAt":""}]}`,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Looking up the corrupt token is a lookup failure, not a miss.
|
||||
result, err := ts.GetUserByPATHash(ctx, corruptHash)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, result)
|
||||
|
||||
// Other users' tokens still resolve.
|
||||
const otherHash = "pat-hash-other-healthy"
|
||||
err = ts.AddUserPersonalAccessToken(ctx, other.ID, &storepb.PersonalAccessTokensUserSetting_PersonalAccessToken{
|
||||
TokenId: "pat-other",
|
||||
TokenHash: otherHash,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
result, err = ts.GetUserByPATHash(ctx, otherHash)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.Equal(t, other.ID, result.UserID)
|
||||
|
||||
// An unknown hash is still a plain miss.
|
||||
result, err = ts.GetUserByPATHash(ctx, "pat-hash-unknown")
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, result)
|
||||
|
||||
ts.Close()
|
||||
}
|
||||
|
||||
func TestUserSettingMultipleSettingTypes(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -126,20 +126,24 @@ func (s *Store) DeleteUserSettings(ctx context.Context, delete *DeleteUserSettin
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserByPATHash finds a user by PAT hash.
|
||||
// GetUserByPATHash finds the user owning the personal access token with the
|
||||
// given hash. It returns (nil, nil) when no token matches or its user no longer
|
||||
// exists, and an error only when the lookup itself fails.
|
||||
func (s *Store) GetUserByPATHash(ctx context.Context, tokenHash string) (*PATQueryResult, error) {
|
||||
result, err := s.driver.GetUserByPATHash(ctx, tokenHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Fetch user info
|
||||
user, err := s.GetUser(ctx, &FindUser{ID: &result.UserID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user == nil {
|
||||
return nil, errors.New("user not found for PAT")
|
||||
return nil, nil
|
||||
}
|
||||
result.User = user
|
||||
|
||||
|
||||
Reference in New Issue
Block a user