Skip to content

Commit 5116928

Browse files
committed
feat: generate key one time
1 parent f07381b commit 5116928

File tree

15 files changed

+163
-204
lines changed

15 files changed

+163
-204
lines changed

api/dev/Unraid.net/myservers.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ wanaccess="yes"
99
wanport="8443"
1010
upnpEnabled="no"
1111
apikey="_______________________BIG_API_KEY_HERE_________________________"
12+
localApiKey="426b62b4d51e441fa97a93dfa1259920390a6eb61bd8675db0caa18dd0e414e9"
1213
1314
username="zspearmint"
1415
avatar="https://via.placeholder.com/200"

api/dev/keys/10f356da-1e9e-43b8-9028-a26a645539a6.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"key": "73717ca0-8c15-40b9-bcca-8d85656d1438",
44
"name": "Test API Key",
55
"description": "Testing API key creation",
6-
"roles": ["guest", "upc"],
6+
"roles": ["guest", "connect"],
77
"createdAt": "2024-10-29T19:59:12.569Z"
88
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"createdAt": "2024-12-19T16:49:56.848Z",
3+
"description": "API key for Connect user",
4+
"id": "d166bf8b-3615-444a-8932-c460b2132ba3",
5+
"key": "3a4e2332891e879d2ac8c3f25ef03a7b54f70b62cd6c5a08a86189cdd19ba203",
6+
"name": "Connect",
7+
"roles": [
8+
"admin"
9+
]
10+
}

api/dev/states/myservers.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ wanaccess="yes"
99
wanport="8443"
1010
upnpEnabled="no"
1111
apikey="_______________________BIG_API_KEY_HERE_________________________"
12+
localApiKey="3a4e2332891e879d2ac8c3f25ef03a7b54f70b62cd6c5a08a86189cdd19ba203"
1213
1314
username="zspearmint"
1415
avatar="https://via.placeholder.com/200"

