UNPKG

@portive/auth

Version:

A library to help generate auth tokens for use with Portive's cloud services for open source components

61 lines (60 loc) 2.21 kB
import { AuthPrivateClaims } from "@portive/api-types"; import JWT from "jsonwebtoken"; /** * Takes an `apiKey` comprising of the parts separates by underscores. The * first part being a preamble checking that it starts with `PRTV`, the * second is the API key id, and the last pare is the API secret key. * * The key has these properties for a few reasons: * * 1. Easy to cut and paste. Double-click and underscores and alphanumeric * are all selected. * 2. `PRTV` makes sure we haven't confused the API key with some other API key * 3. We encode it into one so that we don't need multiple environment vars * to store the API key which also ensures the key id and secret key stay * together. * * e.g. PRTV_xxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */ export declare function parseApiKey(apiKey: string): { keyType: string; keyId: string; secretKey: string; }; /** * Takes the API key id and the API secret key and merges them into a single * API key which includes the `PRTV` preamble. * * e.g. PRTV_xxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */ export declare function stringifyApiKey({ keyId, secretKey, }: { keyId: string; secretKey: string; }): string; declare type ExpiresIn = JWT.SignOptions["expiresIn"]; /** * A lower level version of `generateAuth` which `generateAuth` uses. * Takes the `claims`, `keyId`, `secretKey` and `expiresIn` as separate * arguments to improve readability. * * Probably okay to merge this into `generateAuth` later. */ export declare function _createAuthToken(claims: AuthPrivateClaims, { keyId, secretKey, expiresIn, }: { keyId: string; secretKey: string; expiresIn: ExpiresIn; }): string; /** * Permissions includes both the private claims and the `expiresIn` value * for the JWT token. Think of it as the combination of Options for a * auth token. */ declare type AuthOptions = AuthPrivateClaims & { expiresIn: ExpiresIn; }; /** * Takes an apiKey (which includes the `keyId` and `secretKey`) and a set of * PermitOptions and then generates a permit from it. */ export declare function createAuthToken(apiKey: string, { expiresIn, ...claims }: AuthOptions): string; export {};