auth

package
v0.0.0-...-6dde671 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthenticateMiddleware

func AuthenticateMiddleware(next http.Handler, jwtSecret string) http.Handler

func RegisterHandlers

func RegisterHandlers(r *mux.Router, service Service, logger log.Logger)

RegisterHandlers registers handlers for different HTTP requests.

func RoleMiddleware

func RoleMiddleware(next http.Handler, requiredRole string) http.Handler

RoleMiddleware is a middleware to check the user's role

func WithUser

func WithUser(ctx context.Context, id primitive.ObjectID, name string) context.Context

WithUser returns a context that contains the user identity from the given JWT.

Types

type Identity

type Identity interface {
	// GetID returns the user ID.
	GetID() primitive.ObjectID
	// GetName returns the user name.
	GetName() string

	GetRole() []string
}

Identity represents an authenticated user identity.

func CurrentUser

func CurrentUser(ctx context.Context) Identity

CurrentUser returns the user identity from the given context. Nil is returned if no user identity is found in the context.

type LoginRequest

type LoginRequest struct {
	Username string `json:"email"`
	Password string `json:"password"`
}

func (LoginRequest) Validate

func (m LoginRequest) Validate() error

Validate validates the CreateAlbumRequest fields.

type Service

type Service interface {
	// authenticate authenticates a user using username and password.
	// It returns a JWT token if authentication succeeds. Otherwise, an error is returned.
	Login(ctx context.Context, username string, password string) (string, error)
}

Service encapsulates the authentication logic.

func NewService

func NewService(signingKey string, tokenExpiration int, logger log.Logger, userRepo user.Repository) Service

NewService creates a new authentication service.

type User

type User struct {
	ID       string
	Username string
}

User struct for context value

Jump to

Keyboard shortcuts

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