@logto/js 0.1.1-rc.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.
@@ -0,0 +1,2 @@
1
+ import { Requester } from '../utils';
2
+ export declare const revoke: (revocationEndpoint: string, clientId: string, token: string, requester: Requester) => Promise<void>;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.revoke = void 0;
40
+ var consts_1 = require("../consts");
41
+ var revoke = function (revocationEndpoint, clientId, token, requester) { return __awaiter(void 0, void 0, void 0, function () {
42
+ var _a;
43
+ return __generator(this, function (_b) {
44
+ return [2 /*return*/, requester(revocationEndpoint, {
45
+ method: 'POST',
46
+ headers: consts_1.ContentType.formUrlEncoded,
47
+ body: new URLSearchParams((_a = {},
48
+ _a[consts_1.QueryKey.ClientId] = clientId,
49
+ _a[consts_1.QueryKey.Token] = token,
50
+ _a)),
51
+ })];
52
+ });
53
+ }); };
54
+ exports.revoke = revoke;
@@ -0,0 +1,10 @@
1
+ export declare type SignInUriParameters = {
2
+ authorizationEndpoint: string;
3
+ clientId: string;
4
+ redirectUri: string;
5
+ codeChallenge: string;
6
+ state: string;
7
+ scopes?: string[];
8
+ resources?: string[];
9
+ };
10
+ export declare const generateSignInUri: ({ authorizationEndpoint, clientId, redirectUri, codeChallenge, state, scopes, resources, }: SignInUriParameters) => string;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateSignInUri = void 0;
4
+ var consts_1 = require("../consts");
5
+ var utils_1 = require("../utils");
6
+ var codeChallengeMethod = 'S256';
7
+ var prompt = 'consent';
8
+ var responseType = 'code';
9
+ var generateSignInUri = function (_a) {
10
+ var _b;
11
+ var authorizationEndpoint = _a.authorizationEndpoint, clientId = _a.clientId, redirectUri = _a.redirectUri, codeChallenge = _a.codeChallenge, state = _a.state, scopes = _a.scopes, resources = _a.resources;
12
+ var urlSearchParameters = new URLSearchParams((_b = {},
13
+ _b[consts_1.QueryKey.ClientId] = clientId,
14
+ _b[consts_1.QueryKey.RedirectUri] = redirectUri,
15
+ _b[consts_1.QueryKey.CodeChallenge] = codeChallenge,
16
+ _b[consts_1.QueryKey.CodeChallengeMethod] = codeChallengeMethod,
17
+ _b[consts_1.QueryKey.State] = state,
18
+ _b[consts_1.QueryKey.ResponseType] = responseType,
19
+ _b[consts_1.QueryKey.Prompt] = prompt,
20
+ _b[consts_1.QueryKey.Scope] = (0, utils_1.withReservedScopes)(scopes),
21
+ _b));
22
+ for (var _i = 0, _c = resources !== null && resources !== void 0 ? resources : []; _i < _c.length; _i++) {
23
+ var resource = _c[_i];
24
+ urlSearchParameters.append(consts_1.QueryKey.Resource, resource);
25
+ }
26
+ return "".concat(authorizationEndpoint, "?").concat(urlSearchParameters.toString());
27
+ };
28
+ exports.generateSignInUri = generateSignInUri;
@@ -0,0 +1,7 @@
1
+ declare type SignOutUriParameters = {
2
+ endSessionEndpoint: string;
3
+ idToken: string;
4
+ postLogoutRedirectUri?: string;
5
+ };
6
+ export declare const generateSignOutUri: ({ endSessionEndpoint, idToken, postLogoutRedirectUri, }: SignOutUriParameters) => string;
7
+ export {};
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateSignOutUri = void 0;
4
+ var consts_1 = require("../consts");
5
+ var generateSignOutUri = function (_a) {
6
+ var _b;
7
+ var endSessionEndpoint = _a.endSessionEndpoint, idToken = _a.idToken, postLogoutRedirectUri = _a.postLogoutRedirectUri;
8
+ var urlSearchParameters = new URLSearchParams((_b = {}, _b[consts_1.QueryKey.IdTokenHint] = idToken, _b));
9
+ if (postLogoutRedirectUri) {
10
+ urlSearchParameters.append(consts_1.QueryKey.PostLogoutRedirectUri, postLogoutRedirectUri);
11
+ }
12
+ return "".concat(endSessionEndpoint, "?").concat(urlSearchParameters.toString());
13
+ };
14
+ exports.generateSignOutUri = generateSignOutUri;
@@ -0,0 +1,5 @@
1
+ import { Requester } from '../utils';
2
+ export declare type UserInfoResponse = {
3
+ sub: string;
4
+ };
5
+ export declare const fetchUserInfo: (userInfoEndpoint: string, accessToken: string, requester: Requester) => Promise<UserInfoResponse>;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.fetchUserInfo = void 0;
40
+ var fetchUserInfo = function (userInfoEndpoint, accessToken, requester) { return __awaiter(void 0, void 0, void 0, function () {
41
+ return __generator(this, function (_a) {
42
+ return [2 /*return*/, requester(userInfoEndpoint, {
43
+ headers: { Authorization: "Bearer ".concat(accessToken) },
44
+ })];
45
+ });
46
+ }); };
47
+ exports.fetchUserInfo = fetchUserInfo;
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './core';
2
+ export * from './utils';
3
+ export * from './consts';
package/lib/index.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ /* istanbul ignore file */
14
+ __exportStar(require("./core"), exports);
15
+ __exportStar(require("./utils"), exports);
16
+ __exportStar(require("./consts"), exports);
@@ -0,0 +1,2 @@
1
+ export declare const parseUriParameters: (uri: string) => URLSearchParams;
2
+ export declare const verifyAndParseCodeFromCallbackUri: (callbackUri: string, redirectUri: string, state: string) => string;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.verifyAndParseCodeFromCallbackUri = exports.parseUriParameters = void 0;
4
+ var consts_1 = require("../consts");
5
+ var errors_1 = require("./errors");
6
+ var parseUriParameters = function (uri) {
7
+ var _a = uri.split('?'), _b = _a[1], queryString = _b === void 0 ? '' : _b;
8
+ return new URLSearchParams(queryString);
9
+ };
10
+ exports.parseUriParameters = parseUriParameters;
11
+ var verifyAndParseCodeFromCallbackUri = function (callbackUri, redirectUri, state) {
12
+ if (!callbackUri.startsWith(redirectUri)) {
13
+ throw new errors_1.LogtoError('callback_uri_verification.redirect_uri_mismatched');
14
+ }
15
+ var uriParameters = (0, exports.parseUriParameters)(callbackUri);
16
+ var error = uriParameters.get(consts_1.QueryKey.Error);
17
+ var errorDescription = uriParameters.get(consts_1.QueryKey.ErrorDescription);
18
+ if (error) {
19
+ throw new errors_1.LogtoError('callback_uri_verification.error_found', {
20
+ error: error,
21
+ errorDescription: errorDescription,
22
+ });
23
+ }
24
+ var stateFromCallbackUri = uriParameters.get(consts_1.QueryKey.State);
25
+ if (!stateFromCallbackUri) {
26
+ throw new errors_1.LogtoError('callback_uri_verification.missing_state');
27
+ }
28
+ if (stateFromCallbackUri !== state) {
29
+ throw new errors_1.LogtoError('callback_uri_verification.state_mismatched');
30
+ }
31
+ var code = uriParameters.get(consts_1.QueryKey.Code);
32
+ if (!code) {
33
+ throw new errors_1.LogtoError('callback_uri_verification.missing_code');
34
+ }
35
+ return code;
36
+ };
37
+ exports.verifyAndParseCodeFromCallbackUri = verifyAndParseCodeFromCallbackUri;
@@ -0,0 +1,28 @@
1
+ import { NormalizeKeyPaths } from '@silverhand/essentials';
2
+ declare const logtoErrorCodes: Readonly<{
3
+ id_token: {
4
+ invalid_iat: string;
5
+ invalid_token: string;
6
+ };
7
+ callback_uri_verification: {
8
+ redirect_uri_mismatched: string;
9
+ error_found: string;
10
+ missing_state: string;
11
+ state_mismatched: string;
12
+ missing_code: string;
13
+ };
14
+ requester: {
15
+ not_provide_fetch: string;
16
+ };
17
+ }>;
18
+ export declare type LogtoErrorCode = NormalizeKeyPaths<typeof logtoErrorCodes>;
19
+ export declare class LogtoError extends Error {
20
+ code: LogtoErrorCode;
21
+ data: unknown;
22
+ constructor(code: LogtoErrorCode, data?: unknown);
23
+ }
24
+ export declare class LogtoRequestError extends Error {
25
+ code: string;
26
+ constructor(code: string, message: string);
27
+ }
28
+ export {};
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.LogtoRequestError = exports.LogtoError = void 0;
22
+ var lodash_get_1 = __importDefault(require("lodash.get"));
23
+ var logtoErrorCodes = Object.freeze({
24
+ id_token: {
25
+ invalid_iat: 'Invalid issued at time',
26
+ invalid_token: 'Invalid token',
27
+ },
28
+ callback_uri_verification: {
29
+ redirect_uri_mismatched: 'Redirect URI mismatched',
30
+ error_found: 'Error found',
31
+ missing_state: 'Missing state',
32
+ state_mismatched: 'State mismatched',
33
+ missing_code: 'Missing code',
34
+ },
35
+ requester: {
36
+ not_provide_fetch: 'Should provide a fetch function under Node.js',
37
+ },
38
+ });
39
+ var getMessageByErrorCode = function (errorCode) {
40
+ // TODO: linear issue LOG-1419
41
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
42
+ var message = (0, lodash_get_1.default)(logtoErrorCodes, errorCode);
43
+ if (typeof message === 'string') {
44
+ return message;
45
+ }
46
+ return errorCode;
47
+ };
48
+ var LogtoError = /** @class */ (function (_super) {
49
+ __extends(LogtoError, _super);
50
+ function LogtoError(code, data) {
51
+ var _this = _super.call(this, getMessageByErrorCode(code)) || this;
52
+ _this.code = code;
53
+ _this.data = data;
54
+ return _this;
55
+ }
56
+ return LogtoError;
57
+ }(Error));
58
+ exports.LogtoError = LogtoError;
59
+ var LogtoRequestError = /** @class */ (function (_super) {
60
+ __extends(LogtoRequestError, _super);
61
+ function LogtoRequestError(code, message) {
62
+ var _this = _super.call(this, message) || this;
63
+ _this.code = code;
64
+ return _this;
65
+ }
66
+ return LogtoRequestError;
67
+ }(Error));
68
+ exports.LogtoRequestError = LogtoRequestError;
@@ -0,0 +1,18 @@
1
+ /** @link [Proof Key for Code Exchange by OAuth Public Clients](https://datatracker.ietf.org/doc/html/rfc7636) */
2
+ /**
3
+ * Generates random string for state and encodes them in url safe base64
4
+ */
5
+ export declare const generateState: () => string;
6
+ /**
7
+ * Generates code verifier
8
+ *
9
+ * @link [Client Creates a Code Verifier](https://datatracker.ietf.org/doc/html/rfc7636#section-4.1)
10
+ */
11
+ export declare const generateCodeVerifier: () => string;
12
+ /**
13
+ * Calculates the S256 PKCE code challenge for an arbitrary code verifier and encodes it in url safe base64
14
+ *
15
+ * @param {String} codeVerifier Code verifier to calculate the S256 code challenge for
16
+ * @link [Client Creates the Code Challenge](https://datatracker.ietf.org/doc/html/rfc7636#section-4.2)
17
+ */
18
+ export declare const generateCodeChallenge: (codeVerifier: string) => Promise<string>;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ /** @link [Proof Key for Code Exchange by OAuth Public Clients](https://datatracker.ietf.org/doc/html/rfc7636) */
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ var __generator = (this && this.__generator) || function (thisArg, body) {
13
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
15
+ function verb(n) { return function (v) { return step([n, v]); }; }
16
+ function step(op) {
17
+ if (f) throw new TypeError("Generator is already executing.");
18
+ while (_) try {
19
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
20
+ if (y = 0, t) op = [op[0] & 2, t.value];
21
+ switch (op[0]) {
22
+ case 0: case 1: t = op; break;
23
+ case 4: _.label++; return { value: op[1], done: false };
24
+ case 5: _.label++; y = op[1]; op = [0]; continue;
25
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
26
+ default:
27
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
28
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
29
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
30
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
31
+ if (t[2]) _.ops.pop();
32
+ _.trys.pop(); continue;
33
+ }
34
+ op = body.call(thisArg, _);
35
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
36
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37
+ }
38
+ };
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.generateCodeChallenge = exports.generateCodeVerifier = exports.generateState = void 0;
41
+ var js_base64_1 = require("js-base64");
42
+ /**
43
+ * @param length The length of the raw random data.
44
+ */
45
+ var generateRandomString = function (length) {
46
+ if (length === void 0) { length = 64; }
47
+ return (0, js_base64_1.fromUint8Array)(crypto.getRandomValues(new Uint8Array(length)), true);
48
+ };
49
+ /**
50
+ * Generates random string for state and encodes them in url safe base64
51
+ */
52
+ var generateState = function () { return generateRandomString(); };
53
+ exports.generateState = generateState;
54
+ /**
55
+ * Generates code verifier
56
+ *
57
+ * @link [Client Creates a Code Verifier](https://datatracker.ietf.org/doc/html/rfc7636#section-4.1)
58
+ */
59
+ var generateCodeVerifier = function () { return generateRandomString(); };
60
+ exports.generateCodeVerifier = generateCodeVerifier;
61
+ /**
62
+ * Calculates the S256 PKCE code challenge for an arbitrary code verifier and encodes it in url safe base64
63
+ *
64
+ * @param {String} codeVerifier Code verifier to calculate the S256 code challenge for
65
+ * @link [Client Creates the Code Challenge](https://datatracker.ietf.org/doc/html/rfc7636#section-4.2)
66
+ */
67
+ var generateCodeChallenge = function (codeVerifier) { return __awaiter(void 0, void 0, void 0, function () {
68
+ var encodedCodeVerifier, codeChallenge, _a;
69
+ return __generator(this, function (_b) {
70
+ switch (_b.label) {
71
+ case 0:
72
+ encodedCodeVerifier = new TextEncoder().encode(codeVerifier);
73
+ _a = Uint8Array.bind;
74
+ return [4 /*yield*/, crypto.subtle.digest('SHA-256', encodedCodeVerifier)];
75
+ case 1:
76
+ codeChallenge = new (_a.apply(Uint8Array, [void 0, _b.sent()]))();
77
+ return [2 /*return*/, (0, js_base64_1.fromUint8Array)(codeChallenge, true)];
78
+ }
79
+ });
80
+ }); };
81
+ exports.generateCodeChallenge = generateCodeChallenge;
@@ -0,0 +1,24 @@
1
+ import { JWTVerifyGetKey } from 'jose';
2
+ import * as s from 'superstruct';
3
+ /**
4
+ * @link [ID Token](https://openid.net/specs/openid-connect-core-1_0.html#IDToken)
5
+ */
6
+ declare const IdTokenClaimsSchema: s.Struct<{
7
+ iss: string;
8
+ sub: string;
9
+ aud: string;
10
+ exp: number;
11
+ iat: number;
12
+ at_hash?: string | undefined;
13
+ }, {
14
+ iss: s.Struct<string, null>;
15
+ sub: s.Struct<string, null>;
16
+ aud: s.Struct<string, null>;
17
+ exp: s.Struct<number, null>;
18
+ iat: s.Struct<number, null>;
19
+ at_hash: s.Struct<string | undefined, null>;
20
+ }>;
21
+ export declare type IdTokenClaims = s.Infer<typeof IdTokenClaimsSchema>;
22
+ export declare const verifyIdToken: (idToken: string, clientId: string, issuer: string, jwks: JWTVerifyGetKey) => Promise<void>;
23
+ export declare const decodeIdToken: (token: string) => IdTokenClaims;
24
+ export {};
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ var __generator = (this && this.__generator) || function (thisArg, body) {
31
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
32
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
33
+ function verb(n) { return function (v) { return step([n, v]); }; }
34
+ function step(op) {
35
+ if (f) throw new TypeError("Generator is already executing.");
36
+ while (_) try {
37
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
38
+ if (y = 0, t) op = [op[0] & 2, t.value];
39
+ switch (op[0]) {
40
+ case 0: case 1: t = op; break;
41
+ case 4: _.label++; return { value: op[1], done: false };
42
+ case 5: _.label++; y = op[1]; op = [0]; continue;
43
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
44
+ default:
45
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
46
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
47
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
48
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
49
+ if (t[2]) _.ops.pop();
50
+ _.trys.pop(); continue;
51
+ }
52
+ op = body.call(thisArg, _);
53
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
54
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
55
+ }
56
+ };
57
+ Object.defineProperty(exports, "__esModule", { value: true });
58
+ exports.decodeIdToken = exports.verifyIdToken = void 0;
59
+ var essentials_1 = require("@silverhand/essentials");
60
+ var jose_1 = require("jose");
61
+ var s = __importStar(require("superstruct"));
62
+ var errors_1 = require("./errors");
63
+ var issuedAtTimeTolerance = 60;
64
+ /**
65
+ * @link [ID Token](https://openid.net/specs/openid-connect-core-1_0.html#IDToken)
66
+ */
67
+ var IdTokenClaimsSchema = s.type({
68
+ iss: s.string(),
69
+ sub: s.string(),
70
+ aud: s.string(),
71
+ exp: s.number(),
72
+ iat: s.number(),
73
+ at_hash: s.optional(s.string()),
74
+ });
75
+ var verifyIdToken = function (idToken, clientId, issuer, jwks) { return __awaiter(void 0, void 0, void 0, function () {
76
+ var result;
77
+ var _a;
78
+ return __generator(this, function (_b) {
79
+ switch (_b.label) {
80
+ case 0: return [4 /*yield*/, (0, jose_1.jwtVerify)(idToken, jwks, { audience: clientId, issuer: issuer })];
81
+ case 1:
82
+ result = _b.sent();
83
+ if (Math.abs(((_a = result.payload.iat) !== null && _a !== void 0 ? _a : 0) - Date.now() / 1000) > issuedAtTimeTolerance) {
84
+ throw new errors_1.LogtoError('id_token.invalid_iat');
85
+ }
86
+ return [2 /*return*/];
87
+ }
88
+ });
89
+ }); };
90
+ exports.verifyIdToken = verifyIdToken;
91
+ var decodeIdToken = function (token) {
92
+ var encodedPayload = token.split('.')[1];
93
+ if (!encodedPayload) {
94
+ throw new errors_1.LogtoError('id_token.invalid_token');
95
+ }
96
+ var json = essentials_1.UrlSafeBase64.decode(encodedPayload);
97
+ var idTokenClaims = JSON.parse(json);
98
+ s.assert(idTokenClaims, IdTokenClaimsSchema);
99
+ return idTokenClaims;
100
+ };
101
+ exports.decodeIdToken = decodeIdToken;
@@ -0,0 +1,6 @@
1
+ export * from './callback-uri';
2
+ export * from './errors';
3
+ export * from './generators';
4
+ export * from './id-token';
5
+ export * from './requester';
6
+ export * from './scopes';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./callback-uri"), exports);
14
+ __exportStar(require("./errors"), exports);
15
+ __exportStar(require("./generators"), exports);
16
+ __exportStar(require("./id-token"), exports);
17
+ __exportStar(require("./requester"), exports);
18
+ __exportStar(require("./scopes"), exports);
@@ -0,0 +1,2 @@
1
+ export declare const createRequester: (fetchFunction?: typeof fetch | undefined) => <T>(input: RequestInfo, init?: RequestInit | undefined) => Promise<T>;
2
+ export declare type Requester = ReturnType<typeof createRequester>;