@feasibleone/blong-login 1.11.1 → 1.13.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/CHANGELOG.md +19 -0
- package/browser/orchestrator/subject/init.ts +19 -0
- package/browser.ts +31 -0
- package/config/heft.json +23 -0
- package/error/error.ts +25 -0
- package/eslint.config.mjs +2 -0
- package/gateway/login/login.oidc.getConfiguration.ts +13 -0
- package/gateway/login/login.oidc.getKeys.ts +13 -0
- package/gateway/login/login.token.create.ts +20 -0
- package/gateway/login/login.token.exchange.ts +24 -0
- package/gateway/login/login.token.refresh.ts +14 -0
- package/gateway/login/login.token.restore.ts +13 -0
- package/gateway/login/login.token.revoke.ts +16 -0
- package/orchestrator/login/loginOidcGetConfiguration.ts +31 -0
- package/orchestrator/login/loginOidcGetKeys.ts +9 -0
- package/orchestrator/login/loginTokenCreate.ts +245 -0
- package/orchestrator/login/loginTokenExchange.ts +66 -0
- package/orchestrator/login/loginTokenRefresh.ts +252 -0
- package/orchestrator/login/loginTokenRestore.ts +205 -0
- package/orchestrator/login/loginTokenRevoke.ts +69 -0
- package/orchestrator/login/refresh.ts +22 -0
- package/orchestrator/login/sessionLib.ts +227 -0
- package/orchestrator/login/token.ts +112 -0
- package/orchestrator/loginDispatch.ts +17 -0
- package/package.json +3 -3
- package/server.ts +124 -0
- package/test/test/testUserAdminLogin.ts +13 -0
- package/tsconfig.json +27 -0
- package/types/ut-function.cbc/index.d.ts +6 -0
- package/types/ut-function.cbc/package.json +4 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.13.0](https://github.com/feasibleone/blong/compare/blong-login-v1.12.0...blong-login-v1.13.0) (2026-10-13)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* blong-kustomize improvements ([0a07e50](https://github.com/feasibleone/blong/commit/0a07e50275208a3fd8a23476e0abe9334ec3822d))
|
|
9
|
+
|
|
10
|
+
## [1.12.0](https://github.com/feasibleone/blong/compare/blong-login-v1.11.1...blong-login-v1.12.0) (2026-10-11)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* blong-kustomize ([0ad6915](https://github.com/feasibleone/blong/commit/0ad6915af0d3c300a8741a509232aec7649999a1))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* address CI issues ([bea0f62](https://github.com/feasibleone/blong/commit/bea0f62aba2dcef95cc4363b02c4ee3e05c620b1))
|
|
21
|
+
|
|
3
22
|
## [1.11.1](https://github.com/feasibleone/blong/compare/blong-login-v1.11.0...blong-login-v1.11.1) (2026-09-29)
|
|
4
23
|
|
|
5
24
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {handler} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Browser-side login subject namespace.
|
|
5
|
+
*
|
|
6
|
+
* Lets the portal/backend adapter dispatch `login.*` methods
|
|
7
|
+
* (`login.token.create` / `login.token.refresh` / `login.token.restore` /
|
|
8
|
+
* `login.token.revoke`, `login.oidc.*`, …) to the server gateway — the same
|
|
9
|
+
* way blong-access exposes `access.*` via its own
|
|
10
|
+
* `browser/orchestrator/subject/init.ts`. Without this file the browser has
|
|
11
|
+
* no binding for the `login` namespace, so callers must use the explicit
|
|
12
|
+
* `backend/login.*` form instead.
|
|
13
|
+
*
|
|
14
|
+
* The folder name `subject` stays LITERAL — only the `namespace` value is the
|
|
15
|
+
* realm's subject.
|
|
16
|
+
*/
|
|
17
|
+
export default handler(() => ({
|
|
18
|
+
namespace: 'login',
|
|
19
|
+
}));
|
package/browser.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
import {realm} from '@feasibleone/blong';
|
|
3
|
+
|
|
4
|
+
export default realm(blong => ({
|
|
5
|
+
url: import.meta.url,
|
|
6
|
+
validation: blong.type.Object({}),
|
|
7
|
+
// `./browser` carries the browser-side subject namespace declaration
|
|
8
|
+
// (`browser/orchestrator/subject/init.ts`) so the portal/backend adapter
|
|
9
|
+
// can dispatch `login.*` calls; `./test` is the browser test layer.
|
|
10
|
+
//
|
|
11
|
+
// On the browser platform (Vite bundle, `window` defined) folder strings
|
|
12
|
+
// are skipped — only path-keyed loaders are discovered — so the browser
|
|
13
|
+
// layer is declared with the canonical `import.meta.glob`. The glob keys
|
|
14
|
+
// (e.g. `./browser/orchestrator/subject/init.ts`) nest under the well-known
|
|
15
|
+
// `browser/orchestrator` layer, so the child resolves to the same name and
|
|
16
|
+
// activation config as the explicit path-keyed form. The tap/ts-node
|
|
17
|
+
// runner uses the folder strings (non-window branch).
|
|
18
|
+
children: globalThis.window
|
|
19
|
+
? import.meta.glob(['./browser/**/*.ts'])
|
|
20
|
+
: ['./browser', './test'],
|
|
21
|
+
config: {
|
|
22
|
+
default: {
|
|
23
|
+
browser: {},
|
|
24
|
+
},
|
|
25
|
+
dev: {},
|
|
26
|
+
microservice: {},
|
|
27
|
+
integration: {
|
|
28
|
+
test: true,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
}));
|
package/config/heft.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://developer.microsoft.com/json-schemas/heft/v0/heft.schema.json",
|
|
3
|
+
|
|
4
|
+
"phasesByName": {
|
|
5
|
+
// Define a phase whose name is "build"
|
|
6
|
+
"build": {
|
|
7
|
+
"phaseDescription": "This phase compiles the project source code.",
|
|
8
|
+
|
|
9
|
+
// Before invoking the compiler, delete the "dist" and "lib" folders
|
|
10
|
+
"cleanFiles": [{"sourcePath": "dist"}, {"sourcePath": "lib"}],
|
|
11
|
+
|
|
12
|
+
"tasksByName": {
|
|
13
|
+
// Define a task whose name is "typescript"
|
|
14
|
+
"typescript": {
|
|
15
|
+
"taskPlugin": {
|
|
16
|
+
// This task will invoke the TypeScript plugin
|
|
17
|
+
"pluginPackage": "@rushstack/heft-typescript-plugin"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
package/error/error.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
'login.refreshTokenExpired': {
|
|
3
|
+
message: 'Refresh token has expired',
|
|
4
|
+
statusCode: 401,
|
|
5
|
+
silent: true,
|
|
6
|
+
},
|
|
7
|
+
'login.invalidRefreshToken': {message: 'Invalid refresh token', statusCode: 401},
|
|
8
|
+
'login.sessionNotFound': {message: 'Session not found', statusCode: 401},
|
|
9
|
+
'login.sessionRevoked': {message: 'Session has been revoked', statusCode: 401},
|
|
10
|
+
'login.sessionInactive': {message: 'Session is inactive', statusCode: 401},
|
|
11
|
+
'login.sessionExpired': {message: 'Session has expired', statusCode: 401, silent: true},
|
|
12
|
+
'login.invalidCookie': {message: 'Invalid session cookie', statusCode: 200, silent: true},
|
|
13
|
+
'login.sessionRestoreFailed': {message: 'Session restore failed', statusCode: 401},
|
|
14
|
+
// Login-eligibility gates — a user who is deactivated or no longer holds
|
|
15
|
+
// the `accessLogin` action is refused at login and at every renewal/restore.
|
|
16
|
+
'login.userInactive': {message: 'User account is inactive', statusCode: 401},
|
|
17
|
+
'login.loginNotAllowed': {message: 'User is not allowed to log in', statusCode: 401},
|
|
18
|
+
// Configurable-methods errors — a required `login.methods.*` entry is
|
|
19
|
+
// missing/disabled in this deployment, or a session-backed operation was
|
|
20
|
+
// called while session management is turned off (lightweight suites).
|
|
21
|
+
'login.configurationError': {
|
|
22
|
+
message: 'Login is not configured for this operation',
|
|
23
|
+
statusCode: 400,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {validation} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
export default validation(
|
|
4
|
+
async () =>
|
|
5
|
+
function loginOidcGetConfiguration() {
|
|
6
|
+
return {
|
|
7
|
+
security: true,
|
|
8
|
+
method: 'GET',
|
|
9
|
+
path: '/.well-known/openid-configuration',
|
|
10
|
+
auth: false,
|
|
11
|
+
};
|
|
12
|
+
},
|
|
13
|
+
);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {validation} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
export default validation(
|
|
4
|
+
async ({lib: {type}}) =>
|
|
5
|
+
function loginTokenCreate() {
|
|
6
|
+
return {
|
|
7
|
+
auth: 'login',
|
|
8
|
+
params: type.Object({
|
|
9
|
+
grantType: type.Optional(
|
|
10
|
+
type.Union([type.Literal('password'), type.Literal('client_credentials')]),
|
|
11
|
+
),
|
|
12
|
+
username: type.Optional(type.String()),
|
|
13
|
+
password: type.Optional(type.String()),
|
|
14
|
+
clientId: type.Optional(type.String()),
|
|
15
|
+
clientSecret: type.Optional(type.String()),
|
|
16
|
+
}),
|
|
17
|
+
result: type.Object({}, {additionalProperties: true}),
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {validation} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Public RPC endpoint for the Google OAuth code exchange (no blong token
|
|
5
|
+
* exists yet — the caller only holds a Google authorization code). Uses
|
|
6
|
+
* `auth: 'login'` so the gateway's MLE layer decrypts the request body
|
|
7
|
+
* (same pre-auth level as `login.token.create`).
|
|
8
|
+
*/
|
|
9
|
+
export default validation(
|
|
10
|
+
async ({lib: {type}}) =>
|
|
11
|
+
function loginTokenExchange() {
|
|
12
|
+
return {
|
|
13
|
+
auth: 'login',
|
|
14
|
+
params: type.Object({
|
|
15
|
+
provider: type.String(),
|
|
16
|
+
code: type.String(),
|
|
17
|
+
state: type.Optional(type.String()),
|
|
18
|
+
redirectUri: type.Optional(type.String()),
|
|
19
|
+
flow: type.Optional(type.Union([type.Literal('oidc'), type.Literal('oauth')])),
|
|
20
|
+
}),
|
|
21
|
+
result: type.Object({}, {additionalProperties: true}),
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {validation} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
export default validation(
|
|
4
|
+
async ({lib: {type}}) =>
|
|
5
|
+
function loginTokenRefresh() {
|
|
6
|
+
return {
|
|
7
|
+
auth: 'login',
|
|
8
|
+
params: type.Object({
|
|
9
|
+
refreshToken: type.String(),
|
|
10
|
+
}),
|
|
11
|
+
result: type.Object({}, {additionalProperties: true}),
|
|
12
|
+
};
|
|
13
|
+
},
|
|
14
|
+
);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {validation} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
export default validation(
|
|
4
|
+
async ({lib: {type}}) =>
|
|
5
|
+
function loginTokenRestore() {
|
|
6
|
+
return {
|
|
7
|
+
// Pre-auth — restores a session from the path-scoped HttpOnly cookie.
|
|
8
|
+
auth: 'login',
|
|
9
|
+
params: type.Object({}, {additionalProperties: true}),
|
|
10
|
+
result: type.Object({}, {additionalProperties: true}),
|
|
11
|
+
};
|
|
12
|
+
},
|
|
13
|
+
);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {validation} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
export default validation(
|
|
4
|
+
async ({lib: {type}}) =>
|
|
5
|
+
function loginTokenRevoke() {
|
|
6
|
+
return {
|
|
7
|
+
// Bearer-authenticated (session id comes from the token's `ses`
|
|
8
|
+
// claim) but self-service — skip the RBAC action-list check.
|
|
9
|
+
skipAuthorize: true,
|
|
10
|
+
params: type.Object({
|
|
11
|
+
sessionId: type.Optional(type.String()),
|
|
12
|
+
}),
|
|
13
|
+
result: type.Object({}, {additionalProperties: true}),
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {type IMeta, handler} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
export default handler(
|
|
4
|
+
() =>
|
|
5
|
+
function loginOidcGetConfiguration(
|
|
6
|
+
params: unknown,
|
|
7
|
+
{httpRequest: {url: urlRequest, headers} = {url: '', headers: {}}}: IMeta,
|
|
8
|
+
) {
|
|
9
|
+
const url = new URL(urlRequest);
|
|
10
|
+
if (headers['x-forwarded-host']) {
|
|
11
|
+
url.port = ''; // WTF WHATWG!
|
|
12
|
+
url.host =
|
|
13
|
+
typeof headers['x-forwarded-host'] === 'string'
|
|
14
|
+
? headers['x-forwarded-host']
|
|
15
|
+
: headers['x-forwarded-host'][0];
|
|
16
|
+
if (headers['x-forwarded-proto'])
|
|
17
|
+
url.protocol =
|
|
18
|
+
typeof headers['x-forwarded-proto'] === 'string'
|
|
19
|
+
? headers['x-forwarded-proto']
|
|
20
|
+
: headers['x-forwarded-proto'][0];
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
issuer: 'blong-login',
|
|
24
|
+
id_token_signing_alg_values_supported: ['RS256'],
|
|
25
|
+
code_challenge_methods_supported: ['S256'],
|
|
26
|
+
authorization_endpoint: new URL('../form', url.href).href,
|
|
27
|
+
token_endpoint: new URL('../token', url.href).href,
|
|
28
|
+
jwks_uri: new URL('../jwks', url.href).href,
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
);
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import {type IMeta, handler} from '@feasibleone/blong';
|
|
2
|
+
import crypto from 'node:crypto';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
type ITokenResult,
|
|
6
|
+
type SessionConfig,
|
|
7
|
+
SESSION_COOKIE_DEFAULT,
|
|
8
|
+
sessionCleanupParams,
|
|
9
|
+
} from './sessionLib.ts';
|
|
10
|
+
|
|
11
|
+
type CredentialCheckResult = {
|
|
12
|
+
userId: string;
|
|
13
|
+
userKey: string;
|
|
14
|
+
credentialId: number;
|
|
15
|
+
permissionMap: string;
|
|
16
|
+
actions: string[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type ClientCredentialCheckResult = {
|
|
20
|
+
applicationId: string;
|
|
21
|
+
isActive: boolean;
|
|
22
|
+
applicationKey: string;
|
|
23
|
+
credentialId: number;
|
|
24
|
+
permissionMap: string;
|
|
25
|
+
actions: string[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* OAuth token endpoint (JSON-RPC `login.token.create`).
|
|
30
|
+
*
|
|
31
|
+
* Supports two grant types:
|
|
32
|
+
* - `password` (default) — resource-owner password flow via `access.credential.check`.
|
|
33
|
+
* - `client_credentials` — verifies an application's clientId/clientSecret via
|
|
34
|
+
* `access.credential.checkClient` and mints a standard JWT whose `per` carries
|
|
35
|
+
* the application's subscribed bundle roleBits. Authorization is then uniform
|
|
36
|
+
* in the gateway jwt plugin (the same `access.authorization.list` as users).
|
|
37
|
+
*
|
|
38
|
+
* On success a DB-backed session is created (`access.session.create`), the JWT
|
|
39
|
+
* `ses` claim carries its real id, an opaque restore cookie is set (password
|
|
40
|
+
* grant, path-scoped to the restore endpoint) and the login event is recorded
|
|
41
|
+
* in the audit. Failures are audited too (success=false) then re-thrown.
|
|
42
|
+
*/
|
|
43
|
+
export default handler(
|
|
44
|
+
({
|
|
45
|
+
errors,
|
|
46
|
+
config,
|
|
47
|
+
lib: {methods = {}, token, sessionCookieOptions, newCookieHandle, sha256Hex},
|
|
48
|
+
}) => {
|
|
49
|
+
const sessionConfig = config as SessionConfig;
|
|
50
|
+
const SESSION_COOKIE = sessionConfig.session?.cookieName ?? SESSION_COOKIE_DEFAULT;
|
|
51
|
+
return async function loginTokenCreate(
|
|
52
|
+
{
|
|
53
|
+
grantType = 'password',
|
|
54
|
+
username,
|
|
55
|
+
password,
|
|
56
|
+
clientId,
|
|
57
|
+
clientSecret,
|
|
58
|
+
}: {
|
|
59
|
+
grantType?: 'password' | 'client_credentials';
|
|
60
|
+
username?: string;
|
|
61
|
+
password?: string;
|
|
62
|
+
clientId?: string;
|
|
63
|
+
clientSecret?: string;
|
|
64
|
+
},
|
|
65
|
+
$meta: IMeta,
|
|
66
|
+
) {
|
|
67
|
+
const ipAddress = ($meta as {ipAddress?: string}).ipAddress;
|
|
68
|
+
const cookieOptions = sessionCookieOptions(sessionConfig);
|
|
69
|
+
const recordAudit = (entry: {
|
|
70
|
+
userId?: string;
|
|
71
|
+
actorId?: string;
|
|
72
|
+
credentialType?: string;
|
|
73
|
+
isSuccess: boolean;
|
|
74
|
+
failureReason?: string;
|
|
75
|
+
sessionId?: string;
|
|
76
|
+
}) =>
|
|
77
|
+
methods.auditRecord
|
|
78
|
+
? (
|
|
79
|
+
methods.auditRecord(
|
|
80
|
+
{
|
|
81
|
+
audit: [
|
|
82
|
+
{
|
|
83
|
+
...entry,
|
|
84
|
+
actionName: 'login',
|
|
85
|
+
credentialType:
|
|
86
|
+
entry.credentialType ??
|
|
87
|
+
(grantType === 'client_credentials'
|
|
88
|
+
? 'clientSecret'
|
|
89
|
+
: 'password'),
|
|
90
|
+
ipAddress,
|
|
91
|
+
statusCode: entry.isSuccess ? 200 : 401,
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
$meta,
|
|
96
|
+
) as Promise<{inserted: number}>
|
|
97
|
+
).catch(() => undefined)
|
|
98
|
+
: Promise.resolve(undefined);
|
|
99
|
+
|
|
100
|
+
if (grantType === 'client_credentials') {
|
|
101
|
+
if (!methods.credentialCheckClient) {
|
|
102
|
+
throw errors['login.configurationError']({
|
|
103
|
+
params: {method: 'credentialCheckClient'},
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
let result: ClientCredentialCheckResult;
|
|
107
|
+
try {
|
|
108
|
+
result = (await methods.credentialCheckClient(
|
|
109
|
+
{clientId: clientId!, clientSecret: clientSecret!},
|
|
110
|
+
// Forward $meta for tracing – the handler proxy attaches
|
|
111
|
+
// the method name and routing info automatically.
|
|
112
|
+
$meta,
|
|
113
|
+
)) as ClientCredentialCheckResult;
|
|
114
|
+
} catch (error) {
|
|
115
|
+
await recordAudit({
|
|
116
|
+
actorId: clientId!,
|
|
117
|
+
isSuccess: false,
|
|
118
|
+
failureReason: (error as {type?: string}).type,
|
|
119
|
+
});
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
await recordAudit({
|
|
123
|
+
actorId: result.applicationId,
|
|
124
|
+
isSuccess: true,
|
|
125
|
+
});
|
|
126
|
+
const sessionId = crypto.randomUUID();
|
|
127
|
+
const tokenResult = (await token({
|
|
128
|
+
clientId: clientId!,
|
|
129
|
+
actorId: result.applicationId,
|
|
130
|
+
sessionId,
|
|
131
|
+
language: 'en',
|
|
132
|
+
refresh: '',
|
|
133
|
+
permissionMap: result.permissionMap,
|
|
134
|
+
mlek: $meta?.auth?.mlek,
|
|
135
|
+
mlsk: $meta?.auth?.mlsk,
|
|
136
|
+
actions: result.actions,
|
|
137
|
+
})) as ITokenResult;
|
|
138
|
+
// App tokens (client_credentials) are long-lived machine
|
|
139
|
+
// credentials — no interactive session is created (no refresh
|
|
140
|
+
// rotation, inactivity tracking or restore cookie applies).
|
|
141
|
+
return tokenResult;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!methods.credentialCheck) {
|
|
145
|
+
throw errors['login.configurationError']({params: {method: 'credentialCheck'}});
|
|
146
|
+
}
|
|
147
|
+
let result: CredentialCheckResult;
|
|
148
|
+
try {
|
|
149
|
+
result = (await methods.credentialCheck(
|
|
150
|
+
{username: username!, password: password!},
|
|
151
|
+
// Forward $meta for tracing – the handler proxy attaches
|
|
152
|
+
// the method name and routing info automatically.
|
|
153
|
+
$meta,
|
|
154
|
+
)) as CredentialCheckResult;
|
|
155
|
+
} catch (error) {
|
|
156
|
+
await recordAudit({
|
|
157
|
+
actorId: username!,
|
|
158
|
+
isSuccess: false,
|
|
159
|
+
failureReason: (error as {type?: string}).type,
|
|
160
|
+
});
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Login-eligibility gate (role-based): the user must hold the
|
|
165
|
+
// `accessLogin` action (via role → capability → action). Removing
|
|
166
|
+
// it from a role disables logins for that role.
|
|
167
|
+
if (!result.actions?.includes('accessLogin')) {
|
|
168
|
+
await recordAudit({
|
|
169
|
+
actorId: username!,
|
|
170
|
+
isSuccess: false,
|
|
171
|
+
failureReason: 'login.loginNotAllowed',
|
|
172
|
+
});
|
|
173
|
+
throw errors['login.loginNotAllowed']();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const sessionId = crypto.randomUUID();
|
|
177
|
+
const cookieHandle = newCookieHandle();
|
|
178
|
+
|
|
179
|
+
// Best-effort profile resolution so the UI can apply the user's
|
|
180
|
+
// preferred language right after login. The login succeeds without
|
|
181
|
+
// it — a missing/disabled profile handler just yields 'en'.
|
|
182
|
+
let profile: {actorId?: string; language?: string} | undefined;
|
|
183
|
+
let language = 'en';
|
|
184
|
+
if (methods.profileGet) {
|
|
185
|
+
try {
|
|
186
|
+
const profileData = (await methods.profileGet(
|
|
187
|
+
{},
|
|
188
|
+
{...$meta, auth: {...$meta.auth, actorId: result.userId}},
|
|
189
|
+
)) as {preferredLanguage?: string | null} | undefined;
|
|
190
|
+
language = profileData?.preferredLanguage ?? 'en';
|
|
191
|
+
profile = {actorId: result.userId, language};
|
|
192
|
+
} catch {
|
|
193
|
+
// Profile is optional login metadata — never fails the login.
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const tokenResult = (await token({
|
|
198
|
+
clientId: username!,
|
|
199
|
+
actorId: result.userId,
|
|
200
|
+
sessionId,
|
|
201
|
+
language,
|
|
202
|
+
refresh: '',
|
|
203
|
+
permissionMap: result.permissionMap,
|
|
204
|
+
mlek: $meta?.auth?.mlek,
|
|
205
|
+
mlsk: $meta?.auth?.mlsk,
|
|
206
|
+
actions: result.actions,
|
|
207
|
+
profile,
|
|
208
|
+
})) as ITokenResult;
|
|
209
|
+
if (methods.sessionCreate) {
|
|
210
|
+
await (methods.sessionCreate(
|
|
211
|
+
{
|
|
212
|
+
userId: result.userKey,
|
|
213
|
+
credentialId: result.credentialId,
|
|
214
|
+
tokenHash: sha256Hex(tokenResult.refresh_token),
|
|
215
|
+
expiresAt: new Date(
|
|
216
|
+
Date.now() + tokenResult.refresh_token_expires_in * 1000,
|
|
217
|
+
),
|
|
218
|
+
ipAddress,
|
|
219
|
+
sessionId,
|
|
220
|
+
cookieHash: sha256Hex(cookieHandle),
|
|
221
|
+
},
|
|
222
|
+
$meta,
|
|
223
|
+
) as Promise<{sessionId: string; userId: string}>);
|
|
224
|
+
}
|
|
225
|
+
if (methods.sessionCleanup) {
|
|
226
|
+
await (
|
|
227
|
+
methods.sessionCleanup(sessionCleanupParams(sessionConfig), $meta) as Promise<{
|
|
228
|
+
deleted: number;
|
|
229
|
+
}>
|
|
230
|
+
).catch(() => undefined);
|
|
231
|
+
}
|
|
232
|
+
await recordAudit({
|
|
233
|
+
userId: result.userKey,
|
|
234
|
+
sessionId,
|
|
235
|
+
isSuccess: true,
|
|
236
|
+
});
|
|
237
|
+
// Set the restore cookie via $meta.httpResponse (the codebase
|
|
238
|
+
// pattern — the gateway applies it as Set-Cookie on the response).
|
|
239
|
+
$meta.httpResponse = {
|
|
240
|
+
state: [[SESSION_COOKIE, cookieHandle, cookieOptions]],
|
|
241
|
+
};
|
|
242
|
+
return tokenResult;
|
|
243
|
+
};
|
|
244
|
+
},
|
|
245
|
+
);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {type IMeta, handler} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
type IdentityCheckResult = {
|
|
4
|
+
userId: string;
|
|
5
|
+
permissionMap: string;
|
|
6
|
+
actions: string[];
|
|
7
|
+
isNewUser: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Social login token exchange — the counterpart of `login.token.create` for
|
|
12
|
+
* OAuth code flows. Delegates identity resolution to the configured method
|
|
13
|
+
* (`access.identity.check` by default — override via `login.methods.identityCheck`)
|
|
14
|
+
* and mints a JWT with the resolved permission map. `flow` selects the Google
|
|
15
|
+
* identity flow per call (`oidc` default, `oauth`).
|
|
16
|
+
*/
|
|
17
|
+
export default handler(
|
|
18
|
+
({errors, lib: {methods = {}, token}}) => {
|
|
19
|
+
const identityCheck = methods.identityCheck;
|
|
20
|
+
return async function loginTokenExchange(
|
|
21
|
+
params: {
|
|
22
|
+
provider: string;
|
|
23
|
+
code: string;
|
|
24
|
+
state?: string;
|
|
25
|
+
redirectUri?: string;
|
|
26
|
+
flow?: 'oidc' | 'oauth';
|
|
27
|
+
},
|
|
28
|
+
$meta: IMeta,
|
|
29
|
+
) {
|
|
30
|
+
if (!identityCheck) {
|
|
31
|
+
throw errors['login.configurationError']({params: {method: 'identityCheck'}});
|
|
32
|
+
}
|
|
33
|
+
const {userId, permissionMap, actions, isNewUser} = (await identityCheck(
|
|
34
|
+
{
|
|
35
|
+
provider: params.provider,
|
|
36
|
+
code: params.code,
|
|
37
|
+
redirectUri: params.redirectUri,
|
|
38
|
+
flow: params.flow,
|
|
39
|
+
},
|
|
40
|
+
// Forward $meta for tracing – the handler proxy attaches
|
|
41
|
+
// the method name and routing info automatically.
|
|
42
|
+
$meta,
|
|
43
|
+
)) as IdentityCheckResult;
|
|
44
|
+
|
|
45
|
+
// Login-eligibility gate (role-based) — same rule as password login:
|
|
46
|
+
// the user must hold the `accessLogin` action.
|
|
47
|
+
if (!actions?.includes('accessLogin')) {
|
|
48
|
+
throw errors['login.loginNotAllowed']();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const result = (await token({
|
|
52
|
+
clientId: params.provider,
|
|
53
|
+
actorId: userId,
|
|
54
|
+
sessionId: 'session',
|
|
55
|
+
language: 'en',
|
|
56
|
+
refresh: '',
|
|
57
|
+
permissionMap,
|
|
58
|
+
mlek: $meta?.auth?.mlek,
|
|
59
|
+
mlsk: $meta?.auth?.mlsk,
|
|
60
|
+
actions,
|
|
61
|
+
})) as Record<string, unknown>;
|
|
62
|
+
|
|
63
|
+
return {...result, isNewUser};
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
);
|