forked from JavaScriptSolidServer/JavaScriptSolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.js
More file actions
149 lines (141 loc) · 5.31 KB
/
profile.js
File metadata and controls
149 lines (141 loc) · 5.31 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* WebID Profile generation
*
* Creates profile documents following Solid conventions. Default profile
* shape is now plain JSON-LD at `profile/card.jsonld` — operators who
* want a human-readable HTML shell can serve their own `index.html` with
* an embedded `<script type="application/ld+json">` data island.
*/
const FOAF = 'http://xmlns.com/foaf/0.1/';
const SOLID = 'http://www.w3.org/ns/solid/terms#';
const SCHEMA = 'http://schema.org/';
const LDP = 'http://www.w3.org/ns/ldp#';
const PIM = 'http://www.w3.org/ns/pim/space#';
const CID = 'https://www.w3.org/ns/cid/v1#';
const LWS = 'https://www.w3.org/ns/lws#';
/**
* Generate JSON-LD data for a WebID profile
* @param {object} options
* @param {string} options.webId - Full WebID URI (e.g., https://example.com/alice/profile/card#me)
* @param {string} options.name - Display name
* @param {string} options.podUri - Pod root URI (e.g., https://example.com/alice/)
* @param {string} options.issuer - OIDC issuer URI
* @returns {object} JSON-LD profile data
*/
export function generateProfileJsonLd({ webId, name, podUri, issuer }) {
const pod = podUri.endsWith('/') ? podUri : podUri + '/';
// Document URL is the WebID without its fragment; service entries use
// fragment ids resolved against it.
const docUrl = webId.split('#')[0];
return {
'@context': {
'foaf': FOAF,
'solid': SOLID,
'schema': SCHEMA,
'pim': PIM,
'ldp': LDP,
'cid': CID,
'lws': LWS,
'inbox': { '@id': 'ldp:inbox', '@type': '@id' },
'storage': { '@id': 'pim:storage', '@type': '@id' },
'oidcIssuer': { '@id': 'solid:oidcIssuer', '@type': '@id' },
'preferencesFile': { '@id': 'pim:preferencesFile', '@type': '@id' },
'publicTypeIndex': { '@id': 'solid:publicTypeIndex', '@type': '@id' },
'privateTypeIndex': { '@id': 'solid:privateTypeIndex', '@type': '@id' },
'isPrimaryTopicOf': { '@id': 'foaf:isPrimaryTopicOf', '@type': '@id' },
'mainEntityOfPage': { '@id': 'schema:mainEntityOfPage', '@type': '@id' },
'service': { '@id': 'cid:service', '@container': '@set' },
'serviceEndpoint': { '@id': 'cid:serviceEndpoint', '@type': '@id' }
},
'@id': webId,
'@type': ['foaf:Person', 'schema:Person'],
'foaf:name': name,
'isPrimaryTopicOf': '',
'mainEntityOfPage': '',
'inbox': `${pod}inbox/`,
'storage': pod,
'oidcIssuer': issuer,
'preferencesFile': `${pod}settings/prefs.jsonld`,
'publicTypeIndex': `${pod}settings/publicTypeIndex.jsonld`,
'privateTypeIndex': `${pod}settings/privateTypeIndex.jsonld`,
// LWS 1.0 Controlled Identifier service entry — mirrors `oidcIssuer` so
// LWS-aware verifiers can establish trust. Additive; the legacy
// `solid:oidcIssuer` predicate stays for existing Solid clients.
'service': [
{
'@id': `${docUrl}#oidc`,
'@type': 'lws:OpenIdProvider',
'serviceEndpoint': issuer
}
]
};
}
/**
* Generate the profile document as a plain JSON-LD object.
*
* Previously returned an HTML shell with an embedded data island; that
* shell still exists for hand-curated personal sites, but server-default
* profiles are now plain JSON-LD for predictability and easier
* post-processing by clients.
*
* @param {object} options
* @param {string} options.webId - Full WebID URI
* @param {string} options.name - Display name
* @param {string} options.podUri - Pod root URI
* @param {string} options.issuer - OIDC issuer URI
* @returns {object} JSON-LD profile document
*/
export function generateProfile({ webId, name, podUri, issuer }) {
return generateProfileJsonLd({ webId, name, podUri, issuer });
}
/**
* Generate preferences file as JSON-LD.
* @param {object} options
* @param {string} options.webId - Full WebID URI
* @param {string} options.podUri - Pod root URI
* @returns {object} JSON-LD preferences document
*/
export function generatePreferences({ webId, podUri }) {
const pod = podUri.endsWith('/') ? podUri : podUri + '/';
return {
'@context': {
'solid': SOLID,
'pim': PIM,
'publicTypeIndex': { '@id': 'solid:publicTypeIndex', '@type': '@id' },
'privateTypeIndex': { '@id': 'solid:privateTypeIndex', '@type': '@id' }
},
'@id': `${pod}settings/prefs.jsonld`,
'publicTypeIndex': `${pod}settings/publicTypeIndex.jsonld`,
'privateTypeIndex': `${pod}settings/privateTypeIndex.jsonld`
};
}
/**
* Generate an empty type index.
* Per the Solid Type Indexes spec, a public index is additionally typed
* `solid:ListedDocument` and a private one `solid:UnlistedDocument`.
* @param {string} uri - URI of the type index
* @param {object} [opts]
* @param {boolean} [opts.listed] - true for publicTypeIndex, false for privateTypeIndex.
* If omitted, only `solid:TypeIndex` is set (back-compat).
* @returns {object} JSON-LD type index document
*/
export function generateTypeIndex(uri, opts = {}) {
const types = ['solid:TypeIndex'];
if (opts.listed === true) types.push('solid:ListedDocument');
else if (opts.listed === false) types.push('solid:UnlistedDocument');
return {
'@context': {
'solid': SOLID
},
'@id': uri,
'@type': types.length === 1 ? types[0] : types
};
}
/**
* Serialize JSON-LD to string
* @param {object} jsonLd
* @returns {string}
*/
export function serialize(jsonLd) {
return JSON.stringify(jsonLd, null, 2);
}