security

package module
v0.0.0-...-4a43eda Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2020 License: MIT Imports: 3 Imported by: 2

README

 ____________________ 
< security for noobs >
 -------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

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:

  1. Creating and comparing hashes (for passwords).
  2. 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

func Default(secret string) Security

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 New

func New(secret string) Security

func (Security) HashCompare

func (sec Security) HashCompare(salt, pass, hash string) error

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

func (sec Security) HashCreate(salt, pass string) (string, error)

HashCreate will create a hash for a password.

func (Security) TokenCreate

func (sec Security) TokenCreate(val TokenMap) (string, error)

TokenCreate creates a jwt string value given a `TokenMap`. Will return an error for unable to create token.

func (Security) TokenFrom

func (sec Security) TokenFrom(tokenString string) (_ret TokenMap, recoverErr error)

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.

type TokenMap

type TokenMap = jwt.MapClaims

TokenMap is the type supplied to and returned from TokenCreate and TokenFrom respectively. Use a you would a `map[string]interface{}`. It's an abstraction over `jwt.MapClaims` (github.com/dgrijalva/jwt-go) so you don't have to import both libraries.

Jump to

Keyboard shortcuts

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