file author tngan
*  desc import * as fs from 'fs' ; import { namespace } from './urn' ; import { extract } from './extractor' ; import { isString } from './utility' ; export interface MetadataInterface { xmlString : string ; getMetadata : ( ) => string ; exportMetadata : ( exportFile : string ) => void ; getEntityID : ( ) => string ; getX509Certificate : ( certType : string ) => string | string [ ] ; getNameIDFormat : ( ) => any [ ] ; getSingleLogoutService : ( binding : string | undefined ) => string | object ; getSupportBindings : ( services : string [ ] ) => string [ ] ; } export default class Metadata implements MetadataInterface { xmlString : string ; meta : any ; param { string | Buffer } metadata param { object } extraParse constructor ( xml , extraParse = [ ] ) { this . xmlString = xml . toString ( ) ; this . meta = extract ( this . xmlString , extraParse . concat ( [ { key : 'entityDescriptor' , localPath : [ 'EntityDescriptor' ] , attributes : [ ] , context : true } , { key : 'entityID' , localPath : [ 'EntityDescriptor' ] , attributes : [ 'entityID' ] } , { key : 'sharedCertificate' , localPath : [ 'EntityDescriptor' , '~SSODescriptor' , 'KeyDescriptor' , 'KeyInfo' , 'X509Data' , 'X509Certificate' ] , attributes : [ ] } , { key : 'certificate' , localPath : [ 'EntityDescriptor' , '~SSODescriptor' , 'KeyDescriptor' ] , index : [ 'use' ] , attributePath : [ 'KeyInfo' , 'X509Data' , 'X509Certificate' ] , attributes : [ ] } , { key : 'singleLogoutService' , localPath : [ 'EntityDescriptor' , '~SSODescriptor' , 'SingleLogoutService' ] , attributes : [ 'Binding' , 'Location' ] } , { key : 'nameIDFormat' , localPath : [ 'EntityDescriptor' , '~SSODescriptor' , 'NameIDFormat' ] , attributes : [ ] , } ] ) ) ; const sharedCertificate = this . meta . sharedCertificate ; if ( typeof sharedCertificate === 'string' ) { this . meta . certificate = { signing : sharedCertificate , encryption : sharedCertificate } ; delete this . meta . sharedCertificate ; } if ( Array . isArray ( this . meta . entityDescriptor ) && this . meta . entityDescriptor . length > 1 ) { throw new Error ( 'ERR_MULTIPLE_METADATA_ENTITYDESCRIPTOR' ) ; } } desc return { string } public getMetadata ( ) { return this . xmlString ; } desc param { string } exportFile public exportMetadata ( exportFile ) { fs . writeFileSync ( exportFile , this . xmlString ) ; } desc return { string } public getEntityID ( ) { return this . meta . entityID ; } desc param { string } use return { string } public getX509Certificate ( use ) { return this . meta . certificate [ use ] || null ; } desc return { array } public getNameIDFormat ( ) { return this . meta . nameIDFormat ; } desc param { string } binding return { string public getSingleLogoutService ( binding ) { if ( binding && isString ( binding ) ) { const bindType = namespace . binding [ binding ] ; let singleLogoutService = this . meta . singleLogoutService ; if ( ! ( singleLogoutService instanceof Array ) ) { singleLogoutService = [ singleLogoutService ] ; } const service = singleLogoutService . find ( obj => obj . binding === bindType ) ; if ( service ) { return service . location ; } } return this . meta . singleLogoutService ; } desc param { [ string ] } services return { [ string ] } public getSupportBindings ( services ) { let supportBindings = [ ] ; if ( services ) { supportBindings = services . reduce ( ( acc , service ) => { const supportBinding = Object . keys ( service ) [ 0 ] ; return acc . push ( supportBinding ) ; } , [ ] ) ; } return supportBindings ; } }