Handle not unique errors during auto-registration (#14625)

* Handle RecordNotUniqueException in oauth

* Handle RecordNotUniqueException in openid

* Added RecordNotUniqueException handling for ldap

* Update api/src/auth/drivers/oauth2.ts

* Update api/src/auth/drivers/ldap.ts

* Add LDAP unique user log

* Added unique user log to oauth

* Added unique user log to OpenID

* Update api/src/auth/drivers/ldap.ts

* Update api/src/auth/drivers/oauth2.ts
This commit is contained in:
Aiden Foxx
2022-07-25 20:09:51 +02:00
committed by GitHub
parent 81cd748c6d
commit 28d60131a3
3 changed files with 56 additions and 26 deletions

View File

@@ -18,10 +18,12 @@ import { AuthDriverOptions, User } from '../../types';
import {
InvalidCredentialsException,
InvalidPayloadException,
InvalidProviderException,
ServiceUnavailableException,
InvalidConfigException,
UnexpectedResponseException,
} from '../../exceptions';
import { RecordNotUniqueException } from '../../exceptions/database/record-not-unique';
import { AuthenticationService, UsersService } from '../../services';
import asyncHandler from '../../utils/async-handler';
import env from '../../env';
@@ -274,14 +276,22 @@ export class LDAPAuthDriver extends AuthDriver {
throw new InvalidCredentialsException();
}
await this.usersService.createOne({
provider: this.config.provider,
first_name: userInfo.firstName,
last_name: userInfo.lastName,
email: userInfo.email,
external_identifier: userInfo.dn,
role: userRole?.id ?? defaultRoleId,
});
try {
await this.usersService.createOne({
provider: this.config.provider,
first_name: userInfo.firstName,
last_name: userInfo.lastName,
email: userInfo.email,
external_identifier: userInfo.dn,
role: userRole?.id ?? defaultRoleId,
});
} catch (e) {
if (e instanceof RecordNotUniqueException) {
logger.warn(e, '[LDAP] Failed to register user. User not unique');
throw new InvalidProviderException();
}
throw e;
}
return (await this.fetchUserId(userInfo.dn)) as string;
}

View File

@@ -10,9 +10,11 @@ import env from '../../env';
import {
InvalidConfigException,
InvalidCredentialsException,
InvalidProviderException,
InvalidTokenException,
ServiceUnavailableException,
} from '../../exceptions';
import { RecordNotUniqueException } from '../../exceptions/database/record-not-unique';
import logger from '../../logger';
import { respond } from '../../middleware/respond';
import { AuthenticationService, UsersService } from '../../services';
@@ -152,15 +154,23 @@ export class OAuth2AuthDriver extends LocalAuthDriver {
throw new InvalidCredentialsException();
}
await this.usersService.createOne({
provider,
first_name: userInfo[this.config.firstNameKey],
last_name: userInfo[this.config.lastNameKey],
email: email,
external_identifier: identifier,
role: this.config.defaultRoleId,
auth_data: tokenSet.refresh_token && JSON.stringify({ refreshToken: tokenSet.refresh_token }),
});
try {
await this.usersService.createOne({
provider,
first_name: userInfo[this.config.firstNameKey],
last_name: userInfo[this.config.lastNameKey],
email: email,
external_identifier: identifier,
role: this.config.defaultRoleId,
auth_data: tokenSet.refresh_token && JSON.stringify({ refreshToken: tokenSet.refresh_token }),
});
} catch (e) {
if (e instanceof RecordNotUniqueException) {
logger.warn(e, '[OAuth2] Failed to register user. User not unique');
throw new InvalidProviderException();
}
throw e;
}
return (await this.fetchUserId(identifier)) as string;
}

View File

@@ -10,9 +10,11 @@ import env from '../../env';
import {
InvalidConfigException,
InvalidCredentialsException,
InvalidProviderException,
InvalidTokenException,
ServiceUnavailableException,
} from '../../exceptions';
import { RecordNotUniqueException } from '../../exceptions/database/record-not-unique';
import logger from '../../logger';
import { respond } from '../../middleware/respond';
import { AuthenticationService, UsersService } from '../../services';
@@ -175,15 +177,23 @@ export class OpenIDAuthDriver extends LocalAuthDriver {
throw new InvalidCredentialsException();
}
await this.usersService.createOne({
provider,
first_name: userInfo.given_name,
last_name: userInfo.family_name,
email: email,
external_identifier: identifier,
role: this.config.defaultRoleId,
auth_data: tokenSet.refresh_token && JSON.stringify({ refreshToken: tokenSet.refresh_token }),
});
try {
await this.usersService.createOne({
provider,
first_name: userInfo.given_name,
last_name: userInfo.family_name,
email: email,
external_identifier: identifier,
role: this.config.defaultRoleId,
auth_data: tokenSet.refresh_token && JSON.stringify({ refreshToken: tokenSet.refresh_token }),
});
} catch (e) {
if (e instanceof RecordNotUniqueException) {
logger.warn(e, '[OpenID] Failed to register user. User not unique');
throw new InvalidProviderException();
}
throw e;
}
return (await this.fetchUserId(identifier)) as string;
}