Skip to content

Commit 13f16d5

Browse files
authored
fix: returnUrl from back end was being discarded (#1807)
## Description The "returnUrl" value passed to/from the back end was being ignored at the end of the journey. These minor changes detect a "returnUrl" that's part of a successful OIDC login from the v4 back end, and use the value in subsequent navigation after the login page. ## Motivation Together with the changes in SciCatProject/scicat-backend-next#1815 , this makes "returnUrl" work as designed. ## Fixes / Changes: Detecting and extracting "returnUrl" in a successful OIDC redirect. Using "returnUrl" in place of "returnURL" for consistency. ## Tests included Tests pass as before. ## Documentation No documentation changes needed. ## Backend version No specific back end is mandatory, but for "returnUrl" to fully work, the changes in SciCatProject/scicat-backend-next#1815 should be integrated as well. ## Summary by Sourcery Improve handling of returnUrl during OIDC authentication to ensure consistent redirect behavior across different backend versions Bug Fixes: - Fixed discarding of returnUrl during OIDC login process, ensuring users are redirected to the correct page after authentication Enhancements: - Updated returnUrl handling to support both backend v3 and v4 authentication flows - Standardized returnUrl parameter naming across components
1 parent b1cb348 commit 13f16d5

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/app/_layout/app-header/app-header.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export class AppHeaderComponent implements OnInit {
4747

4848
login(): void {
4949
if (this.config.skipSciCatLoginPageEnabled) {
50-
const returnURL = encodeURIComponent(this.router.url);
50+
const returnUrl = encodeURIComponent(this.router.url);
5151
for (const endpoint of this.oAuth2Endpoints) {
52-
this.document.location.href = `${this.config.lbBaseURL}/${endpoint.authURL}?returnURL=${returnURL}`;
52+
this.document.location.href = `${this.config.lbBaseURL}/${endpoint.authURL}?returnUrl=${returnUrl}`;
5353
}
5454
} else {
5555
this.router.navigateByUrl("/login");

src/app/users/auth-callback/auth-callback.component.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ export class AuthCallbackComponent implements OnInit {
3939
// External authentication will redirect to this component with a access-token and user-id query parameter
4040
const accessToken = params["access-token"];
4141
const userId = params["user-id"];
42-
const parsedToken = this.parseJwt(params["access-token"]);
43-
const ttl = parsedToken.exp - parsedToken.iat;
44-
const created = new Date(parsedToken.iat * 1000);
42+
const returnUrl: string = params["returnUrl"];
4543

4644
if (accessToken && userId) {
45+
const parsedToken = this.parseJwt(accessToken);
46+
const ttl = parsedToken.exp - parsedToken.iat;
47+
const created = new Date(parsedToken.iat * 1000);
48+
4749
// If the user is authenticated, we will store the access token and user id in the store
4850
this.store.dispatch(
4951
loginOIDCAction({
@@ -65,7 +67,6 @@ export class AuthCallbackComponent implements OnInit {
6567

6668
// After the user is authenticated, we will redirect to the home page
6769
// or the value of returnUrl query param
68-
const returnUrl: string = params["returnUrl"];
6970
this.router.navigateByUrl(returnUrl || "/");
7071
}
7172
});

src/app/users/login/login.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ export class LoginComponent implements OnInit, OnDestroy {
8383
}
8484

8585
redirectOIDC(authURL: string) {
86-
const returnURL = this.returnUrl
86+
const returnUrl = this.returnUrl
8787
? encodeURIComponent(this.returnUrl)
8888
: "/datasets";
89-
this.document.location.href = `${this.appConfig.lbBaseURL}/${authURL}?returnURL=${returnURL}`;
89+
this.document.location.href = `${this.appConfig.lbBaseURL}/${authURL}?returnUrl=${returnUrl}`;
9090
}
9191

9292
openPrivacyDialog() {
@@ -134,16 +134,22 @@ export class LoginComponent implements OnInit, OnDestroy {
134134
this.route.queryParams.subscribe((params) => {
135135
// OIDC logins eventually redirect to this componenet, adding information about user
136136
// which are parsed here.
137-
if (params.returnUrl) {
137+
if (params["returnUrl"]) {
138138
// dispatching to the loginOIDCAction passes information to eventually be added to Loopback AccessToken
139139
let accessToken = params["access-token"];
140140
let userId = params["user-id"];
141+
141142
// Required for backend v3 compatibility (access-token and user-id are encoded in returnUrl)
142143
if (!accessToken && !userId) {
143144
const urlqp = new URLSearchParams(params.returnUrl.split("?")[1]);
144145
accessToken = urlqp.get("access-token");
145146
userId = urlqp.get("user-id");
147+
} else {
148+
// A returnUrl coming from v4 should be respected as the destination redirect
149+
// after login and user info fetching.
150+
this.returnUrl = params["returnUrl"];
146151
}
152+
147153
this.store.dispatch(
148154
loginOIDCAction({ oidcLoginResponse: { accessToken, userId } }),
149155
);

0 commit comments

Comments
 (0)