Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ OPENID: Added option to create users on login #4421

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
22 changes: 14 additions & 8 deletions packages/sync-server/src/accounts/openid.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ export async function loginWithOpenIdFinalize(body) {
userInfo.login ??
userInfo.email ??
userInfo.id ??
userInfo.name ??
'default-username';
userInfo.sub;

if (identity == null) {
return { error: 'openid-grant-failed: no identification was found' };
}
Expand All @@ -206,7 +206,10 @@ export async function loginWithOpenIdFinalize(body) {
'SELECT count(*) as countUsersWithUserName FROM users WHERE user_name <> ?',
[''],
);
if (countUsersWithUserName === 0) {
if (
countUsersWithUserName === 0 ||
finalConfig.userCreationMode === 'login'
) {
userId = uuidv4();
// Check if user was created by another transaction
const existingUser = accountDb.first(
Expand All @@ -217,18 +220,21 @@ export async function loginWithOpenIdFinalize(body) {
throw new Error('user-already-exists');
}
accountDb.mutate(
'INSERT INTO users (id, user_name, display_name, enabled, owner, role) VALUES (?, ?, ?, 1, 1, ?)',
'INSERT INTO users (id, user_name, display_name, enabled, owner, role) VALUES (?, ?, ?, 1, ?, ?)',
[
userId,
identity,
userInfo.name ?? userInfo.email ?? identity,
'ADMIN',
countUsersWithUserName === 0 ? '1' : '0',
countUsersWithUserName === 0 ? 'ADMIN' : 'BASIC',
],
);

const userFromPasswordMethod = getUserByUsername('');
if (userFromPasswordMethod) {
transferAllFilesFromUser(userId, userFromPasswordMethod.user_id);
if (countUsersWithUserName === 0) {
const userFromPasswordMethod = getUserByUsername('');
if (userFromPasswordMethod) {
transferAllFilesFromUser(userId, userFromPasswordMethod.user_id);
}
}
} else {
const { id: userIdFromDb, display_name: displayName } =
Expand Down
1 change: 1 addition & 0 deletions packages/sync-server/src/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ export interface Config {
multiuser: boolean;
token_expiration?: 'never' | 'openid-provider' | number;
enforceOpenId: boolean;
userCreationMode?: 'manual' | 'login';
}
4 changes: 4 additions & 0 deletions packages/sync-server/src/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const defaultConfig = {
multiuser: false,
token_expiration: 'never',
enforceOpenId: false,
userCreationMode: 'manual',
};

/** @type {import('./config-types.js').Config} */
Expand Down Expand Up @@ -233,6 +234,9 @@ const finalConfig = {
return value === 'true';
})()
: config.enforceOpenId,
userCreationMode: process.env.ACTUAL_USER_CREATION_MODE
? process.env.ACTUAL_USER_CREATION_MODE
: config.userCreationMode,
};
debug(`using port ${finalConfig.port}`);
debug(`using hostname ${finalConfig.hostname}`);
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4421.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [lelemm]
---

Added `ACTUAL_USER_CREATION_MODE=login` enviroment variable to create users on login
Loading