Documentation ¶
Overview ¶
Package auth provides utility functions for password authentication and JWT access control.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrorIncorrectLogin = errors.New("Invalid username or password")
ErrorIncorrectLogin represents a failed login attempt.
Functions ¶
func AuthorizerHook ¶
func AuthorizerHook(token *TokenSigner) dispatch.MiddlewareHook
AuthorizerHook is a middleware hook that populates the context's Claims object with data from the request's authorization token. If there is no authorization token, or the token is invalid, it returns an error.
This hook effectively acts as a requirement that the authorization token is correct.
func CheckPassword ¶
CheckPassword returns true if the provided password matches the hash; else false.
Types ¶
type LoginManager ¶
type LoginManager struct { DB *kvstore.KeyValueDB Token *TokenSigner }
LoginManager is an object made for managing user signin and authentication using the built-in token signing and key-value storage mechanisms (based on sqlite).
The provided methods can easily be used with the dispatch API framework by adding routes for SignupUser and AuthenticateUser.
func (*LoginManager) AuthenticateUser ¶
func (lm *LoginManager) AuthenticateUser(login UserLogin, ctx *dispatch.Context) (err error)
AuthenticateUser attempts to log in an existing user with the provided credentials, returning an access token if the credentials match.
func (*LoginManager) LogoutUser ¶
func (lm *LoginManager) LogoutUser(ctx *dispatch.Context)
LogoutUser logs out a user by removing the session cookies containing their auth token.
func (*LoginManager) SignupUser ¶
func (lm *LoginManager) SignupUser(login UserLogin, ctx *dispatch.Context) (err error)
SignupUser creates and stores user information for the new user. Upon successful registration, the user is signed in and the new generated token is returned.
type TokenSigner ¶
type TokenSigner struct { // Issuer is the value of the issuer field in the standard claims attached // to tokens generated by this signer. Issuer string // contains filtered or unexported fields }
TokenSigner is an object providing methods for creating and validating JWTs.
func NewTokenSigner ¶
func NewTokenSigner(issuer string, secret []byte) *TokenSigner
NewTokenSigner generates a new TokenSigner object with the specified issuer and secret token.
func (*TokenSigner) CreateToken ¶
func (ts *TokenSigner) CreateToken(username string) (string, error)
CreateToken creates a JWT token for a user to use for authentication.
func (*TokenSigner) ParseToken ¶
func (ts *TokenSigner) ParseToken(tokenStr string) (*dispatch.Claims, error)
ParseToken verifies a token and returns its claims.