Documentation
¶
Overview ¶
AuthService provides authentication-related services such as user registration, login, logout, token refresh, OAuth authentication, session management, and user blocking.
The AuthService struct contains the following fields: - log: Logger for logging purposes. - userProvider: Interface for user-related operations. - appProvider: Interface for application-related operations. - sessionProvider: Interface for session-related operations. - accessTokenTTL: Time-to-live duration for access tokens. - refreshTokenTTL: Time-to-live duration for refresh tokens.
The AuthService struct provides the following methods: - New: Creates a new instance of the AuthService. - Register: Registers a new user with the provided email, username, and password. - Login: Authenticates a user with the provided email, password, appID, userIP, and userAgent, and returns the user and login response. - Logout: Logs out a user by revoking the session associated with the provided sessionID. - RefreshToken: Refreshes the access token using the provided refresh token, IP, and agent, and returns the new access token. - OAuth: Initiates OAuth authentication with the specified provider and appID, and returns the authentication URL. - OAuthCallback: Handles the OAuth callback, completes the authentication process, and returns the user and login response. - Sessions: Retrieves the active sessions for the user associated with the provided access token and appID. - RevokeSession: Revokes a specific session for the user associated with the provided access token, appID, and sessionID. - RevokeAppSession: Revokes all sessions for a specific application for the user associated with the provided access token and appID. - RevokeAllSession: Revokes all sessions for the user associated with the provided access token and appID. - BlockUser: Blocks a user with the specified email and username, and revokes all active sessions for the user.
The AuthService struct also defines various error variables for common authentication-related errors.
Index ¶
- Variables
- type AppProvider
- type AuthService
- func (a *AuthService) BlockUser(ctx context.Context, appID int32, accessToken, email string) error
- func (a *AuthService) Login(ctx context.Context, email, password string, appID int32, ...) (user models.User, resp models.LoginResponse, err error)
- func (a *AuthService) Logout(ctx context.Context, sessionID string) error
- func (a *AuthService) OAuth(ctx context.Context, providerName string, appID int32) (authURL string, err error)
- func (a *AuthService) OAuthCallback(ctx context.Context, providerName, code, state, userIP, userAgent string) (user models.User, resp models.LoginResponse, err error)
- func (a *AuthService) RefreshToken(ctx context.Context, refreshToken, ip, agent string) (models.RefreshTokenResponse, error)
- func (a *AuthService) Register(ctx context.Context, email, username, password string) (err error)
- func (a *AuthService) RevokeAllSession(ctx context.Context, accessToken string, appID int32) error
- func (a *AuthService) RevokeAppSession(ctx context.Context, accessToken string, appID, targetAppID int32) error
- func (a *AuthService) RevokeSession(ctx context.Context, accessToken string, appID int32, sessionID string) error
- func (a *AuthService) Sessions(ctx context.Context, accessToken string, appID int32) ([]models.SessionResponce, error)
- type SessionProvider
- type UserProvider
Constants ¶
This section is empty.
Variables ¶
var ( ErrEmailAlreadyExists = errors.New("the specified email is already registered") ErrUsernameAlreadyExists = errors.New("the specified username is already taken") ErrInvalidCredentials = errors.New("invalid credentials") ErrUserBlocked = errors.New("the user account has been blocked by the administration") ErrSessionRevoked = errors.New("session revoked or invalid") ErrSessionNotFound = errors.New("session not found") ErrInvalidToken = errors.New("invalid token, please log in again") ErrUserNotFound = errors.New("user not found") ErrProviderNotSupported = errors.New("authentication provider not supported") ErrLocalAccountExists = errors.New("a local account is already registered with this email") ErrNotAdmin = errors.New("the user does not have sufficient permissions to perform this operation") ErrBlockUserFailed = errors.New("failed to block user") )
Functions ¶
This section is empty.
Types ¶
type AppProvider ¶
type AuthService ¶
type AuthService struct {
// contains filtered or unexported fields
}
func New ¶
func New( log *slog.Logger, userProvider UserProvider, appProvider AppProvider, sessionProvider SessionProvider, accessTokenTTL time.Duration, refreshTokenTTL time.Duration, ) *AuthService
New creates a new instance of the Auth service.
func (*AuthService) Logout ¶
func (a *AuthService) Logout(ctx context.Context, sessionID string) error
func (*AuthService) OAuthCallback ¶
func (a *AuthService) OAuthCallback(ctx context.Context, providerName, code, state, userIP, userAgent string) (user models.User, resp models.LoginResponse, err error)
func (*AuthService) RefreshToken ¶
func (a *AuthService) RefreshToken(ctx context.Context, refreshToken, ip, agent string) (models.RefreshTokenResponse, error)
func (*AuthService) Register ¶
func (a *AuthService) Register(ctx context.Context, email, username, password string) (err error)
Register: Registers a new user with the provided email, username, and password.
func (*AuthService) RevokeAllSession ¶
func (*AuthService) RevokeAppSession ¶
func (*AuthService) RevokeSession ¶
func (*AuthService) Sessions ¶
func (a *AuthService) Sessions(ctx context.Context, accessToken string, appID int32) ([]models.SessionResponce, error)
type SessionProvider ¶
type SessionProvider interface {
Create(ctx context.Context, key models.SessionKey, appName, userIP, userAgent, refreshHash, refreshSecret string, exp time.Time) error
GetByID(ctx context.Context, key models.SessionKey) (models.Session, error)
GetByParam(ctx context.Context, key models.SessionKey) ([]models.Session, error)
Revoke(ctx context.Context, sessionID string) error
SaveOAuthSession(ctx context.Context, appID int32, provider, session, state string) error
GetOAuthSession(ctx context.Context, provider, state string) (models.OAuthSession, error)
}
type UserProvider ¶
type UserProvider interface {
Register(ctx context.Context, email, username, passHash, avatarURL, provider string) (err error)
UserByEmail(ctx context.Context, email string) (user models.User, err error)
UserByID(ctx context.Context, userID int64) (user models.User, err error)
Update(ctx context.Context, userID int64, email, username, passHash, avatarURL *string, isBlocked *bool) error
}