@insaeng/auth 0.0.0-stage → 0.1.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/dist/testing.d.ts +21 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +28 -0
- package/dist/verify.d.ts +19 -0
- package/dist/verify.d.ts.map +1 -0
- package/dist/verify.js +50 -0
- package/package.json +39 -3
- package/src/testing.ts +65 -0
- package/src/verify.ts +90 -0
- package/README.md +0 -3
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type JSONWebKeySet, type JWTVerifyGetKey } from "jose";
|
|
2
|
+
import type { AccessTokenClaims } from "./verify.js";
|
|
3
|
+
export interface TestIssuerOptions {
|
|
4
|
+
issuer?: string;
|
|
5
|
+
audience?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface SignTestTokenOptions {
|
|
8
|
+
typ?: string;
|
|
9
|
+
issuer?: string;
|
|
10
|
+
audience?: string;
|
|
11
|
+
expirationTime?: string | number | Date;
|
|
12
|
+
}
|
|
13
|
+
export interface TestIssuer {
|
|
14
|
+
issuer: string;
|
|
15
|
+
audience: string;
|
|
16
|
+
jwks: JSONWebKeySet;
|
|
17
|
+
keys: JWTVerifyGetKey;
|
|
18
|
+
signAccessToken(claims: Partial<AccessTokenClaims>, options?: SignTestTokenOptions): Promise<string>;
|
|
19
|
+
}
|
|
20
|
+
export declare function createTestIssuer({ issuer, audience, }?: TestIssuerOptions): Promise<TestIssuer>;
|
|
21
|
+
//# sourceMappingURL=testing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,aAAa,EAClB,KAAK,eAAe,EACrB,MAAM,MAAM,CAAC;AACd,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,MAAM,WAAW,iBAAiB;IAChC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,oBAAoB;IACnC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CACzC;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,aAAa,CAAC;IACpB,IAAI,EAAE,eAAe,CAAC;IACtB,eAAe,CACb,MAAM,EAAE,OAAO,CAAC,iBAAiB,CAAC,EAClC,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAAC,MAAM,CAAC,CAAC;CACpB;AAED,wBAAsB,gBAAgB,CAAC,EACrC,MAAoC,EACpC,QAAiC,GAClC,GAAE,iBAAsB,GAAG,OAAO,CAAC,UAAU,CAAC,CA2B9C"}
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { calculateJwkThumbprint, createLocalJWKSet, exportJWK, generateKeyPair, SignJWT, } from "jose";
|
|
2
|
+
export async function createTestIssuer({ issuer = "https://auth.insaeng.test", audience = "insaeng-test-service", } = {}) {
|
|
3
|
+
const { privateKey, publicKey } = await generateKeyPair("EdDSA", {
|
|
4
|
+
crv: "Ed25519",
|
|
5
|
+
});
|
|
6
|
+
const jwk = await exportJWK(publicKey);
|
|
7
|
+
const kid = await calculateJwkThumbprint(jwk);
|
|
8
|
+
const jwks = {
|
|
9
|
+
keys: [{ ...jwk, kid, alg: "EdDSA", use: "sig" }],
|
|
10
|
+
};
|
|
11
|
+
return {
|
|
12
|
+
issuer,
|
|
13
|
+
audience,
|
|
14
|
+
jwks,
|
|
15
|
+
keys: createLocalJWKSet(jwks),
|
|
16
|
+
async signAccessToken(claims, options = {}) {
|
|
17
|
+
const { sub = "test-user", roles = [], ...rest } = claims;
|
|
18
|
+
return new SignJWT({ roles, ...rest })
|
|
19
|
+
.setProtectedHeader({ alg: "EdDSA", typ: options.typ ?? "at+jwt", kid })
|
|
20
|
+
.setSubject(sub)
|
|
21
|
+
.setIssuer(options.issuer ?? issuer)
|
|
22
|
+
.setAudience(options.audience ?? audience)
|
|
23
|
+
.setIssuedAt()
|
|
24
|
+
.setExpirationTime(options.expirationTime ?? "5m")
|
|
25
|
+
.sign(privateKey);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
package/dist/verify.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type JWTVerifyGetKey } from "jose";
|
|
2
|
+
export interface AccessTokenClaims {
|
|
3
|
+
sub: string;
|
|
4
|
+
roles: string[];
|
|
5
|
+
client_id?: string;
|
|
6
|
+
email?: string;
|
|
7
|
+
name?: string;
|
|
8
|
+
}
|
|
9
|
+
export type AccessTokenKeys = {
|
|
10
|
+
jwksUrl: string | URL;
|
|
11
|
+
} | {
|
|
12
|
+
keys: JWTVerifyGetKey;
|
|
13
|
+
};
|
|
14
|
+
export type VerifyAccessTokenOptions = AccessTokenKeys & {
|
|
15
|
+
issuer: string;
|
|
16
|
+
audience: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function verifyAccessToken(token: string, options: VerifyAccessTokenOptions): Promise<AccessTokenClaims>;
|
|
19
|
+
//# sourceMappingURL=verify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../src/verify.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,eAAe,EACrB,MAAM,MAAM,CAAC;AAEd,MAAM,WAAW,iBAAiB;IAChC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,MAAM,eAAe,GACzB;IAAE,OAAO,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,eAAe,CAAA;CAAE,CAAC;AAExD,MAAM,MAAM,wBAAwB,GAAG,eAAe,GAAG;IACvD,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAsDF,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,wBAAwB,GAChC,OAAO,CAAC,iBAAiB,CAAC,CAU5B"}
|
package/dist/verify.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createRemoteJWKSet, errors, jwtVerify, } from "jose";
|
|
2
|
+
const remoteKeySets = new Map();
|
|
3
|
+
function remoteKeySet(jwksUrl) {
|
|
4
|
+
const href = new URL(jwksUrl).href;
|
|
5
|
+
let keySet = remoteKeySets.get(href);
|
|
6
|
+
if (keySet === undefined) {
|
|
7
|
+
keySet = createRemoteJWKSet(new URL(href));
|
|
8
|
+
remoteKeySets.set(href, keySet);
|
|
9
|
+
}
|
|
10
|
+
return keySet;
|
|
11
|
+
}
|
|
12
|
+
function invalidClaim(payload, claim, message) {
|
|
13
|
+
throw new errors.JWTClaimValidationFailed(message, payload, claim, "invalid");
|
|
14
|
+
}
|
|
15
|
+
function optionalString(payload, claim) {
|
|
16
|
+
const value = payload[claim];
|
|
17
|
+
if (value !== undefined && typeof value !== "string") {
|
|
18
|
+
invalidClaim(payload, claim, `"${claim}" claim must be a string`);
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
function toClaims(payload) {
|
|
23
|
+
const { sub, roles } = payload;
|
|
24
|
+
if (typeof sub !== "string" || sub === "") {
|
|
25
|
+
invalidClaim(payload, "sub", '"sub" claim must be a non-empty string');
|
|
26
|
+
}
|
|
27
|
+
if (!Array.isArray(roles) ||
|
|
28
|
+
!roles.every((role) => typeof role === "string")) {
|
|
29
|
+
invalidClaim(payload, "roles", '"roles" claim must be an array of strings');
|
|
30
|
+
}
|
|
31
|
+
const claims = { sub, roles };
|
|
32
|
+
for (const claim of ["client_id", "email", "name"]) {
|
|
33
|
+
const value = optionalString(payload, claim);
|
|
34
|
+
if (value !== undefined) {
|
|
35
|
+
claims[claim] = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return claims;
|
|
39
|
+
}
|
|
40
|
+
export async function verifyAccessToken(token, options) {
|
|
41
|
+
const keys = "keys" in options ? options.keys : remoteKeySet(options.jwksUrl);
|
|
42
|
+
const { payload } = await jwtVerify(token, keys, {
|
|
43
|
+
typ: "at+jwt",
|
|
44
|
+
issuer: options.issuer,
|
|
45
|
+
audience: options.audience,
|
|
46
|
+
algorithms: ["EdDSA"],
|
|
47
|
+
requiredClaims: ["exp", "sub"],
|
|
48
|
+
});
|
|
49
|
+
return toClaims(payload);
|
|
50
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@insaeng/auth",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Access-token verification for insaeng services",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/my-insaeng/insaeng-service-config.git",
|
|
8
|
+
"directory": "packages/auth"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/verify.d.ts",
|
|
14
|
+
"default": "./dist/verify.js"
|
|
15
|
+
},
|
|
16
|
+
"./testing": {
|
|
17
|
+
"types": "./dist/testing.d.ts",
|
|
18
|
+
"default": "./dist/testing.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public",
|
|
23
|
+
"provenance": true
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"jose": "6.2.19"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@insaeng/tsconfig": "0.1.0",
|
|
34
|
+
"@types/node": "24.19.4",
|
|
35
|
+
"typescript": "6.0.3",
|
|
36
|
+
"vitest": "5.1.2"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc -p tsconfig.build.json",
|
|
40
|
+
"typecheck": "tsc"
|
|
41
|
+
}
|
|
6
42
|
}
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
calculateJwkThumbprint,
|
|
3
|
+
createLocalJWKSet,
|
|
4
|
+
exportJWK,
|
|
5
|
+
generateKeyPair,
|
|
6
|
+
SignJWT,
|
|
7
|
+
type JSONWebKeySet,
|
|
8
|
+
type JWTVerifyGetKey,
|
|
9
|
+
} from "jose";
|
|
10
|
+
import type { AccessTokenClaims } from "./verify.js";
|
|
11
|
+
|
|
12
|
+
export interface TestIssuerOptions {
|
|
13
|
+
issuer?: string;
|
|
14
|
+
audience?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SignTestTokenOptions {
|
|
18
|
+
typ?: string;
|
|
19
|
+
issuer?: string;
|
|
20
|
+
audience?: string;
|
|
21
|
+
expirationTime?: string | number | Date;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface TestIssuer {
|
|
25
|
+
issuer: string;
|
|
26
|
+
audience: string;
|
|
27
|
+
jwks: JSONWebKeySet;
|
|
28
|
+
keys: JWTVerifyGetKey;
|
|
29
|
+
signAccessToken(
|
|
30
|
+
claims: Partial<AccessTokenClaims>,
|
|
31
|
+
options?: SignTestTokenOptions,
|
|
32
|
+
): Promise<string>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function createTestIssuer({
|
|
36
|
+
issuer = "https://auth.insaeng.test",
|
|
37
|
+
audience = "insaeng-test-service",
|
|
38
|
+
}: TestIssuerOptions = {}): Promise<TestIssuer> {
|
|
39
|
+
const { privateKey, publicKey } = await generateKeyPair("EdDSA", {
|
|
40
|
+
crv: "Ed25519",
|
|
41
|
+
});
|
|
42
|
+
const jwk = await exportJWK(publicKey);
|
|
43
|
+
const kid = await calculateJwkThumbprint(jwk);
|
|
44
|
+
const jwks: JSONWebKeySet = {
|
|
45
|
+
keys: [{ ...jwk, kid, alg: "EdDSA", use: "sig" }],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
issuer,
|
|
50
|
+
audience,
|
|
51
|
+
jwks,
|
|
52
|
+
keys: createLocalJWKSet(jwks),
|
|
53
|
+
async signAccessToken(claims, options = {}) {
|
|
54
|
+
const { sub = "test-user", roles = [], ...rest } = claims;
|
|
55
|
+
return new SignJWT({ roles, ...rest })
|
|
56
|
+
.setProtectedHeader({ alg: "EdDSA", typ: options.typ ?? "at+jwt", kid })
|
|
57
|
+
.setSubject(sub)
|
|
58
|
+
.setIssuer(options.issuer ?? issuer)
|
|
59
|
+
.setAudience(options.audience ?? audience)
|
|
60
|
+
.setIssuedAt()
|
|
61
|
+
.setExpirationTime(options.expirationTime ?? "5m")
|
|
62
|
+
.sign(privateKey);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
package/src/verify.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRemoteJWKSet,
|
|
3
|
+
errors,
|
|
4
|
+
jwtVerify,
|
|
5
|
+
type JWTPayload,
|
|
6
|
+
type JWTVerifyGetKey,
|
|
7
|
+
} from "jose";
|
|
8
|
+
|
|
9
|
+
export interface AccessTokenClaims {
|
|
10
|
+
sub: string;
|
|
11
|
+
roles: string[];
|
|
12
|
+
client_id?: string;
|
|
13
|
+
email?: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type AccessTokenKeys =
|
|
18
|
+
{ jwksUrl: string | URL } | { keys: JWTVerifyGetKey };
|
|
19
|
+
|
|
20
|
+
export type VerifyAccessTokenOptions = AccessTokenKeys & {
|
|
21
|
+
issuer: string;
|
|
22
|
+
audience: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const remoteKeySets = new Map<string, JWTVerifyGetKey>();
|
|
26
|
+
|
|
27
|
+
function remoteKeySet(jwksUrl: string | URL): JWTVerifyGetKey {
|
|
28
|
+
const href = new URL(jwksUrl).href;
|
|
29
|
+
let keySet = remoteKeySets.get(href);
|
|
30
|
+
if (keySet === undefined) {
|
|
31
|
+
keySet = createRemoteJWKSet(new URL(href));
|
|
32
|
+
remoteKeySets.set(href, keySet);
|
|
33
|
+
}
|
|
34
|
+
return keySet;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function invalidClaim(
|
|
38
|
+
payload: JWTPayload,
|
|
39
|
+
claim: string,
|
|
40
|
+
message: string,
|
|
41
|
+
): never {
|
|
42
|
+
throw new errors.JWTClaimValidationFailed(message, payload, claim, "invalid");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function optionalString(
|
|
46
|
+
payload: JWTPayload,
|
|
47
|
+
claim: string,
|
|
48
|
+
): string | undefined {
|
|
49
|
+
const value = payload[claim];
|
|
50
|
+
if (value !== undefined && typeof value !== "string") {
|
|
51
|
+
invalidClaim(payload, claim, `"${claim}" claim must be a string`);
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function toClaims(payload: JWTPayload): AccessTokenClaims {
|
|
57
|
+
const { sub, roles } = payload;
|
|
58
|
+
if (typeof sub !== "string" || sub === "") {
|
|
59
|
+
invalidClaim(payload, "sub", '"sub" claim must be a non-empty string');
|
|
60
|
+
}
|
|
61
|
+
if (
|
|
62
|
+
!Array.isArray(roles) ||
|
|
63
|
+
!roles.every((role) => typeof role === "string")
|
|
64
|
+
) {
|
|
65
|
+
invalidClaim(payload, "roles", '"roles" claim must be an array of strings');
|
|
66
|
+
}
|
|
67
|
+
const claims: AccessTokenClaims = { sub, roles };
|
|
68
|
+
for (const claim of ["client_id", "email", "name"] as const) {
|
|
69
|
+
const value = optionalString(payload, claim);
|
|
70
|
+
if (value !== undefined) {
|
|
71
|
+
claims[claim] = value;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return claims;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function verifyAccessToken(
|
|
78
|
+
token: string,
|
|
79
|
+
options: VerifyAccessTokenOptions,
|
|
80
|
+
): Promise<AccessTokenClaims> {
|
|
81
|
+
const keys = "keys" in options ? options.keys : remoteKeySet(options.jwksUrl);
|
|
82
|
+
const { payload } = await jwtVerify(token, keys, {
|
|
83
|
+
typ: "at+jwt",
|
|
84
|
+
issuer: options.issuer,
|
|
85
|
+
audience: options.audience,
|
|
86
|
+
algorithms: ["EdDSA"],
|
|
87
|
+
requiredClaims: ["exp", "sub"],
|
|
88
|
+
});
|
|
89
|
+
return toClaims(payload);
|
|
90
|
+
}
|
package/README.md
DELETED