utils

package
v0.0.0-...-4605876 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultSaltLen is the length of generated salt for the user is 256
	DefaultSaltLen = 256

	// DefaultKeyLen is the length of encoded key in PBKDF2 function is 512
	DefaultKeyLen = 512
)
View Source
const (
	// DebugEnvVarName is used to determine if we should print extended tracing / logging (debugging aids)
	// to the console
	DebugEnvVarName = "DEBUG"
)

Variables

View Source
var DefaultHashFunction = sha512.New

DefaultHashFunction ...

Functions

func BoolEnv

func BoolEnv(envVarName string) bool

BoolEnv gets and parses a boolean environment variable

func ComparePIN

func ComparePIN(rawPwd string, salt string, encodedPwd string, options *Options) bool

ComparePIN takes four arguments, the raw password, its generated salt, the encoded password, and a pointer to the Options struct, and returns a boolean value determining whether the password is the correct one or not. Passing `nil` as the last argument resorts to default options.

func DecodeJSONToTargetStruct

func DecodeJSONToTargetStruct(w http.ResponseWriter, r *http.Request, targetStruct interface{})

DecodeJSONToTargetStruct maps JSON from a HTTP request to a struct.

func EncryptPIN

func EncryptPIN(rawPwd string, options *Options) (string, string)

EncryptPIN takes two arguments, a raw pin, and a pointer to an Options struct. In order to use default options, pass `nil` as the second argument. It returns the generated salt and encoded key for the user.

func ErrorMap

func ErrorMap(err error) map[string]string

ErrorMap turns the supplied error into a map with "error" as the key

func GenerateOTP

func GenerateOTP() (string, error)

GenerateOTP is used to generate a one time password

func GenerateTempPIN

func GenerateTempPIN(ctx context.Context) (string, error)

GenerateTempPIN generates a temporary One Time PIN for a user The PIN will have 4 digits formatted as a string

func GetLoggedInUser

func GetLoggedInUser(ctx context.Context) (string, error)

GetLoggedInUser retrieves the logged in user from the context

func IsDebug

func IsDebug() bool

IsDebug returns true if debug has been turned on in the environment

func ReportErr

func ReportErr(w http.ResponseWriter, err error, status int)

ReportErr writes the indicated error to supplied response writer and also logs it

func RequestDebugMiddleware

func RequestDebugMiddleware() func(http.Handler) http.Handler

RequestDebugMiddleware dumps the incoming HTTP request to the log for inspection

func ValidatePIN

func ValidatePIN(pin string) error

ValidatePIN is used to check for the validity of the PIN provided.

func ValidatePINDigits

func ValidatePINDigits(pin string) error

ValidatePINDigits validates user pin to ensure a PIN only contains digits

func ValidatePINLength

func ValidatePINLength(pin string) error

ValidatePINLength ...

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, source interface{}, status int)

WriteJSONResponse writes the content supplied via the `source` parameter to the supplied http ResponseWriter. The response is returned with the indicated status.

Types

type Claims

type Claims struct {
	UserID string `json:"user_id"`
	jwt.RegisteredClaims
}

Create a struct that will be encoded to a JWT. We add jwt.RegisteredClaims as an embedded type, to provide fields like expiry time

type Options

type Options struct {
	SaltLen      int
	Iterations   int
	KeyLen       int
	HashFunction func() hash.Hash
}

Options is a struct for custom values of salt length, number of iterations, the encoded key's length, and the hash function being used. If set to `nil`, default options are used: &Options{ 256, 10000, 512, "sha512" }

type TokenResponse

type TokenResponse struct {
	Token     string    `json:"token"`
	ExpiresIn time.Time `json:"expiresIn"`
}

TokenResponse represents the response from the token endpoint

func GenerateJWTToken

func GenerateJWTToken(userID string) (*TokenResponse, error)

GenerateJWTToken generates a JWT token

func ValidateJWTToken

func ValidateJWTToken(tokenString string) (*TokenResponse, error)

ValidateJWTToken validates a JWT token

Jump to

Keyboard shortcuts

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