import { Injectable } from '@nestjs/common' ; import { difference , intersection , union , unique } from '@fc/common' ; const MAPPING_ALIAS_SCOPES = { openid : [ 'sub' ] , profile : [ 'given_name' , 'family_name' , 'birthdate' , 'gender' , 'preferred_username' , ] , phone : [ 'phone_number' ] , birth : [ 'birthplace' , 'birthcountry' ] , identite_pivot : [ 'given_name' , 'family_name' , 'birthdate' , 'gender' , 'birthplace' , 'birthcountry' , ] , } ; TODO see https const STANDARD_IDENTITY_SCOPES_LABEL = { openid : 'sub' , gender : `Sexe` , given_name : `Prénom(s)` , family_name : `Nom de naissance` , preferred_username : `Nom d'usage` , birthdate : `Date de naissance` , birthplace : `Lieu de naissance` , birthcountry : `Pays de naissance` , address : `Adresse postale` , phone_number : `Téléphone` , email : `Adresse email` , } ; @ Injectable ( ) export class ScopesService { param { Array < string > } scopes private removeOpenId ( scopes ) { return scopes . filter ( ( scope ) => scope !== 'openid' ) ; } private seperate ( array1 , array2 ) { return [ difference ( array1 , array2 ) , intersection ( array1 , array2 ) ] ; } param { * } scopes getClaimsFromScopes ( scopes ) { return unique ( scopes . flatMap ( ( alias ) => MAPPING_ALIAS_SCOPES [ alias ] || alias ) ) ; } param { Array } scopes private extractAllScopes ( scopes ) { const [ identityScopes , aliasScopes ] = this . seperate ( scopes , Object . keys ( MAPPING_ALIAS_SCOPES ) ) ; const ungroupedScopes = this . getClaimsFromScopes ( aliasScopes ) ; return { identity : identityScopes , alias : ungroupedScopes } ; } param { Array } scopes param { boolean } removeIgnored private flattenAllScopes ( scopes ) { const { identity , alias } = this . extractAllScopes ( scopes ) ; return unique ( union ( identity , alias ) ) ; } param { Array < string > = async mapScopesToLabel ( scopesParam = [ ] ) { if ( ! Array . isArray ( scopesParam ) ) { throw new Error ( `Scopes must be Array and not ${ typeof scopesParam }` ) ; } const scopesIdentity = this . removeOpenId ( scopesParam ) ; const scopes = this . flattenAllScopes ( scopesIdentity ) ; return scopes . map ( ( scope ) => scope in STANDARD_IDENTITY_SCOPES_LABEL ? STANDARD_IDENTITY_SCOPES_LABEL [ scope ] : scope ) ; } }