/**
 * Copyright (c) 2020, 2021 Oracle and/or its affiliates.  All rights reserved.
 * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
 */ import SessionKeySupplier from "./models/session-key-supplier" ; import JWT from "jsonwebtoken" ; /**
 * Helper class to store security token and sessionKeySupplier
 * contain methods to check if token is expired and needs to be refreshed
 */ export default class SecurityTokenAdapter { private jwt : { [ key ] : any ; } | null = null ; constructor ( private securityToken , private sessionKeySupplier ) { this . jwt = this . securityToken ? this . parse ( this . securityToken ) : null ; } returns getSecurityToken ( ) { return this . securityToken ; } param token returns parse ( token ) { try { const payload = JWT . decode ( token , { complete : true } ) ; return payload as object ; } catch ( e ) { throw Error ( `Failed to decode token, error: ${ e }` ) ; } } return isValid ( ) { const secondsSinceEpoch = Math . round ( Date . now ( ) / 1000 ) ; if ( this . jwt == null ) { return false ; } else if ( this . jwt . payload && this . jwt . payload . exp > secondsSinceEpoch ) { return true ; } return false ; } param key return getStringClaim ( key ) { if ( this . jwt == null ) { return null ; } return this . jwt . payload [ key ] ? this . jwt . payload [ key ] : null ; } }