@npmsoluto/authorized-fetch

This module wraps fetch and allows to acquire authorization token before making request.

Usage no npm install needed!

<script type="module">
  import npmsolutoAuthorizedFetch from 'https://cdn.skypack.dev/@npmsoluto/authorized-fetch';
</script>

README

authorized-fetch

This module wraps fetch and allows to acquire authorization token before making request.

It caches tokens and avoids redundant calls to oauth provider. Expired tokens are invalidated and replaced by new ones.

API

getAuthenticationToken

async (authenticationParams: AuthenticationParams) => Promise<string>

Function that receives AuthenticationParams and returns promise of the access token.

default

async (authenticationParams: AuthenticationParams, fetch?: FetchAPI, init: RequestInit = {}) => Promise<Response>

Function that creates fetch object that wraps user-provided fetch with getAuthenticationToken function that is called before each request and the access token is set to the Authorization header.

AuthenticationParams

  • issuer string, mandatory - The issuer of openid connect provider. This field is used to determine the provider discovery endpoint.
  • clientId string, mandatory - The application client_id registered in openid connect provider.
  • clientSecret string, mandatory - The client's secret
  • resource string, optional - Supported only in Azure AD V1. For other providers use scope
  • scope string, optional - A space-separated list of scopes.
  • onTokenExpired function, optional - Hook that' fired on token renewal.

Note: The module makes request to the Token Endpoint of oidc provider with grant_type=client_credentials. Important! Either resource or scope property should be set. For Azure AD v1 compatibility the request to Token endpoint has both properties (resource and scope) set with the same value provided in resource or scope properties.