1- import { entityNotFound , infrastructureError } from '../app.api/app.api.errors'
1+ import { entityNotFound , infrastructureError , invalidInput } from '../app.api/app.api.errors'
22import { AppResponse } from '../app.api/app.api.global'
33import { AdmitFromIdentityProviderOperation , AdmitFromIdentityProviderRequest , authenticationFailedError , EnrollMyselfOperation , EnrollMyselfRequest } from './ingress.app.api'
44import { IdentityProviderRepository , IdentityProviderUser } from './ingress.entities'
55import { AdmissionDeniedReason , AdmitUserFromIdentityProviderAccount , EnrollNewUser } from './ingress.services.api'
6- import { LocalIdpCreateAccountOperation } from './local-idp.app.api'
6+ import { LocalIdpError , LocalIdpInvalidPasswordError } from './local-idp.entities'
7+ import { MageLocalIdentityProviderService } from './local-idp.services.api'
78import { JWTService , TokenAssertion } from './verification'
89
910
10- export function CreateEnrollMyselfOperation ( createLocalIdpAccount : LocalIdpCreateAccountOperation , idpRepo : IdentityProviderRepository , enrollNewUser : EnrollNewUser ) : EnrollMyselfOperation {
11+ export function CreateEnrollMyselfOperation ( localIdp : MageLocalIdentityProviderService , idpRepo : IdentityProviderRepository , enrollNewUser : EnrollNewUser ) : EnrollMyselfOperation {
1112 return async function enrollMyself ( req : EnrollMyselfRequest ) : ReturnType < EnrollMyselfOperation > {
12- const localAccountCreate = await createLocalIdpAccount ( req )
13- if ( localAccountCreate . error ) {
14- return AppResponse . error ( localAccountCreate . error )
13+ const localIdpAccount = await localIdp . createAccount ( req )
14+ if ( localIdpAccount instanceof LocalIdpError ) {
15+ if ( localIdpAccount instanceof LocalIdpInvalidPasswordError ) {
16+ return AppResponse . error ( invalidInput ( localIdpAccount . message ) )
17+ }
18+ console . error ( 'error creating local idp account for self-enrollment' , localIdpAccount )
19+ return AppResponse . error ( invalidInput ( 'Error creating local Mage account' ) )
1520 }
16- const localAccount = localAccountCreate . success !
1721 const candidateMageAccount : IdentityProviderUser = {
18- username : localAccount . username ,
22+ username : localIdpAccount . username ,
1923 displayName : req . displayName ,
2024 phones : [ ] ,
2125 }
@@ -25,12 +29,11 @@ export function CreateEnrollMyselfOperation(createLocalIdpAccount: LocalIdpCreat
2529 if ( req . phone ) {
2630 candidateMageAccount . phones = [ { number : req . phone , type : 'Main' } ]
2731 }
28- const localIdp = await idpRepo . findIdpByName ( 'local' )
29- if ( ! localIdp ) {
32+ const idp = await idpRepo . findIdpByName ( 'local' )
33+ if ( ! idp ) {
3034 throw new Error ( 'local idp not found' )
3135 }
32- const enrollmentResult = await enrollNewUser ( candidateMageAccount , localIdp )
33-
36+ const enrollmentResult = await enrollNewUser ( candidateMageAccount , idp )
3437
3538 // TODO: auto-activate account after enrollment policy
3639 throw new Error ( 'unimplemented' )
0 commit comments