api

package module
v0.0.0-...-a2bad76 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2018 License: MIT Imports: 4 Imported by: 6

README

go-api-guard

a golang package that provides a mechanism for managing and validating API keys.

Installation and docs

Install using go get github.com/tomogoma/go-api-guard

Godocs can be found at http://godoc.org/github.com/tomogoma/go-api-guard

Typical Usage


db := &DBMock{} //implements KeyStore interface

// mocking key generation to demonstrate resulting API key
keyGen := &KeyGenMock{ExpSRBs: []byte("an-api-key")}

g, _ := api.NewGuard(
    db,
    api.WithKeyGenerator(keyGen), // This is optional
)

// Generate API key
APIKey, _ := g.NewAPIKey("my-unique-user-id")

fmt.Println(string(APIKey.Value()))

// Validate API Key
userID, _ := g.APIKeyValid(APIKey.Value())

fmt.Println(userID)

// Output:
// bXktdW5pcXVlLXVzZXItaWQ=.an-api-key
// my-unique-user-id

Documentation

Overview

api provides a mechanism for managing and validating API keys.

Index

Examples

Constants

View Source
const (
	DefaultAPIKeyLength = 56
	MasterUser          = "master"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Guard

type Guard struct {
	errors.ClErrCheck
	errors.AuthErrCheck
	// contains filtered or unexported fields
}
Example
db := &DBMock{}
// mocking key generation to demonstrate resulting API key
keyGen := &KeyGenMock{ExpSRBs: []byte("an-api-key")}

g, _ := api.NewGuard(
	db,
	api.WithKeyGenerator(keyGen), // This is optional
)

// Generate API key
APIKey, _ := g.NewAPIKey("my-unique-user-id")

fmt.Println(string(APIKey.Value()))

// Validate API Key
userID, _ := g.APIKeyValid(APIKey.Value())

fmt.Println(userID)
Output:
bXktdW5pcXVlLXVzZXItaWQ=.an-api-key
my-unique-user-id

func NewGuard

func NewGuard(db KeyStore, opts ...Option) (*Guard, error)

func (*Guard) APIKeyValid

func (s *Guard) APIKeyValid(key []byte) (string, error)

func (*Guard) NewAPIKey

func (s *Guard) NewAPIKey(userID string) (Key, error)

type Key

type Key interface {
	Value() []byte
}

type KeyGenerator

type KeyGenerator interface {
	SecureRandomBytes(length int) ([]byte, error)
}

type KeyStore

type KeyStore interface {
	IsNotFoundError(error) bool
	InsertAPIKey(userID string, key []byte) (Key, error)
	APIKeyByUserIDVal(userID string, key []byte) (Key, error)
}

type Option

type Option func(*Guard) error

func WithAPIKeyLen

func WithAPIKeyLen(l int) Option

func WithKeyGenerator

func WithKeyGenerator(kg KeyGenerator) Option

func WithMasterKey

func WithMasterKey(key string) Option

Jump to

Keyboard shortcuts

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