import { UserManager , WebStorageStateStore } from "oidc-client" ; import IAuthStrategy from "../IAuthStrategy" ; import getLoginHint from "./getLoginHint" ; import isAuthTokenExpired from "./isAuthTokenExpired" ; import * as urlUtils from "./urlUtils" ; export default class OidcAuthStrategy implements IAuthStrategy { static isSilentRedirectPage = urlUtils . isSilentRedirectPage ; name = "oidc" ; displayName : string ; private userManager : UserManager ; constructor ( openidConfigurationUrl , clientId , baseRedirectUrl , providerName ) { this . userManager = new UserManager ( { authority : openidConfigurationUrl , client_id : clientId , redirect_uri : urlUtils . getRedirectUrl ( baseRedirectUrl ) , silent_redirect_uri : urlUtils . getSilentRedirectUrl ( baseRedirectUrl ) , scope : "openid profile" , userStore : new WebStorageStateStore ( { store : window . localStorage } ) , } ) ; this . displayName = providerName ; } async init ( ) { if ( urlUtils . isRedirectPage ( ) ) { await this . userManager . signinRedirectCallback ( ) . catch ( ( ) => null ) ; } if ( urlUtils . isSilentRedirectPage ( ) ) { await this . userManager . signinSilentCallback ( ) . catch ( ( ) => null ) ; } } async login ( ) { await this . userManager . signinRedirect ( ) ; await new Promise ( ( ) => null ) ; } async logout ( ) { await this . userManager . removeUser ( ) ; } async getAuthToken ( ) { if ( urlUtils . isSilentRedirectPage ( ) ) { return null ; } let user = await this . userManager . getUser ( ) ; if ( ! user ) { return null ; } if ( isAuthTokenExpired ( user . id_token ) ) { try { user = await this . userManager . signinSilent ( { login_hint : getLoginHint ( user ) , } ) ; } catch { await this . logout ( ) ; return null ; } } return user . id_token ; } }