import { injectable } from '@cloudbeaver/core-di' ; import { Executor , IExecutor } from '@cloudbeaver/core-executor' ; import type { ObjectOrigin } from '@cloudbeaver/core-sdk' ; import { md5 , uuid } from '@cloudbeaver/core-utils' ; import { AuthProvider , AuthProvidersResource } from './AuthProvidersResource' ; import type { IAuthCredentials } from './IAuthCredentials' ; interface IServiceDescriptionProps { configurationWizard : boolean ; } export type ServiceDescriptionComponent = React . FC < IServiceDescriptionProps > ; interface IServiceDescriptionLinkOptions { isSupported : ( service : AuthProvider ) => boolean ; description : ( ) => ServiceDescriptionComponent ; } interface IServiceDescriptionLink extends IServiceDescriptionLinkOptions { id : string ; } @ injectable ( ) export class AuthProviderService { readonly requestAuthProvider : IExecutor < ObjectOrigin > ; private serviceDescriptionLinker : IServiceDescriptionLink [ ] ; constructor ( private readonly authProvidersResource ) { this . requestAuthProvider = new Executor ( ) ; this . serviceDescriptionLinker = [ ] ; } getServiceDescriptionLinks ( service ) { return this . serviceDescriptionLinker . filter ( link => link . isSupported ( service ) ) ; } addServiceDescriptionLink ( link ) { this . serviceDescriptionLinker . push ( { id : uuid ( ) , ... link , } ) ; } async requireProvider ( type , subType ? ) ; async requireProvider ( origin ) ; async requireProvider ( origin , subType ? ) { if ( typeof origin === 'string' ) { origin = { displayName : '' , type : origin , subType , } ; } const contexts = await this . requestAuthProvider . execute ( origin ) ; const provider = contexts . getContext ( AuthProviderContext ) ; return provider . get ( ) ; } hashValue ( value ) { return md5 ( value ) . toUpperCase ( ) ; } async processCredentials ( providerId , credentials ) { const provider = await this . authProvidersResource . load ( providerId ) ; if ( ! provider ) { return credentials ; } const credentialsProcessed = { profile : credentials . profile , credentials : { ... credentials . credentials } , } ; const profile = provider . credentialProfiles [ credentials . profile as any as number ] ; for ( const parameter of profile . credentialParameters ) { if ( parameter . encryption === 'hash' && parameter . id in credentialsProcessed . credentials ) { credentialsProcessed . credentials [ parameter . id ] = this . hashValue ( credentialsProcessed . credentials [ parameter . id ] ) ; } } return credentialsProcessed ; } } interface IAuthProviderContext { get : ( ) => boolean ; auth : ( ) => void ; } export function AuthProviderContext ( ) { let state = false ; return { get : ( ) => state , auth : ( ) => { state = true ; } , } ; }