@@ -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}
0 commit comments