auth

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const AuthTokenKeyInCtx = "Authorization"

AuthTokenKeyInCtx is the key where the auth token is stored inside a request's context

Variables

This section is empty.

Functions

func AuthLevelVerifierFactory

func AuthLevelVerifierFactory(minLevel common.AuthLevel, jwtProvider JWTProvider, jwtSecret []byte, failCallback gin.HandlerFunc) gin.HandlerFunc

AuthLevelVerifierFactory creates a middleware that checks if the request contains a valid JWT provided by the jwtProvider and confirms that the requester has an auth level which is greater than or equal to minLevel. Auth claims get stored in "Authorization" if token is valid calls failCallback otherwise

func CompareHashAndPassword

func CompareHashAndPassword(hash, password string) error

CompareHashAndPassword compares the hash to the password. If they both represent the same string, returns nill. Returns an error otherwise

func GetHashForPassword

func GetHashForPassword(password string) (string, error)

GetHashForPassword generates a hash for the given password with the given salt

func NewJWT

func NewJWT(user entities.User, timestamp int64, validityDuration int64, tokenType TokenType, secret []byte) (string, error)

NewJWT creates a new JWT for the specified user with the specified secret

Types

type Claims

type Claims struct {
	jwt.StandardClaims
	AuthLevel common.AuthLevel `json:"auth_level"`
	TokenType TokenType        `json:"token_type"`
}

Claims is the model for the claims in the JWT token

func ExtractClaimsFromCtx

func ExtractClaimsFromCtx(ctx *gin.Context) *Claims

ExtractClaimsFromCtx extracts auth claims from a request context. Returns nil if they do not exist or cannot be decoded

func GetJWTClaims

func GetJWTClaims(token string, secret []byte) *Claims

GetJWTClaims checks if the given token is a valid JWT with given secret and returns the claims inside the token. Returns nill if the token is invalid

type JWTProvider

type JWTProvider func(ctx *gin.Context) (jwt string)

JWTProvider is a wrapper type for a function which returns a JWT from a request's context

type TokenType

type TokenType string

TokenType represent an auth token type

var (
	// Email is token type for email tokens
	Email TokenType = "email"
	// Auth is token type for auth tokens
	Auth TokenType = "auth"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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