Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 2ef3971

Browse files
Added command lines to enable/disable openid from console (#527)
* Added command lines to enable/disable openid * md * Update src/scripts/disable-openid.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * changed error codes based on code rabbit review * fix for github auth * code review --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 6c57b4e commit 2ef3971

File tree

7 files changed

+141
-8
lines changed

7 files changed

+141
-8
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"types": "tsc --noEmit --incremental",
1818
"verify": "yarn lint && yarn types",
1919
"reset-password": "node src/scripts/reset-password.js",
20+
"enable-openid": "node src/scripts/enable-openid.js",
21+
"disable-openid": "node src/scripts/disable-openid.js",
2022
"health-check": "node src/scripts/health-check.js"
2123
},
2224
"dependencies": {

src/account-db.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,25 @@ export async function disableOpenID(loginSettings) {
169169
return { error };
170170
}
171171

172-
getAccountDb().mutate('DELETE FROM sessions');
173-
getAccountDb().mutate('DELETE FROM users WHERE user_name <> ?', ['']);
174-
getAccountDb().mutate('DELETE FROM auth WHERE method = ?', ['openid']);
172+
try {
173+
accountDb.transaction(() => {
174+
accountDb.mutate('DELETE FROM sessions');
175+
accountDb.mutate(
176+
`DELETE FROM user_access
177+
WHERE user_access.user_id IN (
178+
SELECT users.id
179+
FROM users
180+
WHERE users.user_name <> ?
181+
);`,
182+
[''],
183+
);
184+
accountDb.mutate('DELETE FROM users WHERE user_name <> ?', ['']);
185+
accountDb.mutate('DELETE FROM auth WHERE method = ?', ['openid']);
186+
});
187+
} catch (err) {
188+
console.error('Error cleaning up openid information:', err);
189+
return { error: 'database-error' };
190+
}
175191
}
176192

177193
export function getSession(token) {

src/accounts/openid.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,22 @@ export async function loginWithOpenIdFinalize(body) {
170170
let { code_verifier, return_url } = pendingRequest;
171171

172172
try {
173-
const params = { code: body.code, state: body.state };
174-
let tokenSet = await client.callback(client.redirect_uris[0], params, {
175-
code_verifier,
176-
state: body.state,
177-
});
173+
let tokenSet = null;
174+
175+
if (!config.authMethod || config.authMethod === 'openid') {
176+
const params = { code: body.code, state: body.state };
177+
tokenSet = await client.callback(client.redirect_uris[0], params, {
178+
code_verifier,
179+
state: body.state,
180+
});
181+
} else {
182+
tokenSet = await client.grant({
183+
grant_type: 'authorization_code',
184+
code: body.code,
185+
redirect_uri: client.redirect_uris[0],
186+
code_verifier,
187+
});
188+
}
178189
const userInfo = await client.userinfo(tokenSet.access_token);
179190
const identity =
180191
userInfo.preferred_username ??

src/config-types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface Config {
3232
client_id: string;
3333
client_secret: string;
3434
server_hostname: string;
35+
authMethod?: 'openid' | 'oauth2';
3536
};
3637
multiuser: boolean;
3738
token_expiration?: 'never' | 'openid-provider' | number;

src/scripts/disable-openid.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
disableOpenID,
3+
getActiveLoginMethod,
4+
needsBootstrap,
5+
} from '../account-db.js';
6+
import { promptPassword } from '../util/prompt.js';
7+
8+
if (needsBootstrap()) {
9+
console.log('System needs to be bootstrapped first. OpenID is not enabled.');
10+
11+
process.exit(1);
12+
} else {
13+
console.log('To disable OpenID, you have to enter your server password:');
14+
try {
15+
const loginMethod = getActiveLoginMethod();
16+
console.log(`Current login method: ${loginMethod}`);
17+
18+
if (loginMethod === 'password') {
19+
console.log('OpenID already disabled.');
20+
process.exit(0);
21+
}
22+
23+
const password = await promptPassword();
24+
const { error } = (await disableOpenID({ password })) || {};
25+
26+
if (error) {
27+
console.log('Error disabling OpenID:', error);
28+
console.log(
29+
'Please report this as an issue: https://github.com/actualbudget/actual-server/issues',
30+
);
31+
process.exit(2);
32+
}
33+
console.log('OpenID disabled!');
34+
console.log(
35+
'Note: you will need to log in with the password on any browsers or devices that are currently logged in.',
36+
);
37+
} catch (err) {
38+
console.log('Unexpected error:', err);
39+
console.log(
40+
'Please report this as an issue: https://github.com/actualbudget/actual-server/issues',
41+
);
42+
process.exit(2);
43+
}
44+
}

src/scripts/enable-openid.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
enableOpenID,
3+
getActiveLoginMethod,
4+
needsBootstrap,
5+
} from '../account-db.js';
6+
import finalConfig from '../load-config.js';
7+
8+
if (needsBootstrap()) {
9+
console.log(
10+
'It looks like you don’t have a password set yet. Password is the fallback authentication method when using OpenID. Execute the command reset-password before using this command!',
11+
);
12+
13+
process.exit(1);
14+
} else {
15+
console.log('Enabling openid based on Environment variables or config.json');
16+
try {
17+
const loginMethod = getActiveLoginMethod();
18+
console.log(`Current login method: ${loginMethod}`);
19+
20+
if (loginMethod === 'openid') {
21+
console.log('OpenID already enabled.');
22+
process.exit(0);
23+
}
24+
const { error } = (await enableOpenID(finalConfig)) || {};
25+
26+
if (error) {
27+
console.log('Error enabling openid:', error);
28+
if (error === 'invalid-login-settings') {
29+
console.log(
30+
'Error configuring OpenID. Please verify that the configuration file or environment variables are correct.',
31+
);
32+
33+
process.exit(1);
34+
} else {
35+
console.log(
36+
'Please report this as an issue: https://github.com/actualbudget/actual-server/issues',
37+
);
38+
39+
process.exit(2);
40+
}
41+
}
42+
console.log('OpenID enabled!');
43+
console.log(
44+
'Note: The first user to login with OpenID will be the owner of the server.',
45+
);
46+
} catch (err) {
47+
console.log('Unexpected error:', err);
48+
console.log(
49+
'Please report this as an issue: https://github.com/actualbudget/actual-server/issues',
50+
);
51+
process.exit(2);
52+
}
53+
}

upcoming-release-notes/527.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
category: Enhancements
3+
authors: [lelemm]
4+
---
5+
6+
Commands to enable/disable OpenID from console. Also, enabling to login with oauth2 (for github).

0 commit comments

Comments
 (0)