refactor: run formatter

This commit is contained in:
Elias Schneider
2024-09-27 16:03:53 +02:00
parent 3310fe53b3
commit 8f16d6b53e
5 changed files with 87 additions and 49 deletions

View File

@@ -29,7 +29,7 @@ export class AuthService {
private emailService: EmailService,
private ldapService: LdapService,
private userService: UserSevice,
) { }
) {}
private readonly logger = new Logger(AuthService.name);
async signUp(dto: AuthRegisterDTO, ip: string, isAdmin?: boolean) {
@@ -88,8 +88,8 @@ export class AuthService {
if (this.config.get("ldap.enabled")) {
/*
* E-mail-like user credentials are passed as the email property
* instead of the username. Since the username format does not matter
* when searching for users in LDAP, we simply use the username
* instead of the username. Since the username format does not matter
* when searching for users in LDAP, we simply use the username
* in whatever format it is provided.
*/
const ldapUsername = dto.username || dto.email;
@@ -99,10 +99,7 @@ export class AuthService {
dto.password,
);
if (ldapUser) {
const user = await this.userService.findOrCreateFromLDAP(
dto,
ldapUser,
);
const user = await this.userService.findOrCreateFromLDAP(dto, ldapUser);
this.logger.log(
`Successful LDAP login for user ${ldapUsername} (${user.id}) from IP ${ip}`,
);

View File

@@ -9,7 +9,7 @@ export class LdapService {
constructor(
@Inject(ConfigService)
private readonly serviceConfig: ConfigService,
) { }
) {}
private async createLdapConnection(): Promise<Client> {
const ldapUrl = this.serviceConfig.get("ldap.url");
@@ -26,7 +26,10 @@ export class LdapService {
const bindDn = this.serviceConfig.get("ldap.bindDn") || null;
if (bindDn) {
try {
await ldapClient.bind(bindDn, this.serviceConfig.get("ldap.bindPassword"));
await ldapClient.bind(
bindDn,
this.serviceConfig.get("ldap.bindPassword"),
);
} catch (error) {
this.logger.warn(`Failed to bind to default user: ${error}`);
throw new Error("failed to bind to default user");
@@ -41,7 +44,9 @@ export class LdapService {
password: string,
): Promise<Entry | null> {
if (!username.match(/^[a-zA-Z0-9-_.@]+$/)) {
this.logger.verbose(`Username ${username} does not match username pattern. Authentication failed.`);
this.logger.verbose(
`Username ${username} does not match username pattern. Authentication failed.`,
);
return null;
}
@@ -57,27 +62,35 @@ export class LdapService {
scope: "sub",
attributes: ["*"],
returnAttributeValues: true
returnAttributeValues: true,
});
if (searchEntries.length > 1) {
/* too many users found */
this.logger.verbose(`Authentication for username ${username} failed. Too many users found with query ${searchQuery}`);
this.logger.verbose(
`Authentication for username ${username} failed. Too many users found with query ${searchQuery}`,
);
return null;
} else if (searchEntries.length == 0) {
/* user not found */
this.logger.verbose(`Authentication for username ${username} failed. No user found with query ${searchQuery}`);
this.logger.verbose(
`Authentication for username ${username} failed. No user found with query ${searchQuery}`,
);
return null;
}
const targetEntity = searchEntries[0];
this.logger.verbose(`Trying to authenticate ${username} against LDAP user ${targetEntity.dn}`);
this.logger.verbose(
`Trying to authenticate ${username} against LDAP user ${targetEntity.dn}`,
);
try {
await ldapClient.bind(targetEntity.dn, password);
return targetEntity;
} catch (error) {
if (error instanceof InvalidCredentialsError) {
this.logger.verbose(`Failed to authenticate ${username} against ${targetEntity.dn}. Invalid credentials.`);
this.logger.verbose(
`Failed to authenticate ${username} against ${targetEntity.dn}. Invalid credentials.`,
);
return null;
}