Skip to content

Commit 18070a0

Browse files
committed
split files up for easier testing
1 parent 69ecfab commit 18070a0

6 files changed

Lines changed: 89 additions & 159 deletions

File tree

src/chat/keys.ts

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { CERT } from './signature'
55
import { store } from 'solid-logic'
66
import { NamedNode } from 'rdflib'
77
import * as $rdf from 'rdflib'
8-
import { getExistingPublicKey, pubKeyUrl, privKeyUrl, getExistingPrivateKey } from '../utils/cryptoKeyHelpers'
8+
import { getExistingPublicKey, pubKeyUrl, privKeyUrl, getExistingPrivateKey } from '../utils/keyHelpers/accessData'
9+
import { setAcl, keyContainerAclBody, keyAclBody } from '../utils/keyHelpers/acl'
910

1011
export function generatePrivateKey (): string {
1112
return bytesToHex(schnorr.utils.randomPrivateKey())
@@ -77,82 +78,6 @@ export async function getPrivateKey (webId: NamedNode) {
7778
return privateKey as string
7879
}
7980

80-
/**
81-
* key container ACL
82-
* @param me
83-
* @returns aclBody
84-
*/
85-
const keyContainerAclBody = (me: string) => {
86-
const aclBody = `
87-
@prefix : <#>.
88-
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
89-
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
90-
@prefix key: <./>.
91-
92-
:ReadWrite
93-
a acl:Authorization;
94-
acl:accessTo key:;
95-
acl:default key:;
96-
acl:agent <${me}>;
97-
acl:mode acl:Read, acl:Write.
98-
`
99-
return aclBody
100-
}
101-
102-
/**
103-
* Read only ACL
104-
* @param keyDoc
105-
* @param me
106-
* @returns aclBody
107-
*/
108-
const keyAclBody = (keyDoc, me) => {
109-
let keyAgent = 'acl:agentClass foaf:Agent' // publicKey
110-
if (me?.length) keyAgent = `acl:agent <${me}>` // privateKey
111-
const aclBody = `
112-
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
113-
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
114-
<#Read>
115-
a acl:Authorization;
116-
${keyAgent};
117-
acl:accessTo <${keyDoc.split('/').pop()}>;
118-
acl:mode acl:Read.
119-
`
120-
return aclBody
121-
}
122-
123-
/**
124-
* set ACL
125-
* @param keyDoc
126-
* @param aclBody
127-
*/
128-
async function setAcl (keyDoc: string, aclBody: string) {
129-
// Some servers don't present a Link http response header
130-
// if the container doesn't exist yet, refetch the resource
131-
132-
await store.fetcher.load(keyDoc)
133-
134-
// FIXME: check the Why value on this quad:
135-
debug.log(store.statementsMatching(store.sym(keyDoc), store.sym('http://www.iana.org/assignments/link-relations/acl')))
136-
const keyAclDoc = store.any(store.sym(keyDoc), store.sym('http://www.iana.org/assignments/link-relations/acl'))
137-
if (!keyAclDoc) {
138-
throw new Error('Key ACL doc not found!')
139-
}
140-
141-
// delete READ only keyAclDoc. This is possible if the webId is an owner
142-
try {
143-
const response = await store.fetcher.webOperation('DELETE', keyAclDoc.value) // this may fail if webId is not an owner
144-
debug.log('delete ' + keyAclDoc.value + ' ' + response.status) // should test 404 and 2xx
145-
} catch (err) {
146-
if (err.response.status !== 404) { throw new Error(err) }
147-
debug.log('delete ' + keyAclDoc.value + ' ' + err.response.status) // should test 404 and 2xx
148-
}
149-
150-
const aclResponse = await store.fetcher.webOperation('PUT', keyAclDoc.value, {
151-
data: aclBody,
152-
contentType: 'text/turtle'
153-
})
154-
}
155-
15681
/**
15782
* delete acl if keydoc exists
15883
* create/edit keyDoc

src/utils/cryptoKeyHelpers.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/utils/keyHelpers/acl.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import * as debug from '../../debug'
2+
import { store } from 'solid-logic'
3+
4+
/**
5+
* set ACL
6+
* @param keyDoc
7+
* @param aclBody
8+
*/
9+
export async function setAcl (keyDoc: string, aclBody: string) {
10+
// Some servers don't present a Link http response header
11+
// if the container doesn't exist yet, refetch the resource
12+
13+
await store.fetcher.load(keyDoc)
14+
15+
// FIXME: check the Why value on this quad:
16+
debug.log(store.statementsMatching(store.sym(keyDoc), store.sym('http://www.iana.org/assignments/link-relations/acl')))
17+
const keyAclDoc = store.any(store.sym(keyDoc), store.sym('http://www.iana.org/assignments/link-relations/acl'))
18+
if (!keyAclDoc) {
19+
throw new Error('Key ACL doc not found!')
20+
}
21+
22+
// delete READ only keyAclDoc. This is possible if the webId is an owner
23+
try {
24+
const response = await store.fetcher.webOperation('DELETE', keyAclDoc.value) // this may fail if webId is not an owner
25+
debug.log('delete ' + keyAclDoc.value + ' ' + response.status) // should test 404 and 2xx
26+
} catch (err) {
27+
if (err.response.status !== 404) { throw new Error(err) }
28+
debug.log('delete ' + keyAclDoc.value + ' ' + err.response.status) // should test 404 and 2xx
29+
}
30+
31+
const aclResponse = await store.fetcher.webOperation('PUT', keyAclDoc.value, {
32+
data: aclBody,
33+
contentType: 'text/turtle'
34+
})
35+
}
36+
37+
/**
38+
* key container ACL
39+
* @param me
40+
* @returns aclBody
41+
*/
42+
export const keyContainerAclBody = (me: string) => {
43+
const aclBody = `
44+
@prefix : <#>.
45+
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
46+
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
47+
@prefix key: <./>.
48+
49+
:ReadWrite
50+
a acl:Authorization;
51+
acl:accessTo key:;
52+
acl:default key:;
53+
acl:agent <${me}>;
54+
acl:mode acl:Read, acl:Write.
55+
`
56+
return aclBody
57+
}
58+
59+
/**
60+
* Read only ACL
61+
* @param keyDoc
62+
* @param me
63+
* @returns aclBody
64+
*/
65+
export const keyAclBody = (keyDoc, me) => {
66+
let keyAgent = 'acl:agentClass foaf:Agent' // publicKey
67+
if (me?.length) keyAgent = `acl:agent <${me}>` // privateKey
68+
const aclBody = `
69+
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
70+
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
71+
<#Read>
72+
a acl:Authorization;
73+
${keyAgent};
74+
acl:accessTo <${keyDoc.split('/').pop()}>;
75+
acl:mode acl:Read.
76+
`
77+
return aclBody
78+
}

test/unit/utils/cryptoKeyHelpers.test.ts renamed to test/unit/utils/keyHelpers/accessData.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { store } from 'solid-logic'
22
import Fetcher from 'rdflib/lib/fetcher'
3-
import { getKeyIfExists, pubKeyUrl, privKeyUrl, getPodRoot, getExistingPublicKey, getExistingPrivateKey } from '../../../src/utils/keyHelpers/cryptoKeyHelpers'
3+
import { getKeyIfExists, pubKeyUrl, privKeyUrl, getPodRoot, getExistingPublicKey, getExistingPrivateKey } from '../../../../src/utils/keyHelpers/accessData'
44
import { NamedNode } from 'rdflib'
55

66
/* data */
@@ -55,7 +55,7 @@ describe('cryptoKeyHelpers', () => {
5555
})
5656
describe('getKeyIfExists', () => {
5757
/* @ts-ignore */
58-
store.any.mockReturnValue({ value: 'testing' })
58+
store.any.mockReturnValue({ value: PUB_KEY })
5959
it('returns a key if it exists', async () => {
6060
const webId = new NamedNode('https://alice.solid.example/profile/card#me')
6161
const result = await getKeyIfExists(webId, PUB_KEY, 'PublicKey')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
describe('acl Helpers', () => {
2+
describe.skip('setAcl', () => {
3+
it.skip('not sure yet', () => {
4+
5+
})
6+
})
7+
})

0 commit comments

Comments
 (0)