( input: RefreshAccessTokenInput, )
| 1376 | }; |
| 1377 | |
| 1378 | export const refreshAccessToken = ( |
| 1379 | input: RefreshAccessTokenInput, |
| 1380 | ): Effect.Effect<OAuth2TokenResponse, OAuth2Error> => { |
| 1381 | const requestedScopes = input.scopes && input.scopes.length > 0 ? input.scopes : undefined; |
| 1382 | const scopeParameter = (scopes: readonly string[] | undefined): string | undefined => |
| 1383 | scopes === undefined ? undefined : scopes.join(input.scopeSeparator ?? " "); |
| 1384 | |
| 1385 | const attempt = ( |
| 1386 | scopes: readonly string[] | undefined, |
| 1387 | ): Effect.Effect<OAuth2TokenResponse, OAuth2Error> => |
| 1388 | Effect.tryPromise({ |
| 1389 | try: async () => { |
| 1390 | const as = asFromTokenUrlAndIssuer(input.tokenUrl, input.issuerUrl, { |
| 1391 | idTokenSigningAlgValuesSupported: input.idTokenSigningAlgValuesSupported, |
| 1392 | endpointUrlPolicy: input.endpointUrlPolicy, |
| 1393 | }); |
| 1394 | const client: oauth.Client = { client_id: input.clientId }; |
| 1395 | const clientAuth = pickClientAuth( |
| 1396 | input.clientSecret, |
| 1397 | input.clientAuth ?? DEFAULT_CLIENT_AUTH_METHOD, |
| 1398 | ); |
| 1399 | const scope = scopeParameter(scopes); |
| 1400 | const extraParams = new URLSearchParams(); |
| 1401 | if (scope !== undefined) { |
| 1402 | extraParams.set("scope", scope); |
| 1403 | } |
| 1404 | if (input.resource) { |
| 1405 | extraParams.set("resource", input.resource); |
| 1406 | } |
| 1407 | const additionalParameters = |
| 1408 | Array.from(extraParams.keys()).length > 0 ? extraParams : undefined; |
| 1409 | if (input.requestFormat === "json") { |
| 1410 | const response = await jsonTokenEndpointRequest({ |
| 1411 | tokenUrl: input.tokenUrl, |
| 1412 | clientId: input.clientId, |
| 1413 | clientSecret: input.clientSecret, |
| 1414 | clientAuth: input.clientAuth ?? DEFAULT_CLIENT_AUTH_METHOD, |
| 1415 | grantType: "refresh_token", |
| 1416 | parameters: { |
| 1417 | refresh_token: input.refreshToken, |
| 1418 | ...(scope !== undefined ? { scope } : {}), |
| 1419 | ...(input.resource ? { resource: input.resource } : {}), |
| 1420 | }, |
| 1421 | timeoutMs: input.timeoutMs, |
| 1422 | endpointUrlPolicy: input.endpointUrlPolicy, |
| 1423 | fetch: input.fetch, |
| 1424 | }); |
| 1425 | return await processTokenEndpointResponse(as, client, response); |
| 1426 | } |
| 1427 | const response = await oauth.refreshTokenGrantRequest( |
| 1428 | as, |
| 1429 | client, |
| 1430 | clientAuth, |
| 1431 | input.refreshToken, |
| 1432 | { |
| 1433 | ...oauth4webapiRequestOptions( |
| 1434 | input.tokenUrl, |
| 1435 | input.timeoutMs, |
no test coverage detected