auth

package
v0.0.0-...-0b42950 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(verificationKey string) routing.Handler

Handler returns a JWT-based authentication middleware.

func MockAuthHandler

func MockAuthHandler(c *routing.Context) error

MockAuthHandler creates a mock authentication middleware for testing purpose. If the request contains an Authorization header whose value is "TEST", then it considers the user is authenticated as "Tester" whose ID is "100". It fails the authentication otherwise.

func MockAuthHeader

func MockAuthHeader() http.Header

MockAuthHeader returns an HTTP header that can pass the authentication check by MockAuthHandler.

func RegisterHandlers

func RegisterHandlers(rg *routing.RouteGroup, service Service, logger log.Logger)

RegisterHandlers registers handlers for different HTTP requests.

func WithUser

func WithUser(ctx context.Context, id, 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() string
	// GetName returns the user name.
	GetName() 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 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, password string) (string, error)
}

Service encapsulates the authentication logic.

func NewService

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

NewService creates a new authentication service.

Jump to

Keyboard shortcuts

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