Code Monkey home page Code Monkey logo

prefixed-api-key's Introduction

A Rust implementation of Prefixed API Key

This library is a Rust implementation of the Prefixed API Key typescript library. Though its interface differs slightly from the typescript version, this library provides the same set of features and functionality as the typescript version.

โš ๏ธ This library is still a work in progress.

Prefixed API Key (Seam-style)

Example key: mycompany_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG

See discussion on Hacker News

Seam-style API Keys have many advantages:

  • Double clicking the api key selects the entire api key
  • The alphabet is standard across languages thanks to the base58 RFC and its usage in cryptocurrencies
  • They are shorter than hex and base32 api keys
  • They have prefixes allowing secret scanning by github
  • They have a hashed component so the server doesn't need to store the api key (reducing attack surface)
  • They have unhashed short tokens which can be mutually used by the server and key bearer/customer to identify the api key
  • They default to roughly the same number of entropy bits as UUIDv4

The Format

Seam-style api keys look like this:

mycompany_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG

Let's break down each component of the API key...

mycompany ..._...  BRTRKFsL ..._...  51FwqftsmMDHHbJAMEXXHCgG
^                  ^                 ^
Prefix             Short Token       Long Token
  • The Prefix is used to identify the company or service creating the API Key. This is very helpful in secret scanning.
  • The Short Token is stored by both the server and the key bearer/customer, it can be used to identify an API key in logs or displayed on a customer's dashboard. A token can be blocklisted by its short token.
  • The Long Token is how we authenticate this key. The long token is never stored on the server, but a hash of it is stored on the server. When we receive an incoming request, we search our database for short_token and hash(long_token).

Getting Started

The original Typescript implementation of Prefixed API Keys has a few technical decisions hardcoded, but this crates aims to give full control over which hashing algorithm and random number generator are used. However this adds more complexity than may be desirable, so helpers are available to make configuration relatively painless.

By installing the crate with the sha2 feature flag, you can create an almost-entirely configured PrefixedApiKeyController instance using the seam_defaults function, which configures the controller the same way as Seam's Typescript implementation.

use prefixed_api_key::PrefixedApiKeyController;

fn main() {
    // A controller using `rand::rng::OsRng` as the RNG source, and
    // `sha2::Sha256` as the hashing algorithm.
    let builder_result = PrefixedApiKeyController::configure()
        .prefix("mycompany".to_owned())
        .seam_defaults()
        .finalize();

    assert!(builder_result.is_ok());

    let mut controller = builder_result.unwrap();

    // Generate a new PrefixedApiKey
    let (pak, hash) = controller.generate_key_and_hash();

    // Assert that the returned key matches the hash
    assert!(controller.check_hash(&pak, &hash));

    // Stringify the key to be sent to the user. This creates a string from the
    // PrefixedApiKey which follows the `<prefix>_<short token>_<long token>` convention
    let pak_string = pak.to_string();
}

Using the seam_defaults() function with the sha2 feature flag is equivalent to doing the following without using the sha2 feature:

use sha2::Sha256;
use prefixed_api_key::PrefixedApiKeyController;

fn main() {
    let controller = PrefixedApiKeyController::<_, Sha256>::configure()
        .prefix("mycompany".to_owned())
        .rng_osrng()
        .short_token_length(8)
        .long_token_length(24)
        .finalize();
}

prefixed-api-key's People

Contributors

brahmlower avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

prefixed-api-key's Issues

Avoid needing `&mut` for generating keys

Needing a mutable borrow for generate_key and generate_key_and_hash is pretty inconvenient. This requirement stems from the use of the private function get_random_bytes which is mutating the rng source while generating new bytes.

Instead of the PrefixedApiKeyController carrying the rng source with it, the rng source could be provided during the call to generate a new key, this way the caller still has control over the rng source (just at call time rather than struct instantiation time) and the controller isn't forced to be mutably borrowed any time it need to generate a key.

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.