api/src/graphql/generated/api/types.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,12 +1187,12 @@ export type RemoveRoleFromApiKeyInput = {
11871187

11881188
/** Available resources for permissions */
11891189
export enum Resource {
1190-
API_KEY = 'api_key',
1190+
APIKEY = 'apikey',
11911191
ARRAY = 'array',
11921192
CLOUD = 'cloud',
11931193
CONFIG = 'config',
11941194
CONNECT = 'connect',
1195-
CRASH_REPORTING_ENABLED = 'crash_reporting_enabled',
1195+
CONNECT__REMOTE_ACCESS = 'connect__remote_access',
11961196
CUSTOMIZATIONS = 'customizations',
11971197
DASHBOARD = 'dashboard',
11981198
DISK = 'disk',
@@ -1220,10 +1220,8 @@ export enum Resource {
12201220
/** Available roles for API keys and users */
12211221
export enum Role {
12221222
ADMIN = 'admin',
1223-
GUEST = 'guest',
1224-
MY_SERVERS = 'my_servers',
1225-
NOTIFIER = 'notifier',
1226-
UPC = 'upc'
1223+
CONNECT = 'connect',
1224+
GUEST = 'guest'
12271225
}
12281226

12291227
export type Server = {

api/src/graphql/resolvers/mutation/connect/connect-sign-in.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { decodeJwt } from 'jose';
22

33
import type { ConnectSignInInput } from '@app/graphql/generated/api/types';
44
import { NODE_ENV } from '@app/environment';
5-
import { Role } from '@app/graphql/generated/api/types';
65
import { API_KEY_STATUS } from '@app/mothership/api-key/api-key-types';
76
import { validateApiKeyWithKeyServer } from '@app/mothership/api-key/validate-api-key-with-keyserver';
87
import { getters, store } from '@app/store/index';
@@ -44,11 +43,7 @@ export const connectSignIn = async (input: ConnectSignInInput): Promise<boolean>
4443
if (localApiKeyFromConfig == '') {
4544
const apiKeyService = new ApiKeyService();
4645
// Create local API key
47-
const localApiKey = await apiKeyService.create(
48-
`LOCAL_KEY_${userInfo.preferred_username.toUpperCase()}`,
49-
`Local API key for Connect user ${userInfo.email}`,
50-
[Role.ADMIN]
51-
);
46+
const localApiKey = await apiKeyService.createLocalConnectApiKey();
5247

5348
if (!localApiKey?.key) {
5449
throw new Error('Failed to create local API key');
@@ -74,4 +69,4 @@ export const connectSignIn = async (input: ConnectSignInInput): Promise<boolean>
7469
} else {
7570
return false;
7671
}
77-
};
72+
};

api/src/graphql/schema/types/auth/auth.graphql

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,3 @@
1-
"""
2-
Available resources for permissions
3-
"""
4-
enum Resource {
5-
api_key
6-
cloud
7-
config
8-
crash_reporting_enabled
9-
customizations
10-
disk
11-
display
12-
flash
13-
info
14-
logs
15-
online
16-
os
17-
owner
18-
permission
19-
registration
20-
servers
21-
share
22-
vars
23-
connect
24-
notifications
25-
array
26-
dashboard
27-
docker
28-
network
29-
services
30-
vms
31-
me
32-
welcome
33-
}
34-
35-
"""
36-
Available roles for API keys and users
37-
"""
38-
enum Role {
39-
admin
40-
upc
41-
my_servers
42-
notifier
43-
guest
44-
}
45-
461
type ApiKey {
472
id: ID!
483
name: String!
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Available resources for permissions
3+
"""
4+
enum Resource {
5+
apikey
6+
array
7+
cloud
8+
config
9+
connect
10+
connect__remote_access
11+
customizations
12+
dashboard
13+
disk
14+
display
15+
docker
16+
flash
17+
info
18+
logs
19+
me
20+
network
21+
notifications
22+
online
23+
os
24+
owner
25+
permission
26+
registration
27+
servers
28+
services
29+
share
30+
vars
31+
vms
32+
welcome
33+
}
34+
35+
"""
36+
Available roles for API keys and users
37+
"""
38+
enum Role {
39+
admin
40+
connect
41+
guest
42+
}

api/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { setupRegistrationKeyWatch } from '@app/store/watch/registration-watch';
3232
import { StateManager } from '@app/store/watch/state-watch';
3333
import { setupVarRunWatch } from '@app/store/watch/var-run-watch';
3434
import { bootstrapNestServer } from '@app/unraid-api/main';
35+
import { createLocalApiKeyForConnectIfNecessary } from '@app/mothership/utils/create-local-connect-api-key';
3536

3637
let server: NestFastifyApplication<RawServerDefault> | null = null;
3738

@@ -84,6 +85,8 @@ try {
8485
// Start listening to dynamix config file changes
8586
setupDynamixConfigWatch();
8687

88+
await createLocalApiKeyForConnectIfNecessary();
89+
8790
// Disabled until we need the access token to work
8891
// TokenRefresh.init();
8992

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { minigraphLogger } from '@app/core/log';
2+
import { getters, store } from '@app/store/index';
3+
import { updateUserConfig } from '@app/store/modules/config';
4+
import { FileLoadStatus } from '@app/store/types';
5+
import { ApiKeyService } from '@app/unraid-api/auth/api-key.service';
6+
7+
export const createLocalApiKeyForConnectIfNecessary = async () => {
8+
if (getters.config().status !== FileLoadStatus.LOADED) {
9+
minigraphLogger.error('Config file not loaded, cannot create local API key');
10+
return;
11+
}
12+
13+
const { remote } = getters.config();
14+
const service = new ApiKeyService();
15+
// If the remote API Key is set and the local key is either not set or not found on disk, create a key
16+
if (remote.apikey && (!remote.localApiKey || !(await service.findById(remote.localApiKey)))) {
17+
minigraphLogger.debug('Creating local API key for Connect');
18+
// Create local API key
19+
const apiKeyService = new ApiKeyService();
20+
const localApiKey = await apiKeyService.createLocalConnectApiKey();
21+
22+
if (localApiKey?.key) {
23+
store.dispatch(
24+
updateUserConfig({
25+
remote: {
26+
localApiKey: localApiKey.key,
27+
},
28+
})
29+
);
30+
} else {
31+
throw new Error('Failed to create local API key - no key returned');
32+
}
33+
}
34+
};

0 commit comments

Comments
 (0)