Skip to content

Commit eba015d

Browse files
committed
fix(auth): continue OIDC PKCE flow after register and return users to SPA callback
1 parent f8694b2 commit eba015d

3 files changed

Lines changed: 39 additions & 21 deletions

File tree

src/app/core/auth/services/auth.service.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,7 @@ export class AuthService {
3939
}
4040

4141
async startLoginRedirect(): Promise<void> {
42-
const state = this.createRandomUrlSafeString(32);
43-
const verifier = this.createRandomUrlSafeString(64);
44-
const challenge = await this.createPkceChallenge(verifier);
45-
46-
const requestState: PkceAuthorizationRequestState = {
47-
state,
48-
verifier,
49-
createdAtUtcMs: Date.now()
50-
};
51-
52-
sessionStorage.setItem(PKCE_REQUEST_STORAGE_KEY, JSON.stringify(requestState));
53-
54-
const authorizeUrl = this.createAuthorizeUrl(state, challenge);
42+
const authorizeUrl = await this.createAuthorizeRedirectUrl();
5543
window.location.assign(authorizeUrl);
5644
}
5745

@@ -111,8 +99,9 @@ export class AuthService {
11199
window.location.assign(logoutUrl);
112100
}
113101

114-
openRegisterPage(): void {
115-
const registerUrl = `${this.appEnvironment.auth.authority.replace(/\/$/, '')}/Identity/Account/Register`;
102+
async openRegisterPage(): Promise<void> {
103+
const authorizePath = await this.createAuthorizeRedirectPath();
104+
const registerUrl = `${this.appEnvironment.auth.authority.replace(/\/$/, '')}/Identity/Account/Register?returnUrl=${encodeURIComponent(authorizePath)}`;
116105
window.location.assign(registerUrl);
117106
}
118107

@@ -158,18 +147,47 @@ export class AuthService {
158147

159148
private createAuthorizeUrl(state: string, challenge: string): string {
160149
const authorizeEndpoint = `${this.appEnvironment.auth.authority.replace(/\/$/, '')}/connect/authorize`;
161-
const scopes = this.appEnvironment.auth.scopes.join(' ');
162-
const params = new URLSearchParams({
150+
const params = this.createAuthorizeParams(state, challenge);
151+
152+
return `${authorizeEndpoint}?${params.toString()}`;
153+
}
154+
155+
private async createAuthorizeRedirectUrl(): Promise<string> {
156+
const { state, challenge } = await this.createPkceAuthorizationRequestState();
157+
return this.createAuthorizeUrl(state, challenge);
158+
}
159+
160+
private async createAuthorizeRedirectPath(): Promise<string> {
161+
const { state, challenge } = await this.createPkceAuthorizationRequestState();
162+
const params = this.createAuthorizeParams(state, challenge);
163+
return `/connect/authorize?${params.toString()}`;
164+
}
165+
166+
private createAuthorizeParams(state: string, challenge: string): URLSearchParams {
167+
return new URLSearchParams({
163168
response_type: this.appEnvironment.auth.responseType,
164169
client_id: this.appEnvironment.auth.clientId,
165170
redirect_uri: this.appEnvironment.auth.redirectUri,
166-
scope: scopes,
171+
scope: this.appEnvironment.auth.scopes.join(' '),
167172
state,
168173
code_challenge: challenge,
169174
code_challenge_method: 'S256'
170175
});
176+
}
171177

172-
return `${authorizeEndpoint}?${params.toString()}`;
178+
private async createPkceAuthorizationRequestState(): Promise<{ state: string; challenge: string }> {
179+
const state = this.createRandomUrlSafeString(32);
180+
const verifier = this.createRandomUrlSafeString(64);
181+
const challenge = await this.createPkceChallenge(verifier);
182+
183+
const requestState: PkceAuthorizationRequestState = {
184+
state,
185+
verifier,
186+
createdAtUtcMs: Date.now()
187+
};
188+
189+
sessionStorage.setItem(PKCE_REQUEST_STORAGE_KEY, JSON.stringify(requestState));
190+
return { state, challenge };
173191
}
174192

175193
private createLogoutUrl(idTokenHint?: string): string {

src/app/features/landing/components/landing-page/landing-page.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class LandingPageComponent {
2121
}
2222

2323
register(): void {
24-
this.authService.openRegisterPage();
24+
void this.authService.openRegisterPage();
2525
}
2626

2727
startDebugMode(): void {

src/app/features/profile/components/user-profile-security/user-profile-security.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class UserProfileSecurityComponent {
121121
}
122122

123123
register(): void {
124-
this.authService.openRegisterPage();
124+
void this.authService.openRegisterPage();
125125
}
126126

127127
private serializeClaimValue(value: unknown): string {

0 commit comments

Comments
 (0)