@spica-devkit/identity
Log identities in, handle two-factor challenges, and manage identities, their policies and login strategies.
@spica-devkit/identity works with identities, the accounts of your team. Use it in internal tools and automation.
Terminal
npm install @spica-devkit/identityTypeScript
import * as Identity from "@spica-devkit/identity";
Identity.initialize({apikey: process.env.SPICA_API_KEY, publicUrl: "https://api.example.com/api"});Log in
TypeScript
const result = await Identity.login("editor", "a-long-random-password", 3600);
if (Identity.isChallenge(result)) {
const code = await askUserForTotpCode(result.show());
const token = await result.answer(code);
} else {
const token = result;
}login returns the JWT, or a challenge when the identity has two-factor authentication. show() returns the challenge text and answer(code) resolves to the token.
| Function | Description |
|---|---|
login(identifier, password, tokenLifeSpan?, headers?) | Log in. Returns a token or a challenge. |
isChallenge(result) | Tell the two apart. |
verifyToken(token, baseUrl?, headers?) | Verify a token and return its payload. |
getStrategies() | List login strategies. |
loginWithStrategy(strategyId) | Start a SAML login. |
Manage identities
| Function | Description |
|---|---|
get(id), getAll(queryParams?) | Read identities. |
insert({identifier, password}) | Create an identity. |
update(id, identity) | Update an identity. |
remove(id), removeMany(ids) | Delete identities. |
policy.attach(identityId, policyIds), policy.detach(identityId, policyIds) | Manage policies. |
authfactor.list() | Available second factors. |
authfactor.register(identityId, {type: "totp", config: {}}) | Start enabling a factor. Returns a challenge. |
authfactor.unregister(identityId) | Disable two-factor authentication. |