lemonldap-ng-handler 0.9.5 → 0.10.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 +33 -0
- package/lib/cda.d.mts +64 -0
- package/lib/handlerAuthBasic.d.mts +53 -0
- package/lib/handlerCDA.d.mts +58 -0
- package/lib/handlerDevOps.d.mts +21 -0
- package/lib/handlerDevOpsST.d.mts +9 -0
- package/lib/handlerMain.d.mts +37 -0
- package/lib/handlerMain.d.ts +3 -3
- package/lib/handlerOAuth2.d.mts +140 -0
- package/lib/handlerOAuth2.d.ts +121 -10
- package/lib/handlerServiceToken.d.mts +7 -0
- package/lib/index.d.mts +20 -0
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +431 -91
- package/lib/index.mjs.map +1 -1
- package/lib/init.d.mts +79 -0
- package/lib/msgActions.d.mts +34 -0
- package/lib/tsv.d.mts +84 -0
- package/lib/tsv.d.ts +2 -0
- package/package.json +20 -12
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.10.0
|
|
4
|
+
|
|
5
|
+
- All packages: ship `.d.mts` declarations and declare per-condition
|
|
6
|
+
`types` in the `exports` map, so that TypeScript resolves the types of
|
|
7
|
+
ESM imports under `node16`, `nodenext` and `bundler` module resolution
|
|
8
|
+
instead of failing with TS7016 (#64)
|
|
9
|
+
- All packages: bump to 0.10.0, internal dependencies now require `^0.10.0`
|
|
10
|
+
- handler: complete the OAuth2 handler port
|
|
11
|
+
(`Lemonldap::NG::Handler::Lib::OAuth2`). A Bearer token now designates an
|
|
12
|
+
access token session, which is followed to the user (or offline) session it
|
|
13
|
+
was issued for, instead of being used directly as a user session id. Token
|
|
14
|
+
attributes (`_scope`, `_clientId`, `_clientConfKey`, `_oidc_grant_type`,
|
|
15
|
+
`_audiences`, `_accessToken`) are exposed to rules and headers,
|
|
16
|
+
unauthenticated requests get a `401` with a `WWW-Authenticate: Bearer`
|
|
17
|
+
challenge (RFC 6750) instead of a redirection to the portal, the
|
|
18
|
+
`Authorization` header is hidden from the protected application, and
|
|
19
|
+
`unlog`/`delSession` also purge the OIDC session caches so that a revoked
|
|
20
|
+
token stops being honoured at once. `oidcStorage`, `oidcStorageOptions` and
|
|
21
|
+
`oidcRPMetaDataOptions` are read from the configuration and applied again on
|
|
22
|
+
every reload
|
|
23
|
+
- handler: **breaking** — a user session id is no longer accepted as a Bearer
|
|
24
|
+
token, and a session id not shaped like one (`/^[A-Za-z0-9_-]+$/`) is refused
|
|
25
|
+
before reaching the storage. Deployments using a custom session id generator
|
|
26
|
+
producing other characters must adapt it
|
|
27
|
+
- handler: `fetchId()` may now return a promise, and `retrieveSession()`
|
|
28
|
+
receives the request as an optional second argument
|
|
29
|
+
- jwt: `getAccessTokenSessionId()` only returns a `jti` claim that is a
|
|
30
|
+
non-empty string, as its signature promises
|
|
31
|
+
|
|
32
|
+
## 0.9.6
|
|
33
|
+
|
|
34
|
+
- Update re2 to 1.26.1
|
|
35
|
+
|
|
3
36
|
## 0.9.5
|
|
4
37
|
|
|
5
38
|
- Fix npm audit vulnerabilities and update dependencies
|
package/lib/cda.d.mts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CDA (Cross-Domain Authentication) utilities for LemonLDAP::NG
|
|
3
|
+
* Port of Lemonldap::NG::Handler::Main::CDA
|
|
4
|
+
*
|
|
5
|
+
* CDA allows authentication to be shared across different domains
|
|
6
|
+
* by using encrypted cookies or URL parameters.
|
|
7
|
+
*/
|
|
8
|
+
import Crypto from "@lemonldap-ng/crypto";
|
|
9
|
+
/**
|
|
10
|
+
* CDA cookie value structure
|
|
11
|
+
*/
|
|
12
|
+
export interface CDAValue {
|
|
13
|
+
sessionId: string;
|
|
14
|
+
timestamp: number;
|
|
15
|
+
domain?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a CDA cookie value
|
|
19
|
+
* @param cipher - Crypto instance for encryption
|
|
20
|
+
* @param sessionId - Session ID to encode
|
|
21
|
+
* @param timestamp - Optional timestamp (defaults to now)
|
|
22
|
+
* @returns Encrypted CDA cookie value
|
|
23
|
+
*/
|
|
24
|
+
export declare function createCDACookie(cipher: Crypto, sessionId: string, timestamp?: number): string;
|
|
25
|
+
/**
|
|
26
|
+
* Parse and validate a CDA cookie value
|
|
27
|
+
* @param cipher - Crypto instance for decryption
|
|
28
|
+
* @param cookieValue - Encrypted cookie value
|
|
29
|
+
* @param maxAge - Maximum age in seconds (default: 120)
|
|
30
|
+
* @returns Parsed CDA value or null if invalid
|
|
31
|
+
*/
|
|
32
|
+
export declare function parseCDACookie(cipher: Crypto, cookieValue: string, maxAge?: number): CDAValue | null;
|
|
33
|
+
/**
|
|
34
|
+
* Generate CDA URL parameter
|
|
35
|
+
* @param cipher - Crypto instance
|
|
36
|
+
* @param sessionId - Session ID
|
|
37
|
+
* @param url - Target URL
|
|
38
|
+
* @returns URL with CDA parameter
|
|
39
|
+
*/
|
|
40
|
+
export declare function appendCDAParam(cipher: Crypto, sessionId: string, url: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Extract CDA value from URL
|
|
43
|
+
* @param url - URL potentially containing CDA parameter
|
|
44
|
+
* @returns CDA parameter value or null
|
|
45
|
+
*/
|
|
46
|
+
export declare function extractCDAFromUrl(url: string): string | null;
|
|
47
|
+
/**
|
|
48
|
+
* Build Set-Cookie header for CDA
|
|
49
|
+
* @param cookieName - Cookie name
|
|
50
|
+
* @param value - Cookie value
|
|
51
|
+
* @param domain - Cookie domain
|
|
52
|
+
* @param expiration - Expiration in seconds (0 for session cookie)
|
|
53
|
+
* @param secure - Whether to set Secure flag
|
|
54
|
+
* @param httpOnly - Whether to set HttpOnly flag
|
|
55
|
+
* @returns Cookie header string
|
|
56
|
+
*/
|
|
57
|
+
export declare function buildCDACookieHeader(cookieName: string, value: string, domain: string, expiration?: number, secure?: boolean, httpOnly?: boolean): string;
|
|
58
|
+
/**
|
|
59
|
+
* Delete CDA cookie header
|
|
60
|
+
* @param cookieName - Cookie name
|
|
61
|
+
* @param domain - Cookie domain
|
|
62
|
+
* @returns Cookie header string that expires the cookie
|
|
63
|
+
*/
|
|
64
|
+
export declare function deleteCDACookieHeader(cookieName: string, domain: string): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AuthBasic Handler for LemonLDAP::NG
|
|
3
|
+
* Port of Lemonldap::NG::Handler::Lib::AuthBasic
|
|
4
|
+
*
|
|
5
|
+
* This handler supports HTTP Basic Authentication by:
|
|
6
|
+
* 1. Computing a deterministic session ID from credentials
|
|
7
|
+
* 2. Checking if session exists
|
|
8
|
+
* 3. If not, asking the portal to create it via REST API
|
|
9
|
+
* 4. Sending WWW-Authenticate challenge when needed
|
|
10
|
+
*/
|
|
11
|
+
import express from "express";
|
|
12
|
+
import http from "http";
|
|
13
|
+
import { LLNG_Session } from "@lemonldap-ng/types";
|
|
14
|
+
import LemonldapNGHandler from "./handlerMain.mjs";
|
|
15
|
+
declare class LemonldapNGHandlerAuthBasic extends LemonldapNGHandler {
|
|
16
|
+
private pendingAuth;
|
|
17
|
+
/**
|
|
18
|
+
* Override fetchId to compute session ID from Basic Auth credentials
|
|
19
|
+
* Session ID = sha256(base64_creds + pepper)
|
|
20
|
+
* pepper = int(time / timeout) + keyH (changes daily)
|
|
21
|
+
* @param req - HTTP request
|
|
22
|
+
*/
|
|
23
|
+
fetchId(req: express.Request | http.IncomingMessage): string;
|
|
24
|
+
/**
|
|
25
|
+
* Override retrieveSession to create session via portal if needed
|
|
26
|
+
* @param id - Session ID
|
|
27
|
+
*/
|
|
28
|
+
retrieveSession(id: string): Promise<LLNG_Session>;
|
|
29
|
+
/**
|
|
30
|
+
* Create session via portal REST API
|
|
31
|
+
* POST to portal/sessions/global/{id}?auth
|
|
32
|
+
* @param id - Session ID to create
|
|
33
|
+
* @param username - Username from Basic auth
|
|
34
|
+
* @param password - Password from Basic auth
|
|
35
|
+
* @param xff - X-Forwarded-For header value
|
|
36
|
+
* @returns true if session created successfully
|
|
37
|
+
*/
|
|
38
|
+
private createSession;
|
|
39
|
+
/**
|
|
40
|
+
* Hide Authorization header from protected application
|
|
41
|
+
* Called after successful authentication
|
|
42
|
+
* @param req - HTTP request
|
|
43
|
+
*/
|
|
44
|
+
hideCookie(req: express.Request | http.IncomingMessage): void;
|
|
45
|
+
/**
|
|
46
|
+
* Override goToPortal to send Basic challenge instead of redirect
|
|
47
|
+
* @param res - HTTP response
|
|
48
|
+
* @param uri - Original URI
|
|
49
|
+
* @param args - Additional arguments
|
|
50
|
+
*/
|
|
51
|
+
goToPortal(res: http.ServerResponse, uri: string, args?: string): void;
|
|
52
|
+
}
|
|
53
|
+
export default LemonldapNGHandlerAuthBasic;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CDA (Cross-Domain Authentication) Handler for LemonLDAP::NG
|
|
3
|
+
* Port of Lemonldap::NG::Handler::Main with CDA support
|
|
4
|
+
*
|
|
5
|
+
* This handler enables cross-domain authentication by:
|
|
6
|
+
* 1. Checking for CDA cookies/parameters on incoming requests
|
|
7
|
+
* 2. Setting CDA cookies when users authenticate
|
|
8
|
+
* 3. Propagating authentication across configured domains
|
|
9
|
+
*/
|
|
10
|
+
import express from "express";
|
|
11
|
+
import http from "http";
|
|
12
|
+
import LemonldapNGHandler from "./handlerMain.mjs";
|
|
13
|
+
declare class LemonldapNGHandlerCDA extends LemonldapNGHandler {
|
|
14
|
+
/**
|
|
15
|
+
* Override fetchId to check for CDA cookie/parameter first
|
|
16
|
+
* @param req - HTTP request
|
|
17
|
+
*/
|
|
18
|
+
fetchId(req: express.Request | http.IncomingMessage): string;
|
|
19
|
+
/**
|
|
20
|
+
* Get CDA cookie value from request
|
|
21
|
+
* @param req - HTTP request
|
|
22
|
+
* @param cookieName - Cookie name to look for
|
|
23
|
+
*/
|
|
24
|
+
private getCDACookie;
|
|
25
|
+
/**
|
|
26
|
+
* Set CDA cookie on response (placeholder for middleware integration)
|
|
27
|
+
*
|
|
28
|
+
* TODO: Implement full CDA cookie setting mechanism. Options include:
|
|
29
|
+
* 1. Store the value in req.llngCdaInfo and set in sendHeaders via response
|
|
30
|
+
* 2. Use express middleware to intercept and set the cookie
|
|
31
|
+
* 3. Pass response object through the call chain
|
|
32
|
+
*
|
|
33
|
+
* Currently a no-op as we don't have access to the response object here.
|
|
34
|
+
*
|
|
35
|
+
* @param _req - HTTP request (unused)
|
|
36
|
+
* @param _sessionId - Session ID to set (unused)
|
|
37
|
+
*/
|
|
38
|
+
private setCDACookie;
|
|
39
|
+
/**
|
|
40
|
+
* Override sendHeaders to also set CDA cookie
|
|
41
|
+
* @param req - HTTP request
|
|
42
|
+
* @param session - User session
|
|
43
|
+
*/
|
|
44
|
+
sendHeaders(req: express.Request | http.IncomingMessage, session: import("@lemonldap-ng/types").LLNG_Session): void;
|
|
45
|
+
/**
|
|
46
|
+
* Get domain for CDA cookie
|
|
47
|
+
* @param req - HTTP request
|
|
48
|
+
*/
|
|
49
|
+
private getCDACookieDomain;
|
|
50
|
+
/**
|
|
51
|
+
* Override goToPortal to include CDA parameter
|
|
52
|
+
* @param res - HTTP response
|
|
53
|
+
* @param uri - Original URI
|
|
54
|
+
* @param args - Additional arguments
|
|
55
|
+
*/
|
|
56
|
+
goToPortal(res: http.ServerResponse, uri: string, args?: string): void;
|
|
57
|
+
}
|
|
58
|
+
export default LemonldapNGHandlerCDA;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { LLNG_Session } from "@lemonldap-ng/types";
|
|
2
|
+
import { Handler_Args } from "./init.mjs";
|
|
3
|
+
import LemonldapNGHandler from "./handlerMain.mjs";
|
|
4
|
+
import express from "express";
|
|
5
|
+
import http from "http";
|
|
6
|
+
type Vhost_Config = {
|
|
7
|
+
prot: string;
|
|
8
|
+
host: string;
|
|
9
|
+
path: string;
|
|
10
|
+
port: string;
|
|
11
|
+
headers?: {
|
|
12
|
+
[k: string]: string;
|
|
13
|
+
};
|
|
14
|
+
lb?: boolean;
|
|
15
|
+
};
|
|
16
|
+
declare class LemonldapNGHandlerDevOps extends LemonldapNGHandler {
|
|
17
|
+
constructor(args: Handler_Args);
|
|
18
|
+
grant(req: express.Request | http.IncomingMessage, uri: string, session: LLNG_Session): Promise<boolean>;
|
|
19
|
+
loadVhostConfig(req: express.Request | http.IncomingMessage, vhost: string, lvOpts: Vhost_Config): Promise<boolean>;
|
|
20
|
+
}
|
|
21
|
+
export default LemonldapNGHandlerDevOps;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Request } from "express";
|
|
2
|
+
import { ParamsDictionary } from "express-serve-static-core";
|
|
3
|
+
import { IncomingMessage } from "http";
|
|
4
|
+
import { ParsedQs } from "qs";
|
|
5
|
+
import LemonldapNGHandlerDevOps from "./handlerDevOps.mjs";
|
|
6
|
+
declare class LemonldapNGHandlerDevOpsST extends LemonldapNGHandlerDevOps {
|
|
7
|
+
fetchId(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>> | IncomingMessage): string;
|
|
8
|
+
}
|
|
9
|
+
export default LemonldapNGHandlerDevOpsST;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { LLNG_Session } from "@lemonldap-ng/types";
|
|
2
|
+
import HandlerInit from "./init.mjs";
|
|
3
|
+
import express from "express";
|
|
4
|
+
import http from "http";
|
|
5
|
+
export type FastCGI_Opts = {
|
|
6
|
+
mode: string | undefined;
|
|
7
|
+
port: number | undefined;
|
|
8
|
+
ip: string | undefined;
|
|
9
|
+
};
|
|
10
|
+
declare class LemonldapNGHandler extends HandlerInit {
|
|
11
|
+
protected lastSessionId: string | null;
|
|
12
|
+
private lastSessionData;
|
|
13
|
+
private lastSessionTime;
|
|
14
|
+
init(): Promise<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* run(), the access control function
|
|
17
|
+
*/
|
|
18
|
+
run(req: express.Request | http.IncomingMessage, res: http.ServerResponse, next: Function): any;
|
|
19
|
+
selfUri(vhost: string, uri: string): string;
|
|
20
|
+
nginxServer(fcgiOpt: FastCGI_Opts): void;
|
|
21
|
+
setError(res: http.ServerResponse, uri: string, code: number, txt: string): void;
|
|
22
|
+
isUnprotected(req: express.Request | http.IncomingMessage, uri: string): number | false;
|
|
23
|
+
resolveAlias(req: express.Request | http.IncomingMessage): string;
|
|
24
|
+
fetchId(req: express.Request | http.IncomingMessage): string | Promise<string>;
|
|
25
|
+
retrieveSession(id: string, _req?: express.Request | http.IncomingMessage): Promise<LLNG_Session>;
|
|
26
|
+
/**
|
|
27
|
+
* Local unlog - clear session from all local caches
|
|
28
|
+
* @param sessionId - Optional session ID to clear (uses last cached if not provided)
|
|
29
|
+
*/
|
|
30
|
+
localUnlog(sessionId?: string): Promise<void>;
|
|
31
|
+
grant(req: express.Request | http.IncomingMessage, uri: string, session: LLNG_Session): Promise<boolean>;
|
|
32
|
+
sendHeaders(req: express.Request | http.IncomingMessage, session: LLNG_Session): void;
|
|
33
|
+
hideCookie(req: express.Request | http.IncomingMessage): void;
|
|
34
|
+
forbidden(req: express.Request | http.IncomingMessage, res: http.ServerResponse, session: LLNG_Session): void;
|
|
35
|
+
goToPortal(res: http.ServerResponse, uri: string, args?: string): void;
|
|
36
|
+
}
|
|
37
|
+
export default LemonldapNGHandler;
|
package/lib/handlerMain.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type FastCGI_Opts = {
|
|
|
8
8
|
ip: string | undefined;
|
|
9
9
|
};
|
|
10
10
|
declare class LemonldapNGHandler extends HandlerInit {
|
|
11
|
-
|
|
11
|
+
protected lastSessionId: string | null;
|
|
12
12
|
private lastSessionData;
|
|
13
13
|
private lastSessionTime;
|
|
14
14
|
init(): Promise<boolean>;
|
|
@@ -21,8 +21,8 @@ declare class LemonldapNGHandler extends HandlerInit {
|
|
|
21
21
|
setError(res: http.ServerResponse, uri: string, code: number, txt: string): void;
|
|
22
22
|
isUnprotected(req: express.Request | http.IncomingMessage, uri: string): number | false;
|
|
23
23
|
resolveAlias(req: express.Request | http.IncomingMessage): string;
|
|
24
|
-
fetchId(req: express.Request | http.IncomingMessage): string
|
|
25
|
-
retrieveSession(id: string): Promise<LLNG_Session>;
|
|
24
|
+
fetchId(req: express.Request | http.IncomingMessage): string | Promise<string>;
|
|
25
|
+
retrieveSession(id: string, _req?: express.Request | http.IncomingMessage): Promise<LLNG_Session>;
|
|
26
26
|
/**
|
|
27
27
|
* Local unlog - clear session from all local caches
|
|
28
28
|
* @param sessionId - Optional session ID to clear (uses last cached if not provided)
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth2/OIDC Handler for LemonLDAP::NG
|
|
3
|
+
* Port of Lemonldap::NG::Handler::Lib::OAuth2
|
|
4
|
+
*
|
|
5
|
+
* A Bearer access token is an *opaque credential*: the token itself (or the
|
|
6
|
+
* `jti` claim of its JWT serialization) is the identifier of an access token
|
|
7
|
+
* session created by the Portal. This handler therefore:
|
|
8
|
+
*
|
|
9
|
+
* 1. resolves the access token session in the OIDC session storage,
|
|
10
|
+
* 2. follows it to the user session (or offline session) it was issued for,
|
|
11
|
+
* 3. exposes the token attributes (scope, client, audiences, grant type) as
|
|
12
|
+
* session attributes so that rules and headers can use them.
|
|
13
|
+
*
|
|
14
|
+
* Nothing but the session identifier is ever read from the token: the trust
|
|
15
|
+
* anchor is the session store, not the token payload.
|
|
16
|
+
*/
|
|
17
|
+
import express from "express";
|
|
18
|
+
import http from "http";
|
|
19
|
+
import LemonldapNGHandler from "./handlerMain.mjs";
|
|
20
|
+
import { LLNG_Session } from "@lemonldap-ng/types";
|
|
21
|
+
declare class LemonldapNGHandlerOAuth2 extends LemonldapNGHandler {
|
|
22
|
+
private oidcSessionAcc;
|
|
23
|
+
private ownOidcSessionAcc;
|
|
24
|
+
private state;
|
|
25
|
+
/**
|
|
26
|
+
* Override reload() - and not init() - to set up the OIDC session storage.
|
|
27
|
+
*
|
|
28
|
+
* init() runs once, while reload() runs again on every configuration change
|
|
29
|
+
* announced by the message broker, and it rebuilds the main session
|
|
30
|
+
* accessor. The OIDC accessor must follow: else the handler would keep
|
|
31
|
+
* querying the previous storage (with its own cache), localUnlog() would
|
|
32
|
+
* only purge the new one, and a modified oidcStorage would never be applied.
|
|
33
|
+
*/
|
|
34
|
+
reload(): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* (Re)build the accessor used for access token and offline sessions
|
|
37
|
+
*
|
|
38
|
+
* The new accessor is only installed once it is known to be usable: a
|
|
39
|
+
* reload announcing a broken oidcStorage must leave the handler running on
|
|
40
|
+
* its previous storage rather than answering 401 to every token, and must
|
|
41
|
+
* not leak the accessor it failed to replace (its node-persist timers would
|
|
42
|
+
* keep the process alive).
|
|
43
|
+
*/
|
|
44
|
+
private initOidcSessionStorage;
|
|
45
|
+
/**
|
|
46
|
+
* Override localUnlog to purge the OIDC caches too
|
|
47
|
+
*
|
|
48
|
+
* When the Portal revokes an access token, it broadcasts an unlog/delSession
|
|
49
|
+
* message holding the id of the deleted session. With a dedicated
|
|
50
|
+
* oidcStorage, that session lives in the OIDC accessor, whose caches
|
|
51
|
+
* Main::localUnlog() does not know about: the revoked token would keep
|
|
52
|
+
* authenticating until the cache entry expires.
|
|
53
|
+
* @param sessionId - Session ID to clear (uses last cached if not provided)
|
|
54
|
+
*/
|
|
55
|
+
localUnlog(sessionId?: string): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Override run to bind the per-request state to both req and res: fetchId()
|
|
58
|
+
* and retrieveSession() only see the request, goToPortal() only sees the
|
|
59
|
+
* response.
|
|
60
|
+
*/
|
|
61
|
+
run(req: express.Request | http.IncomingMessage, res: http.ServerResponse, next: Function): any;
|
|
62
|
+
/**
|
|
63
|
+
* Override fetchId to resolve an OAuth2 Bearer token into a session id
|
|
64
|
+
*
|
|
65
|
+
* Contrary to the cookie case, the value carried by the request is *not* the
|
|
66
|
+
* user session id: it identifies an access token session, which in turn
|
|
67
|
+
* references the user session (or an offline session).
|
|
68
|
+
* @param req - HTTP request
|
|
69
|
+
*/
|
|
70
|
+
fetchId(req: express.Request | http.IncomingMessage): Promise<string>;
|
|
71
|
+
/**
|
|
72
|
+
* Override retrieveSession to handle offline sessions and to expose the
|
|
73
|
+
* access token attributes to rules and headers
|
|
74
|
+
* @param id - Session ID, prefixed by "O-" for offline sessions
|
|
75
|
+
* @param req - HTTP request, when available
|
|
76
|
+
*/
|
|
77
|
+
retrieveSession(id: string, req?: express.Request | http.IncomingMessage): Promise<LLNG_Session>;
|
|
78
|
+
/**
|
|
79
|
+
* Hide the access token from the protected application
|
|
80
|
+
* @param req - HTTP request
|
|
81
|
+
*/
|
|
82
|
+
hideCookie(req: express.Request | http.IncomingMessage): void;
|
|
83
|
+
/**
|
|
84
|
+
* A resource server never redirects: answer with a 401 and the challenge
|
|
85
|
+
* described in RFC 6750 section 3
|
|
86
|
+
* @param res - HTTP response
|
|
87
|
+
* @param _uri - Original URI (unused)
|
|
88
|
+
* @param _args - Additional arguments (unused)
|
|
89
|
+
*/
|
|
90
|
+
goToPortal(res: http.ServerResponse, _uri: string, _args?: string): void;
|
|
91
|
+
/**
|
|
92
|
+
* Close the dedicated OIDC session storage, if any
|
|
93
|
+
*/
|
|
94
|
+
stopEventLoop(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Get a session from the OIDC storage and check that it is not expired
|
|
97
|
+
*
|
|
98
|
+
* Note: the session backends do not tell an unknown session apart from an
|
|
99
|
+
* unreachable storage, so both end up as "notFound" here, exactly as in the
|
|
100
|
+
* Perl handler and as in the cookie path of Handler::Main. A storage outage
|
|
101
|
+
* is therefore reported to the client as an invalid token. Fixing this
|
|
102
|
+
* requires a "not found" signal in every @lemonldap-ng/session-* backend.
|
|
103
|
+
* @param id - Access token, offline or OIDC session id
|
|
104
|
+
*/
|
|
105
|
+
private getOIDCInfos;
|
|
106
|
+
/**
|
|
107
|
+
* Collect the access token session fields exposed to rules and headers
|
|
108
|
+
* @param accessToken - Bearer token, as received
|
|
109
|
+
* @param infos - Access token session
|
|
110
|
+
*/
|
|
111
|
+
private tokenAttributes;
|
|
112
|
+
/**
|
|
113
|
+
* Build a copy of the session enriched with the access token attributes.
|
|
114
|
+
* A copy is mandatory: the session object may be shared with the session
|
|
115
|
+
* caches.
|
|
116
|
+
*/
|
|
117
|
+
private withTokenAttributes;
|
|
118
|
+
/**
|
|
119
|
+
* Check that a value may be handed over to the session storage
|
|
120
|
+
*
|
|
121
|
+
* Applied both to the id carried by the token and to the ids read from the
|
|
122
|
+
* access token session: the latter are written by the Portal, but they end
|
|
123
|
+
* up in a LDAP DN or in an URL just the same.
|
|
124
|
+
* @param id - Candidate session id
|
|
125
|
+
*/
|
|
126
|
+
private isSessionId;
|
|
127
|
+
/**
|
|
128
|
+
* Get (or create) the state attached to a request or a response
|
|
129
|
+
*/
|
|
130
|
+
private stateFor;
|
|
131
|
+
/**
|
|
132
|
+
* Reject the request with an "invalid_token" OAuth2 error
|
|
133
|
+
*/
|
|
134
|
+
private invalidToken;
|
|
135
|
+
/**
|
|
136
|
+
* Use the LLNG cookie when the Bearer token can not be resolved
|
|
137
|
+
*/
|
|
138
|
+
private fallbackToCookie;
|
|
139
|
+
}
|
|
140
|
+
export default LemonldapNGHandlerOAuth2;
|
package/lib/handlerOAuth2.d.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* OAuth2/OIDC Handler for LemonLDAP::NG
|
|
3
|
-
* Port of Lemonldap::NG::Handler::OAuth2
|
|
3
|
+
* Port of Lemonldap::NG::Handler::Lib::OAuth2
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* A Bearer access token is an *opaque credential*: the token itself (or the
|
|
6
|
+
* `jti` claim of its JWT serialization) is the identifier of an access token
|
|
7
|
+
* session created by the Portal. This handler therefore:
|
|
8
|
+
*
|
|
9
|
+
* 1. resolves the access token session in the OIDC session storage,
|
|
10
|
+
* 2. follows it to the user session (or offline session) it was issued for,
|
|
11
|
+
* 3. exposes the token attributes (scope, client, audiences, grant type) as
|
|
12
|
+
* session attributes so that rules and headers can use them.
|
|
13
|
+
*
|
|
14
|
+
* Nothing but the session identifier is ever read from the token: the trust
|
|
15
|
+
* anchor is the session store, not the token payload.
|
|
7
16
|
*/
|
|
8
17
|
import express from "express";
|
|
9
18
|
import http from "http";
|
|
@@ -11,19 +20,121 @@ import LemonldapNGHandler from "./handlerMain";
|
|
|
11
20
|
import { LLNG_Session } from "@lemonldap-ng/types";
|
|
12
21
|
declare class LemonldapNGHandlerOAuth2 extends LemonldapNGHandler {
|
|
13
22
|
private oidcSessionAcc;
|
|
23
|
+
private ownOidcSessionAcc;
|
|
24
|
+
private state;
|
|
25
|
+
/**
|
|
26
|
+
* Override reload() - and not init() - to set up the OIDC session storage.
|
|
27
|
+
*
|
|
28
|
+
* init() runs once, while reload() runs again on every configuration change
|
|
29
|
+
* announced by the message broker, and it rebuilds the main session
|
|
30
|
+
* accessor. The OIDC accessor must follow: else the handler would keep
|
|
31
|
+
* querying the previous storage (with its own cache), localUnlog() would
|
|
32
|
+
* only purge the new one, and a modified oidcStorage would never be applied.
|
|
33
|
+
*/
|
|
34
|
+
reload(): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* (Re)build the accessor used for access token and offline sessions
|
|
37
|
+
*
|
|
38
|
+
* The new accessor is only installed once it is known to be usable: a
|
|
39
|
+
* reload announcing a broken oidcStorage must leave the handler running on
|
|
40
|
+
* its previous storage rather than answering 401 to every token, and must
|
|
41
|
+
* not leak the accessor it failed to replace (its node-persist timers would
|
|
42
|
+
* keep the process alive).
|
|
43
|
+
*/
|
|
44
|
+
private initOidcSessionStorage;
|
|
45
|
+
/**
|
|
46
|
+
* Override localUnlog to purge the OIDC caches too
|
|
47
|
+
*
|
|
48
|
+
* When the Portal revokes an access token, it broadcasts an unlog/delSession
|
|
49
|
+
* message holding the id of the deleted session. With a dedicated
|
|
50
|
+
* oidcStorage, that session lives in the OIDC accessor, whose caches
|
|
51
|
+
* Main::localUnlog() does not know about: the revoked token would keep
|
|
52
|
+
* authenticating until the cache entry expires.
|
|
53
|
+
* @param sessionId - Session ID to clear (uses last cached if not provided)
|
|
54
|
+
*/
|
|
55
|
+
localUnlog(sessionId?: string): Promise<void>;
|
|
14
56
|
/**
|
|
15
|
-
* Override
|
|
57
|
+
* Override run to bind the per-request state to both req and res: fetchId()
|
|
58
|
+
* and retrieveSession() only see the request, goToPortal() only sees the
|
|
59
|
+
* response.
|
|
16
60
|
*/
|
|
17
|
-
|
|
61
|
+
run(req: express.Request | http.IncomingMessage, res: http.ServerResponse, next: Function): any;
|
|
18
62
|
/**
|
|
19
|
-
* Override fetchId to
|
|
63
|
+
* Override fetchId to resolve an OAuth2 Bearer token into a session id
|
|
64
|
+
*
|
|
65
|
+
* Contrary to the cookie case, the value carried by the request is *not* the
|
|
66
|
+
* user session id: it identifies an access token session, which in turn
|
|
67
|
+
* references the user session (or an offline session).
|
|
20
68
|
* @param req - HTTP request
|
|
21
69
|
*/
|
|
22
|
-
fetchId(req: express.Request | http.IncomingMessage): string
|
|
70
|
+
fetchId(req: express.Request | http.IncomingMessage): Promise<string>;
|
|
71
|
+
/**
|
|
72
|
+
* Override retrieveSession to handle offline sessions and to expose the
|
|
73
|
+
* access token attributes to rules and headers
|
|
74
|
+
* @param id - Session ID, prefixed by "O-" for offline sessions
|
|
75
|
+
* @param req - HTTP request, when available
|
|
76
|
+
*/
|
|
77
|
+
retrieveSession(id: string, req?: express.Request | http.IncomingMessage): Promise<LLNG_Session>;
|
|
78
|
+
/**
|
|
79
|
+
* Hide the access token from the protected application
|
|
80
|
+
* @param req - HTTP request
|
|
81
|
+
*/
|
|
82
|
+
hideCookie(req: express.Request | http.IncomingMessage): void;
|
|
83
|
+
/**
|
|
84
|
+
* A resource server never redirects: answer with a 401 and the challenge
|
|
85
|
+
* described in RFC 6750 section 3
|
|
86
|
+
* @param res - HTTP response
|
|
87
|
+
* @param _uri - Original URI (unused)
|
|
88
|
+
* @param _args - Additional arguments (unused)
|
|
89
|
+
*/
|
|
90
|
+
goToPortal(res: http.ServerResponse, _uri: string, _args?: string): void;
|
|
91
|
+
/**
|
|
92
|
+
* Close the dedicated OIDC session storage, if any
|
|
93
|
+
*/
|
|
94
|
+
stopEventLoop(): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Get a session from the OIDC storage and check that it is not expired
|
|
97
|
+
*
|
|
98
|
+
* Note: the session backends do not tell an unknown session apart from an
|
|
99
|
+
* unreachable storage, so both end up as "notFound" here, exactly as in the
|
|
100
|
+
* Perl handler and as in the cookie path of Handler::Main. A storage outage
|
|
101
|
+
* is therefore reported to the client as an invalid token. Fixing this
|
|
102
|
+
* requires a "not found" signal in every @lemonldap-ng/session-* backend.
|
|
103
|
+
* @param id - Access token, offline or OIDC session id
|
|
104
|
+
*/
|
|
105
|
+
private getOIDCInfos;
|
|
106
|
+
/**
|
|
107
|
+
* Collect the access token session fields exposed to rules and headers
|
|
108
|
+
* @param accessToken - Bearer token, as received
|
|
109
|
+
* @param infos - Access token session
|
|
110
|
+
*/
|
|
111
|
+
private tokenAttributes;
|
|
112
|
+
/**
|
|
113
|
+
* Build a copy of the session enriched with the access token attributes.
|
|
114
|
+
* A copy is mandatory: the session object may be shared with the session
|
|
115
|
+
* caches.
|
|
116
|
+
*/
|
|
117
|
+
private withTokenAttributes;
|
|
118
|
+
/**
|
|
119
|
+
* Check that a value may be handed over to the session storage
|
|
120
|
+
*
|
|
121
|
+
* Applied both to the id carried by the token and to the ids read from the
|
|
122
|
+
* access token session: the latter are written by the Portal, but they end
|
|
123
|
+
* up in a LDAP DN or in an URL just the same.
|
|
124
|
+
* @param id - Candidate session id
|
|
125
|
+
*/
|
|
126
|
+
private isSessionId;
|
|
127
|
+
/**
|
|
128
|
+
* Get (or create) the state attached to a request or a response
|
|
129
|
+
*/
|
|
130
|
+
private stateFor;
|
|
131
|
+
/**
|
|
132
|
+
* Reject the request with an "invalid_token" OAuth2 error
|
|
133
|
+
*/
|
|
134
|
+
private invalidToken;
|
|
23
135
|
/**
|
|
24
|
-
*
|
|
25
|
-
* @param id - Session ID
|
|
136
|
+
* Use the LLNG cookie when the Bearer token can not be resolved
|
|
26
137
|
*/
|
|
27
|
-
|
|
138
|
+
private fallbackToCookie;
|
|
28
139
|
}
|
|
29
140
|
export default LemonldapNGHandlerOAuth2;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import http from "http";
|
|
3
|
+
import LemonldapNGHandler from "./handlerMain.mjs";
|
|
4
|
+
declare class LemonldapNGHandlerServiceToken extends LemonldapNGHandler {
|
|
5
|
+
fetchId(req: express.Request | http.IncomingMessage): string;
|
|
6
|
+
}
|
|
7
|
+
export default LemonldapNGHandlerServiceToken;
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import http from "http";
|
|
3
|
+
import { Handler_Args } from "./init.mjs";
|
|
4
|
+
import LemonldapNGHandler, { FastCGI_Opts } from "./handlerMain.mjs";
|
|
5
|
+
import LemonldapNGHandlerDevOps from "./handlerDevOps.mjs";
|
|
6
|
+
declare let currentClass: typeof LemonldapNGHandler | typeof LemonldapNGHandlerDevOps;
|
|
7
|
+
export declare const init: (args: Handler_Args) => Promise<LemonldapNGHandler>;
|
|
8
|
+
export declare const run: (req: express.Request | http.IncomingMessage, res: http.ServerResponse, next: Function) => any;
|
|
9
|
+
export declare const nginxServer: (args: FastCGI_Opts) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Returns the portal URL from LemonLDAP::NG configuration
|
|
12
|
+
* @returns Portal URL string
|
|
13
|
+
*/
|
|
14
|
+
export declare const portal: () => string;
|
|
15
|
+
/**
|
|
16
|
+
* Shutdown the handler (stops event loop and cleanup)
|
|
17
|
+
* Call this in tests' afterAll to prevent Jest from hanging
|
|
18
|
+
*/
|
|
19
|
+
export declare const shutdown: () => Promise<void>;
|
|
20
|
+
export { currentClass as class };
|