Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Security ¶
type Security struct {
// contains filtered or unexported fields
}
Security is the security service object. Security presents an easy to use interface for common actions needed in CRUD applications such as:
- Creating and comparing hashes (for passwords).
- Creating and reading from tokens (for persistents when sessions are not available).
Usage:
// Do this once in your application. sec := security.Default("MY_SECRET_HERE") // Create password hash for database hash, err := sec.HashCreate(someSalt, somePass) if err != nil {} // Check hash for attempted login. if err := HashCompare(userInput, hash); err != nil { panic("incorrect password") } // Create token for user struct. token, err := sec.TokenCreate(TokenMap{"UserID": user.ID()}) if err != nil {} // Decode user struct token. claims, err := sec.TokenFrom(token) if err != nil {} id, ok := claims["UserID"].(string) if !ok {}
func Default ¶
Default builds the security service object.
Usage:
// Do this once in your application. sec := security.Default("MY_SECRET_HERE") // Create password hash for database hash, err := sec.HashCreate(someSalt, somePass) if err != nil {} // Check hash for attempted login. if err := HashCompare(userInput, hash); err != nil { panic("incorrect password") } // Create token for user struct. token, err := sec.TokenCreate(TokenMap{"UserID": user.ID()}) if err != nil {} // Decode user struct token. claims, err := sec.TokenFrom(token) if err != nil {} id, ok := claims["UserID"].(string) if !ok {}
func (Security) HashCompare ¶
HashCompare will compare a user input with a previously created hash value. Commonly used for password verification while logging users in. Returns an error when password differs from the hash input.
func (Security) HashCreate ¶
HashCreate will create a hash for a password.
func (Security) TokenCreate ¶
TokenCreate creates a jwt string value given a `TokenMap`. Will return an error for unable to create token.
func (Security) TokenFrom ¶
TokenFrom returns a `TokenMap` item given a jwt string value. Be careful, you will need to type check these values at run time like so:
claims, err := sec.TokenFrom(token) if err != nil {} id, ok := claims["UserID"].(string) if !ok {} // no you can use the `id` item stored within the token.