forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessData.ts
More file actions
68 lines (62 loc) · 1.95 KB
/
accessData.ts
File metadata and controls
68 lines (62 loc) · 1.95 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
import * as debug from '../../debug'
import { store } from 'solid-logic-jss'
import ns from '../../ns'
import { NamedNode } from 'rdflib'
import { getRootIfPreferencesExist } from './otherHelpers'
export const pubKeyUrl = (webId: NamedNode) => {
let url
try {
const root = getRootIfPreferencesExist(webId)
url = `${root}/profile/keys/publicKey.ttl`
} catch (err) {
debug.error(err)
}
return url
}
export const privKeyUrl = (webId: NamedNode) => {
let url
try {
const root = getRootIfPreferencesExist(webId)
url = `${root}/settings/keys/privateKey.ttl`
} catch (err) {
debug.error(err)
}
return url
}
export async function getExistingPublicKey (webId: NamedNode, publicKeyUrl: string) {
// find publickey
return await getKeyIfExists(webId, publicKeyUrl, 'publicKey')
}
export async function getExistingPrivateKey (webId: NamedNode, privateKeyUrl: string) {
// find privateKey
return await getKeyIfExists(webId, privateKeyUrl, 'privateKey')
}
type KeyType = 'publicKey' | 'privateKey'
export async function getKeyIfExists (webId: NamedNode, keyUrl: string, keyType: KeyType) {
try {
await store.fetcher.load(keyUrl)
const key = store.any(webId, ns.solid(keyType)) // store.sym(CERT + keyType))
return key?.value // as NamedNode
} catch (err) {
if (err.response.status === 404) {
debug.log(
'createIfNotExists: doc does NOT exist, will create... ' + keyUrl
)
try {
await store.fetcher.webOperation('PUT', keyUrl, {
data: '',
contentType: 'text/turtle'
})
} catch (err) {
debug.log('createIfNotExists doc FAILED: ' + keyUrl + ': ' + err)
throw err
}
delete store.fetcher.requested[keyUrl] // delete cached 404 error
// debug.log('createIfNotExists doc created ok ' + doc)
return undefined // response
} else {
debug.log('createIfNotExists doc FAILED: ' + keyUrl + ': ' + err)
throw err
}
}
}