@comity/auth-tokens 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Filippo Bovo and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # @comity/auth-tokens
2
+
3
+ Token envelope and facade primitives for Comity.
4
+
5
+ ---
6
+
7
+ ## Purpose
8
+
9
+ Defines token-facing contracts used to structure and issue authentication tokens. Provides a default facade that transports and authentication adapters consume without coupling to a specific token implementation.
10
+
11
+ ---
12
+
13
+ ## Scope
14
+
15
+ This package:
16
+
17
+ - ✅ defines token envelope contracts for issued tokens
18
+ - ✅ provides a token facade contract and a default implementation
19
+ - ✅ exposes typed input structure for token issuance
20
+
21
+ This package does NOT:
22
+
23
+ - ❌ verify or sign tokens
24
+ - ❌ manage authentication sessions or assurance
25
+ - ❌ implement transport-level concerns
26
+
27
+ ---
28
+
29
+ ## Public API
30
+
31
+ - `AuthTokenEnvelope` — structured wrapper for issued token pairs
32
+ - `AuthTokenFacade` — domain-level contract for token operations
33
+ - `IssueTokensInput` — typed input for token issuance
34
+ - `DefaultAuthTokenFacade` — default encapsulation of token issuance
35
+
36
+ No exhaustive reference; see docs for constraints.
37
+
38
+ ---
39
+
40
+ ## Documentation
41
+
42
+ - docs/overview.md
43
+ - docs/conventions.md
44
+
45
+ ---
46
+
47
+ ## Related Packages
48
+
49
+ - @comity/auth — authentication domain contracts
50
+ - @comity/auth-jose — JOSE token adapter
51
+
52
+ ---
53
+
54
+ ## Status
55
+
56
+ Stable
57
+
58
+ _Review Completed: 2026-07-25_
59
+ _Reviewer: Hobiri MAGI (DeepSeek v4 Pro)_
60
+ _Compliance Score: 99.5% (Green)_
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=envelope.js.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=facade.js.map
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultAuthTokenFacade = void 0;
4
+ const errors_1 = require("@comity/auth/errors");
5
+ /**
6
+ * Default implementation of the AuthTokenFacade.
7
+ */
8
+ class DefaultAuthTokenFacade {
9
+ /** */
10
+ #auth;
11
+ /** */
12
+ #tokens;
13
+ /**
14
+ * @param auth - The authentication facade to use for session management
15
+ * @param tokens - The token service to use for token operations
16
+ */
17
+ constructor(auth, tokens) {
18
+ this.#auth = auth;
19
+ this.#tokens = tokens;
20
+ }
21
+ /**
22
+ * @inheritdoc
23
+ */
24
+ async authenticate(token, now) {
25
+ const result = await this.#tokens.verifyAccessToken(token);
26
+ if (!result.ok) {
27
+ return result;
28
+ }
29
+ try {
30
+ this.#auth.assertSession(result.value, now);
31
+ }
32
+ catch (error) {
33
+ if (error instanceof errors_1.AuthError) {
34
+ return {
35
+ ok: false,
36
+ error,
37
+ };
38
+ }
39
+ return {
40
+ ok: false,
41
+ error: new errors_1.AuthError("internal_error", {
42
+ details: {
43
+ policy: "authentication",
44
+ subject: result.value.id.toString(),
45
+ },
46
+ cause: error instanceof Error ? error : undefined,
47
+ }),
48
+ };
49
+ }
50
+ return result;
51
+ }
52
+ /**
53
+ * @inheritdoc
54
+ */
55
+ async issueTokens(input, now) {
56
+ // 1. Create session
57
+ const session = await this.#auth.createSession(input, now);
58
+ if (!session.ok) {
59
+ return session;
60
+ }
61
+ // 2. Sign tokens
62
+ const accessToken = await this.#tokens.signAccessToken(session.value);
63
+ if (!accessToken.ok) {
64
+ return accessToken;
65
+ }
66
+ // 3. Sign refresh token
67
+ const refreshToken = await this.#tokens.signRefreshToken(session.value);
68
+ if (!refreshToken.ok) {
69
+ return refreshToken;
70
+ }
71
+ return {
72
+ ok: true,
73
+ value: {
74
+ session: session.value,
75
+ accessToken: accessToken.value,
76
+ refreshToken: refreshToken.value,
77
+ },
78
+ };
79
+ }
80
+ /**
81
+ * @inheritdoc
82
+ */
83
+ async refreshTokens(token, id, now) {
84
+ // 1. Verify refresh token
85
+ const verified = await this.#tokens.verifyRefreshToken(token);
86
+ if (!verified.ok) {
87
+ return verified;
88
+ }
89
+ // 2. Refresh session
90
+ const session = await this.#auth.refreshSession({
91
+ id,
92
+ originalId: verified.value.id,
93
+ }, now);
94
+ if (!session.ok) {
95
+ return session;
96
+ }
97
+ // 3. Sign new tokens
98
+ const accessToken = await this.#tokens.signAccessToken(session.value);
99
+ if (!accessToken.ok) {
100
+ return accessToken;
101
+ }
102
+ // 4. Sign new refresh token
103
+ const refreshToken = await this.#tokens.signRefreshToken(session.value);
104
+ if (!refreshToken.ok) {
105
+ return refreshToken;
106
+ }
107
+ return {
108
+ ok: true,
109
+ value: {
110
+ session: session.value,
111
+ accessToken: accessToken.value,
112
+ refreshToken: refreshToken.value,
113
+ },
114
+ };
115
+ }
116
+ }
117
+ exports.DefaultAuthTokenFacade = DefaultAuthTokenFacade;
118
+ //# sourceMappingURL=default.js.map
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultAuthTokenFacade = void 0;
4
+ var default_js_1 = require("./facade/default.js");
5
+ Object.defineProperty(exports, "DefaultAuthTokenFacade", { enumerable: true, get: function () { return default_js_1.DefaultAuthTokenFacade; } });
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=envelope.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=facade.js.map
@@ -0,0 +1,114 @@
1
+ import { AuthError } from "@comity/auth/errors";
2
+ /**
3
+ * Default implementation of the AuthTokenFacade.
4
+ */
5
+ export class DefaultAuthTokenFacade {
6
+ /** */
7
+ #auth;
8
+ /** */
9
+ #tokens;
10
+ /**
11
+ * @param auth - The authentication facade to use for session management
12
+ * @param tokens - The token service to use for token operations
13
+ */
14
+ constructor(auth, tokens) {
15
+ this.#auth = auth;
16
+ this.#tokens = tokens;
17
+ }
18
+ /**
19
+ * @inheritdoc
20
+ */
21
+ async authenticate(token, now) {
22
+ const result = await this.#tokens.verifyAccessToken(token);
23
+ if (!result.ok) {
24
+ return result;
25
+ }
26
+ try {
27
+ this.#auth.assertSession(result.value, now);
28
+ }
29
+ catch (error) {
30
+ if (error instanceof AuthError) {
31
+ return {
32
+ ok: false,
33
+ error,
34
+ };
35
+ }
36
+ return {
37
+ ok: false,
38
+ error: new AuthError("internal_error", {
39
+ details: {
40
+ policy: "authentication",
41
+ subject: result.value.id.toString(),
42
+ },
43
+ cause: error instanceof Error ? error : undefined,
44
+ }),
45
+ };
46
+ }
47
+ return result;
48
+ }
49
+ /**
50
+ * @inheritdoc
51
+ */
52
+ async issueTokens(input, now) {
53
+ // 1. Create session
54
+ const session = await this.#auth.createSession(input, now);
55
+ if (!session.ok) {
56
+ return session;
57
+ }
58
+ // 2. Sign tokens
59
+ const accessToken = await this.#tokens.signAccessToken(session.value);
60
+ if (!accessToken.ok) {
61
+ return accessToken;
62
+ }
63
+ // 3. Sign refresh token
64
+ const refreshToken = await this.#tokens.signRefreshToken(session.value);
65
+ if (!refreshToken.ok) {
66
+ return refreshToken;
67
+ }
68
+ return {
69
+ ok: true,
70
+ value: {
71
+ session: session.value,
72
+ accessToken: accessToken.value,
73
+ refreshToken: refreshToken.value,
74
+ },
75
+ };
76
+ }
77
+ /**
78
+ * @inheritdoc
79
+ */
80
+ async refreshTokens(token, id, now) {
81
+ // 1. Verify refresh token
82
+ const verified = await this.#tokens.verifyRefreshToken(token);
83
+ if (!verified.ok) {
84
+ return verified;
85
+ }
86
+ // 2. Refresh session
87
+ const session = await this.#auth.refreshSession({
88
+ id,
89
+ originalId: verified.value.id,
90
+ }, now);
91
+ if (!session.ok) {
92
+ return session;
93
+ }
94
+ // 3. Sign new tokens
95
+ const accessToken = await this.#tokens.signAccessToken(session.value);
96
+ if (!accessToken.ok) {
97
+ return accessToken;
98
+ }
99
+ // 4. Sign new refresh token
100
+ const refreshToken = await this.#tokens.signRefreshToken(session.value);
101
+ if (!refreshToken.ok) {
102
+ return refreshToken;
103
+ }
104
+ return {
105
+ ok: true,
106
+ value: {
107
+ session: session.value,
108
+ accessToken: accessToken.value,
109
+ refreshToken: refreshToken.value,
110
+ },
111
+ };
112
+ }
113
+ }
114
+ //# sourceMappingURL=default.js.map
@@ -0,0 +1,2 @@
1
+ export { DefaultAuthTokenFacade } from "./facade/default.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,12 @@
1
+ import type { AuthSession } from "@comity/auth";
2
+ /**
3
+ * Data structure returned when multiple tokens are issued.
4
+ */
5
+ export interface AuthTokenEnvelope {
6
+ /** The issued access token. */
7
+ readonly accessToken: string;
8
+ /** The issued refresh token, if applicable. */
9
+ readonly refreshToken?: string;
10
+ /** The session associated with the issued tokens. */
11
+ readonly session: AuthSession;
12
+ }
@@ -0,0 +1,58 @@
1
+ import type { AuthSession, AuthSessionAssuranceInput, AuthSessionId, AuthSessionTransport } from "@comity/auth";
2
+ import type { AuthError } from "@comity/auth/errors";
3
+ import type { Result } from "@comity/primitives/result";
4
+ import type { AuthTokenEnvelope } from "./envelope.js";
5
+ /**
6
+ * Input data required to issue new tokens, including session creation parameters and optional refresh capabilities.
7
+ */
8
+ export interface IssueTokensInput extends AuthSessionAssuranceInput {
9
+ /** Session identifier */
10
+ readonly id: AuthSessionId;
11
+ /** Hard expiration */
12
+ readonly expiresAt?: number;
13
+ /** Session transport mechanism */
14
+ readonly transport: AuthSessionTransport;
15
+ /** Refresh capabilities */
16
+ readonly refresh?: false | number;
17
+ /** Step-up parent */
18
+ readonly parent?: AuthSessionId;
19
+ /** Authorization scopes */
20
+ readonly scopes?: readonly string[];
21
+ }
22
+ /**
23
+ * AuthTokenFacade provides a simplified interface for authentication operations, abstracting away the underlying complexities of token management and session handling.
24
+ */
25
+ export interface AuthTokenFacade {
26
+ /**
27
+ * Authenticates a request by verifying the access token and
28
+ * asserting that the underlying session is still valid.
29
+ * (Verify Token -> Get Session -> Assert Session)
30
+ *
31
+ * @param token - The access token to authenticate
32
+ * @param now - Current timestamp for session validation
33
+ *
34
+ * @returns The result of the authentication, containing either the auth session or an auth error
35
+ */
36
+ authenticate(token: string, now: number): Promise<Result<AuthSession, AuthError, "ok">>;
37
+ /**
38
+ * Issues a new set of tokens for a given session creation input.
39
+ * (Create Session -> Sign Access Token -> Sign Refresh Token)
40
+ *
41
+ * @param input - The input data required to create a new session
42
+ * @param now - Current timestamp for session creation
43
+ *
44
+ * @returns The result of the token issuance, containing either the token envelope or an auth error
45
+ */
46
+ issueTokens(input: IssueTokensInput, now: number): Promise<Result<AuthTokenEnvelope, AuthError, "ok">>;
47
+ /**
48
+ * Refreshes an existing session using a refresh token.
49
+ * (Verify Refresh Token -> Refresh Session -> Sign New Tokens)
50
+ *
51
+ * @param token - The refresh token to use for refreshing the session
52
+ * @param id - The new session ID to assign to the refreshed session
53
+ * @param now - Current timestamp for session refresh
54
+ *
55
+ * @returns The result of the token refresh, containing either the new token envelope or an auth error
56
+ */
57
+ refreshTokens(token: string, id: AuthSessionId, now: number): Promise<Result<AuthTokenEnvelope, AuthError, "ok">>;
58
+ }
@@ -0,0 +1,28 @@
1
+ import type { AuthFacade, AuthSession, AuthSessionId, AuthTokenService } from "@comity/auth";
2
+ import type { Result } from "@comity/primitives/result";
3
+ import type { AuthTokenEnvelope } from "../contracts/envelope";
4
+ import type { AuthTokenFacade, IssueTokensInput } from "../contracts/facade";
5
+ import { AuthError } from "@comity/auth/errors";
6
+ /**
7
+ * Default implementation of the AuthTokenFacade.
8
+ */
9
+ export declare class DefaultAuthTokenFacade implements AuthTokenFacade {
10
+ #private;
11
+ /**
12
+ * @param auth - The authentication facade to use for session management
13
+ * @param tokens - The token service to use for token operations
14
+ */
15
+ constructor(auth: AuthFacade, tokens: AuthTokenService);
16
+ /**
17
+ * @inheritdoc
18
+ */
19
+ authenticate(token: string, now: number): Promise<Result<AuthSession, AuthError, "ok">>;
20
+ /**
21
+ * @inheritdoc
22
+ */
23
+ issueTokens(input: IssueTokensInput, now: number): Promise<Result<AuthTokenEnvelope, AuthError, "ok">>;
24
+ /**
25
+ * @inheritdoc
26
+ */
27
+ refreshTokens(token: string, id: AuthSessionId, now: number): Promise<Result<AuthTokenEnvelope, AuthError, "ok">>;
28
+ }
@@ -0,0 +1,3 @@
1
+ export type { AuthTokenEnvelope } from "./contracts/envelope.js";
2
+ export type { AuthTokenFacade, IssueTokensInput } from "./contracts/facade.js";
3
+ export { DefaultAuthTokenFacade } from "./facade/default.js";
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@comity/auth-tokens",
3
+ "version": "0.9.0",
4
+ "description": "Token envelope and facade primitives for Comity.",
5
+ "type": "module",
6
+ "private": false,
7
+ "author": "Filippo Bovo <hello@filippobovo.com>",
8
+ "license": "MIT",
9
+ "comity": {
10
+ "layer": "core"
11
+ },
12
+ "homepage": "https://github.com/comityjs/framework#readme",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/comityjs/framework.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/comityjs/framework/issues"
19
+ },
20
+ "engines": {
21
+ "node": ">=24.0.0"
22
+ },
23
+ "keywords": [
24
+ "comity",
25
+ "comityjs",
26
+ "auth",
27
+ "authentication",
28
+ "jwt",
29
+ "refresh-token",
30
+ "security",
31
+ "session",
32
+ "token",
33
+ "typescript"
34
+ ],
35
+ "files": [
36
+ "./dist",
37
+ "!./dist/**/*.map"
38
+ ],
39
+ "main": "./dist/cjs/index.js",
40
+ "module": "./dist/esm/index.js",
41
+ "types": "./dist/types/index.d.ts",
42
+ "exports": {
43
+ ".": {
44
+ "import": {
45
+ "types": "./dist/types/index.d.ts",
46
+ "default": "./dist/esm/index.js"
47
+ },
48
+ "require": {
49
+ "types": "./dist/types/index.d.ts",
50
+ "default": "./dist/cjs/index.js"
51
+ }
52
+ },
53
+ "./package.json": "./package.json"
54
+ },
55
+ "typesVersions": {
56
+ "*": {}
57
+ },
58
+ "publishConfig": {
59
+ "registry": "https://registry.npmjs.org",
60
+ "access": "public"
61
+ },
62
+ "sideEffects": false,
63
+ "dependencies": {
64
+ "@comity/auth": "0.9.0",
65
+ "@comity/primitives": "0.9.0"
66
+ },
67
+ "devDependencies": {
68
+ "@types/node": "^24.13.4",
69
+ "typescript": "^5.9.3"
70
+ },
71
+ "scripts": {
72
+ "build": "node ../../scripts/build.mjs",
73
+ "dev": "node ../../scripts/build.mjs --watch",
74
+ "test": "vitest run --coverage",
75
+ "type-check": "tsc -p tsconfig.json --noEmit",
76
+ "lint": "eslint --ext .ts src"
77
+ }
78
+ }