import { JwtPayload } from './jsonwebtoken-type' ; import { checkMandatoryValue , JwtKeyMapping , readPropertyWithWarn } from './jwt' ; internal export interface UserData { id : string ; userName : string ; givenName ? : string ; familyName ? : string ; email ? : string ; scopes : Scope [ ] ; customAttributes : Map < string , string [ ] > ; } internal export interface User extends UserData { hasScope : ( scope : Scope ) => boolean ; } param jwtPayload returns internal export function customAttributes ( jwtPayload ) { if ( jwtPayload [ mappingUserFields . customAttributes . keyInJwt ] ) { return readPropertyWithWarn ( jwtPayload , mappingUserFields . customAttributes . keyInJwt ) as Map < string , string [ ] > ; } return new Map < string , string [ ] > ( ) ; } internal export const mappingUserFields = { id : { keyInJwt : 'user_id' , extractorFunction : userId } , userName : { keyInJwt : 'user_name' , extractorFunction : userName } , givenName : { keyInJwt : 'given_name' , extractorFunction : userGivenName } , familyName : { keyInJwt : 'family_name' , extractorFunction : userFamilyName } , email : { keyInJwt : 'email' , extractorFunction : userEmail } , scopes : { keyInJwt : 'scope' , extractorFunction : userScopes } , customAttributes : { keyInJwt : 'xs.user.attributes' , extractorFunction : customAttributes } } ; param jwtPayload returns internal export function userGivenName ( jwtPayload ) { if ( mappingUserFields . givenName ) { return readPropertyWithWarn ( jwtPayload , mappingUserFields . givenName . keyInJwt ) ; } } param jwtPayload returns internal export function userFamilyName ( jwtPayload ) { if ( mappingUserFields . familyName ) { return readPropertyWithWarn ( jwtPayload , mappingUserFields . familyName . keyInJwt ) ; } } param jwtPayload returns internal export function userName ( jwtPayload ) { return readPropertyWithWarn ( jwtPayload , mappingUserFields . userName . keyInJwt ) ; } param jwtPayload returns internal export function userEmail ( jwtPayload ) { if ( mappingUserFields . email ) { return readPropertyWithWarn ( jwtPayload , mappingUserFields . email . keyInJwt ) ; } } param jwtPayload returns internal export function userScopes ( jwtPayload ) { if ( ! ( jwtPayload . scope instanceof Array && jwtPayload . scope . length ) ) { return [ ] ; } return jwtPayload . scope . map ( s => ( s . includes ( '.' ) ? s . substr ( s . indexOf ( '.' ) + 1 , s . length ) : s ) ) . map ( s => ( { name : s } ) ) ; } param jwtPayload returns internal export function userId ( jwtPayload ) { return readPropertyWithWarn ( jwtPayload , mappingUserFields . id . keyInJwt ) ; } internal export interface Scope { name : string ; } type hasScopeType = ( s : Scope ) => boolean ; function hasScopeWrapper ( scopes ) { return ( scope ) : boolean => scopes . find ( scopeFromList => scopeFromList . name === scope . name ) !== undefined ; } param jwtPayload returns internal export function userFromJwt ( jwtPayload ) { checkMandatoryValue ( 'id' , mappingUserFields , jwtPayload ) ; checkMandatoryValue ( 'userName' , mappingUserFields , jwtPayload ) ; return { id : userId ( jwtPayload ) ! , givenName : userGivenName ( jwtPayload ) , familyName : userFamilyName ( jwtPayload ) , email : userEmail ( jwtPayload ) , userName : userName ( jwtPayload ) ! , scopes : userScopes ( jwtPayload ) , customAttributes : customAttributes ( jwtPayload ) , hasScope : hasScopeWrapper ( userScopes ( jwtPayload ) ) } ; }