Skip to content

Commit 8c8bad2

Browse files
refactor(authentication-service): streamline state handling in KeycloakLoginController and add optional state property to ClientAuthRequest (#2355)
* refactor(authentication-service): streamline state handling in KeycloakLoginController and add optional state property to ClientAuthRequest - Simplified state parameter management in KeycloakLoginController by consolidating logic for handling existing and new state values. - Introduced an optional property in ClientAuthRequest DTO to enhance flexibility in authentication requests. * refactor(authentication-service): remove unused description from state property in ClientAuthRequest DTO - Eliminated the description for the state property in the ClientAuthRequest DTO to streamline the model and enhance clarity.
1 parent ab2636d commit 8c8bad2

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

services/authentication-service/src/modules/auth/controllers/keycloak-login.controller.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,21 @@ const queryGen = (from: 'body' | 'query') => {
4141
const clientId = req[from].client_id;
4242
const existingState = req[from].state;
4343

44-
let stateString: string;
45-
44+
// Case 1: No state passed, use default client_id
4645
if (!existingState) {
47-
// Case 1: No state passed, use default client_id
48-
stateString = `client_id=${clientId}`;
49-
} else {
50-
// Parse existing state to check if client_id is present
51-
const stateParams = new URLSearchParams(existingState);
52-
const hasClientId = stateParams.has('client_id');
53-
54-
if (!hasClientId) {
55-
// Case 2: State passed without client_id, append it
56-
stateString = `${existingState}&client_id=${clientId}`;
57-
} else {
58-
// Case 3: State passed with client_id (and possibly other properties)
59-
stateString = existingState;
60-
}
46+
return {state: `client_id=${clientId}`};
6147
}
6248

63-
return {
64-
state: stateString,
65-
};
49+
// Parse existing state to check if client_id is present
50+
const stateParams = new URLSearchParams(existingState);
51+
52+
// Case 2: State passed without client_id, append it
53+
// Case 3: State passed with client_id (and possibly other properties)
54+
const stateString = stateParams.has('client_id')
55+
? existingState
56+
: `${existingState}&client_id=${clientId}`;
57+
58+
return {state: stateString};
6659
};
6760
};
6861

@@ -221,11 +214,9 @@ export class KeycloakLoginController {
221214
redirectParams.set('code', token);
222215

223216
// Add all other state params to the redirect URL
224-
stateParams.forEach((value, key) => {
225-
if (key !== 'client_id') {
226-
redirectParams.set(key, value);
227-
}
228-
});
217+
Array.from(stateParams.entries())
218+
.filter(([key]) => key !== 'client_id')
219+
.forEach(([key, value]) => redirectParams.set(key, value));
229220

230221
response.redirect(`${client.redirectUrl}?${redirectParams.toString()}`);
231222
} catch (error) {

services/authentication-service/src/modules/auth/models/client-auth-request.dto.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ export class ClientAuthRequest extends CoreModel<ClientAuthRequest> {
2525
required: true,
2626
})
2727
client_secret: string; //NOSONAR
28+
29+
@property({
30+
type: 'string',
31+
})
32+
state?: string;
2833
}

0 commit comments

Comments
 (0)