-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
109 lines (90 loc) · 3.22 KB
/
Copy pathauth.js
File metadata and controls
109 lines (90 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import Secret from 'gi://Secret';
const SECRET_SCHEMA = new Secret.Schema('org.gnome.shell.extensions.copilot-usage.token', Secret.SchemaFlags.NONE, {
service: Secret.SchemaAttributeType.STRING,
account: Secret.SchemaAttributeType.STRING,
});
const SECRET_ATTRIBUTES = {
service: 'gnome-shell-extension',
account: 'copilot-usage@davidpcls',
};
const LEGACY_SECRET_ATTRIBUTES = {
service: 'github',
account: 'copilot-usage',
};
const SECRET_LABEL = 'GNOME Extension: Copilot Usage API Token';
export function extractTokenCandidate(rawValue) {
const value = String(rawValue ?? '').trim();
if (value === '') {
return '';
}
const lowered = value.toLowerCase();
if (lowered.startsWith('github_pat_') || lowered.startsWith('ghp_') || lowered.startsWith('gho_')) {
return value;
}
if (lowered.startsWith('token ')) {
return value.slice('token '.length).trim();
}
if (lowered.startsWith('bearer ')) {
return value.slice('bearer '.length).trim();
}
return value;
}
export class TokenStore {
migrateFromSettings(settings) {
const legacyToken = extractTokenCandidate(settings.get_string('api-token'));
if (legacyToken === '') {
return {migrated: false, errorCode: null};
}
try {
this.storeToken(legacyToken);
settings.set_string('api-token', '');
return {migrated: true, errorCode: null};
} catch (e) {
return {migrated: false, errorCode: 'keyring-unavailable'};
}
}
getToken() {
try {
const currentToken = Secret.password_lookup_sync(SECRET_SCHEMA, SECRET_ATTRIBUTES, null);
const parsedCurrent = extractTokenCandidate(currentToken);
if (parsedCurrent !== '') {
return {token: parsedCurrent, errorCode: null};
}
const legacyToken = Secret.password_lookup_sync(SECRET_SCHEMA, LEGACY_SECRET_ATTRIBUTES, null);
const parsedLegacy = extractTokenCandidate(legacyToken);
if (parsedLegacy === '') {
return {token: null, errorCode: 'missing-token'};
}
this.storeToken(parsedLegacy);
Secret.password_clear_sync(SECRET_SCHEMA, LEGACY_SECRET_ATTRIBUTES, null);
return {token: parsedLegacy, errorCode: null};
} catch (e) {
return {token: null, errorCode: 'keyring-unavailable'};
}
}
hasStoredToken() {
const result = this.getToken();
return result.errorCode === null && result.token !== null;
}
storeToken(rawToken) {
const token = extractTokenCandidate(rawToken);
if (token === '') {
throw new Error('empty-token');
}
const stored = Secret.password_store_sync(
SECRET_SCHEMA,
SECRET_ATTRIBUTES,
Secret.COLLECTION_DEFAULT,
SECRET_LABEL,
token,
null
);
if (!stored) {
throw new Error('store-failed');
}
}
clearToken() {
Secret.password_clear_sync(SECRET_SCHEMA, SECRET_ATTRIBUTES, null);
Secret.password_clear_sync(SECRET_SCHEMA, LEGACY_SECRET_ATTRIBUTES, null);
}
}