1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-10-31 00:07:48 +02:00

Server: Fixes #13490: Make server less likely to generate non-unique SSO codes (#13501)

This commit is contained in:
Henry Heino
2025-10-28 03:34:22 -07:00
committed by GitHub
parent 76989ddc45
commit 7ffcbdf60a
2 changed files with 35 additions and 6 deletions

View File

@@ -497,4 +497,28 @@ describe('UserModel', () => {
}
});
test('should generate a unique SSO code', async () => {
const createExternalUser = async (index: number) => {
const user = await createUser(index);
return await models().user().save({
id: user.id,
is_external: 1,
}, { skipValidation: true });
};
const user1 = await createExternalUser(1);
const user2 = await createExternalUser(2);
config().SAML_ENABLED = true;
try {
await models().user().generateSsoCode(user1);
await models().user().generateSsoCode(user2);
const code1 = (await models().user().load(user1.id)).sso_auth_code;
const code2 = (await models().user().load(user2.id)).sso_auth_code;
expect(code1).not.toBe(code2);
} finally {
config().SAML_ENABLED = false;
}
});
});

View File

@@ -191,14 +191,19 @@ export default class UserModel extends BaseModel<User> {
}
public async generateSsoCode(user: User) {
let authCode;
const codeInUse = async (authCode: string) => {
return !!await this.loadBySsoAuthCode(authCode);
};
// Make sure that the code is not already in use.
do {
authCode = randomInt(0, 999999999).toString().padStart(9, '0');
} while (await this.loadBySsoAuthCode(authCode) === null);
const getUniqueAuthCode = async () => {
let authCode;
do {
authCode = randomInt(0, 999999999).toString().padStart(9, '0');
} while (await codeInUse(authCode));
return authCode;
};
user.sso_auth_code = authCode;
user.sso_auth_code = await getUniqueAuthCode();
user.sso_auth_code_expire_at = Date.now() + this.authCodeTtl;
await this.save(user, { skipValidation: true });