@postxl/generators 1.28.0 → 1.30.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/backend-authentication/authentication.generator.js +5 -15
- package/dist/backend-authentication/authentication.generator.js.map +1 -1
- package/dist/backend-authentication/generators/authentication-module.generator.js +3 -4
- package/dist/backend-authentication/generators/authentication-module.generator.js.map +1 -1
- package/dist/backend-authentication/generators/authentication-service.generator.js +53 -242
- package/dist/backend-authentication/generators/authentication-service.generator.js.map +1 -1
- package/dist/backend-authentication/generators/authentication-types.generator.js +1 -13
- package/dist/backend-authentication/generators/authentication-types.generator.js.map +1 -1
- package/dist/backend-authentication/template/README.md +18 -23
- package/dist/backend-authentication/template/src/authentication.config.ts +0 -15
- package/dist/backend-authentication/template/src/authentication.mock.service.ts +0 -56
- package/dist/backend-core/generators/api-config.generator.js +4 -2
- package/dist/backend-core/generators/api-config.generator.js.map +1 -1
- package/dist/backend-core/generators/dotenv-example.generator.js +3 -0
- package/dist/backend-core/generators/dotenv-example.generator.js.map +1 -1
- package/dist/backend-core/generators/main.generator.js +10 -12
- package/dist/backend-core/generators/main.generator.js.map +1 -1
- package/dist/backend-core/modules/backend-module-xlport.generator.js +1 -1
- package/dist/backend-core/modules/backend-module-xlport.generator.js.map +1 -1
- package/dist/backend-core/template/apps/api/src/console.logger.ts +56 -0
- package/dist/backend-core/template/apps/api/src/e2e.ts +32 -23
- package/dist/backend-repositories/generators/model-repository.generator.js +20 -14
- package/dist/backend-repositories/generators/model-repository.generator.js.map +1 -1
- package/dist/backend-router-trpc/template/viewer.router.ts +6 -11
- package/dist/backend-update/model-update-service.generator.js +32 -16
- package/dist/backend-update/model-update-service.generator.js.map +1 -1
- package/dist/devops/devops.generator.d.ts +0 -1
- package/dist/devops/devops.generator.js +0 -1
- package/dist/devops/devops.generator.js.map +1 -1
- package/dist/devops/generators/configure-keycloak-sh.generator.js +3 -5
- package/dist/devops/generators/configure-keycloak-sh.generator.js.map +1 -1
- package/dist/devops/generators/docker-compose-yml.generator.js +1 -3
- package/dist/devops/generators/docker-compose-yml.generator.js.map +1 -1
- package/dist/devops/generators/jenkinsfile.generator.js +2 -12
- package/dist/devops/generators/jenkinsfile.generator.js.map +1 -1
- package/dist/frontend-core/template/src/context-providers/auth-context-provider.tsx +1 -1
- package/dist/types/generators/dto-types.generator.js +20 -4
- package/dist/types/generators/dto-types.generator.js.map +1 -1
- package/package.json +4 -4
- package/dist/backend-authentication/template/src/authentication.controller.ts +0 -105
|
@@ -50,7 +50,7 @@ exports.generator = {
|
|
|
50
50
|
requires: [backend_core_1.backendGeneratorId, base_1.baseGeneratorId, backend_view_1.backendViewGeneratorId, backend_actions_1.backendActionsGeneratorId],
|
|
51
51
|
register: (context) => {
|
|
52
52
|
context.backend.packageJson.devDependencies.push({ packageName: '@types/jsonwebtoken', version: '9.0.10' });
|
|
53
|
-
context.backend.packageJson.dependencies.push({ packageName: '@fastify/cookie', version: '11.0.2' }, { packageName: '@fastify/cors', version: '11.2.0' }, { packageName: '@fastify/helmet', version: '13.0.2' }, { packageName: 'jsonwebtoken', version: '9.0.3' }
|
|
53
|
+
context.backend.packageJson.dependencies.push({ packageName: '@fastify/cookie', version: '11.0.2' }, { packageName: '@fastify/cors', version: '11.2.0' }, { packageName: '@fastify/helmet', version: '13.0.2' }, { packageName: 'jsonwebtoken', version: '9.0.3' });
|
|
54
54
|
const module = {
|
|
55
55
|
name: Generator.toClassName('AuthenticationModule'),
|
|
56
56
|
location: Generator.toBackendModuleLocation(`@authentication/authentication.module`),
|
|
@@ -66,12 +66,9 @@ exports.generator = {
|
|
|
66
66
|
imports: [Generator.ts(`import { parseStringList } from '@authentication/utils'`)],
|
|
67
67
|
decoder: Generator.ts(`
|
|
68
68
|
AUTH: zEnvBoolean.optional().default(true),
|
|
69
|
-
KEYCLOAK_URL: z.
|
|
69
|
+
KEYCLOAK_URL: z.url().transform((val) => new URL(val)),
|
|
70
70
|
KEYCLOAK_CLIENT_ID: z.string(),
|
|
71
|
-
KEYCLOAK_CLIENT_SECRET: z.string(),
|
|
72
71
|
KEYCLOAK_REALM: z.string(),
|
|
73
|
-
KEYCLOAK_REDIRECT_URL: z.string().url().transform((val) => new URL(val)),
|
|
74
|
-
KEYCLOAK_LOGOUT_REDIRECT_URL: z.string().url().transform((val) => new URL(val)),
|
|
75
72
|
AUTH_MOCK_SUB: z.string().optional(),
|
|
76
73
|
AUTH_MOCK_NAME: z.string().optional(),
|
|
77
74
|
AUTH_MOCK_EMAIL: z.string().optional(),
|
|
@@ -84,17 +81,13 @@ exports.generator = {
|
|
|
84
81
|
roleClaimPath: ${JSON.stringify(context.schema.auth?.roleClaimPath ?? 'realm_access.roles')},
|
|
85
82
|
url: val.API_URL,
|
|
86
83
|
urlPrefix: val.API_PREFIX,
|
|
87
|
-
|
|
84
|
+
|
|
88
85
|
frontendUrl: val.FRONTEND_URL,
|
|
89
|
-
|
|
86
|
+
|
|
90
87
|
clientId: val.KEYCLOAK_CLIENT_ID,
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
|
|
93
89
|
realm: val.KEYCLOAK_REALM,
|
|
94
90
|
keycloakUrl: val.KEYCLOAK_URL,
|
|
95
|
-
redirectUrl: val.KEYCLOAK_REDIRECT_URL,
|
|
96
|
-
|
|
97
|
-
postLogoutRedirectUrl: val.KEYCLOAK_LOGOUT_REDIRECT_URL,
|
|
98
91
|
} : {
|
|
99
92
|
enableAuthentication: false as const,
|
|
100
93
|
mockIdentity: {
|
|
@@ -109,10 +102,7 @@ exports.generator = {
|
|
|
109
102
|
dotEnvExample: `
|
|
110
103
|
KEYCLOAK_URL=http://localhost:8080
|
|
111
104
|
KEYCLOAK_CLIENT_ID=development
|
|
112
|
-
KEYCLOAK_CLIENT_SECRET="secret"
|
|
113
105
|
KEYCLOAK_REALM=postxl
|
|
114
|
-
KEYCLOAK_REDIRECT_URL=\${API_URL}/auth/callback
|
|
115
|
-
KEYCLOAK_LOGOUT_REDIRECT_URL=\${FRONTEND_URL}
|
|
116
106
|
AUTH_MOCK_SUB=test
|
|
117
107
|
AUTH_MOCK_NAME="Test User"
|
|
118
108
|
AUTH_MOCK_EMAIL=test@postxl.com
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authentication.generator.js","sourceRoot":"","sources":["../../src/backend-authentication/authentication.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAmC;AAEnC,6DAA8C;AAE9C,wDAA2E;AAC3E,kDAA+E;AAC/E,kDAAkE;AAClE,kCAAmD;AAGnD,4EAAqE;AACrE,kGAA2F;AAC3F,oGAA6F;AAC7F,gGAAyF;AAiB5E,QAAA,WAAW,GAAG,SAAS,CAAC,sBAAsB,CAAC,wBAAwB,CAAC,CAAA;AAExE,QAAA,SAAS,GAAiC;IACrD,EAAE,EAAE,mBAAW;IACf,QAAQ,EAAE,CAAC,iCAAkB,EAAE,sBAAe,EAAE,qCAAsB,EAAE,2CAAyB,CAAC;IAElG,QAAQ,EAAE,CAAsC,OAAgB,EAAiB,EAAE;QACjF,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,WAAW,EAAE,qBAAqB,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,CAAA;QAC3G,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,YAAY,CAAC,IAAI,CAC3C,EAAE,WAAW,EAAE,iBAAiB,EAAE,OAAO,EAAE,QAAQ,EAAE,EACrD,EAAE,WAAW,EAAE,eAAe,EAAE,OAAO,EAAE,QAAQ,EAAE,EACnD,EAAE,WAAW,EAAE,iBAAiB,EAAE,OAAO,EAAE,QAAQ,EAAE,EACrD,EAAE,WAAW,EAAE,cAAc,EAAE,OAAO,EAAE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"authentication.generator.js","sourceRoot":"","sources":["../../src/backend-authentication/authentication.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;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AAa,CAAC,EAAE,UAAU,EAAE,6BAA6B,EAAE,GAAG,EAAE,CAAC,CAAA;QAE7E,OAAO,OAAO,CAAA;IAChB,CAAC;CACF,CAAA"}
|
|
@@ -46,7 +46,6 @@ function generateAuthenticationModule(context) {
|
|
|
46
46
|
${imports.generate()}
|
|
47
47
|
|
|
48
48
|
import { MockAuthenticationService } from './authentication.mock.service'
|
|
49
|
-
import { AuthenticationController } from './authentication.controller'
|
|
50
49
|
import type { AuthenticationConfig } from './authentication.config'
|
|
51
50
|
|
|
52
51
|
export class ${context.authentication.module.name} {
|
|
@@ -54,7 +53,7 @@ export class ${context.authentication.module.name} {
|
|
|
54
53
|
if (process.env.NODE_ENV === 'production' && !config.enableAuthentication) {
|
|
55
54
|
throw new Error('Authentication is disabled in production mode. Remove the env variable "AUTH=false"!')
|
|
56
55
|
}
|
|
57
|
-
|
|
56
|
+
|
|
58
57
|
const serviceProvider: Provider =
|
|
59
58
|
config.enableAuthentication
|
|
60
59
|
? {
|
|
@@ -64,13 +63,13 @@ export class ${context.authentication.module.name} {
|
|
|
64
63
|
: {
|
|
65
64
|
provide: ${context.authentication.service.name},
|
|
66
65
|
useValue: new MockAuthenticationService(config),
|
|
67
|
-
}
|
|
66
|
+
}
|
|
68
67
|
|
|
69
68
|
return {
|
|
70
69
|
module: AuthenticationModule,
|
|
71
70
|
providers: [serviceProvider],
|
|
72
71
|
exports: [serviceProvider],
|
|
73
|
-
controllers: [
|
|
72
|
+
controllers: [],
|
|
74
73
|
global: true,
|
|
75
74
|
}
|
|
76
75
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authentication-module.generator.js","sourceRoot":"","sources":["../../../src/backend-authentication/generators/authentication-module.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,
|
|
1
|
+
{"version":3,"file":"authentication-module.generator.js","sourceRoot":"","sources":["../../../src/backend-authentication/generators/authentication-module.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,oEAwCC;AA5CD,6DAA8C;AAI9C,SAAgB,4BAA4B,CAAC,OAAsB;IACjE,MAAM,OAAO,GAAG,SAAS,CAAC,eAAe;QACvC,EAAE;SACD,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,CAAC;SAC7C,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,CAAA;IAEzG,OAAO,QAAQ,CAAC;;;IAGd,OAAO,CAAC,QAAQ,EAAE;;;;;eAKP,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,IAAI;;;;;;;;;uBAS1B,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI;4BAC9B,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI;;;uBAGxC,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI;;;;;;;;;;;;EAYxD,CAAA;AACF,CAAC"}
|
|
@@ -3,188 +3,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.generateAuthenticationService = generateAuthenticationService;
|
|
4
4
|
function generateAuthenticationService(context) {
|
|
5
5
|
return /* ts */ `
|
|
6
|
-
|
|
7
|
-
import '@fastify/cookie'
|
|
8
|
-
|
|
9
|
-
import * as z from 'zod'
|
|
10
|
-
import { Injectable, Logger, OnModuleInit } from '@nestjs/common'
|
|
6
|
+
import { Injectable, Logger } from '@nestjs/common'
|
|
11
7
|
|
|
12
8
|
import { FastifyRequest } from 'fastify'
|
|
13
9
|
import jwt from 'jsonwebtoken'
|
|
14
10
|
import { JwksClient } from 'jwks-rsa'
|
|
15
|
-
import { OidcClient } from 'oidc-client-ts'
|
|
16
11
|
|
|
17
12
|
import { USER_ROLES } from '@types'
|
|
18
13
|
import type { UserRole, UserRoles } from '@types'
|
|
19
14
|
|
|
20
15
|
import type { AuthenticationConfig_EnabledKeycloak } from './authentication.config'
|
|
21
|
-
import {
|
|
16
|
+
import { UserInfo, zUserInfo } from './authentication.types'
|
|
22
17
|
import { getByPath, parseStringList, unknownToErrorMessage } from './utils'
|
|
23
18
|
|
|
24
|
-
/**
|
|
25
|
-
* State object for the authentication service.
|
|
26
|
-
*/
|
|
27
|
-
const zState = z.object({
|
|
28
|
-
redirectTo: z.string().optional(),
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* State object for the authentication service.
|
|
33
|
-
*/
|
|
34
|
-
export type State = z.infer<typeof zState>
|
|
35
|
-
|
|
36
19
|
@Injectable()
|
|
37
|
-
export class ${context.authentication.service.name}
|
|
20
|
+
export class ${context.authentication.service.name} {
|
|
38
21
|
private readonly logger = new Logger(${context.authentication.service.name}.name)
|
|
39
|
-
|
|
40
|
-
private readonly oidc: OidcClient
|
|
22
|
+
private readonly keycloakBaseUrl: string
|
|
41
23
|
|
|
42
24
|
private readonly jwks: JwksClient
|
|
43
25
|
|
|
44
|
-
// collects subscriptions of users, with the key being the user's email and the value being the last check date
|
|
45
|
-
// this is used to cache the subscription check for a certain amount of time
|
|
46
|
-
private readonly subscriptions: Map<string, Date> = new Map()
|
|
47
|
-
|
|
48
|
-
private readonly _cookieDomain: string
|
|
49
|
-
public get cookieDomain(): string {
|
|
50
|
-
return this._cookieDomain
|
|
51
|
-
}
|
|
52
|
-
|
|
53
26
|
constructor(public readonly config: AuthenticationConfig_EnabledKeycloak) {
|
|
54
|
-
|
|
55
|
-
this.oidc = new OidcClient({
|
|
56
|
-
authority: this.config.keycloakUrl.toString(),
|
|
57
|
-
client_id: this.config.clientId,
|
|
58
|
-
redirect_uri: this.config.redirectUrl.toString(),
|
|
59
|
-
client_authentication: 'client_secret_basic',
|
|
60
|
-
client_secret: this.config.clientSecret,
|
|
61
|
-
response_type: 'code',
|
|
62
|
-
// prettier-ignore
|
|
63
|
-
metadataUrl: \`\${this.config.keycloakUrl.toString()}/realms/\${this.config.realm}/.well-known/openid-configuration\`,
|
|
64
|
-
})
|
|
65
|
-
|
|
27
|
+
this.keycloakBaseUrl = this.config.keycloakUrl.toString().replace(/\\/$/, '')
|
|
66
28
|
this.jwks = new JwksClient({
|
|
67
|
-
jwksUri: \`\${this.
|
|
29
|
+
jwksUri: \`\${this.keycloakBaseUrl}/realms/\${this.config.realm}/protocol/openid-connect/certs\`,
|
|
68
30
|
timeout: 30_000,
|
|
69
31
|
})
|
|
70
|
-
|
|
71
|
-
// Setting cookie domain from the URL by removing the subdomain unless the URL
|
|
72
|
-
// is already a top-level domain.
|
|
73
|
-
// Background: The cookie domain shall match the server domain without subdomain so
|
|
74
|
-
// that browser forwards the cookie to the server.
|
|
75
|
-
const parts = this.config.url.hostname.split('.')
|
|
76
|
-
|
|
77
|
-
if (parts.length > 2) {
|
|
78
|
-
this._cookieDomain = parts.slice(1).join('.')
|
|
79
|
-
} else {
|
|
80
|
-
this._cookieDomain = this.config.url.hostname
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public async onModuleInit() {
|
|
85
|
-
// Clear Keycloak stale state to check if Keycloak is running
|
|
86
|
-
try {
|
|
87
|
-
await this.oidc.clearStaleState()
|
|
88
|
-
} catch (error) {
|
|
89
|
-
this.logger.error('Error clearing stale state', error)
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Refreshes the access token via the refresh token.
|
|
95
|
-
*/
|
|
96
|
-
public async refreshAccessToken(req: FastifyRequest): Promise<
|
|
97
|
-
| {
|
|
98
|
-
ok: true
|
|
99
|
-
accessToken: string
|
|
100
|
-
idToken: string
|
|
101
|
-
refreshToken?: string
|
|
102
|
-
state: State | null
|
|
103
|
-
}
|
|
104
|
-
| {
|
|
105
|
-
ok: false
|
|
106
|
-
error: unknown
|
|
107
|
-
}
|
|
108
|
-
> {
|
|
109
|
-
const refreshToken = req.cookies[\`\${authenticationCookieName}-refresh\`]
|
|
110
|
-
if (!refreshToken) {
|
|
111
|
-
return { ok: false, error: new Error('No refresh token provided') }
|
|
112
|
-
}
|
|
113
|
-
const url = \`\${this.config.keycloakUrl.toString()}/realms/\${this.config.realm}/protocol/openid-connect/token\`
|
|
114
|
-
const params = new URLSearchParams({
|
|
115
|
-
grant_type: 'refresh_token',
|
|
116
|
-
refresh_token: refreshToken,
|
|
117
|
-
client_id: this.config.clientId,
|
|
118
|
-
client_secret: this.config.clientSecret,
|
|
119
|
-
})
|
|
120
|
-
try {
|
|
121
|
-
const response = await fetch(url, {
|
|
122
|
-
method: 'POST',
|
|
123
|
-
headers: {
|
|
124
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
125
|
-
},
|
|
126
|
-
body: params.toString(),
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
if (!response.ok) {
|
|
130
|
-
return { ok: false, error: new Error(\`Failed to refresh access token: \${response.statusText}\`) }
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const data = (await response.json()) as {
|
|
134
|
-
access_token: string
|
|
135
|
-
id_token: string
|
|
136
|
-
refresh_token?: string
|
|
137
|
-
state?: string
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return {
|
|
141
|
-
ok: true,
|
|
142
|
-
accessToken: data.access_token,
|
|
143
|
-
idToken: data.id_token,
|
|
144
|
-
refreshToken: data.refresh_token,
|
|
145
|
-
state: null,
|
|
146
|
-
}
|
|
147
|
-
} catch (error) {
|
|
148
|
-
return { ok: false, error: error }
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Extracts the code from URL and exchanges it for an access token.
|
|
154
|
-
*/
|
|
155
|
-
public async getAccessTokenFromURL(url: string): Promise<
|
|
156
|
-
| {
|
|
157
|
-
ok: true
|
|
158
|
-
accessToken: string
|
|
159
|
-
refreshToken?: string
|
|
160
|
-
state: State | null
|
|
161
|
-
}
|
|
162
|
-
| {
|
|
163
|
-
ok: false
|
|
164
|
-
error: unknown
|
|
165
|
-
}
|
|
166
|
-
> {
|
|
167
|
-
try {
|
|
168
|
-
this.logger.log(\`Getting access token from URL \${url}\`)
|
|
169
|
-
const res = await this.oidc.processSigninResponse(url)
|
|
170
|
-
const state = this.decodeState(res.userState)
|
|
171
|
-
|
|
172
|
-
return { ok: true, accessToken: res.access_token, refreshToken: res.refresh_token, state }
|
|
173
|
-
} catch (error) {
|
|
174
|
-
return { ok: false, error: error }
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Returns the URL that the user may use to perform authentication.
|
|
180
|
-
*/
|
|
181
|
-
public async getAuthenticationUrl({ redirectTo }: { redirectTo?: string }): Promise<string> {
|
|
182
|
-
const request = await this.oidc.createSigninRequest({
|
|
183
|
-
scope: 'email profile openid',
|
|
184
|
-
state: this.encodeState({ redirectTo }),
|
|
185
|
-
})
|
|
186
|
-
|
|
187
|
-
return replaceDockerHost(request.url)
|
|
188
32
|
}
|
|
189
33
|
|
|
190
34
|
/**
|
|
@@ -203,10 +47,10 @@ export class ${context.authentication.service.name} implements OnModuleInit {
|
|
|
203
47
|
}
|
|
204
48
|
|
|
205
49
|
/**
|
|
206
|
-
* Extracts access token from
|
|
50
|
+
* Extracts the access token from the Authorization header.
|
|
207
51
|
*/
|
|
208
52
|
private getAccessTokenFromRequest(req: FastifyRequest): string | undefined {
|
|
209
|
-
const bearer = req.
|
|
53
|
+
const bearer = req.headers.authorization
|
|
210
54
|
|
|
211
55
|
if (!bearer) {
|
|
212
56
|
return
|
|
@@ -218,22 +62,29 @@ export class ${context.authentication.service.name} implements OnModuleInit {
|
|
|
218
62
|
/**
|
|
219
63
|
* Extracts the user from the API token.
|
|
220
64
|
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
65
|
+
* First tries to read verified user claims from the access token payload; falls back
|
|
66
|
+
* to fetching the userinfo endpoint directly.
|
|
223
67
|
*/
|
|
224
68
|
public async getUserInfo(accessToken: string): Promise<{ ok: true; userInfo: UserInfo } | { ok: false }> {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (userInfo?.exp && userInfo.exp > new Date()) {
|
|
69
|
+
const userInfo = await this.accessTokenToUserInfo(accessToken)
|
|
70
|
+
if (userInfo) {
|
|
228
71
|
return { ok: true, userInfo }
|
|
229
72
|
}
|
|
230
73
|
|
|
231
74
|
try {
|
|
232
|
-
const endpoint =
|
|
75
|
+
const endpoint = \`\${this.keycloakBaseUrl}/realms/\${this.config.realm}/protocol/openid-connect/userinfo\`
|
|
76
|
+
const abortController = new AbortController()
|
|
77
|
+
const timeout = setTimeout(() => abortController.abort(), 30_000)
|
|
233
78
|
const response = await fetch(endpoint, {
|
|
234
79
|
method: 'GET',
|
|
235
80
|
headers: { Authorization: \`Bearer \${accessToken}\` },
|
|
236
|
-
|
|
81
|
+
signal: abortController.signal,
|
|
82
|
+
}).finally(() => clearTimeout(timeout))
|
|
83
|
+
|
|
84
|
+
if (!response.ok) {
|
|
85
|
+
this.logger.warn(\`Userinfo endpoint returned \${response.status}\`)
|
|
86
|
+
return { ok: false }
|
|
87
|
+
}
|
|
237
88
|
|
|
238
89
|
const raw = await response.text()
|
|
239
90
|
|
|
@@ -251,6 +102,11 @@ export class ${context.authentication.service.name} implements OnModuleInit {
|
|
|
251
102
|
return { ok: false }
|
|
252
103
|
}
|
|
253
104
|
} catch (error) {
|
|
105
|
+
if ((error as { name?: string })?.name === 'AbortError') {
|
|
106
|
+
this.logger.warn('Userinfo request timed out')
|
|
107
|
+
return { ok: false }
|
|
108
|
+
}
|
|
109
|
+
|
|
254
110
|
this.logger.error(unknownToErrorMessage(error, 'Failed to fetch user info'))
|
|
255
111
|
return { ok: false }
|
|
256
112
|
}
|
|
@@ -272,17 +128,7 @@ export class ${context.authentication.service.name} implements OnModuleInit {
|
|
|
272
128
|
|
|
273
129
|
public async getUserRoles(accessToken: string): Promise<{ ok: true; userRoles: UserRoles } | { ok: false }> {
|
|
274
130
|
try {
|
|
275
|
-
const
|
|
276
|
-
this.jwks.getSigningKey(header.kid, (err, key) => {
|
|
277
|
-
if (err || !key) {
|
|
278
|
-
callback(err)
|
|
279
|
-
} else {
|
|
280
|
-
callback(null, key.getPublicKey())
|
|
281
|
-
}
|
|
282
|
-
})
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const decoded = await _jwtVerify(accessToken, getKey, { algorithms: ['RS256'] })
|
|
131
|
+
const decoded = await this.verifyAccessToken(accessToken)
|
|
286
132
|
|
|
287
133
|
if (typeof decoded === 'object' && decoded !== null) {
|
|
288
134
|
const rawUserRoles = getByPath(decoded, this.config.roleClaimPath)
|
|
@@ -297,64 +143,45 @@ export class ${context.authentication.service.name} implements OnModuleInit {
|
|
|
297
143
|
|
|
298
144
|
return { ok: true, userRoles: [] }
|
|
299
145
|
} catch (error) {
|
|
300
|
-
|
|
146
|
+
this.logger.error(unknownToErrorMessage(error, 'Failed to verify user roles'))
|
|
301
147
|
|
|
302
148
|
return { ok: false }
|
|
303
149
|
}
|
|
304
150
|
}
|
|
305
151
|
|
|
306
152
|
/**
|
|
307
|
-
* Retrieves
|
|
153
|
+
* Retrieves UserInfo from a verified Access Token.
|
|
308
154
|
*/
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
})
|
|
316
|
-
return signoutRequest.url
|
|
317
|
-
}
|
|
155
|
+
private async accessTokenToUserInfo(accessToken: string): Promise<UserInfo | undefined> {
|
|
156
|
+
try {
|
|
157
|
+
const decodedToken = await this.verifyAccessToken(accessToken)
|
|
158
|
+
if (!decodedToken || typeof decodedToken !== 'object') {
|
|
159
|
+
return undefined
|
|
160
|
+
}
|
|
318
161
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
}
|
|
327
|
-
const parsedToken = zUserInfo.safeParse(decodedToken.payload)
|
|
328
|
-
if (!parsedToken.success) {
|
|
162
|
+
const parsedToken = zUserInfo.safeParse(decodedToken)
|
|
163
|
+
if (!parsedToken.success) {
|
|
164
|
+
return undefined
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return parsedToken.data
|
|
168
|
+
} catch {
|
|
329
169
|
return undefined
|
|
330
170
|
}
|
|
331
|
-
|
|
332
|
-
return parsedToken.data
|
|
333
171
|
}
|
|
334
172
|
|
|
335
|
-
private
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
return null
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
if (typeof state !== 'string') {
|
|
348
|
-
return null
|
|
173
|
+
private verifyAccessToken(accessToken: string): Promise<string | jwt.JwtPayload | undefined> {
|
|
174
|
+
const getKey = (header: jwt.JwtHeader, callback: jwt.SigningKeyCallback) => {
|
|
175
|
+
this.jwks.getSigningKey(header.kid, (err, key) => {
|
|
176
|
+
if (err || !key) {
|
|
177
|
+
callback(err)
|
|
178
|
+
} else {
|
|
179
|
+
callback(null, key.getPublicKey())
|
|
180
|
+
}
|
|
181
|
+
})
|
|
349
182
|
}
|
|
350
183
|
|
|
351
|
-
|
|
352
|
-
const raw = JSON.parse(Buffer.from(state, 'base64').toString()) as unknown
|
|
353
|
-
|
|
354
|
-
return zState.parse(raw)
|
|
355
|
-
} catch (_error) {
|
|
356
|
-
return null
|
|
357
|
-
}
|
|
184
|
+
return _jwtVerify(accessToken, getKey, { algorithms: ['RS256'] })
|
|
358
185
|
}
|
|
359
186
|
}
|
|
360
187
|
|
|
@@ -375,22 +202,6 @@ function _jwtVerify(
|
|
|
375
202
|
}
|
|
376
203
|
})
|
|
377
204
|
})
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* Replace Docker internal hostname with localhost for local development.
|
|
382
|
-
*
|
|
383
|
-
* SECURITY NOTE: These HTTP URLs are for local development only.
|
|
384
|
-
* In production, use HTTPS by configuring the keycloakUrl in the authentication config
|
|
385
|
-
* to use https:// protocol. The URLs here are only used for Docker Compose development.
|
|
386
|
-
*/
|
|
387
|
-
function replaceDockerHost(url: string): string {
|
|
388
|
-
// Development-only HTTP URLs for Docker Compose
|
|
389
|
-
// prettier-ignore
|
|
390
|
-
if (url.startsWith('http://keycloak:8080')) { // NOSONAR - this is only for execution in test env
|
|
391
|
-
return url.replace('http://keycloak:8080', 'http://localhost:3000/auth') // NOSONAR - this is only for execution in test env
|
|
392
|
-
}
|
|
393
|
-
return url
|
|
394
205
|
}`;
|
|
395
206
|
}
|
|
396
207
|
//# sourceMappingURL=authentication-service.generator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authentication-service.generator.js","sourceRoot":"","sources":["../../../src/backend-authentication/generators/authentication-service.generator.ts"],"names":[],"mappings":";;AAEA,
|
|
1
|
+
{"version":3,"file":"authentication-service.generator.js","sourceRoot":"","sources":["../../../src/backend-authentication/generators/authentication-service.generator.ts"],"names":[],"mappings":";;AAEA,sEA0MC;AA1MD,SAAgB,6BAA6B,CAAC,OAAsB;IAClE,OAAO,QAAQ,CAAC;;;;;;;;;;;;;;;eAeH,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI;yCACT,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAwL1E,CAAA;AACF,CAAC"}
|
|
@@ -47,19 +47,7 @@ export const zUserRole = z.enum(USER_ROLES)
|
|
|
47
47
|
|
|
48
48
|
export const z${context.authentication.types.userRoles.name} = z.array(zUserRole)
|
|
49
49
|
|
|
50
|
-
export type ViewerResult = Result<
|
|
51
|
-
Viewer,
|
|
52
|
-
| {
|
|
53
|
-
type: 'redirect'
|
|
54
|
-
redirectUrl: string
|
|
55
|
-
}
|
|
56
|
-
| { type: 'waitingForServer' }
|
|
57
|
-
>
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* The name of the cookie that holds the authentication token.
|
|
61
|
-
*/
|
|
62
|
-
export const authenticationCookieName = 'pxl-auth'
|
|
50
|
+
export type ViewerResult = Result<Viewer, { type: 'waitingForServer' }>
|
|
63
51
|
`;
|
|
64
52
|
}
|
|
65
53
|
//# sourceMappingURL=authentication-types.generator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authentication-types.generator.js","sourceRoot":"","sources":["../../../src/backend-authentication/generators/authentication-types.generator.ts"],"names":[],"mappings":";;AAEA,
|
|
1
|
+
{"version":3,"file":"authentication-types.generator.js","sourceRoot":"","sources":["../../../src/backend-authentication/generators/authentication-types.generator.ts"],"names":[],"mappings":";;AAEA,kEAgDC;AAhDD,SAAgB,2BAA2B,CAAC,OAAsB;IAChE,OAAO,QAAQ,CAAC;;;;;;;KAOb,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI;;;gBAG/B,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;KA2BrD,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI;;cAEjC,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,sBAAsB,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI;;;;gBAIxG,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI;;;CAG1D,CAAA;AACD,CAAC"}
|
|
@@ -2,34 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
PostXL uses Keycloak for authentication and authorization.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Authentication is performed on the frontend via [`keycloak-js`](https://www.keycloak.org/securing-apps/javascript-adapter)
|
|
6
|
+
using the public-client PKCE flow. The browser obtains the access token from Keycloak directly and attaches it to
|
|
7
|
+
backend requests via the `Authorization: Bearer <token>` header.
|
|
6
8
|
|
|
7
|
-
The
|
|
9
|
+
The backend does not participate in the OAuth2 authorization-code exchange. Its responsibilities are:
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
- Verify the JWT access token signature against the Keycloak JWKS endpoint.
|
|
12
|
+
- Read the user's roles from the token claim configured via `roleClaimPath`.
|
|
13
|
+
- Optionally fetch the full userinfo from Keycloak's `/realms/<realm>/protocol/openid-connect/userinfo` endpoint
|
|
14
|
+
when the token payload does not include the required fields.
|
|
12
15
|
|
|
13
|
-
###
|
|
16
|
+
### Token Validation
|
|
14
17
|
|
|
15
|
-
The
|
|
18
|
+
The `AuthGuard` extracts the access token from the `Authorization` header on every request, verifies it, and attaches
|
|
19
|
+
the resulting viewer context (`user`, `userRoles`) to the request.
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
<img src="assets/auth.png" width="600" />
|
|
19
|
-
</div>
|
|
21
|
+
### Environment Variables
|
|
20
22
|
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
console.log(url)
|
|
23
|
+
```
|
|
24
|
+
AUTH=true # set to false for mock identity (dev only)
|
|
25
|
+
KEYCLOAK_URL=https://... # base URL of the Keycloak server
|
|
26
|
+
KEYCLOAK_REALM=... # realm name
|
|
27
|
+
KEYCLOAK_CLIENT_ID=... # public client id
|
|
27
28
|
```
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
The following diagram shows how a request from a browser with a valid session is made to the server including the SSR pre-rendering and client hydration.
|
|
32
|
-
|
|
33
|
-
<div style="text-align: center;">
|
|
34
|
-
<img src="assets/page-request.png" width="600" />
|
|
35
|
-
</div>
|
|
30
|
+
The Keycloak client is expected to be configured as a **public client** with PKCE enabled.
|
|
@@ -33,11 +33,6 @@ export type AuthenticationConfig_EnabledKeycloak = {
|
|
|
33
33
|
*/
|
|
34
34
|
clientId: string
|
|
35
35
|
|
|
36
|
-
/**
|
|
37
|
-
* The secret of the Keycloak client.
|
|
38
|
-
*/
|
|
39
|
-
clientSecret: string
|
|
40
|
-
|
|
41
36
|
/**
|
|
42
37
|
* The Id of the Keycloak realm.
|
|
43
38
|
*/
|
|
@@ -47,14 +42,4 @@ export type AuthenticationConfig_EnabledKeycloak = {
|
|
|
47
42
|
* The URL of the Keycloak server.
|
|
48
43
|
*/
|
|
49
44
|
keycloakUrl: URL
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The URI to redirect to with a token.
|
|
53
|
-
*/
|
|
54
|
-
redirectUrl: URL
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* The URL to redirect to after logout.
|
|
58
|
-
*/
|
|
59
|
-
postLogoutRedirectUrl: URL | undefined
|
|
60
45
|
}
|