auth

package
v0.0.0-...-1842cec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API struct {
	// contains filtered or unexported fields
}

API describes dependencies for auth endpoints

func NewAPI

func NewAPI(log *zap.Logger, googleIDTokenClient GoogleIDTokenClient, githubOAuthClient GitHubOAuthClient, userFacade UserFacade, cfg APICfg) (*API, error)

NewAPI return new instance of auth api

func (*API) DeleteAccountHandler

func (a *API) DeleteAccountHandler(c fiber.Ctx) error

DeleteAccountHandler godoc @Summary Delete user account @Description Deletes a user account @Tags auth @Produce json @Param Authorization header string true "Bearer token" @Success 204 "Successfully deleted account" @Failure 401 {object} web.ErrResp "Unauthorized" @Failure 500 {object} web.ErrResp "Internal server error" @Router /account [delete]

func (*API) GitHubOAuthHandler

func (a *API) GitHubOAuthHandler(c fiber.Ctx) error

GitHubOAuthHandler godoc @Summary GitHub OAuth sign in handler @Description Handles GitHub OAuth 2.0 authentication using authorization code @Tags auth @Accept json @Produce json @Param code body GitHubOAuthRequest true "GitHub OAuth authorization code" @Success 200 {object} TokenResp "User credentials" @Failure 400 {object} web.ErrResp @Failure 401 {object} web.ErrResp @Failure 403 {object} web.ErrResp @Failure 409 {object} web.ErrResp @Router /oauth/github [post]

func (*API) GoogleOAuthHandler

func (a *API) GoogleOAuthHandler(c fiber.Ctx) error

GoogleOAuthHandler godoc @Summary Google OAuth sign in handler @Description Handles Google OAuth 2.0 authentication @Tags auth @Accept json @Produce json @Param token body GoogleOAuthRequest true "Google OAuth token" @Success 200 {object} TokenResp "User credentials" @Failure 400 {object} web.ErrResp @Failure 401 {object} web.ErrResp @Failure 403 {object} web.ErrResp @Failure 409 {object} web.ErrResp @Router /oauth/google [post]

func (*API) LogoutHandler

func (a *API) LogoutHandler(c fiber.Ctx) error

LogoutHandler godoc @Summary Logout user @Description Revokes the refresh token and clears the refresh token cookie @Tags auth @Success 204 "Successfully logged out" @Failure 500 {object} web.ErrResp @Router /logout [post]

func (*API) RefreshTokenHandler

func (a *API) RefreshTokenHandler(c fiber.Ctx) error

RefreshTokenHandler godoc @Summary Refresh access and refresh tokens @Description Use a refresh token from httpOnly cookie to obtain new access and refresh tokens @Tags auth @Produce json @Success 200 {object} TokenResp @Failure 401 {object} web.ErrResp "Invalid or expired refresh token" @Failure 500 {object} web.ErrResp @Router /refresh [post]

func (*API) ResendVerificationEmailHandler

func (a *API) ResendVerificationEmailHandler(c fiber.Ctx) error

ResendVerificationEmailHandler godoc @Summary Resend email verification code @Description Resends email verification code to the user's email address @Tags auth @Security Bearer @Produce json @Success 204 "Successfully resent verification email" @Failure 400 {object} web.ErrResp "User email is already verified" @Failure 401 {object} web.ErrResp "Invalid or missing authorization token" @Failure 429 {object} web.ErrResp "Too many resend requests" @Failure 500 {object} web.ErrResp "Internal server error" @Router /resend-verification [post]

func (*API) SignInHandler

func (a *API) SignInHandler(c fiber.Ctx) error

SignInHandler godoc @Summary Sign in @Description Authenticate a user and return an access token @Tags auth @Accept json @Produce json @Param signin body SignInReq true "User credentials" @Success 200 {object} TokenResp @Failure 400 {object} web.ErrResp @Failure 401 {object} web.ErrResp @Failure 500 {object} web.ErrResp @Router /signin [post]

func (*API) SignUpHandler

func (a *API) SignUpHandler(c fiber.Ctx) error

SignUpHandler godoc @Summary Register a new user @Description Create a new user account with the provided information @Tags auth @Accept json @Produce json @Param signup body SignUpReq true "User signup information" @Success 200 {object} TokenResp "User credentials" @Failure 400 {object} web.ErrResp "Invalid input data" @Failure 409 {object} web.ErrResp "Username or publisher name already exists" @Failure 500 {object} web.ErrResp "Internal server error" @Router /signup [post]

func (*API) UpdateProfileHandler

func (a *API) UpdateProfileHandler(c fiber.Ctx) error

UpdateProfileHandler godoc @Summary Update user profile @Description Updates the profile information of a user @Tags auth @Security Bearer @Accept json @Produce json @Param Authorization header string true "Bearer token" @Param profile body UpdateProfileReq true "Update profile parameters" @Success 200 {object} TokenResp "Returns new access token" @Failure 400 {object} web.ErrResp "Bad request" @Failure 401 {object} web.ErrResp "Invalid password or token" @Failure 404 {object} web.ErrResp "User not found" @Failure 500 {object} web.ErrResp "Internal server error" @Router /account [patch]

