Documentation
¶
Index ¶
- Constants
- Variables
- type TokenValidator
- func (t *TokenValidator) AddAccessToken(ctx context.Context, id, parentRefreshTokenID string, expiresAt time.Time) error
- func (t *TokenValidator) AddRefreshToken(ctx context.Context, id string, expiresAt time.Time) error
- func (t *TokenValidator) Connect(ctx context.Context) error
- func (t *TokenValidator) IsAccessTokenValid(ctx context.Context, id string) (bool, error)
- func (t *TokenValidator) IsRefreshTokenValid(ctx context.Context, id string) (bool, error)
- func (t *TokenValidator) IsTokenValid(ctx context.Context, token gojwttoken.Token, id string) (bool, error)
- func (t *TokenValidator) RevokeAccessToken(ctx context.Context, id string) error
- func (t *TokenValidator) RevokeAccessTokenByRefreshToken(ctx context.Context, id string) error
- func (t *TokenValidator) RevokeRefreshToken(ctx context.Context, id string) error
- func (t *TokenValidator) RevokeToken(ctx context.Context, token gojwttoken.Token, id string) error
Constants ¶
const ( // CreateRefreshTokensTableQuery is the SQL query to create the refresh_tokens table CreateRefreshTokensTableQuery = ` CREATE TABLE IF NOT EXISTS refresh_tokens (id TEXT PRIMARY KEY, expires_at DATETIME NOT NULL); ` // CreateAccessTokensTableQuery is the SQL query to create the access_tokens table CreateAccessTokensTableQuery = ` CREATE TABLE IF NOT EXISTS access_tokens (id TEXT PRIMARY KEY, parent_refresh_token_id TEXT, expires_at DATETIME NOT NULL); ` )
Variables ¶
var ( // InsertRefreshTokenQuery is the SQL query to insert a new refresh token InsertRefreshTokenQuery = ` INSERT OR IGNORE INTO refresh_tokens (id, expires_at) VALUES (?, ?); ` // DeleteRefreshTokenQuery is the SQL query to delete a refresh token DeleteRefreshTokenQuery = ` DELETE FROM refresh_tokens WHERE id = ?; ` // CheckRefreshTokenQuery is the SQL query to check if a refresh token exists CheckRefreshTokenQuery = ` SELECT COUNT(1) FROM refresh_tokens WHERE id = ? AND expires_at > CURRENT_TIMESTAMP; ` // InsertAccessTokenQuery is the SQL query to insert a new access token InsertAccessTokenQuery = ` INSERT OR IGNORE INTO access_tokens (id, parent_refresh_token_id, expires_at) VALUES (?, ?, ?); ` // DeleteAccessTokenQuery is the SQL query to delete an access token DeleteAccessTokenQuery = ` DELETE FROM access_tokens WHERE id = ?; ` // DeleteAccessTokenByRefreshTokenQuery deletes access tokens by refresh token JTI DeleteAccessTokenByRefreshTokenQuery = ` DELETE FROM access_tokens WHERE parent_refresh_token_id = ?; ` // CheckAccessTokenQuery is the SQL query to check if an access token exists CheckAccessTokenQuery = ` SELECT COUNT(1) FROM access_tokens WHERE id = ? AND expires_at > CURRENT_TIMESTAMP; ` )
Functions ¶
This section is empty.
Types ¶
type TokenValidator ¶
type TokenValidator struct {
godatabasessql.Service
// contains filtered or unexported fields
}
TokenValidator is the default implementation of the Service interface
func NewTokenValidator ¶
func NewTokenValidator( service godatabasessql.Service, logger *slog.Logger, ) (*TokenValidator, error)
NewTokenValidator creates a new TokenValidator
Parameters:
- service: the SQL connection service
- logger: the logger (optional, can be nil)
Returns:
- *TokenValidator: the TokenValidator instance
- error: an error if the data source or driver name is empty
func (*TokenValidator) AddAccessToken ¶
func (t *TokenValidator) AddAccessToken( ctx context.Context, id, parentRefreshTokenID string, expiresAt time.Time, ) error
AddAccessToken inserts an access token JTI into the database
Parameters:
- ctx: the context for the query
- id: the access token JTI to insert
- parentRefreshTokenID: the parent refresh token JTI
- expiresAt: the expiration time of the access token
Returns:
- error: an error if the insertion could not be performed
func (*TokenValidator) AddRefreshToken ¶
func (t *TokenValidator) AddRefreshToken( ctx context.Context, id string, expiresAt time.Time, ) error
AddRefreshToken inserts a refresh token JTI into the database
Parameters:
- ctx: the context for the query
- id: the refresh token JTI to insert
- expiresAt: the expiration time of the refresh token
Returns:
- error: an error if the insertion could not be performed
func (*TokenValidator) Connect ¶
func (t *TokenValidator) Connect(ctx context.Context) error
Connect opens the database connection
Parameters:
- ctx: the context
Returns:
- error: an error if the connection could not be opened
func (*TokenValidator) IsAccessTokenValid ¶
IsAccessTokenValid checks if the given access token JTI exists in the database
Parameters:
- ctx: the context for the query
- id: the access token JTI to validate
Returns:
- bool: true if the access token JTI exists, false otherwise
- error: an error if the validation could not be performed
func (*TokenValidator) IsRefreshTokenValid ¶
IsRefreshTokenValid checks if the given refresh token JTI exists in the database
Parameters:
- id: the refresh token JTI to validate
Returns:
- bool: true if the refresh token JTI exists, false otherwise
- error: an error if the validation could not be performed
func (*TokenValidator) IsTokenValid ¶
func (t *TokenValidator) IsTokenValid(ctx context.Context, token gojwttoken.Token, id string) ( bool, error, )
IsTokenValid validates the token
Parameters:
- ctx: the context for the query
- token: the token type
- id: the ID associated with the token
Returns:
- bool: true if the claims are valid, false otherwise
- error: an error if the validation could not be performed
func (*TokenValidator) RevokeAccessToken ¶
func (t *TokenValidator) RevokeAccessToken(ctx context.Context, id string) error
RevokeAccessToken revokes an access token JTI from the database
Parameters:
- ctx: the context for the query
- id: the access token JTI to revoke
Returns:
- error: an error if the revocation could not be performed
func (*TokenValidator) RevokeAccessTokenByRefreshToken ¶
func (t *TokenValidator) RevokeAccessTokenByRefreshToken(ctx context.Context, id string) error
RevokeAccessTokenByRefreshToken revokes access tokens associated with the given refresh token JTIs
Parameters:
- ctx: the context for the query
- id: the refresh token JTI whose associated access tokens are to be revoked
Returns:
- error: an error if the revocation could not be performed
func (*TokenValidator) RevokeRefreshToken ¶
func (t *TokenValidator) RevokeRefreshToken(ctx context.Context, id string) error
RevokeRefreshToken revokes a refresh token JTI from the database
Parameters:
- ctx: the context for the query
- id: the refresh token JTI to revoke
Returns:
- error: an error if the revocation could not be performed
func (*TokenValidator) RevokeToken ¶
func (t *TokenValidator) RevokeToken( ctx context.Context, token gojwttoken.Token, id string, ) error
RevokeToken revokes a token JTI from the database based on the token type
Parameters:
- ctx: the context for the query
- token: the token type (access or refresh)
- id: the token JTI to revoke
Returns:
- error: an error if the revocation could not be performed