Code Monkey home page Code Monkey logo

isokey's Introduction

Isokey

Isokey allows you to make and verify self-contained API keys without a database via HMAC signatures.

Features

  • Important information such as userID, key expire time, and flags are stored within the key.
  • Use mutliple secrets simultaneously
  • Invalidate secrets and compromised keys

Table Of Contents

Basic usage

Creating a key

    isokey.Secret = []byte("super_secret_symmetric_key")
    key := isokey.Key{
        UserID: 1,
        Expires: time.Now().Add(time.Hour * 24),
    }
    fmt.Printf("Your key is %v", key.Digest())

Validating a key

	key, err = isokey.Validate(digest)
	if err != nil {
		return err
	}
    //Key secure here
    fmt.Printf("%v", key.Made)

Using multiple secrets

The SecretVersion is in included in each key to enable implementors to use multiple secrets.

Use a map

    isokey.SecretMap = map[uint32][]byte{
        1: []byte("sec1"),
        2: []byte("sec2"),
    }

Alternatively get full control with a function

    isokey.GetSecret = function(key *Key)(secret []byte){
        if key.SecretVersion == 1 {
            return []byte("sec1") 
        }
        return nil
    }

Invalidating keys

Custom invalidation can be useful if you'd like to support cases where the client has been compromised.

You can invalidate keys like so

isokey.Invalidate = function(key *isokey.Key) bool {
    if key.UserID == 10 && key.Made.Before(time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC)) {
        return true
    }
    //Make sure to handle expired keys when overriding
    if key.Expires.Before(time.Now()) {
        return true
    }
    return false
}

Remember when overriding Invalidate to handle expired keys

Digest Structure

All binary values are big endian.

Field Type
Signature [16]byte
Made Time (Unix epoch timestamp) uint32
Expire Time (Unix epoch timestamp) uint32
Secret Version uint32
User ID uint32
Flags uint32

Digests are encoded with Bitcoin's base58 alphabet.

It may seem intuitive to put the signature at the end of the digest. It's located at the beginning as it makes eyeballing different keys more easy due to the avalanche effect.

isokey's People

Contributors

ammario avatar

Watchers

CL.Lam avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.