2017 © Pedro Peláez
 

library branca

Secure alternative to JWT. Authenticated Encrypted API Tokens.

image

tuupola/branca

Secure alternative to JWT. Authenticated Encrypted API Tokens.

  • Thursday, April 5, 2018
  • by tuupola
  • Repository
  • 2 Watchers
  • 13 Stars
  • 1,100 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 7 Versions
  • 11 % Grown

The README.md

Branca Tokens for PHP

Authenticated and encrypted API tokens using modern crypto., (*1)

Latest Version Software License Build Status Coverage, (*2)

What?

Branca is a secure easy to use token format which makes it hard to shoot yourself in the foot. It uses IETF XChaCha20-Poly1305 AEAD symmetric encryption to create encrypted and tamperproof tokens. Payload itself is an arbitrary sequence of bytes. You can use for example a JSON object, plain text string or even binary data serialized by MessagePack or Protocol Buffers., (*3)

It is possible to use Branca as an alternative to JWT. There is also an authentication middleware for frameworks which support PSR-7 doublepass or PSR-15 standards., (*4)

Install

Install the library using Composer., (*5)

``` bash $ composer require tuupola/branca, (*6)


This branch requires PHP 7.2 or up. The older 1.x branch supports also PHP 5.6, 7.0 and 7.1. ``` bash $ composer require "tuupola/branca:^1.0"

Usage

Token payload can be any arbitrary data such as string containing an email address. You also must provide a 32 byte secret key. The key is used for encrypting the payload., (*7)

use Branca\Branca;

$key = random_bytes(32);
$branca = new Branca($key);

$payload = "tuupola@appelsiini.net";
$token = $branca->encode($payload);
/* hGgg0dPSseaUPZqGloWlDGb2i8hb6iamFBIQaatgYDRhEuaXyByaX0nzmyQk1WYAuSBEMWpB20Z1dENLFItwf1 */

$decoded = $branca->decode($token);
/* tuupola@appelsiini.net */

Sometimes you might prefer JSON., (*8)

use Branca\Branca;

$key = random_bytes(32);
$branca = new Branca($key);

$payload = json_encode(["scope" => ["read", "write", "delete"]]);
$token = $branca->encode($payload);

/*
5R7p5pC1gU5kfVuBUzhl43Ndh4HLT9fxAHrhN1zNRivTuehY8zYYzrVZ8C6d6VcNLfCk3EUgBwwW6kIk0wm32O34OFIYz5LnOIezwcV2Xsfc
*/

$decoded = $branca->decode($token);
$array = json_decode($decoded, true);

/*
Array
(
    [scope] => Array
        (
            [0] => read
            [1] => write
            [2] => delete
        )

)
*/

You can keep the token size small by using a space efficient serialization method such as MessagePack or Protocol Buffers., (*9)

use Branca\Branca;
use MessagePack\MessagePack;
use MessagePack\Packer;
use MessagePack\BufferUnpacker;

$key = random_bytes(32);
$branca = new Branca($key);

$payload = (new Packer)->pack(["scope" => ["read", "write", "delete"]]);
$token = $branca->encode($payload);

/*
3iJt0CjqTRh3FGuAf0DHEmhULFIbPVInjguWIkmyCm7RMps5BMJZKa1KwZMN0z58IpPeCxdjoTdkurn9pl0YNrxAQfg3deP0
*/

$decoded = $branca->decode($token);
$unpacked = (new BufferUnpacker($decoded))->unpack();
print_r($unpacked);

/*
Array
(
    [scope] => Array
        (
            [0] => read
            [1] => write
            [2] => delete
        )

)
*/

Timestamp

Branca token includes a timestamp when it was created. When decoding you can optionally pass a ttl parameter. Value is passed in seconds. Below example throws en exception if token is older than 60 minutes., (*10)

use Branca\Branca;

$key = hex2bin("73757065727365637265746b6579796f7573686f756c646e6f74636f6d6d6974");
$branca = new Branca($key);

$token = "1jJDJOEeG2FutA8g7NAOHK4Mh5RIE8jtbXd63uYbrFDSR06dtQl9o2gZYhBa36nZHXVfiGFz";

print $branca->timestamp($token); /* 123206400 */

try {
    $decoded = $branca->decode($token, 3600);
} catch (RuntimeException $exception) {
    print $exception->getMessage(); /* Token is expired */
}

Testing

You can run tests either manually or automatically on every code change. Automatic tests require entr to work., (*11)

``` bash $ make test, (*12)

``` bash
$ brew install entr
$ make watch

Contributing

Please see CONTRIBUTING for details., (*13)

Security

If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker., (*14)

License

The MIT License (MIT). Please see License File for more information., (*15)

The Versions

05/04 2018

dev-master

9999999-dev https://github.com/tuupola/branca-php

Secure alternative to JWT. Authenticated Encrypted API Tokens.

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication jwt token fernet

05/04 2018

0.3.2

0.3.2.0 https://github.com/tuupola/branca-php

Secure alternative to JWT. Authenticated Encrypted API Tokens.

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication jwt token fernet

12/12 2017

0.3.1

0.3.1.0 https://github.com/tuupola/branca-php

Authenticated Encrypted API Tokens (IETF XChaCha20-Poly1305 AEAD)

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication jwt token fernet

23/07 2017

0.3.0

0.3.0.0 https://github.com/tuupola/branca-php

Authenticated Encrypted API Tokens (IETF XChaCha20-Poly1305 AEAD)

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication jwt token fernet

23/07 2017

dev-xchaca20-poly1305-aead

dev-xchaca20-poly1305-aead https://github.com/tuupola/branca

Authenticated Encrypted API Tokens (IETF XChaCha20-Poly1305 AEAD)

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication jwt token fernet

21/07 2017

0.2.0

0.2.0.0 https://github.com/tuupola/branca

Authenticated Encrypted API Tokens with Associated Data

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication jwt token fernet

20/07 2017

0.1.0

0.1.0.0 https://github.com/tuupola/branca

Authenticated Encrypted API Tokens with Associated Data

  Sources   Download

MIT

The Requires

 

The Development Requires

authentication jwt token fernet