@haste-health/jwt 0.11.2
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/README.md +122 -0
- package/lib/certifications.d.ts +71 -0
- package/lib/certifications.js +84 -0
- package/lib/certifications.js.map +1 -0
- package/lib/constants.d.ts +12 -0
- package/lib/constants.js +12 -0
- package/lib/constants.js.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +5 -0
- package/lib/index.js.map +1 -0
- package/lib/token.d.ts +14 -0
- package/lib/token.js +26 -0
- package/lib/token.js.map +1 -0
- package/lib/types.d.ts +162 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/package.json +34 -0
- package/src/certifications.ts +139 -0
- package/src/constants.ts +16 -0
- package/src/index.ts +4 -0
- package/src/token.ts +42 -0
- package/src/types.ts +180 -0
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# JWT
|
|
2
|
+
|
|
3
|
+
JWT utility functions for creating and parsing JWTs. Tags JWTS with Payload so serialize and deserialize will return back payload type.
|
|
4
|
+
|
|
5
|
+
## Types
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
export interface AccessTokenPayload<role> {
|
|
9
|
+
/**
|
|
10
|
+
* REQUIRED. Issuer Identifier for the Issuer of the response.
|
|
11
|
+
* The iss value is a case-sensitive URL using the https scheme that contains scheme, host, and optionally,
|
|
12
|
+
* port number and path components and no query or fragment components.
|
|
13
|
+
*/
|
|
14
|
+
iss: Issuer;
|
|
15
|
+
/**
|
|
16
|
+
* sub REQUIRED. Subject Identifier.
|
|
17
|
+
* A locally unique and never reassigned identifier within the Issuer for the End-User,
|
|
18
|
+
* which is intended to be consumed by the Client, e.g., 24400320 or AItOawmwtWwcT0k51BayewNvutrJUqsvl6qs7A4.
|
|
19
|
+
* It MUST NOT exceed 255 ASCII [RFC20] characters in length. The sub value is a case-sensitive string.
|
|
20
|
+
*/
|
|
21
|
+
sub: Subject;
|
|
22
|
+
/**
|
|
23
|
+
* Token can be associated with an Operation, Client or Membership this claim distinguishes between the three.
|
|
24
|
+
*/
|
|
25
|
+
[CUSTOM_CLAIMS.RESOURCE_TYPE]: TOKEN_RESOURCE_TYPES;
|
|
26
|
+
/**
|
|
27
|
+
* The ID of the resource the token is associated with.
|
|
28
|
+
*/
|
|
29
|
+
[CUSTOM_CLAIMS.RESOURCE_ID]?: id;
|
|
30
|
+
/**
|
|
31
|
+
* What tenants the user has access to.
|
|
32
|
+
*/
|
|
33
|
+
[CUSTOM_CLAIMS.TENANTS]: TenantClaim<role>[];
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Allow token to have additional claims.
|
|
37
|
+
*/
|
|
38
|
+
[key: string]: unknown;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* ID Token Payload. Extends Access token with additional claims from https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims.
|
|
43
|
+
*/
|
|
44
|
+
export interface IDTokenPayload<role> extends AccessTokenPayload<role> {
|
|
45
|
+
/**
|
|
46
|
+
* End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences.
|
|
47
|
+
*/
|
|
48
|
+
name?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Given name(s) or first name(s) of the End-User. Note that in some cultures, people can have multiple given names; all can be present, with the names being separated by space characters.
|
|
51
|
+
*/
|
|
52
|
+
given_name?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Surname(s) or last name(s) of the End-User. Note that in some cultures, people can have multiple family names or no family name; all can be present, with the names being separated by space characters.
|
|
55
|
+
*/
|
|
56
|
+
family_name?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Middle name(s) of the End-User. Note that in some cultures, people can have multiple middle names; all can be present, with the names being separated by space characters. Also note that in some cultures, middle names are not used.
|
|
59
|
+
*/
|
|
60
|
+
middle_name?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Casual name of the End-User that may or may not be the same as the given_name. For instance, a nickname value of Mike might be returned alongside a given_name value of Michael.
|
|
63
|
+
*/
|
|
64
|
+
nickname?: string;
|
|
65
|
+
/**
|
|
66
|
+
* Shorthand name by which the End-User wishes to be referred to at the RP, such as janedoe or j.doe. This value MAY be any valid JSON string including special characters such as @, /, or whitespace. The RP MUST NOT rely upon this value being unique, as discussed in Section 5.7.
|
|
67
|
+
*/
|
|
68
|
+
preferred_username?: string;
|
|
69
|
+
/**
|
|
70
|
+
* URL of the End-User's profile page. The contents of this Web page SHOULD be about the End-User.
|
|
71
|
+
*/
|
|
72
|
+
profile?: string;
|
|
73
|
+
/**
|
|
74
|
+
* URL of the End-User's profile picture. This URL MUST refer to an image file (for example, a PNG, JPEG, or GIF image file), rather than to a Web page containing an image. Note that this URL SHOULD specifically reference a profile photo of the End-User suitable for displaying when describing the End-User, rather than an arbitrary photo taken by the End-User.
|
|
75
|
+
*/
|
|
76
|
+
picture?: string;
|
|
77
|
+
/**
|
|
78
|
+
* URL of the End-User's Web page or blog. This Web page SHOULD contain information published by the End-User or an organization that the End-User is affiliated with.
|
|
79
|
+
*/
|
|
80
|
+
website?: string;
|
|
81
|
+
/**
|
|
82
|
+
* End-User's preferred e-mail address. Its value MUST conform to the RFC 5322 [RFC5322] addr-spec syntax. The RP MUST NOT rely upon this value being unique, as discussed in Section 5.7.
|
|
83
|
+
*/
|
|
84
|
+
email?: string;
|
|
85
|
+
/**
|
|
86
|
+
* True if the End-User's e-mail address has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this e-mail address was controlled by the End-User at the time the verification was performed. The means by which an e-mail address is verified is context specific, and dependent upon the trust framework or contractual agreements within which the parties are operating.
|
|
87
|
+
*/
|
|
88
|
+
email_verified?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* End-User's gender. Values defined by this specification are female and male. Other values MAY be used when neither of the defined values are applicable.
|
|
91
|
+
*/
|
|
92
|
+
gender?: string;
|
|
93
|
+
/**
|
|
94
|
+
* End-User's birthday, represented as an ISO 8601-1 [ISO8601‑1] YYYY-MM-DD format. The year MAY be 0000, indicating that it is omitted. To represent only the year, YYYY format is allowed. Note that depending on the underlying platform's date related function, providing just year can result in varying month and day, so the implementers need to take this factor into account to correctly process the dates.
|
|
95
|
+
*/
|
|
96
|
+
birthdate?: string;
|
|
97
|
+
/**
|
|
98
|
+
* String from IANA Time Zone Database [IANA.time‑zones] representing the End-User's time zone. For example, Europe/Paris or America/Los_Angeles.
|
|
99
|
+
*/
|
|
100
|
+
zoneinfo?: string;
|
|
101
|
+
/**
|
|
102
|
+
* End-User's locale, represented as a BCP47 [RFC5646] language tag. This is typically an ISO 639 Alpha-2 [ISO639] language code in lowercase and an ISO 3166-1 Alpha-2 [ISO3166‑1] country code in uppercase, separated by a dash. For example, en-US or fr-CA. As a compatibility note, some implementations have used an underscore as the separator rather than a dash, for example, en_US; Relying Parties MAY choose to accept this locale syntax as well.
|
|
103
|
+
*/
|
|
104
|
+
locale?: string;
|
|
105
|
+
/**
|
|
106
|
+
* End-User's preferred telephone number. E.164 [E.164] is RECOMMENDED as the format of this Claim, for example, +1 (425) 555-1212 or +56 (2) 687 2400. If the phone number contains an extension, it is RECOMMENDED that the extension be represented using the RFC 3966 [RFC3966] extension syntax, for example, +1 (604) 555-1234;ext=5678.
|
|
107
|
+
*/
|
|
108
|
+
phone_number?: string;
|
|
109
|
+
/**
|
|
110
|
+
* True if the End-User's phone number has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this phone number was controlled by the End-User at the time the verification was performed. The means by which a phone number is verified is context specific, and dependent upon the trust framework or contractual agreements within which the parties are operating. When true, the phone_number Claim MUST be in E.164 format and any extensions MUST be represented in RFC 3966 format.
|
|
111
|
+
*/
|
|
112
|
+
phone_number_verified?: boolean;
|
|
113
|
+
/**
|
|
114
|
+
* JSON object End-User's preferred postal address. The value of the address member is a JSON [RFC8259] structure containing some or all of the members defined in Section 5.1.1.
|
|
115
|
+
*/
|
|
116
|
+
address?: Address;
|
|
117
|
+
/**
|
|
118
|
+
* Time the End-User's information was last updated. Its value is a JSON number representing the number of seconds from 1970-01-01T00:00:00Z as measured in UTC until the date/time.
|
|
119
|
+
*/
|
|
120
|
+
updated_at?: number;
|
|
121
|
+
}
|
|
122
|
+
```
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ALGORITHMS_ALLOWED } from "./constants.js";
|
|
2
|
+
interface BaseOption {
|
|
3
|
+
alg: ALGORITHMS_ALLOWED;
|
|
4
|
+
}
|
|
5
|
+
interface JWTCertificationFileConfig extends BaseOption {
|
|
6
|
+
type: "file";
|
|
7
|
+
directory: string;
|
|
8
|
+
kid: string;
|
|
9
|
+
}
|
|
10
|
+
interface JWTCertificationEnvironmentConfig extends BaseOption {
|
|
11
|
+
type: "environment";
|
|
12
|
+
kid: string;
|
|
13
|
+
privateKey: string;
|
|
14
|
+
publicKey: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param directory
|
|
19
|
+
* @param alg
|
|
20
|
+
* @returns JSON Web Key Set with all public keys in the directory. Uses filename to distinguish between keys (sets the kid field in the JWK).
|
|
21
|
+
*/
|
|
22
|
+
export declare function getJWKS(option: JWTCertificationConfig): Promise<{
|
|
23
|
+
keys: {
|
|
24
|
+
alg: ALGORITHMS_ALLOWED;
|
|
25
|
+
use: string;
|
|
26
|
+
kid: string;
|
|
27
|
+
crv?: string;
|
|
28
|
+
d?: string;
|
|
29
|
+
dp?: string;
|
|
30
|
+
dq?: string;
|
|
31
|
+
e?: string;
|
|
32
|
+
k?: string;
|
|
33
|
+
n?: string;
|
|
34
|
+
p?: string;
|
|
35
|
+
q?: string;
|
|
36
|
+
qi?: string;
|
|
37
|
+
x?: string;
|
|
38
|
+
y?: string;
|
|
39
|
+
pub?: string;
|
|
40
|
+
priv?: string;
|
|
41
|
+
kty?: string;
|
|
42
|
+
key_ops?: string[];
|
|
43
|
+
ext?: boolean;
|
|
44
|
+
x5c?: string[];
|
|
45
|
+
x5t?: string;
|
|
46
|
+
'x5t#S256'?: string;
|
|
47
|
+
x5u?: string;
|
|
48
|
+
}[];
|
|
49
|
+
}>;
|
|
50
|
+
export type JWTCertificationConfig = JWTCertificationFileConfig | JWTCertificationEnvironmentConfig;
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* @param directory
|
|
54
|
+
* @param kid
|
|
55
|
+
* @param alg
|
|
56
|
+
* @returns Returns the private key for a given kid.
|
|
57
|
+
*/
|
|
58
|
+
export declare function getSigningKey(option: JWTCertificationConfig): Promise<{
|
|
59
|
+
key: CryptoKey;
|
|
60
|
+
kid: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Create certifications if not exist. Saves by default
|
|
64
|
+
* private keys under /${directory}/{kid}.p8 and public keys under /${directory}/{kid}.spki.
|
|
65
|
+
* Should only be used in development environment as these should be injected in environment in prod.
|
|
66
|
+
* @param directory
|
|
67
|
+
* @param kid
|
|
68
|
+
* @param alg
|
|
69
|
+
*/
|
|
70
|
+
export declare function createCertsIfNoneExists(option: JWTCertificationConfig): Promise<void>;
|
|
71
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as jose from "jose";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
/**
|
|
5
|
+
* Generates a keypair using the provided algorithm.
|
|
6
|
+
* @param alg
|
|
7
|
+
* @returns privateKey and publicKey generated using jose library.
|
|
8
|
+
*/
|
|
9
|
+
async function generateKeyPair(alg, options) {
|
|
10
|
+
const { privateKey, publicKey } = await jose.generateKeyPair(alg, options);
|
|
11
|
+
return { privateKey, publicKey };
|
|
12
|
+
}
|
|
13
|
+
async function writeToFile(directory, kid, publicKey, privateKey) {
|
|
14
|
+
const p8PublicKey = await jose.exportSPKI(publicKey);
|
|
15
|
+
const p8Private = await jose.exportPKCS8(privateKey);
|
|
16
|
+
if (!existsSync(directory)) {
|
|
17
|
+
mkdirSync(directory, { recursive: true });
|
|
18
|
+
}
|
|
19
|
+
writeFileSync(path.join(directory, `${kid}.spki`), p8PublicKey);
|
|
20
|
+
writeFileSync(path.join(directory, `${kid}.p8`), p8Private);
|
|
21
|
+
}
|
|
22
|
+
async function getPublicPrivateKey(option) {
|
|
23
|
+
switch (option.type) {
|
|
24
|
+
case "environment": {
|
|
25
|
+
const publicKey = await jose.importSPKI(option.publicKey, option.alg);
|
|
26
|
+
const privateKey = await jose.importPKCS8(option.privateKey, option.alg);
|
|
27
|
+
return { publicKey, privateKey };
|
|
28
|
+
}
|
|
29
|
+
case "file": {
|
|
30
|
+
const publicKey = await jose.importSPKI(readFileSync(path.join(option.directory, `${option.kid}.spki`), "utf-8"), option.alg);
|
|
31
|
+
const privateKey = await jose.importPKCS8(readFileSync(path.join(option.directory, `${option.kid}.p8`), "utf-8"), option.alg);
|
|
32
|
+
return { privateKey, publicKey };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
*
|
|
38
|
+
* @param directory
|
|
39
|
+
* @param alg
|
|
40
|
+
* @returns JSON Web Key Set with all public keys in the directory. Uses filename to distinguish between keys (sets the kid field in the JWK).
|
|
41
|
+
*/
|
|
42
|
+
export async function getJWKS(option) {
|
|
43
|
+
const { publicKey } = await getPublicPrivateKey(option);
|
|
44
|
+
const pubJWK = await jose.exportJWK(publicKey);
|
|
45
|
+
return {
|
|
46
|
+
keys: [{ ...pubJWK, alg: option.alg, use: "sig", kid: option.kid }],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
*
|
|
51
|
+
* @param directory
|
|
52
|
+
* @param kid
|
|
53
|
+
* @param alg
|
|
54
|
+
* @returns Returns the private key for a given kid.
|
|
55
|
+
*/
|
|
56
|
+
export async function getSigningKey(option) {
|
|
57
|
+
const { privateKey } = await getPublicPrivateKey(option);
|
|
58
|
+
return {
|
|
59
|
+
key: privateKey,
|
|
60
|
+
kid: option.kid,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Create certifications if not exist. Saves by default
|
|
65
|
+
* private keys under /${directory}/{kid}.p8 and public keys under /${directory}/{kid}.spki.
|
|
66
|
+
* Should only be used in development environment as these should be injected in environment in prod.
|
|
67
|
+
* @param directory
|
|
68
|
+
* @param kid
|
|
69
|
+
* @param alg
|
|
70
|
+
*/
|
|
71
|
+
export async function createCertsIfNoneExists(option) {
|
|
72
|
+
try {
|
|
73
|
+
await getSigningKey(option);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
if (option.type === "environment")
|
|
77
|
+
throw new Error(`Cannot create certs for 'environment' type.`);
|
|
78
|
+
const { publicKey, privateKey } = await generateKeyPair(option.alg, {
|
|
79
|
+
extractable: true,
|
|
80
|
+
});
|
|
81
|
+
await writeToFile(option.directory, option.kid, publicKey, privateKey);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=certifications.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"certifications.js","sourceRoot":"","sources":["../src/certifications.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const CUSTOM_CLAIMS: {
|
|
2
|
+
RESOURCE_TYPE: "https://haste.health/resourceType";
|
|
3
|
+
RESOURCE_ID: "https://haste.health/resourceId";
|
|
4
|
+
ACCESS_POLICY_VERSION_IDS: "https://haste.health/accessPolicyVersionIds";
|
|
5
|
+
TENANT: "https://haste.health/tenant";
|
|
6
|
+
ROLE: "https://haste.health/user_role";
|
|
7
|
+
};
|
|
8
|
+
export type ALGORITHMS_ALLOWED = (typeof ALGORITHMS)[keyof typeof ALGORITHMS];
|
|
9
|
+
export declare const ALGORITHMS: {
|
|
10
|
+
readonly RS256: "RS256";
|
|
11
|
+
readonly RS384: "RS384";
|
|
12
|
+
};
|
package/lib/constants.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const CUSTOM_CLAIMS = {
|
|
2
|
+
RESOURCE_TYPE: "https://haste.health/resourceType",
|
|
3
|
+
RESOURCE_ID: "https://haste.health/resourceId",
|
|
4
|
+
ACCESS_POLICY_VERSION_IDS: ("https://haste.health/accessPolicyVersionIds"),
|
|
5
|
+
TENANT: "https://haste.health/tenant",
|
|
6
|
+
ROLE: "https://haste.health/user_role",
|
|
7
|
+
};
|
|
8
|
+
export const ALGORITHMS = {
|
|
9
|
+
RS256: "RS256",
|
|
10
|
+
RS384: "RS384",
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,aAAa,EAAS,mCAAmC;IACzD,WAAW,EAAS,iCAAiC;IACrD,yBAAyB,EAAS,CAChC,6CAA6C,CAC9C;IACD,MAAM,EAAS,6BAA6B;IAC5C,IAAI,EAAS,gCAAgC;CAC9C,CAAC;AAIF,MAAM,CAAC,MAAM,UAAU,GAAU;IAC/B,KAAK,EAAE,OAAO;IACd,KAAK,EAAE,OAAO;CACf,CAAC"}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,qBAAqB,CAAC"}
|
package/lib/token.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as jose from "jose";
|
|
2
|
+
import { JWT } from "./types.js";
|
|
3
|
+
export declare function parseJwt<A, Token extends JWT<A>>(token: Token): Token["payload"];
|
|
4
|
+
/**
|
|
5
|
+
* Creates a JWT token using the provided private key and payload.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createToken<Payload extends jose.JWTPayload>({ signingKey, payload, expiresIn, }: {
|
|
8
|
+
signingKey: {
|
|
9
|
+
kid: string;
|
|
10
|
+
key: CryptoKey;
|
|
11
|
+
};
|
|
12
|
+
payload: Payload;
|
|
13
|
+
expiresIn?: string;
|
|
14
|
+
}): Promise<JWT<Payload>>;
|
package/lib/token.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as jose from "jose";
|
|
2
|
+
import { ALGORITHMS } from "./types.js";
|
|
3
|
+
export function parseJwt(token) {
|
|
4
|
+
const base64Url = token.split(".")[1];
|
|
5
|
+
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
|
6
|
+
const jsonPayload = decodeURIComponent(window
|
|
7
|
+
.atob(base64)
|
|
8
|
+
.split("")
|
|
9
|
+
.map(function (c) {
|
|
10
|
+
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
|
|
11
|
+
})
|
|
12
|
+
.join(""));
|
|
13
|
+
return JSON.parse(jsonPayload);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates a JWT token using the provided private key and payload.
|
|
17
|
+
*/
|
|
18
|
+
export async function createToken({ signingKey, payload, expiresIn = "1h", }) {
|
|
19
|
+
const signedJWT = (await new jose.SignJWT(payload)
|
|
20
|
+
.setProtectedHeader({ kid: signingKey.kid, alg: ALGORITHMS.RS384 })
|
|
21
|
+
.setIssuedAt()
|
|
22
|
+
.setExpirationTime(expiresIn)
|
|
23
|
+
.sign(signingKey.key));
|
|
24
|
+
return signedJWT;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=token.js.map
|
package/lib/token.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,OAAO,EAAE,UAAU,EAAO,MAAM,YAAY,CAAC;AAE7C,MAAM,UAAU,QAAQ,CACtB,KAAY;IAEZ,MAAM,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACtC,MAAM,MAAM,GAAG,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC/D,MAAM,WAAW,GAAG,kBAAkB,CACpC,MAAM;SACH,IAAI,CAAC,MAAM,CAAC;SACZ,KAAK,CAAC,EAAE,CAAC;SACT,GAAG,CAAC,UAAU,CAAC;QACd,OAAO,GAAG,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/D,CAAC,CAAC;SACD,IAAI,CAAC,EAAE,CAAC,CACZ,CAAC;IAEF,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AACjC,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAkC,EACjE,UAAU,EACV,OAAO,EACP,SAAS,GAAG,IAAI,GAKjB;IACC,MAAM,SAAS,GAAG,CAAC,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;SAC/C,kBAAkB,CAAC,EAAE,GAAG,EAAE,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,UAAU,CAAC,KAAK,EAAE,CAAC;SAClE,WAAW,EAAE;SACb,iBAAiB,CAAC,SAAS,CAAC;SAC5B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAiB,CAAC;IAEzC,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import * as jose from "jose";
|
|
2
|
+
import { Address, ClientApplication, Membership, OperationDefinition, Reference, canonical, id } from "@haste-health/fhir-types/r4/types";
|
|
3
|
+
import { CUSTOM_CLAIMS } from "./constants.js";
|
|
4
|
+
export * from "./constants.js";
|
|
5
|
+
declare const __tenant: unique symbol;
|
|
6
|
+
export type TenantId = string & {
|
|
7
|
+
[__tenant]: string;
|
|
8
|
+
};
|
|
9
|
+
export interface TenantClaim<role> {
|
|
10
|
+
id: TenantId;
|
|
11
|
+
userRole: role;
|
|
12
|
+
}
|
|
13
|
+
declare const __project: unique symbol;
|
|
14
|
+
export type ProjectId = string & {
|
|
15
|
+
[__project]: string;
|
|
16
|
+
};
|
|
17
|
+
declare const __subject: unique symbol;
|
|
18
|
+
export type Subject = string & {
|
|
19
|
+
[__subject]: string;
|
|
20
|
+
};
|
|
21
|
+
declare const __iss: unique symbol;
|
|
22
|
+
export type Issuer = string & {
|
|
23
|
+
[__iss]: string;
|
|
24
|
+
};
|
|
25
|
+
export type TOKEN_RESOURCE_TYPES = OperationDefinition["resourceType"] | ClientApplication["resourceType"] | Membership["resourceType"];
|
|
26
|
+
export interface HasteHealthCustomClaims<role> {
|
|
27
|
+
/**
|
|
28
|
+
* Token can be associated with an Operation, Client or Membership this claim distinguishes between the three.
|
|
29
|
+
*/
|
|
30
|
+
[CUSTOM_CLAIMS.RESOURCE_TYPE]: TOKEN_RESOURCE_TYPES;
|
|
31
|
+
/**
|
|
32
|
+
* The ID of the resource the token is associated with.
|
|
33
|
+
*/
|
|
34
|
+
[CUSTOM_CLAIMS.RESOURCE_ID]: id;
|
|
35
|
+
[CUSTOM_CLAIMS.ACCESS_POLICY_VERSION_IDS]: id[];
|
|
36
|
+
/**
|
|
37
|
+
* The users role for the tenant.
|
|
38
|
+
*/
|
|
39
|
+
[CUSTOM_CLAIMS.ROLE]: role;
|
|
40
|
+
/**
|
|
41
|
+
* The tenant the token is associated with.
|
|
42
|
+
*/
|
|
43
|
+
[CUSTOM_CLAIMS.TENANT]: TenantId;
|
|
44
|
+
}
|
|
45
|
+
export interface SMARTPayload {
|
|
46
|
+
fhirUser?: canonical;
|
|
47
|
+
patient?: id;
|
|
48
|
+
encounter?: id;
|
|
49
|
+
fhirContext?: Reference[];
|
|
50
|
+
}
|
|
51
|
+
export interface AccessTokenPayload<role> extends HasteHealthCustomClaims<role>, SMARTPayload, jose.JWTPayload {
|
|
52
|
+
/**
|
|
53
|
+
* REQUIRED. Issuer Identifier for the Issuer of the response.
|
|
54
|
+
* The iss value is a case-sensitive URL using the https scheme that contains scheme, host, and optionally,
|
|
55
|
+
* port number and path components and no query or fragment components.
|
|
56
|
+
*/
|
|
57
|
+
iss: Issuer;
|
|
58
|
+
/**
|
|
59
|
+
* sub REQUIRED. Subject Identifier.
|
|
60
|
+
* A locally unique and never reassigned identifier within the Issuer for the End-User,
|
|
61
|
+
* which is intended to be consumed by the Client, e.g., 24400320 or AItOawmwtWwcT0k51BayewNvutrJUqsvl6qs7A4.
|
|
62
|
+
* It MUST NOT exceed 255 ASCII [RFC20] characters in length. The sub value is a case-sensitive string.
|
|
63
|
+
*/
|
|
64
|
+
sub: Subject;
|
|
65
|
+
/**
|
|
66
|
+
* REQUIRED. Audience(s) that this ID Token is intended for.
|
|
67
|
+
* It MUST contain the OAuth 2.0 client_id of the Relying Party as an audience value.
|
|
68
|
+
* It MAY also contain identifiers for other audiences. In the general case, the aud value is an array of case-sensitive strings.
|
|
69
|
+
* In the common special case when there is one audience, the aud value MAY be a single case-sensitive string.
|
|
70
|
+
*/
|
|
71
|
+
aud: string;
|
|
72
|
+
/**
|
|
73
|
+
* scope Required. OAuth 2.0 scopes. Space-separated string.
|
|
74
|
+
*/
|
|
75
|
+
scope: string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* ID Token Payload. Extends Access token with additional claims from https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims.
|
|
79
|
+
*/
|
|
80
|
+
export interface IDTokenPayload<role> extends AccessTokenPayload<role> {
|
|
81
|
+
/**
|
|
82
|
+
* End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences.
|
|
83
|
+
*/
|
|
84
|
+
name?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Given name(s) or first name(s) of the End-User. Note that in some cultures, people can have multiple given names; all can be present, with the names being separated by space characters.
|
|
87
|
+
*/
|
|
88
|
+
given_name?: string;
|
|
89
|
+
/**
|
|
90
|
+
* Surname(s) or last name(s) of the End-User. Note that in some cultures, people can have multiple family names or no family name; all can be present, with the names being separated by space characters.
|
|
91
|
+
*/
|
|
92
|
+
family_name?: string;
|
|
93
|
+
/**
|
|
94
|
+
* Middle name(s) of the End-User. Note that in some cultures, people can have multiple middle names; all can be present, with the names being separated by space characters. Also note that in some cultures, middle names are not used.
|
|
95
|
+
*/
|
|
96
|
+
middle_name?: string;
|
|
97
|
+
/**
|
|
98
|
+
* Casual name of the End-User that may or may not be the same as the given_name. For instance, a nickname value of Mike might be returned alongside a given_name value of Michael.
|
|
99
|
+
*/
|
|
100
|
+
nickname?: string;
|
|
101
|
+
/**
|
|
102
|
+
* Shorthand name by which the End-User wishes to be referred to at the RP, such as janedoe or j.doe. This value MAY be any valid JSON string including special characters such as @, /, or whitespace. The RP MUST NOT rely upon this value being unique, as discussed in Section 5.7.
|
|
103
|
+
*/
|
|
104
|
+
preferred_username?: string;
|
|
105
|
+
/**
|
|
106
|
+
* URL of the End-User's profile page. The contents of this Web page SHOULD be about the End-User.
|
|
107
|
+
*/
|
|
108
|
+
profile?: string;
|
|
109
|
+
/**
|
|
110
|
+
* URL of the End-User's profile picture. This URL MUST refer to an image file (for example, a PNG, JPEG, or GIF image file), rather than to a Web page containing an image. Note that this URL SHOULD specifically reference a profile photo of the End-User suitable for displaying when describing the End-User, rather than an arbitrary photo taken by the End-User.
|
|
111
|
+
*/
|
|
112
|
+
picture?: string;
|
|
113
|
+
/**
|
|
114
|
+
* URL of the End-User's Web page or blog. This Web page SHOULD contain information published by the End-User or an organization that the End-User is affiliated with.
|
|
115
|
+
*/
|
|
116
|
+
website?: string;
|
|
117
|
+
/**
|
|
118
|
+
* End-User's preferred e-mail address. Its value MUST conform to the RFC 5322 [RFC5322] addr-spec syntax. The RP MUST NOT rely upon this value being unique, as discussed in Section 5.7.
|
|
119
|
+
*/
|
|
120
|
+
email?: string;
|
|
121
|
+
/**
|
|
122
|
+
* True if the End-User's e-mail address has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this e-mail address was controlled by the End-User at the time the verification was performed. The means by which an e-mail address is verified is context specific, and dependent upon the trust framework or contractual agreements within which the parties are operating.
|
|
123
|
+
*/
|
|
124
|
+
email_verified?: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* End-User's gender. Values defined by this specification are female and male. Other values MAY be used when neither of the defined values are applicable.
|
|
127
|
+
*/
|
|
128
|
+
gender?: string;
|
|
129
|
+
/**
|
|
130
|
+
* End-User's birthday, represented as an ISO 8601-1 [ISO8601‑1] YYYY-MM-DD format. The year MAY be 0000, indicating that it is omitted. To represent only the year, YYYY format is allowed. Note that depending on the underlying platform's date related function, providing just year can result in varying month and day, so the implementers need to take this factor into account to correctly process the dates.
|
|
131
|
+
*/
|
|
132
|
+
birthdate?: string;
|
|
133
|
+
/**
|
|
134
|
+
* String from IANA Time Zone Database [IANA.time‑zones] representing the End-User's time zone. For example, Europe/Paris or America/Los_Angeles.
|
|
135
|
+
*/
|
|
136
|
+
zoneinfo?: string;
|
|
137
|
+
/**
|
|
138
|
+
* End-User's locale, represented as a BCP47 [RFC5646] language tag. This is typically an ISO 639 Alpha-2 [ISO639] language code in lowercase and an ISO 3166-1 Alpha-2 [ISO3166‑1] country code in uppercase, separated by a dash. For example, en-US or fr-CA. As a compatibility note, some implementations have used an underscore as the separator rather than a dash, for example, en_US; Relying Parties MAY choose to accept this locale syntax as well.
|
|
139
|
+
*/
|
|
140
|
+
locale?: string;
|
|
141
|
+
/**
|
|
142
|
+
* End-User's preferred telephone number. E.164 [E.164] is RECOMMENDED as the format of this Claim, for example, +1 (425) 555-1212 or +56 (2) 687 2400. If the phone number contains an extension, it is RECOMMENDED that the extension be represented using the RFC 3966 [RFC3966] extension syntax, for example, +1 (604) 555-1234;ext=5678.
|
|
143
|
+
*/
|
|
144
|
+
phone_number?: string;
|
|
145
|
+
/**
|
|
146
|
+
* True if the End-User's phone number has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this phone number was controlled by the End-User at the time the verification was performed. The means by which a phone number is verified is context specific, and dependent upon the trust framework or contractual agreements within which the parties are operating. When true, the phone_number Claim MUST be in E.164 format and any extensions MUST be represented in RFC 3966 format.
|
|
147
|
+
*/
|
|
148
|
+
phone_number_verified?: boolean;
|
|
149
|
+
/**
|
|
150
|
+
* JSON object End-User's preferred postal address. The value of the address member is a JSON [RFC8259] structure containing some or all of the members defined in Section 5.1.1.
|
|
151
|
+
*/
|
|
152
|
+
address?: Address;
|
|
153
|
+
/**
|
|
154
|
+
* Time the End-User's information was last updated. Its value is a JSON number representing the number of seconds from 1970-01-01T00:00:00Z as measured in UTC until the date/time.
|
|
155
|
+
*/
|
|
156
|
+
updated_at?: number;
|
|
157
|
+
}
|
|
158
|
+
export type JWT<Payload> = string & {
|
|
159
|
+
["payload"]: Payload;
|
|
160
|
+
};
|
|
161
|
+
export type AccessToken<role> = JWT<AccessTokenPayload<role>>;
|
|
162
|
+
export type IDToken<role> = JWT<IDTokenPayload<role>>;
|
package/lib/types.js
ADDED
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE/C,cAAc,gBAAgB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@haste-health/jwt",
|
|
3
|
+
"version": "0.11.2",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"jose": "^6.0.11"
|
|
6
|
+
},
|
|
7
|
+
"type": "module",
|
|
8
|
+
"description": "JWT utilities for Igu Health",
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"types": "lib/index.d.ts",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"publish": "pnpm build && pnpm npm publish --access public --tolerate-republish"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@haste-health/fhir-types": "workspace:^",
|
|
17
|
+
"@jest/globals": "^29.7.0",
|
|
18
|
+
"@types/jest": "^29.5.14",
|
|
19
|
+
"@types/node": "^24.3.0",
|
|
20
|
+
"jest": "^29.7.0",
|
|
21
|
+
"ts-jest": "^29.3.2",
|
|
22
|
+
"typescript": "5.9.2"
|
|
23
|
+
},
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./lib/index.js",
|
|
26
|
+
"./token": "./lib/token.js",
|
|
27
|
+
"./certifications": "./lib/certifications.js",
|
|
28
|
+
"./types": "./lib/types.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"lib/**",
|
|
32
|
+
"src/**"
|
|
33
|
+
]
|
|
34
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import * as jose from "jose";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
import { ALGORITHMS_ALLOWED } from "./constants.js";
|
|
6
|
+
|
|
7
|
+
interface BaseOption {
|
|
8
|
+
alg: ALGORITHMS_ALLOWED;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface JWTCertificationFileConfig extends BaseOption {
|
|
12
|
+
type: "file";
|
|
13
|
+
directory: string;
|
|
14
|
+
kid: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface JWTCertificationEnvironmentConfig extends BaseOption {
|
|
18
|
+
type: "environment";
|
|
19
|
+
kid: string;
|
|
20
|
+
privateKey: string;
|
|
21
|
+
publicKey: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Generates a keypair using the provided algorithm.
|
|
26
|
+
* @param alg
|
|
27
|
+
* @returns privateKey and publicKey generated using jose library.
|
|
28
|
+
*/
|
|
29
|
+
async function generateKeyPair(
|
|
30
|
+
alg: ALGORITHMS_ALLOWED,
|
|
31
|
+
options: { extractable: boolean },
|
|
32
|
+
) {
|
|
33
|
+
const { privateKey, publicKey } = await jose.generateKeyPair(alg, options);
|
|
34
|
+
return { privateKey, publicKey };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function writeToFile(
|
|
38
|
+
directory: string,
|
|
39
|
+
kid: string,
|
|
40
|
+
publicKey: CryptoKey,
|
|
41
|
+
privateKey: CryptoKey,
|
|
42
|
+
) {
|
|
43
|
+
const p8PublicKey = await jose.exportSPKI(publicKey);
|
|
44
|
+
const p8Private = await jose.exportPKCS8(privateKey);
|
|
45
|
+
|
|
46
|
+
if (!existsSync(directory)) {
|
|
47
|
+
mkdirSync(directory, { recursive: true });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
writeFileSync(path.join(directory, `${kid}.spki`), p8PublicKey);
|
|
51
|
+
writeFileSync(path.join(directory, `${kid}.p8`), p8Private);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function getPublicPrivateKey(option: JWTCertificationConfig): Promise<{
|
|
55
|
+
publicKey: CryptoKey;
|
|
56
|
+
privateKey: CryptoKey;
|
|
57
|
+
}> {
|
|
58
|
+
switch (option.type) {
|
|
59
|
+
case "environment": {
|
|
60
|
+
const publicKey = await jose.importSPKI(option.publicKey, option.alg);
|
|
61
|
+
const privateKey = await jose.importPKCS8(option.privateKey, option.alg);
|
|
62
|
+
return { publicKey, privateKey };
|
|
63
|
+
}
|
|
64
|
+
case "file": {
|
|
65
|
+
const publicKey = await jose.importSPKI(
|
|
66
|
+
readFileSync(
|
|
67
|
+
path.join(option.directory, `${option.kid}.spki`),
|
|
68
|
+
"utf-8",
|
|
69
|
+
),
|
|
70
|
+
option.alg,
|
|
71
|
+
);
|
|
72
|
+
const privateKey = await jose.importPKCS8(
|
|
73
|
+
readFileSync(path.join(option.directory, `${option.kid}.p8`), "utf-8"),
|
|
74
|
+
option.alg,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return { privateKey, publicKey };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
*
|
|
84
|
+
* @param directory
|
|
85
|
+
* @param alg
|
|
86
|
+
* @returns JSON Web Key Set with all public keys in the directory. Uses filename to distinguish between keys (sets the kid field in the JWK).
|
|
87
|
+
*/
|
|
88
|
+
export async function getJWKS(option: JWTCertificationConfig) {
|
|
89
|
+
const { publicKey } = await getPublicPrivateKey(option);
|
|
90
|
+
|
|
91
|
+
const pubJWK = await jose.exportJWK(publicKey);
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
keys: [{ ...pubJWK, alg: option.alg, use: "sig", kid: option.kid }],
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export type JWTCertificationConfig =
|
|
99
|
+
| JWTCertificationFileConfig
|
|
100
|
+
| JWTCertificationEnvironmentConfig;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
*
|
|
104
|
+
* @param directory
|
|
105
|
+
* @param kid
|
|
106
|
+
* @param alg
|
|
107
|
+
* @returns Returns the private key for a given kid.
|
|
108
|
+
*/
|
|
109
|
+
export async function getSigningKey(
|
|
110
|
+
option: JWTCertificationConfig,
|
|
111
|
+
): Promise<{ key: CryptoKey; kid: string }> {
|
|
112
|
+
const { privateKey } = await getPublicPrivateKey(option);
|
|
113
|
+
return {
|
|
114
|
+
key: privateKey,
|
|
115
|
+
kid: option.kid,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Create certifications if not exist. Saves by default
|
|
121
|
+
* private keys under /${directory}/{kid}.p8 and public keys under /${directory}/{kid}.spki.
|
|
122
|
+
* Should only be used in development environment as these should be injected in environment in prod.
|
|
123
|
+
* @param directory
|
|
124
|
+
* @param kid
|
|
125
|
+
* @param alg
|
|
126
|
+
*/
|
|
127
|
+
export async function createCertsIfNoneExists(option: JWTCertificationConfig) {
|
|
128
|
+
try {
|
|
129
|
+
await getSigningKey(option);
|
|
130
|
+
} catch {
|
|
131
|
+
if (option.type === "environment")
|
|
132
|
+
throw new Error(`Cannot create certs for 'environment' type.`);
|
|
133
|
+
|
|
134
|
+
const { publicKey, privateKey } = await generateKeyPair(option.alg, {
|
|
135
|
+
extractable: true,
|
|
136
|
+
});
|
|
137
|
+
await writeToFile(option.directory, option.kid, publicKey, privateKey);
|
|
138
|
+
}
|
|
139
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const CUSTOM_CLAIMS = {
|
|
2
|
+
RESOURCE_TYPE: <const>"https://haste.health/resourceType",
|
|
3
|
+
RESOURCE_ID: <const>"https://haste.health/resourceId",
|
|
4
|
+
ACCESS_POLICY_VERSION_IDS: <const>(
|
|
5
|
+
"https://haste.health/accessPolicyVersionIds"
|
|
6
|
+
),
|
|
7
|
+
TENANT: <const>"https://haste.health/tenant",
|
|
8
|
+
ROLE: <const>"https://haste.health/user_role",
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type ALGORITHMS_ALLOWED = (typeof ALGORITHMS)[keyof typeof ALGORITHMS];
|
|
12
|
+
|
|
13
|
+
export const ALGORITHMS = <const>{
|
|
14
|
+
RS256: "RS256",
|
|
15
|
+
RS384: "RS384",
|
|
16
|
+
};
|
package/src/index.ts
ADDED
package/src/token.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as jose from "jose";
|
|
2
|
+
|
|
3
|
+
import { ALGORITHMS, JWT } from "./types.js";
|
|
4
|
+
|
|
5
|
+
export function parseJwt<A, Token extends JWT<A>>(
|
|
6
|
+
token: Token,
|
|
7
|
+
): Token["payload"] {
|
|
8
|
+
const base64Url = token.split(".")[1];
|
|
9
|
+
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
|
10
|
+
const jsonPayload = decodeURIComponent(
|
|
11
|
+
window
|
|
12
|
+
.atob(base64)
|
|
13
|
+
.split("")
|
|
14
|
+
.map(function (c) {
|
|
15
|
+
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
|
|
16
|
+
})
|
|
17
|
+
.join(""),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
return JSON.parse(jsonPayload);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates a JWT token using the provided private key and payload.
|
|
25
|
+
*/
|
|
26
|
+
export async function createToken<Payload extends jose.JWTPayload>({
|
|
27
|
+
signingKey,
|
|
28
|
+
payload,
|
|
29
|
+
expiresIn = "1h",
|
|
30
|
+
}: {
|
|
31
|
+
signingKey: { kid: string; key: CryptoKey };
|
|
32
|
+
payload: Payload;
|
|
33
|
+
expiresIn?: string;
|
|
34
|
+
}): Promise<JWT<Payload>> {
|
|
35
|
+
const signedJWT = (await new jose.SignJWT(payload)
|
|
36
|
+
.setProtectedHeader({ kid: signingKey.kid, alg: ALGORITHMS.RS384 })
|
|
37
|
+
.setIssuedAt()
|
|
38
|
+
.setExpirationTime(expiresIn)
|
|
39
|
+
.sign(signingKey.key)) as JWT<Payload>;
|
|
40
|
+
|
|
41
|
+
return signedJWT;
|
|
42
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import * as jose from "jose";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
Address,
|
|
5
|
+
ClientApplication,
|
|
6
|
+
Membership,
|
|
7
|
+
OperationDefinition,
|
|
8
|
+
Reference,
|
|
9
|
+
canonical,
|
|
10
|
+
id,
|
|
11
|
+
} from "@haste-health/fhir-types/r4/types";
|
|
12
|
+
|
|
13
|
+
import { CUSTOM_CLAIMS } from "./constants.js";
|
|
14
|
+
|
|
15
|
+
export * from "./constants.js";
|
|
16
|
+
|
|
17
|
+
declare const __tenant: unique symbol;
|
|
18
|
+
export type TenantId = string & { [__tenant]: string };
|
|
19
|
+
export interface TenantClaim<role> {
|
|
20
|
+
id: TenantId;
|
|
21
|
+
userRole: role;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare const __project: unique symbol;
|
|
25
|
+
export type ProjectId = string & {
|
|
26
|
+
[__project]: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
declare const __subject: unique symbol;
|
|
30
|
+
export type Subject = string & { [__subject]: string };
|
|
31
|
+
declare const __iss: unique symbol;
|
|
32
|
+
export type Issuer = string & { [__iss]: string };
|
|
33
|
+
|
|
34
|
+
export type TOKEN_RESOURCE_TYPES =
|
|
35
|
+
| OperationDefinition["resourceType"]
|
|
36
|
+
| ClientApplication["resourceType"]
|
|
37
|
+
| Membership["resourceType"];
|
|
38
|
+
|
|
39
|
+
export interface HasteHealthCustomClaims<role> {
|
|
40
|
+
/**
|
|
41
|
+
* Token can be associated with an Operation, Client or Membership this claim distinguishes between the three.
|
|
42
|
+
*/
|
|
43
|
+
[CUSTOM_CLAIMS.RESOURCE_TYPE]: TOKEN_RESOURCE_TYPES;
|
|
44
|
+
/**
|
|
45
|
+
* The ID of the resource the token is associated with.
|
|
46
|
+
*/
|
|
47
|
+
[CUSTOM_CLAIMS.RESOURCE_ID]: id;
|
|
48
|
+
[CUSTOM_CLAIMS.ACCESS_POLICY_VERSION_IDS]: id[];
|
|
49
|
+
/**
|
|
50
|
+
* The users role for the tenant.
|
|
51
|
+
*/
|
|
52
|
+
[CUSTOM_CLAIMS.ROLE]: role;
|
|
53
|
+
/**
|
|
54
|
+
* The tenant the token is associated with.
|
|
55
|
+
*/
|
|
56
|
+
[CUSTOM_CLAIMS.TENANT]: TenantId;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface SMARTPayload {
|
|
60
|
+
fhirUser?: canonical;
|
|
61
|
+
patient?: id;
|
|
62
|
+
encounter?: id;
|
|
63
|
+
fhirContext?: Reference[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface AccessTokenPayload<role>
|
|
67
|
+
extends HasteHealthCustomClaims<role>,
|
|
68
|
+
SMARTPayload,
|
|
69
|
+
jose.JWTPayload {
|
|
70
|
+
/**
|
|
71
|
+
* REQUIRED. Issuer Identifier for the Issuer of the response.
|
|
72
|
+
* The iss value is a case-sensitive URL using the https scheme that contains scheme, host, and optionally,
|
|
73
|
+
* port number and path components and no query or fragment components.
|
|
74
|
+
*/
|
|
75
|
+
iss: Issuer;
|
|
76
|
+
/**
|
|
77
|
+
* sub REQUIRED. Subject Identifier.
|
|
78
|
+
* A locally unique and never reassigned identifier within the Issuer for the End-User,
|
|
79
|
+
* which is intended to be consumed by the Client, e.g., 24400320 or AItOawmwtWwcT0k51BayewNvutrJUqsvl6qs7A4.
|
|
80
|
+
* It MUST NOT exceed 255 ASCII [RFC20] characters in length. The sub value is a case-sensitive string.
|
|
81
|
+
*/
|
|
82
|
+
sub: Subject;
|
|
83
|
+
/**
|
|
84
|
+
* REQUIRED. Audience(s) that this ID Token is intended for.
|
|
85
|
+
* It MUST contain the OAuth 2.0 client_id of the Relying Party as an audience value.
|
|
86
|
+
* It MAY also contain identifiers for other audiences. In the general case, the aud value is an array of case-sensitive strings.
|
|
87
|
+
* In the common special case when there is one audience, the aud value MAY be a single case-sensitive string.
|
|
88
|
+
*/
|
|
89
|
+
aud: string;
|
|
90
|
+
/**
|
|
91
|
+
* scope Required. OAuth 2.0 scopes. Space-separated string.
|
|
92
|
+
*/
|
|
93
|
+
scope: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* ID Token Payload. Extends Access token with additional claims from https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims.
|
|
98
|
+
*/
|
|
99
|
+
export interface IDTokenPayload<role> extends AccessTokenPayload<role> {
|
|
100
|
+
/**
|
|
101
|
+
* End-User's full name in displayable form including all name parts, possibly including titles and suffixes, ordered according to the End-User's locale and preferences.
|
|
102
|
+
*/
|
|
103
|
+
name?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Given name(s) or first name(s) of the End-User. Note that in some cultures, people can have multiple given names; all can be present, with the names being separated by space characters.
|
|
106
|
+
*/
|
|
107
|
+
given_name?: string;
|
|
108
|
+
/**
|
|
109
|
+
* Surname(s) or last name(s) of the End-User. Note that in some cultures, people can have multiple family names or no family name; all can be present, with the names being separated by space characters.
|
|
110
|
+
*/
|
|
111
|
+
family_name?: string;
|
|
112
|
+
/**
|
|
113
|
+
* Middle name(s) of the End-User. Note that in some cultures, people can have multiple middle names; all can be present, with the names being separated by space characters. Also note that in some cultures, middle names are not used.
|
|
114
|
+
*/
|
|
115
|
+
middle_name?: string;
|
|
116
|
+
/**
|
|
117
|
+
* Casual name of the End-User that may or may not be the same as the given_name. For instance, a nickname value of Mike might be returned alongside a given_name value of Michael.
|
|
118
|
+
*/
|
|
119
|
+
nickname?: string;
|
|
120
|
+
/**
|
|
121
|
+
* Shorthand name by which the End-User wishes to be referred to at the RP, such as janedoe or j.doe. This value MAY be any valid JSON string including special characters such as @, /, or whitespace. The RP MUST NOT rely upon this value being unique, as discussed in Section 5.7.
|
|
122
|
+
*/
|
|
123
|
+
preferred_username?: string;
|
|
124
|
+
/**
|
|
125
|
+
* URL of the End-User's profile page. The contents of this Web page SHOULD be about the End-User.
|
|
126
|
+
*/
|
|
127
|
+
profile?: string;
|
|
128
|
+
/**
|
|
129
|
+
* URL of the End-User's profile picture. This URL MUST refer to an image file (for example, a PNG, JPEG, or GIF image file), rather than to a Web page containing an image. Note that this URL SHOULD specifically reference a profile photo of the End-User suitable for displaying when describing the End-User, rather than an arbitrary photo taken by the End-User.
|
|
130
|
+
*/
|
|
131
|
+
picture?: string;
|
|
132
|
+
/**
|
|
133
|
+
* URL of the End-User's Web page or blog. This Web page SHOULD contain information published by the End-User or an organization that the End-User is affiliated with.
|
|
134
|
+
*/
|
|
135
|
+
website?: string;
|
|
136
|
+
/**
|
|
137
|
+
* End-User's preferred e-mail address. Its value MUST conform to the RFC 5322 [RFC5322] addr-spec syntax. The RP MUST NOT rely upon this value being unique, as discussed in Section 5.7.
|
|
138
|
+
*/
|
|
139
|
+
email?: string;
|
|
140
|
+
/**
|
|
141
|
+
* True if the End-User's e-mail address has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this e-mail address was controlled by the End-User at the time the verification was performed. The means by which an e-mail address is verified is context specific, and dependent upon the trust framework or contractual agreements within which the parties are operating.
|
|
142
|
+
*/
|
|
143
|
+
email_verified?: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* End-User's gender. Values defined by this specification are female and male. Other values MAY be used when neither of the defined values are applicable.
|
|
146
|
+
*/
|
|
147
|
+
gender?: string;
|
|
148
|
+
/**
|
|
149
|
+
* End-User's birthday, represented as an ISO 8601-1 [ISO8601‑1] YYYY-MM-DD format. The year MAY be 0000, indicating that it is omitted. To represent only the year, YYYY format is allowed. Note that depending on the underlying platform's date related function, providing just year can result in varying month and day, so the implementers need to take this factor into account to correctly process the dates.
|
|
150
|
+
*/
|
|
151
|
+
birthdate?: string;
|
|
152
|
+
/**
|
|
153
|
+
* String from IANA Time Zone Database [IANA.time‑zones] representing the End-User's time zone. For example, Europe/Paris or America/Los_Angeles.
|
|
154
|
+
*/
|
|
155
|
+
zoneinfo?: string;
|
|
156
|
+
/**
|
|
157
|
+
* End-User's locale, represented as a BCP47 [RFC5646] language tag. This is typically an ISO 639 Alpha-2 [ISO639] language code in lowercase and an ISO 3166-1 Alpha-2 [ISO3166‑1] country code in uppercase, separated by a dash. For example, en-US or fr-CA. As a compatibility note, some implementations have used an underscore as the separator rather than a dash, for example, en_US; Relying Parties MAY choose to accept this locale syntax as well.
|
|
158
|
+
*/
|
|
159
|
+
locale?: string;
|
|
160
|
+
/**
|
|
161
|
+
* End-User's preferred telephone number. E.164 [E.164] is RECOMMENDED as the format of this Claim, for example, +1 (425) 555-1212 or +56 (2) 687 2400. If the phone number contains an extension, it is RECOMMENDED that the extension be represented using the RFC 3966 [RFC3966] extension syntax, for example, +1 (604) 555-1234;ext=5678.
|
|
162
|
+
*/
|
|
163
|
+
phone_number?: string;
|
|
164
|
+
/**
|
|
165
|
+
* True if the End-User's phone number has been verified; otherwise false. When this Claim Value is true, this means that the OP took affirmative steps to ensure that this phone number was controlled by the End-User at the time the verification was performed. The means by which a phone number is verified is context specific, and dependent upon the trust framework or contractual agreements within which the parties are operating. When true, the phone_number Claim MUST be in E.164 format and any extensions MUST be represented in RFC 3966 format.
|
|
166
|
+
*/
|
|
167
|
+
phone_number_verified?: boolean;
|
|
168
|
+
/**
|
|
169
|
+
* JSON object End-User's preferred postal address. The value of the address member is a JSON [RFC8259] structure containing some or all of the members defined in Section 5.1.1.
|
|
170
|
+
*/
|
|
171
|
+
address?: Address;
|
|
172
|
+
/**
|
|
173
|
+
* Time the End-User's information was last updated. Its value is a JSON number representing the number of seconds from 1970-01-01T00:00:00Z as measured in UTC until the date/time.
|
|
174
|
+
*/
|
|
175
|
+
updated_at?: number;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export type JWT<Payload> = string & { ["payload"]: Payload };
|
|
179
|
+
export type AccessToken<role> = JWT<AccessTokenPayload<role>>;
|
|
180
|
+
export type IDToken<role> = JWT<IDTokenPayload<role>>;
|