PrefixedAPIKey

PrefixedAPIKey is a quick Elixir port of Seam's Javascript Prefixed API Key library. It creates and verifies simple, random authentication tokens that have two useful prefix parts.

This version is a little different (it has a smaller set of functions and uses a slightly bigger character set) but appears to be compatible with the original.

Installation

The package can be installed by adding prefixed_api_key to your list of dependencies in mix.exs:

def deps do
[
{:prefixed_api_key, "~> 0.1.0"}
]
end

Overview

Example key:

mycompany_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG

Seam-style API Keys have these benefits:

The Format

Prefixed API Keys look like this:

mycompany_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG

Components:

mycompany ..._... BRTRKFsL ..._... 51FwqftsmMDHHbJAMEXXHCgG
^ ^ ^
Prefix Short Token Long Token

Examples

{:ok, key} = PrefixedApiKey.generate("myapp")
# =>
# {:ok,
# %PrefixedApiKey{
# api_key: "myapp_ZLXZ3PYn_E34CUQSRtlmf0CMLsKFjMOf7",
# hash: "d5264a8fef50459c35306c35396c446cf88f8755c06ff70c341eb3fbd606ca44",
# long_token: "E34CUQSRtlmf0CMLsKFjMOf7",
# prefix: "myapp",
# short_token: "ZLXZ3PYn"
# }}
key.api_key
# => "myapp_ZLXZ3PYn_E34CUQSRtlmf0CMLsKFjMOf7"
{:ok, key} = PrefixedApiKey.parse("myapp_ZLXZ3PYn_E34CUQSRtlmf0CMLsKFjMOf7")
# =>
# {:ok,
# %PrefixedApiKey{
# api_key: "myapp_ZLXZ3PYn_E34CUQSRtlmf0CMLsKFjMOf7",
# hash: "d5264a8fef50459c35306c35396c446cf88f8755c06ff70c341eb3fbd606ca44",
# long_token: "E34CUQSRtlmf0CMLsKFjMOf7",
# prefix: "myapp",
# short_token: "ZLXZ3PYn"
# }}
PrefixedApiKey.verify?("myapp_ZLXZ3PYn_E34CUQSRtlmf0CMLsKFjMOf7", "d5264a8fef50459c35306c35396c446cf88f8755c06ff70c341eb3fbd606ca44")
# => true

API Documentation

Full API documentation can be found at https://hexdocs.pm/prefixed_api_key.

Contributing

You can request new features by creating an issue, or submit a pull request with your contribution.

References

Copyright (c) 2022 Digital Identity Ltd, UK

PrefixedAPIKey is MIT licensed.