All Projects → tuupola → Branca Spec

tuupola / Branca Spec

Authenticated and encrypted API tokens using modern crypto

Labels

Projects that are alternatives of or similar to Branca Spec

Python Api Development Fundamentals
Develop a full-stack web application with Python and Flask
Stars: ✭ 44 (-73.01%)
Mutual labels:  api, jwt
Branca Js
Authenticated encrypted API Tokens for JavaScript.
Stars: ✭ 69 (-57.67%)
Mutual labels:  api, jwt
Slim3 Jwt Auth Example
Server side implementation example of JWT (JSON Web Token) authentication using Slim3
Stars: ✭ 45 (-72.39%)
Mutual labels:  api, jwt
Go Book Store Api
Go Sample project to understand Mysql CRUD operation with best practises Includes logging, JWT, Swagger and Transactions
Stars: ✭ 18 (-88.96%)
Mutual labels:  api, jwt
Golang Gin Realworld Example App
Exemplary real world application built with Golang + Gin
Stars: ✭ 1,780 (+992.02%)
Mutual labels:  api, jwt
Go Base
Go RESTful API Boilerplate with JWT Authentication backed by PostgreSQL
Stars: ✭ 928 (+469.33%)
Mutual labels:  api, jwt
Laravel Api Boilerplate Jwt
A Laravel 5.8 API Boilerplate to create a ready-to-use REST API in seconds.
Stars: ✭ 1,155 (+608.59%)
Mutual labels:  api, jwt
Full Stack
Full stack, modern web application generator. Using Flask, PostgreSQL DB, Docker, Swagger, automatic HTTPS and more.
Stars: ✭ 451 (+176.69%)
Mutual labels:  api, jwt
Laravel Vue Starter
Well Documented Laravel Starter App From Development to Production. For Full Blown RESTFUL API and SPA with Beautiful UI Using Buefy / ElementUi For Reusable Vue Components
Stars: ✭ 76 (-53.37%)
Mutual labels:  api, jwt
Nextjs Jwt Example
next.js authorization example including private route protection
Stars: ✭ 72 (-55.83%)
Mutual labels:  api, jwt
Snake
🐍 一款小巧的基于Go构建的开发框架,可以快速构建API服务或者Web网站进行业务开发,遵循SOLID设计原则
Stars: ✭ 615 (+277.3%)
Mutual labels:  api, jwt
Branca
🔑 Secure alternative to JWT. Authenticated Encrypted API Tokens for Go.
Stars: ✭ 147 (-9.82%)
Mutual labels:  api, jwt
Node Express Mongodb Jwt Rest Api Skeleton
This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API). Demo of frontend in VueJS here: https://github.com/davellanedam/vue-skeleton-mvp
Stars: ✭ 603 (+269.94%)
Mutual labels:  api, jwt
Api guard
JWT authentication solution for Rails APIs
Stars: ✭ 159 (-2.45%)
Mutual labels:  api, jwt
Beauty Vuejs Boilerplate
❤️ Real world base Vue.js app. Access/refresh tokens auth, api services, http client, vuex modules
Stars: ✭ 583 (+257.67%)
Mutual labels:  api, jwt
Go Restful Api
An idiomatic Go REST API starter kit (boilerplate) following SOLID principles and Clean Architecture
Stars: ✭ 1,043 (+539.88%)
Mutual labels:  api, jwt
Jwt sessions
XSS/CSRF safe JWT auth designed for SPA
Stars: ✭ 431 (+164.42%)
Mutual labels:  api, jwt
Go Gin Example
An example of gin
Stars: ✭ 4,992 (+2962.58%)
Mutual labels:  api, jwt
Foal
Elegant and all-inclusive Node.Js web framework based on TypeScript. 🚀.
Stars: ✭ 1,176 (+621.47%)
Mutual labels:  api, jwt
Node Express Mongoose Passport Jwt Rest Api Auth
Node, express, mongoose, passport and JWT REST API authentication example
Stars: ✭ 146 (-10.43%)
Mutual labels:  api, jwt

Branca Token

Authenticated and encrypted API tokens using modern crypto.

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.

Although not a goal, it is possible to use Branca as an alternative to JWT. Also see getting started instructions.

This specification defines the external format and encryption scheme of the token to help developers create their own implementations. Branca is closely based on Fernet specification.

Design Goals

  1. Secure
  2. Easy to implement
  3. Small token size

Token Format

Branca token consists of header, ciphertext and an authentication tag. Header consists of version, timestamp and nonce. Putting them all together we get following structure.

Version (1B) || Timestamp (4B) || Nonce (24B) || Ciphertext (*B) || Tag (16B)

String representation of the above binary token must use base62 encoding with the following character set.

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Version

