auth

package
v0.0.0-...-0871600 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package auth provides utility functions for password authentication and JWT access control.

Index

Constants

This section is empty.

Variables

View Source
var ErrorIncorrectLogin = errors.New("Invalid username or password")

ErrorIncorrectLogin represents a failed login attempt.

Functions

func CheckPassword

func CheckPassword(password string, hash []byte) bool

CheckPassword returns true if the provided password matches the hash; else false.

func GetHash

func GetHash(password string) ([]byte, error)

GetHash returns the bcrypt hash of the provided password.

Types

type Claims

type Claims struct {
	jwt.StandardClaims
}

Claims stores the set of user claims for JWTs.

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) (token string, err error)

AuthenticateUser attempts to log in an existing user with the provided credentials, returning an access token if the credentials match.

func (*LoginManager) SignupUser

func (lm *LoginManager) SignupUser(login UserLogin) (token string, 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 SavedUser

type SavedUser struct {
	Username       string
	HashedPassword []byte
}

SavedUser represents the data stored for a signed-up user.

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) (*Claims, error)

ParseToken verifies a token and returns its claims.

type UserLogin

type UserLogin struct {
	Username string
	Password string
}

UserLogin stores the information needed for a login attempt.

Jump to

Keyboard shortcuts

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