constructor param { String } token param { String } clientId export class Token { public token : string ; public clientId ? : string ; public header ? : any ; public content ? : any ; public signature ? : Buffer ; public signed ? : string ; constructor ( token , clientId ? ) { this . token = token ; this . clientId = clientId ; if ( token ) { try { const parts = token . split ( '.' ) ; this . header = JSON . parse ( Buffer . from ( parts [ 0 ] , 'base64' ) . toString ( ) ) ; this . content = JSON . parse ( Buffer . from ( parts [ 1 ] , 'base64' ) . toString ( ) ) ; this . signature = Buffer . from ( parts [ 2 ] , 'base64' ) ; this . signed = parts [ 0 ] + '.' + parts [ 1 ] ; } catch ( err ) { this . content = { exp : 0 } ; } } } return { boolean } public isExpired ( ) { return ( ( this . content . exp * 1000 ) < Date . now ( ) ) ; } param { String } name return { boolean } public hasRole ( name ) { if ( ! this . clientId ) { return false ; } const parts = name . split ( ':' ) ; if ( parts . length === 1 ) { return this . hasApplicationRole ( this . clientId , parts [ 0 ] ) ; } if ( parts [ 0 ] === 'realm' ) { return this . hasRealmRole ( parts [ 1 ] ) ; } return this . hasApplicationRole ( parts [ 0 ] , parts [ 1 ] ) ; } param { String } appName param { String } roleName return { boolean } public hasApplicationRole ( appName , roleName ) { const appRoles = this . content . resource_access [ appName ] ; if ( ! appRoles ) { return false ; } return ( appRoles . roles . indexOf ( roleName ) >= 0 ) ; } param { String } appName param { String } roleName return { boolean } public hasRealmRole ( roleName ) { if ( ! this . content . realm_access || ! this . content . realm_access . roles ) { return false ; } return ( this . content . realm_access . roles . indexOf ( roleName ) >= 0 ) ; } param { String } permission return { boolean } public hasPermission ( resource , scope ) { const permissions = this . content . authorization ? this . content . authorization . permissions : undefined ; if ( ! permissions ) { return false ; } for ( const permission of permissions ) { if ( permission . rsid === resource || permission . rsname === resource ) { if ( scope ) { if ( permission . scopes && permission . scopes . length > 0 ) { if ( ! permission . scopes . includes ( scope ) ) { return false ; } } } return true ; } } return false ; } }