module:api/token
- Description:
API token based authentication, it reuses the users module to keep API token inside the same users table, as a special type of user, the
loginandsecretcolumns use properties from the generated session bymodule:api/token.create.Parsed token session is stored in the
context.sessionas an object:{ type: "token", login: string, secret: string }`API tokens can be created via shell command:
# bksh -user-add-token name test flags api -prefix api_ 'api_7e717a99e4b642ce88010629d32921b7caf92f4d165c46f59f983f880aa25c78'
- Source:
Methods
(static) parse(context) → {undefined|object}
- Description:
Parse API token and return as session object with id and secret if valid or undefined
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
context |
RequestContext |
Returns:
| Type | Description |
|---|---|
| undefined | object |
parsed token session if present, the format of the object is:
|
(static) prepare(context, optionsopt) → {object}
- Description:
Create an API token to be saved in the database, stores it in
context.session. The format is similar tomodule:api/session.Generated login and secret are random UUIDv4, so the secret hash without a salt is more about database exposure of secrets and not weak encryption.
Still if the context options contains
noHashSecret: truethen session secret will not be hashed when comparing with user secret, for cases when storing raw secret is acceptable. Usemodule:api.contextOptionsThe session object contains the following properties:
type:tokenlogin- generated UUIVv4 user login, no prefixsecret: generated UUIDv4 hashed with SHA256(optionally), to be saved in the users table as secrettoken: concatenated prefix, login and unhashed secret, to be used in API Authorization headerraw: unhashed secret
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
context |
RequestContext | ||||||||||||||
options |
object |
<optional> |
Properties
|
Returns:
| Type | Description |
|---|---|
| object |
a session object |
Example
const session = api.token.create(context, { prefix: "test_" })
{ type: "token", login: 'a2a3912ba6df4d1ebc87278be49f8aa0', token: 'test_a2a....' }
const user = {
login: session.login,
secret: session.secret,
name: "Test user",
....
};
await db.aput("bk_user", user);
(static) verify(context, secret, optionsopt)
- Description:
Verify API token from Authorization header and sets user in the context on success, this can be used standalone or as middleware.
It must be in format:
Authorization: Basic base64(token)orAuthorization: Bearer token,An API tokens are created by
module:api/token.createand parsed bymodule:api/token.parse.If the context options contains
noHashSecret: truethen session secret will not be hashed when comparing with user secret, for cases when storing raw secret is acceptable. Usemodule:api.contextOptions
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
context |
RequestContext | ||||||||||
secret |
string | ||||||||||
options |
object |
<optional> |
Properties
|