Giter Club home page Giter Club logo

isokey's Introduction

Isokey

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

Features

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

Table Of Contents

Symmetric Keys

Make a key service

ks := NewSymKeyService([]byte("super_secure111"))

Sign a new key

key := &Key{
	UserID:  1,
	Expires: time.Now().AddDate(0, 1, 0),
}

digest, err := ks.Sign(key)
if err != nil {
	log.Fatalf("Failed to sign key: %v", err)
}
fmt.Printf("Digest is %v\n", digest)

Verify key

key, err = ks.Verify(digest)

if err != nil {
	log.Fatalf("Failed to verify digest: %v", err)
}

// Key authenticated
fmt.Printf("Key: %+v\n", key)

Using multiple secrets

The SecretVersion field is in included in the key object to enable implementors to easily use multiple secrets.

A secret can be decided based on any feature of a key.

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

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 easy.

Asymmetric Keys

Make a key pair

Make your private key openssl ecparam -genkey -name prime256v1 -outform DER -noout -out privatekey.der

Make your public key openssl ec -in privatekey.der -inform DER -outform DER -pubout -out publickey.der

Make key digest

privKey, _ = isokey.LoadPrivateKey("priv.key")

ks := NewAsymKeySigner(privKey)

key := &Key{
    User: 1,
    Expires: time.Now().Add(time.Hour)
}

digest, _ := ks.Sign(key)

fmt.Printf("Digest: %v", digest)

Verify key

pubKey, err := isokey.LoadPublicKey("pub.key")
if err != nil {
	log.Fatalf("Failed to load pubkey: %v", err)
}

kv := NewAsymKeyVerifier(pubKey)

key, err := kv.Verify(digest)
if err != nil {
	log.Fatalf("Failed to verify key: %v", err)
}

fmt.Printf("Key verified %+v\n", key)

Digest Structure

All binary values are big endian.

Field Type
R len uint8
R []byte
S Len uint8
S []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.

Invalidating keys

Expired keys always fail to validate.

You can add custom invalidation logic via the Invalidator field of verifiers.

verifier.Invalidator = function(key *isokey.Key) bool {
    // reject keys made before some time
    if key.UserID == 10 && key.Made.Before(time.Date(2015, time.November, 10, 23, 0, 0, 0, time.UTC)) {
        return true
    }
    return false
}

isokey's People

Contributors

ammario avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

isokey's Issues

Write better tests

The tests could be a little bit more thorough and include less eyeballing. A testing framework will likely be useful in writing succinct tests.

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.