Skip to content

Commit 4640eb5

Browse files
committed
Update to use random string as default password for SSO / Social logins
1 parent 00ba3d7 commit 4640eb5

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

.env.sample

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,6 @@ SERVICEACC02_CID="devadmin1"
143143
SERVICEACC02_SECRET="devadmin1"
144144
SERVICEACC02_UID="100000027"
145145

146-
DEFAULT_REGISTRATION_PASS="default-password"
146+
# Note: Registration default password is no longer configurable; for social/SSO
147+
# registrations without a provided password, a unique 16-character random
148+
# password is generated at registration time.

src/api/user/user.service.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ describe('UserService', () => {
11631163
).rejects.toThrow(BadRequestException);
11641164
});
11651165

1166-
it('should apply default password when profile present and password missing', async () => {
1166+
it('should generate a random password when profile present and password missing', async () => {
11671167
const dto: CreateUserBodyDto = {
11681168
param: {
11691169
handle: 'socialuser',
@@ -1206,8 +1206,10 @@ describe('UserService', () => {
12061206

12071207
await service.registerUser(dto);
12081208

1209-
// Since configService.get('defaultPassword') is undefined in mock, it should fall back to 'default-password'
1210-
expect(mockEncode).toHaveBeenCalledWith('default-password');
1209+
// Should generate and encode a 16-character alphanumeric random password
1210+
expect(mockEncode).toHaveBeenCalledWith(
1211+
expect.stringMatching(/^[A-Za-z0-9]{16}$/),
1212+
);
12111213
});
12121214

12131215
it('should throw BadRequestException for short password', async () => {

src/api/user/user.service.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export class UserService {
7070
private readonly logger = new Logger(UserService.name);
7171
private readonly AUTH0_PROVIDER_NAME = 'auth0'; // Define constant for Auth0 provider name
7272
private legacyBlowfishKey: string; // Changed: Store the raw Base64 key string directly
73-
private readonly defaultPassword: string;
7473

7574
constructor(
7675
@Inject(PRISMA_CLIENT)
@@ -107,9 +106,6 @@ export class UserService {
107106
this.legacyBlowfishKey = '';
108107
}
109108
}
110-
this.defaultPassword = this.configService.get<string>(
111-
'DEFAULT_REGISTRATION_PASS',
112-
);
113109
}
114110

115111
// --- Core User Methods ---
@@ -617,6 +613,18 @@ export class UserService {
617613
return otp;
618614
}
619615

616+
// Generates a cryptographically-strong random password consisting of
617+
// alphanumeric characters of the requested length.
618+
private generateRandomPassword(length: number): string {
619+
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
620+
const bytes = crypto.randomBytes(length);
621+
let result = '';
622+
for (let i = 0; i < length; i++) {
623+
result += charset[bytes[i] % charset.length];
624+
}
625+
return result;
626+
}
627+
620628
/**
621629
* Encodes password using the legacy Blowfish/ECB/PKCS5Padding method.
622630
* Matches the logic from the old Java Utils.encodePassword.
@@ -690,7 +698,8 @@ export class UserService {
690698
userParams.credential = {} as CredentialDto;
691699
}
692700
if (!CommonUtils.validateString(userParams.credential.password)) {
693-
userParams.credential.password = this.defaultPassword;
701+
// Generate a unique random password for social/SSO registrations
702+
userParams.credential.password = this.generateRandomPassword(16);
694703
}
695704
}
696705
// perform initial static validations

0 commit comments

Comments
 (0)