Documentation ¶
Index ¶
- Constants
- Variables
- func BoolEnv(envVarName string) bool
- func ComparePIN(rawPwd string, salt string, encodedPwd string, options *Options) bool
- func DecodeJSONToTargetStruct(w http.ResponseWriter, r *http.Request, targetStruct interface{})
- func EncryptPIN(rawPwd string, options *Options) (string, string)
- func ErrorMap(err error) map[string]string
- func GenerateOTP() (string, error)
- func GenerateTempPIN(ctx context.Context) (string, error)
- func GetLoggedInUser(ctx context.Context) (string, error)
- func IsDebug() bool
- func ReportErr(w http.ResponseWriter, err error, status int)
- func RequestDebugMiddleware() func(http.Handler) http.Handler
- func ValidatePIN(pin string) error
- func ValidatePINDigits(pin string) error
- func ValidatePINLength(pin string) error
- func WriteJSONResponse(w http.ResponseWriter, source interface{}, status int)
- type Claims
- type Options
- type TokenResponse
Constants ¶
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 )
const ( // DebugEnvVarName is used to determine if we should print extended tracing / logging (debugging aids) // to the console DebugEnvVarName = "DEBUG" )
Variables ¶
var DefaultHashFunction = sha512.New
DefaultHashFunction ...
Functions ¶
func ComparePIN ¶
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 ¶
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 GenerateOTP ¶
GenerateOTP is used to generate a one time password
func GenerateTempPIN ¶
GenerateTempPIN generates a temporary One Time PIN for a user The PIN will have 4 digits formatted as a string
func GetLoggedInUser ¶
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 ¶
RequestDebugMiddleware dumps the incoming HTTP request to the log for inspection
func ValidatePIN ¶
ValidatePIN is used to check for the validity of the PIN provided.
func ValidatePINDigits ¶
ValidatePINDigits validates user pin to ensure a PIN only contains digits
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 ¶
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 ¶
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