odin-auth-verify
v0.0.4
Published
Server-side verification of Odin Connect logins: Internet Computer delegation chains, canister signatures and session-key signatures.
Maintainers
Readme
odin-auth-verify
Server-side verification of Odin Connect logins. It checks Internet Computer delegation chains (Ed25519, secp256k1 and canister-signature roots) and the session-key signature (Ed25519 and P-256) that a browser produces when signing in with Odin, so a backend can confirm a user controls the identity they claim.
Status
Pre-release. The package is not published yet and the API is not stable.
Verifying a login
verifyOdinLogin runs every check a backend needs and returns the user's
principal. The browser signs messagePrefix + timestamp with its Odin Connect
session key and sends the delegation chain, timestamp and signature:
// Browser (odin-connect). This package is Node-only, so build the message here;
// it is the same bytes as buildOdinLoginMessage(prefix, timestamp).
const timestamp = Date.now().toString();
const signature = await identity.sign(
new TextEncoder().encode(`my-app-login:v1\n${timestamp}`),
);
// send { delegation: JSON.stringify(identity.getDelegation().toJSON()),
// timestamp, signature: base64(signature) }// Server
import {
OdinLoginError,
createMemoryReplayGuard,
verifyOdinLogin,
} from 'odin-auth-verify';
const replayGuard = createMemoryReplayGuard(); // one process only
try {
const { principal } = await verifyOdinLogin(body, {
appCanisterId: process.env.APP_CANISTER_ID!,
messagePrefix: 'my-app-login:v1\n',
replayGuard,
});
// issue your own session for `principal`
} catch (error) {
if (error instanceof OdinLoginError) {
// reject with 401; error.code says why
}
throw error;
}It checks, in order:
- The timestamp is within
maxSkewMsof the server clock (default 5 minutes). - No delegation in the chain has expired.
- The chain is scoped to
appCanisterId: at least one hop sets targets, and every hop that does includes the app. A delegation issued to another app is rejected. - Every signature in the chain verifies.
- The chain's session key signed
messagePrefix + timestamp. The prefix keeps a login signed for another app, or Odin's own bare-timestamp login, from being accepted. - The login has not been used before.
replayGuard.claimruns only after all other checks pass, so forged requests cannot burn a real login.
createMemoryReplayGuard only works for a single server process. With several
instances, implement ReplayGuard on a shared store; claim(key, expiresAtMs)
must return true exactly once per key, e.g. Redis
SET key 1 NX PXAT expiresAtMs or an insert into a table with a unique key.
Rejected logins throw OdinLoginError with a code: INVALID_INPUT,
TIMESTAMP_OUT_OF_WINDOW, DELEGATION_EXPIRED, NOT_SCOPED_TO_APP,
INVALID_DELEGATION_SIGNATURE, INVALID_SIGNATURE or REPLAYED. Invalid
options (empty prefix, malformed canister ID) throw TypeError.
Local replicas
verifyOdinLogin always verifies canister signatures against the IC mainnet
root key. Against a local replica (dfx, PocketIC), build the verifier once at
startup with its root key:
import { createOdinLoginVerifier } from 'odin-auth-verify';
const verifyLogin = createOdinLoginVerifier({
appCanisterId: process.env.APP_CANISTER_ID!,
messagePrefix: 'my-app-login:v1\n',
replayGuard,
rootKey: localReplicaRootKey, // DER bytes, from `agent.fetchRootKey()`
});
const { principal } = await verifyLogin(body);Whoever controls rootKey can forge any login, so it must come from startup
configuration, never from the request. Omit it in production.
Lower-level API
import {
verifyDelegationChain,
verifySessionKeySignature,
verifyCanisterSignature,
parseCanisterSigPubkey,
} from 'odin-auth-verify';verifyDelegationChain(chain, options?)checks every signature in aDelegationChain. The root may be Ed25519, secp256k1 (Plug) or a canister signature (SIWB, Internet Identity, NFID); later hops are Ed25519. It does not check expiration or targets, so also runisDelegationValid. Delegation expiry is the only time bound: the certificate inside a canister signature is accepted at any age, because canisters sign it when they last updated their certified data, which can be long before the login.verifySessionKeySignature({ signingPubKey, signature, message })checks the login-challenge signature made by an Ed25519 or P-256 session key. It throws on an unrecognised key type; treat a throw as a failed login.verifyCanisterSignature({ rootDer, signatureBlob, message })checks an IC canister signature against the IC root key. It does not check the certificate's time, so the caller must bound replay some other way (for delegations, their expiration).parseCanisterSigPubkey(der)extracts the canister ID and seed from a canister-signature public key.
Canister signatures verify against the Internet Computer mainnet root key by
default. verifyDelegationChain and verifyCanisterSignature accept a
rootKey option for tests and local replicas, like createOdinLoginVerifier;
never pass a key taken from the request being verified.
Requirements
Node.js 22 or later, and @icp-sdk/core 6.x as a peer dependency:
npm install odin-auth-verify @icp-sdk/coreverifyDelegationChain accepts a DelegationChain from either
@icp-sdk/core or the older @dfinity/identity.
Development
npm install
npm run typecheck
npm test
npm run build
npm run lint:pkgReleasing
Releases use release-it and publish from your machine:
- Update
CHANGELOG.mdby hand and merge it tomain. - On an up-to-date
mainwith a clean working tree, runnpm run release.
release-it runs the typecheck, tests, build and package lint, bumps the
version, commits chore: release vX.Y.Z, tags vX.Y.Z, pushes, and publishes
to npm. You need npm login as a package maintainer (npm prompts for a 2FA
code) and push access to the GitHub repo.
"private": true in package.json blocks publishing until the package is
cleared for its first release.
Security
See SECURITY.md to report a vulnerability.