Version is 8 bits ie. one byte. Currently the only version is 0xBA. This is a magic byte which you can use to quickly identify a given token. Version number guarantees the token format and encryption algorithm.

Timestamp

Timestamp is 32 bits ie. unsigned big endian 4 byte UNIX timestamp. By having a timestamp instead of expiration time enables the consuming side to decide how long tokens are valid. You cannot accidentally create tokens which are valid for the next 10 years.

Storing timestamp as unsigned integer allows us to avoid 2038 problem. Unsigned integer overflow will happen in the year 2106. Possible values are 0 - 4294967295.

Nonce

Nonce is 192 bits ie. 24 bytes. It should be cryptographically secure random bytes. A nonce should never be used more than once with the same secret key between different payloads. It should be generated automatically by the implementing library. Allowing end user to provide their own nonce is a foot gun.

Ciphertext

Payload is encrypted and authenticated using IETF XChaCha20-Poly1305. Note that this is Authenticated Encryption with Additional Data (AEAD) where the header part of the token is the additional data. This means the data in the header (version, timestamp and nonce) is not encrypted, it is only authenticated. In laymans terms, header can be seen but it cannot be tampered.

Tag

The authentication tag is 128 bits ie. 16 bytes. This is the Poly1305 message authentication code. It is used to make sure that the payload, as well as the non-encrypted header have not been tampered with.

Working With Tokens

Instructions below assume your crypto library supports combined mode. In combined mode the authentication tag and the encrypted message are stored together. If your crypto library does not provide combined mode the tag is last 16 bytes of the ciphertext|tag combination.

Generating a Token

Given a 256 bit ie. 32 byte secret key and an arbitrary payload, generate a token with the following steps in order:

  1. Generate a 24 byte cryptographically secure random nonce.
  2. If user has not provided timestamp use the current unixtime.
  3. Construct the header by concatenating version, timestamp and nonce.
  4. Encrypt the user given payload with IETF XChaCha20-Poly1305 AEAD with user provided secret key. Use header as the additional data for AEAD.
  5. Concatenate the header and the returned ciphertext|tag combination from step 4.
  6. Base62 encode the entire token.

Verifying a Token

Given a 256 bit ie. 32 byte secret key and a token to verify that the token is valid and recover the original unencrypted payload, perform the following steps, in order.

  1. Base62 decode the token.
  2. Make sure the first byte of the decoded token is 0xBA.
  3. Extract the header ie. the first 29 bytes from the decoded token.
  4. Extract the nonce ie. the last 24 bytes from the header.
  5. Extract the timestamp ie. bytes 2 to 5 from the header.
  6. Extract ciphertext|tag combination ie. everything starting from byte 30.
  7. Decrypt and verify the ciphertext|tag combination with IETF XChaCha20-Poly1305 AEAD using the secret key and nonce. Use header as the additional data for AEAD.

Working With the Timestamp

Optionally the implementing library may use the timestamp for additional verification. For example the library could provide a non-negative ttl parameter which is used to check if token is expired by adding the ttl to the timestamp and comparing the result with the current unixtime. Any optional timestamp check must happen as the last step after decrypting and verifying the token. Further, the implementing library must ensure adding ttl to timestamp does not overflow 4294967295.

Libraries

Currently known implementations in the wild.

Language Repository License Crypto library used
Clojure miikka/clj-branca EPL-2.0 terl/lazysodium-java
DotNet thangchung/branca-dotnet MIT NaCl.Core
DotNet scottbrady91/IdentityModel Apache-2.0 Bouncy Castle
Elixir tuupola/branca-elixir MIT ArteMisc/libsalty
Erlang 1ma/branca-erl MIT jedisct1/libsodium
Go essentialkaos/branca MIT golang/crypto
Go hako/branca MIT golang/crypto
Go juranki/branca MIT golang/crypto
Java Kowalski-IO/branca MIT Bouncy Castle
Java bjoernw/jbranca Apache-2.0 Bouncy Castle
JavaScript tuupola/branca-js MIT jedisct1/libsodium.js
Kotlin petersamokhin/kbranca Apache-2.0 Bouncy Castle
PHP tuupola/branca-php MIT jedisct1/libsodium
Python tuupola/branca-python MIT jedisct1/libsodium
Ruby thadeu/branca-ruby MIT RubyCrypto/rbnacl
Ruby crossoverhealth/branca MIT RubyCrypto/rbnacl
Rust return/branca MIT brycx/orion

Acceptance Test Vectors

All test vectors can be found here.

Similar Projects

  • PASETO ie. Platform-Agnostic Security Tokens.
  • Fernet which provides AES 128 in CBC mode tokens.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].