Skip to content

Commit f9d50e9

Browse files
committed
wip (fixup): Register unregistered users
Signed-off-by: Fon E. Noel NFEBE <fenn25.fn@gmail.com>
1 parent 8e2ce11 commit f9d50e9

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ PERMANENT_API_BASE_PATH=${LOCAL_TEMPORARY_AUTH_TOKEN}
4040
# See https://fusionauth.io/docs/v1/tech/apis/api-keys
4141
FUSION_AUTH_HOST=${FUSION_AUTH_HOST}
4242
FUSION_AUTH_KEY=${FUSION_AUTH_KEY}
43-
FUSION_AUTH_APP_ID=${FUSION_AUTH_APP_ID}
43+
FUSION_AUTH_SFTP_APP_ID=${FUSION_AUTH_SFTP_APP_ID}

src/classes/AuthenticationSession.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ enum FusionAuthStatusCode {
1313
}
1414

1515
export class AuthenticationSession {
16+
private static readonly sftpFusionAuthAppId = process.env.FUSION_AUTH_SFTP_APP_ID ?? '';
17+
1618
public authToken = '';
1719

1820
public refreshToken = '';
@@ -23,8 +25,6 @@ export class AuthenticationSession {
2325

2426
private readonly fusionAuthClient;
2527

26-
private readonly fusionAuthAppId = process.env.FUSION_AUTH_APP_ID ?? '';
27-
2828
private twoFactorId = '';
2929

3030
private twoFactorMethods: TwoFactorMethod[] = [];
@@ -78,13 +78,12 @@ export class AuthenticationSession {
7878

7979
private processPasswordResponse([password]: string[]): void {
8080
this.fusionAuthClient.login({
81-
applicationId: this.fusionAuthAppId,
81+
applicationId: AuthenticationSession.sftpFusionAuthAppId,
8282
loginId: this.authContext.username,
8383
password,
8484
}).then((clientResponse) => {
8585
switch (clientResponse.statusCode) {
86-
case FusionAuthStatusCode.Success:
87-
case FusionAuthStatusCode.SuccessButUnregisteredInApp:
86+
case FusionAuthStatusCode.Success: {
8887
if (clientResponse.response.token !== undefined) {
8988
logger.verbose('Successful password authentication attempt.', {
9089
username: this.authContext.username,
@@ -93,28 +92,41 @@ export class AuthenticationSession {
9392
this.authTokenExpiresAt = clientResponse.response.tokenExpirationInstant ?? 0;
9493
this.refreshToken = clientResponse.response.refreshToken ?? '';
9594
this.authContext.accept();
96-
return;
95+
} else {
96+
this.authContext.reject();
9797
}
98-
this.authContext.reject();
9998
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: {
101111
if (clientResponse.response.twoFactorId !== undefined) {
102112
logger.verbose('Successful password authentication attempt; MFA required.', {
103113
username: this.authContext.username,
104114
});
105115
this.twoFactorId = clientResponse.response.twoFactorId;
106116
this.twoFactorMethods = clientResponse.response.methods ?? [];
107117
this.promptForTwoFactorMethod();
108-
return;
118+
} else {
119+
this.authContext.reject();
109120
}
110-
this.authContext.reject();
111121
return;
112-
default:
122+
}
123+
default: {
113124
logger.verbose('Failed password authentication attempt.', {
114125
username: this.authContext.username,
115126
response: clientResponse.response,
116127
});
117128
this.authContext.reject();
129+
}
118130
}
119131
}).catch((clientResponse: unknown) => {
120132
const message = isPartialClientResponse(clientResponse)
@@ -125,6 +137,29 @@ export class AuthenticationSession {
125137
});
126138
}
127139

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+
128163
private promptForTwoFactorMethod(): void {
129164
const promptOptions = this.twoFactorMethods.map(
130165
(method, index) => `[${index + 1}] ${method.method ?? ''}`,

0 commit comments

Comments
 (0)