Skip to content

Commit 52ee724

Browse files
authored
Merge pull request #2 from SeeClickFix/gaurav/feat/add_support_custom_params
[TP-213169] feat: add support for custom parameters
2 parents a48c36a + 9a0dc54 commit 52ee724

3 files changed

Lines changed: 87 additions & 27 deletions

File tree

addon/authenticators/oidc.js

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class OidcAuthenticator extends BaseAuthenticator {
6262
* @param {String} options.code The authentication code
6363
* @returns {Object} The parsed response data
6464
*/
65-
async authenticate({ code, redirectUri, codeVerifier, isRefresh }) {
65+
async authenticate(options) {
6666
if (!this.hasEndpointsConfigured) {
6767
await this._fetchAuthConfiguration.perform();
6868

@@ -73,27 +73,19 @@ export default class OidcAuthenticator extends BaseAuthenticator {
7373
}
7474
}
7575

76+
const { isRefresh = false, redirectUri, customParams = {} } = options;
77+
7678
if (isRefresh) {
79+
const DEFAULT_RETRY_COUNT = 0;
7780
return await this._refresh(
7881
this.session.data.authenticated.refresh_token,
7982
redirectUri,
83+
DEFAULT_RETRY_COUNT,
84+
customParams,
8085
);
8186
}
8287

83-
const bodyObject = {
84-
code,
85-
client_id: this.configuration.clientId,
86-
grant_type: "authorization_code",
87-
redirect_uri: redirectUri,
88-
};
89-
90-
if (this.configuration.enablePkce) {
91-
bodyObject.code_verifier = codeVerifier;
92-
}
93-
94-
const body = Object.keys(bodyObject)
95-
.map((k) => `${k}=${encodeURIComponent(bodyObject[k])}`)
96-
.join("&");
88+
const body = this._buildBodyQuery(options);
9789

9890
const response = await fetch(
9991
getAbsoluteUrl(this.configuration.tokenEndpoint, this.config.host),
@@ -201,18 +193,20 @@ export default class OidcAuthenticator extends BaseAuthenticator {
201193
* @param {String} refresh_token The refresh token
202194
* @returns {Object} The parsed response data
203195
*/
204-
async _refresh(refresh_token, redirectUri, retryCount = 0) {
196+
async _refresh(
197+
refresh_token,
198+
redirectUri,
199+
retryCount = 0,
200+
customParams = {},
201+
) {
205202
let isServerError = false;
206203
try {
207-
const bodyObject = {
204+
const body = this._buildBodyQuery({
205+
redirectUri,
208206
refresh_token,
209-
client_id: this.configuration.clientId,
210-
grant_type: "refresh_token",
211-
redirect_uri: redirectUri,
212-
};
213-
const body = Object.keys(bodyObject)
214-
.map((k) => `${k}=${encodeURIComponent(bodyObject[k])}`)
215-
.join("&");
207+
isRefresh: true,
208+
customParams,
209+
});
216210

217211
const response = await fetch(
218212
getAbsoluteUrl(this.configuration.tokenEndpoint, this.config.host),
@@ -316,4 +310,43 @@ export default class OidcAuthenticator extends BaseAuthenticator {
316310
redirectUri,
317311
});
318312
}
313+
314+
/**
315+
* Builds query parameters string for the authorize or refresh request
316+
*
317+
* @param {*} options
318+
* @returns string
319+
*/
320+
_buildBodyQuery({
321+
code,
322+
redirectUri,
323+
codeVerifier,
324+
isRefresh = false,
325+
refresh_token,
326+
customParams = {},
327+
}) {
328+
const bodyObject = {
329+
redirect_uri: redirectUri,
330+
client_id: this.configuration.clientId,
331+
grant_type: isRefresh ? "refresh_token" : "authorization_code",
332+
...customParams,
333+
};
334+
335+
if (!isRefresh && code) {
336+
bodyObject.code = code;
337+
if (this.configuration.enablePkce) {
338+
bodyObject.code_verifier = codeVerifier;
339+
}
340+
}
341+
342+
if (isRefresh && refresh_token) {
343+
bodyObject.refresh_token = refresh_token;
344+
}
345+
346+
const bodyQuery = Object.keys(bodyObject)
347+
.map((k) => `${k}=${encodeURIComponent(bodyObject[k])}`)
348+
.join("&");
349+
350+
return bodyQuery;
351+
}
319352
}

addon/routes/oidc-authentication.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ export default class OIDCAuthenticationRoute extends Route {
6464
* @param {Object} transition.to.queryParams The query params of the transition
6565
* @param {String} transition.to.queryParams.code The authentication code given by the identity provider
6666
* @param {String} transition.to.queryParams.state The state given by the identity provider
67+
* @param {Object} customParams Custom query params to be added to the redirect URL
6768
*/
68-
async afterModel(_, transition) {
69+
async afterModel(_, transition, customParams = {}) {
6970
if (!this.config.authEndpoint) {
7071
throw new Error(
7172
"Please define all OIDC endpoints (auth, token, logout, userinfo)",
@@ -84,7 +85,7 @@ export default class OIDCAuthenticationRoute extends Route {
8485
);
8586
}
8687

87-
return this._handleRedirectRequest(queryParams);
88+
return this._handleRedirectRequest(queryParams, customParams);
8889
}
8990

9091
/**
@@ -130,7 +131,7 @@ export default class OIDCAuthenticationRoute extends Route {
130131
* match this state, otherwise the authentication will fail to prevent from
131132
* CSRF attacks.
132133
*/
133-
_handleRedirectRequest(queryParams) {
134+
_handleRedirectRequest(queryParams, customParams = {}) {
134135
const state = v4();
135136

136137
// Store state to session data
@@ -155,6 +156,7 @@ export default class OIDCAuthenticationRoute extends Route {
155156
`state=${state}`,
156157
`scope=${this.config.scope}`,
157158
queryParams[key] ? `${key}=${queryParams[key]}` : null,
159+
new URLSearchParams(customParams).toString(),
158160
];
159161

160162
if (this.config.enablePkce) {

tests/unit/authenticators/oidc-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,29 @@ module("Unit | Authenticator | OIDC", function (hooks) {
7777

7878
subject.singleLogout("myIdToken");
7979
});
80+
81+
test("it supports sending custom parameters", function (assert) {
82+
const bodyOptions = {
83+
code: "test-code",
84+
codeVerifier: "test-verifier",
85+
redirectUri: "test/redirect",
86+
isRefresh: true,
87+
refresh_token: "test-refresh-token",
88+
customParams: { foo: "bar" },
89+
};
90+
91+
const subject = this.owner.lookup("authenticator:oidc");
92+
const bodyWithRefresh = subject._buildBodyQuery(bodyOptions);
93+
assert.strictEqual(
94+
bodyWithRefresh,
95+
"redirect_uri=test%2Fredirect&client_id=test-client&grant_type=refresh_token&foo=bar&refresh_token=test-refresh-token",
96+
);
97+
98+
bodyOptions.isRefresh = false;
99+
const bodyWithoutRefresh = subject._buildBodyQuery(bodyOptions);
100+
assert.strictEqual(
101+
bodyWithoutRefresh,
102+
"redirect_uri=test%2Fredirect&client_id=test-client&grant_type=authorization_code&foo=bar&code=test-code",
103+
);
104+
});
80105
});

0 commit comments

Comments
 (0)