@@ -13,6 +13,8 @@ enum FusionAuthStatusCode {
13
13
}
14
14
15
15
export class AuthenticationSession {
16
+ private static readonly sftpFusionAuthAppId = process . env . FUSION_AUTH_SFTP_APP_ID ?? '' ;
17
+
16
18
public authToken = '' ;
17
19
18
20
public refreshToken = '' ;
@@ -23,8 +25,6 @@ export class AuthenticationSession {
23
25
24
26
private readonly fusionAuthClient ;
25
27
26
- private readonly fusionAuthAppId = process . env . FUSION_AUTH_APP_ID ?? '' ;
27
-
28
28
private twoFactorId = '' ;
29
29
30
30
private twoFactorMethods : TwoFactorMethod [ ] = [ ] ;
@@ -78,13 +78,12 @@ export class AuthenticationSession {
78
78
79
79
private processPasswordResponse ( [ password ] : string [ ] ) : void {
80
80
this . fusionAuthClient . login ( {
81
- applicationId : this . fusionAuthAppId ,
81
+ applicationId : AuthenticationSession . sftpFusionAuthAppId ,
82
82
loginId : this . authContext . username ,
83
83
password,
84
84
} ) . then ( ( clientResponse ) => {
85
85
switch ( clientResponse . statusCode ) {
86
- case FusionAuthStatusCode . Success :
87
- case FusionAuthStatusCode . SuccessButUnregisteredInApp :
86
+ case FusionAuthStatusCode . Success : {
88
87
if ( clientResponse . response . token !== undefined ) {
89
88
logger . verbose ( 'Successful password authentication attempt.' , {
90
89
username : this . authContext . username ,
@@ -93,28 +92,41 @@ export class AuthenticationSession {
93
92
this . authTokenExpiresAt = clientResponse . response . tokenExpirationInstant ?? 0 ;
94
93
this . refreshToken = clientResponse . response . refreshToken ?? '' ;
95
94
this . authContext . accept ( ) ;
96
- return ;
95
+ } else {
96
+ this . authContext . reject ( ) ;
97
97
}
98
- this . authContext . reject ( ) ;
99
98
return ;
100
- case FusionAuthStatusCode . SuccessNeedsTwoFactorAuth :
99
+ }
100
+ case FusionAuthStatusCode . SuccessButUnregisteredInApp : {
101
+ const userId : string = clientResponse . response . user ?. id ?? '' ;
102
+ this . registerUserInApp ( userId )
103
+ . then ( ( ) => { this . processPasswordResponse ( [ password ] ) ; } )
104
+ . catch ( ( error ) => {
105
+ logger . warn ( 'Error during registration and authentication:' , error ) ;
106
+ this . authContext . reject ( ) ;
107
+ } ) ;
108
+ return ;
109
+ }
110
+ case FusionAuthStatusCode . SuccessNeedsTwoFactorAuth : {
101
111
if ( clientResponse . response . twoFactorId !== undefined ) {
102
112
logger . verbose ( 'Successful password authentication attempt; MFA required.' , {
103
113
username : this . authContext . username ,
104
114
} ) ;
105
115
this . twoFactorId = clientResponse . response . twoFactorId ;
106
116
this . twoFactorMethods = clientResponse . response . methods ?? [ ] ;
107
117
this . promptForTwoFactorMethod ( ) ;
108
- return ;
118
+ } else {
119
+ this . authContext . reject ( ) ;
109
120
}
110
- this . authContext . reject ( ) ;
111
121
return ;
112
- default :
122
+ }
123
+ default : {
113
124
logger . verbose ( 'Failed password authentication attempt.' , {
114
125
username : this . authContext . username ,
115
126
response : clientResponse . response ,
116
127
} ) ;
117
128
this . authContext . reject ( ) ;
129
+ }
118
130
}
119
131
} ) . catch ( ( clientResponse : unknown ) => {
120
132
const message = isPartialClientResponse ( clientResponse )
@@ -125,6 +137,29 @@ export class AuthenticationSession {
125
137
} ) ;
126
138
}
127
139
140
+ private async registerUserInApp ( userId : string ) : Promise < void > {
141
+ return this . fusionAuthClient . register ( userId , {
142
+ registration : {
143
+ applicationId : AuthenticationSession . sftpFusionAuthAppId ,
144
+ } ,
145
+ } ) . then ( ( clientResponse ) => {
146
+ switch ( clientResponse . statusCode ) {
147
+ case FusionAuthStatusCode . Success :
148
+ logger . verbose ( 'User registered successfully after authentication.' , {
149
+ userId,
150
+ } ) ;
151
+ break ;
152
+ default :
153
+ logger . verbose ( 'User registration after authentication failed.' , {
154
+ userId,
155
+ response : clientResponse . response ,
156
+ } ) ;
157
+ }
158
+ } ) . catch ( ( error ) => {
159
+ logger . warn ( 'Error during user registration after authentication:' , error ) ;
160
+ } ) ;
161
+ }
162
+
128
163
private promptForTwoFactorMethod ( ) : void {
129
164
const promptOptions = this . twoFactorMethods . map (
130
165
( method , index ) => `[${ index + 1 } ] ${ method . method ?? '' } ` ,
0 commit comments