func (*API) VerifyEmailHandler

func (a *API) VerifyEmailHandler(c fiber.Ctx) error

VerifyEmailHandler godoc @Summary Verify email address using verification code @Description Verifies user email address using the code sent via email @Tags auth @Security Bearer @Accept json @Produce json @Param verification body VerifyEmailReq true "Email verification code" @Success 200 {object} TokenResp @Failure 400 {object} web.ErrResp "Invalid or expired verification code" @Failure 401 {object} web.ErrResp "Invalid or missing authorization token" @Failure 404 {object} web.ErrResp "Verification code is not found" @Failure 500 {object} web.ErrResp "Internal server error" @Router /verify-email [post]

type APICfg

type APICfg struct {
	RefreshTokenCookieSameSite string
	RefreshTokenCookieSecure   bool
	ContactEmail               string
}

APICfg describes configuration for auth api

type GitHubOAuthClient

type GitHubOAuthClient interface {
	ExchangeCodeForUser(ctx context.Context, code string) (githubapi.UserInfo, error)
}

GitHubOAuthClient provides methods for GitHub OAuth authentication

type GitHubOAuthRequest

type GitHubOAuthRequest struct {
	Code string `json:"code" validate:"required"`
}

GitHubOAuthRequest represents GitHub OAuth request

type GoogleIDTokenClient

type GoogleIDTokenClient interface {
	ValidateIDToken(ctx context.Context, idToken string) (*idtoken.Payload, error)
}

GoogleIDTokenClient provides methods for validating Google ID tokens

type GoogleOAuthRequest

type GoogleOAuthRequest struct {
	IDToken string `json:"idToken" validate:"required"`
}

GoogleOAuthRequest represents Google OAuth request

type SignInReq

type SignInReq struct {
	Username string `json:"username" validate:"required,min=4,max=64"`
	Password string `json:"password" validate:"required,min=8,max=64"`
}

SignInReq represents user sign in request

type SignUpReq

type SignUpReq struct {
	Username        string `json:"username" validate:"required,min=4,usernameregex"`
	DisplayName     string `json:"name" validate:"required"`
	Email           string `json:"email" validate:"omitempty,email"`
	Password        string `json:"password" validate:"required,min=8,max=64"`
	ConfirmPassword string `json:"confirmPassword" validate:"eqfield=Password"`
	IsPublisher     bool   `json:"isPublisher"`
}

SignUpReq represents user sign up request

type TokenResp

type TokenResp struct {
	AccessToken string `json:"accessToken"`
}

TokenResp represents response with JWT access token

type UpdateProfileReq

type UpdateProfileReq struct {
	Name               *string `json:"name"`
	Password           *string `json:"password" validate:"omitempty,min=8,max=64"`
	NewPassword        *string `json:"newPassword" validate:"omitempty,min=8,max=64"`
	ConfirmNewPassword *string `json:"confirmNewPassword" validate:"omitempty,min=8,max=64"`
}

UpdateProfileReq represents update profile request

type UserFacade

type UserFacade interface {
	GoogleOAuth(ctx context.Context, oauthID, email string, emailVerified bool) (model.User, error)
	GitHubOAuth(ctx context.Context, oauthID, email, username string, emailVerified bool) (model.User, error)
	DeleteUser(ctx context.Context, userID string) error
	UpdateUserProfile(ctx context.Context, userID string, params model.UpdateProfileParams) (model.User, error)
	VerifyEmail(ctx context.Context, userID string, code string) (model.User, error)
	ResendVerificationEmail(ctx context.Context, userID string) error
	SignIn(ctx context.Context, username, password string) (model.User, error)
	SignUp(ctx context.Context, username, displayName, email, password string, isPublisher bool) (model.User, error)
	CreateTokens(ctx context.Context, user model.User) (facade.TokenPair, error)
	RefreshTokens(ctx context.Context, refreshTokenStr string) (facade.TokenPair, error)
	RevokeRefreshToken(ctx context.Context, refreshTokenStr string) error
	ValidateAccessToken(tokenStr string) bool
	GetClaimsFromAccessToken(tokenStr string) (auth.Claims, error)
}

UserFacade provides methods for working with user facade

type VerifyEmailReq

type VerifyEmailReq struct {
	Code string `json:"code" validate:"required,len=6"`
}

VerifyEmailReq represents email verification request with 6-digit code

type VerifyTokenReq

type VerifyTokenReq struct {
	Token string `json:"token" validate:"jwt"`
}

VerifyTokenReq represents verify JWT request

type VerifyTokenResp

type VerifyTokenResp struct {
	Valid bool `json:"valid"`
}

VerifyTokenResp represents verify JWT response

Directories

Path Synopsis
Package auth_mocks is a generated GoMock package.
Package auth_mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL