solid-logic 3.1.1-da65b2c → 3.1.1-e3eba68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/README.md +2 -108
  2. package/eslint.config.js +29 -0
  3. package/jest.config.js +10 -0
  4. package/lib/acl/aclLogic.d.ts +5 -0
  5. package/{dist → lib}/acl/aclLogic.d.ts.map +1 -1
  6. package/lib/acl/aclLogic.js +168 -0
  7. package/lib/acl/aclLogic.js.map +1 -0
  8. package/{dist → lib}/authSession/authSession.js +1 -1
  9. package/{dist → lib}/authSession/authSession.js.map +1 -1
  10. package/{dist → lib}/authn/SolidAuthnLogic.d.ts +3 -3
  11. package/{dist → lib}/authn/SolidAuthnLogic.d.ts.map +1 -1
  12. package/lib/authn/SolidAuthnLogic.js +193 -0
  13. package/lib/authn/SolidAuthnLogic.js.map +1 -0
  14. package/{dist → lib}/authn/authUtil.d.ts +1 -1
  15. package/{dist → lib}/authn/authUtil.js +12 -12
  16. package/lib/authn/authUtil.js.map +1 -0
  17. package/{dist → lib}/chat/chatLogic.d.ts +1 -1
  18. package/lib/chat/chatLogic.js +272 -0
  19. package/lib/chat/chatLogic.js.map +1 -0
  20. package/{dist → lib}/inbox/inboxLogic.d.ts +1 -1
  21. package/{dist → lib}/inbox/inboxLogic.d.ts.map +1 -1
  22. package/lib/inbox/inboxLogic.js +139 -0
  23. package/lib/inbox/inboxLogic.js.map +1 -0
  24. package/{dist → lib}/index.d.ts +1 -1
  25. package/{dist → lib}/index.d.ts.map +1 -1
  26. package/{dist → lib}/index.js +4 -4
  27. package/{dist → lib}/index.js.map +1 -1
  28. package/lib/issuer/issuerLogic.js +52 -0
  29. package/lib/issuer/issuerLogic.js.map +1 -0
  30. package/{dist → lib}/logic/CustomError.d.ts.map +1 -1
  31. package/lib/logic/CustomError.js +89 -0
  32. package/lib/logic/CustomError.js.map +1 -0
  33. package/lib/logic/solidLogic.d.ts +6 -0
  34. package/{dist → lib}/logic/solidLogic.d.ts.map +1 -1
  35. package/{dist → lib}/logic/solidLogic.js +33 -32
  36. package/lib/logic/solidLogic.js.map +1 -0
  37. package/lib/logic/solidLogicSingleton.js +94 -0
  38. package/lib/logic/solidLogicSingleton.js.map +1 -0
  39. package/{dist → lib}/profile/profileLogic.d.ts +1 -1
  40. package/lib/profile/profileLogic.js +259 -0
  41. package/lib/profile/profileLogic.js.map +1 -0
  42. package/lib/typeIndex/typeIndexLogic.js +399 -0
  43. package/lib/typeIndex/typeIndexLogic.js.map +1 -0
  44. package/{dist → lib}/types.d.ts +2 -2
  45. package/{dist → lib}/util/containerLogic.d.ts +1 -1
  46. package/{dist → lib}/util/containerLogic.d.ts.map +1 -1
  47. package/lib/util/containerLogic.js +102 -0
  48. package/lib/util/containerLogic.js.map +1 -0
  49. package/lib/util/debug.d.ts.map +1 -0
  50. package/lib/util/debug.js +40 -0
  51. package/lib/util/debug.js.map +1 -0
  52. package/{dist → lib}/util/ns.js +2 -2
  53. package/{dist → lib}/util/ns.js.map +1 -1
  54. package/{dist → lib}/util/utilityLogic.d.ts +1 -1
  55. package/{dist → lib}/util/utilityLogic.d.ts.map +1 -1
  56. package/lib/util/utilityLogic.js +284 -0
  57. package/lib/util/utilityLogic.js.map +1 -0
  58. package/{dist → lib}/util/utils.d.ts +1 -1
  59. package/{dist → lib}/util/utils.d.ts.map +1 -1
  60. package/lib/util/utils.js +47 -0
  61. package/lib/util/utils.js.map +1 -0
  62. package/package.json +20 -58
  63. package/src/acl/aclLogic.ts +156 -0
  64. package/src/authSession/authSession.ts +7 -0
  65. package/src/authn/SolidAuthnLogic.ts +127 -0
  66. package/src/authn/authUtil.ts +70 -0
  67. package/src/chat/chatLogic.ts +226 -0
  68. package/src/inbox/inboxLogic.ts +59 -0
  69. package/src/index.ts +22 -0
  70. package/src/issuer/issuerLogic.ts +40 -0
  71. package/src/logic/CustomError.ts +29 -0
  72. package/src/logic/solidLogic.ts +76 -0
  73. package/src/logic/solidLogicSingleton.ts +20 -0
  74. package/src/profile/profileLogic.ts +125 -0
  75. package/src/typeIndex/typeIndexLogic.ts +198 -0
  76. package/src/types.ts +122 -0
  77. package/{dist/util/containerLogic.js → src/util/containerLogic.ts} +25 -20
  78. package/src/util/debug.ts +20 -0
  79. package/src/util/ns.ts +5 -0
  80. package/src/util/utilityLogic.ts +156 -0
  81. package/src/util/utils.ts +52 -0
  82. package/test/aclLogic.test.ts +24 -0
  83. package/test/authUtil.test.ts +23 -0
  84. package/test/chatLogic.test.ts +316 -0
  85. package/test/container.test.ts +58 -0
  86. package/test/helpers/dataSetup.ts +134 -0
  87. package/test/helpers/setup.ts +17 -0
  88. package/test/inboxLogic.test.ts +218 -0
  89. package/test/logic.test.ts +29 -0
  90. package/test/profileLogic.test.ts +246 -0
  91. package/test/solidAuthLogic.test.ts +49 -0
  92. package/test/typeIndexLogic.test.ts +255 -0
  93. package/test/utilityLogic.test.ts +179 -0
  94. package/test/utils.test.ts +32 -0
  95. package/tsconfig.json +75 -0
  96. package/dist/789.solid-logic.js +0 -11
  97. package/dist/789.solid-logic.js.map +0 -1
  98. package/dist/789.solid-logic.min.js +0 -1
  99. package/dist/841.solid-logic.js +0 -12133
  100. package/dist/841.solid-logic.js.map +0 -1
  101. package/dist/841.solid-logic.min.js +0 -73
  102. package/dist/841.solid-logic.min.js.map +0 -1
  103. package/dist/acl/aclLogic.d.ts +0 -5
  104. package/dist/acl/aclLogic.js +0 -122
  105. package/dist/acl/aclLogic.js.map +0 -1
  106. package/dist/authn/SolidAuthnLogic.js +0 -147
  107. package/dist/authn/SolidAuthnLogic.js.map +0 -1
  108. package/dist/authn/authUtil.js.map +0 -1
  109. package/dist/chat/chatLogic.js +0 -160
  110. package/dist/chat/chatLogic.js.map +0 -1
  111. package/dist/inbox/inboxLogic.js +0 -55
  112. package/dist/inbox/inboxLogic.js.map +0 -1
  113. package/dist/issuer/issuerLogic.js +0 -40
  114. package/dist/issuer/issuerLogic.js.map +0 -1
  115. package/dist/logic/CustomError.js +0 -38
  116. package/dist/logic/CustomError.js.map +0 -1
  117. package/dist/logic/solidLogic.d.ts +0 -6
  118. package/dist/logic/solidLogic.js.map +0 -1
  119. package/dist/logic/solidLogicSingleton.js +0 -54
  120. package/dist/logic/solidLogicSingleton.js.map +0 -1
  121. package/dist/profile/profileLogic.js +0 -165
  122. package/dist/profile/profileLogic.js.map +0 -1
  123. package/dist/solid-logic.esm.external.js +0 -12725
  124. package/dist/solid-logic.esm.external.js.map +0 -1
  125. package/dist/solid-logic.esm.external.min.js +0 -32
  126. package/dist/solid-logic.esm.external.min.js.map +0 -1
  127. package/dist/solid-logic.external.js +0 -12802
  128. package/dist/solid-logic.external.js.map +0 -1
  129. package/dist/solid-logic.external.min.js +0 -32
  130. package/dist/solid-logic.external.min.js.map +0 -1
  131. package/dist/solid-logic.js +0 -40921
  132. package/dist/solid-logic.js.map +0 -1
  133. package/dist/solid-logic.min.js +0 -41
  134. package/dist/solid-logic.min.js.map +0 -1
  135. package/dist/typeIndex/typeIndexLogic.js +0 -222
  136. package/dist/typeIndex/typeIndexLogic.js.map +0 -1
  137. package/dist/util/containerLogic.js.map +0 -1
  138. package/dist/util/debug.d.ts.map +0 -1
  139. package/dist/util/debug.js +0 -20
  140. package/dist/util/debug.js.map +0 -1
  141. package/dist/util/utilityLogic.js +0 -180
  142. package/dist/util/utilityLogic.js.map +0 -1
  143. package/dist/util/utils.js +0 -47
  144. package/dist/util/utils.js.map +0 -1
  145. package/dist/versionInfo.js +0 -33
  146. package/dist/versionInfo.js.map +0 -1
  147. /package/{dist → lib}/authSession/authSession.d.ts +0 -0
  148. /package/{dist → lib}/authSession/authSession.d.ts.map +0 -0
  149. /package/{dist → lib}/authn/authUtil.d.ts.map +0 -0
  150. /package/{dist → lib}/chat/chatLogic.d.ts.map +0 -0
  151. /package/{dist → lib}/issuer/issuerLogic.d.ts +0 -0
  152. /package/{dist → lib}/issuer/issuerLogic.d.ts.map +0 -0
  153. /package/{dist → lib}/logic/CustomError.d.ts +0 -0
  154. /package/{dist → lib}/logic/solidLogicSingleton.d.ts +0 -0
  155. /package/{dist → lib}/logic/solidLogicSingleton.d.ts.map +0 -0
  156. /package/{dist → lib}/profile/profileLogic.d.ts.map +0 -0
  157. /package/{dist → lib}/typeIndex/typeIndexLogic.d.ts +0 -0
  158. /package/{dist → lib}/typeIndex/typeIndexLogic.d.ts.map +0 -0
  159. /package/{dist → lib}/types.d.ts.map +0 -0
  160. /package/{dist → lib}/types.js +0 -0
  161. /package/{dist → lib}/types.js.map +0 -0
  162. /package/{dist → lib}/util/debug.d.ts +0 -0
  163. /package/{dist → lib}/util/ns.d.ts +0 -0
  164. /package/{dist → lib}/util/ns.d.ts.map +0 -0
@@ -0,0 +1,127 @@
1
+ import { namedNode, NamedNode, sym } from "rdflib";
2
+ import { appContext, offlineTestID } from "./authUtil";
3
+ import * as debug from '../util/debug'
4
+ import { EVENTS, Session } from "@inrupt/solid-client-authn-browser";
5
+ import { AuthenticationContext, AuthnLogic } from "../types";
6
+
7
+ export class SolidAuthnLogic implements AuthnLogic {
8
+ private session: Session;
9
+
10
+ constructor(solidAuthSession: Session) {
11
+ this.session = solidAuthSession;
12
+ }
13
+
14
+ // we created authSession getter because we want to access it as authn.authSession externally
15
+ get authSession():Session { return this.session }
16
+
17
+ currentUser(): NamedNode | null {
18
+ const app = appContext()
19
+ if (app.viewingNoAuthPage) {
20
+ return sym(app.webId)
21
+ }
22
+ if (this && this.session && this.session.info && this.session.info.webId && this.session.info.isLoggedIn) {
23
+ return sym(this.session.info.webId)
24
+ }
25
+ return offlineTestID() // null unless testing
26
+ }
27
+
28
+ /**
29
+ * Retrieves currently logged in webId from either
30
+ * defaultTestUser or SolidAuth
31
+ * Also activates a session after login
32
+ * @param [setUserCallback] Optional callback
33
+ * @returns Resolves with webId uri, if no callback provided
34
+ */
35
+ async checkUser<T> (
36
+ setUserCallback?: (me: NamedNode | null) => T
37
+ ): Promise<NamedNode | T | null> {
38
+ // Save hash for "restorePreviousSession"
39
+ const preLoginRedirectHash = new URL(window.location.href).hash
40
+ if (preLoginRedirectHash) {
41
+ window.localStorage.setItem('preLoginRedirectHash', preLoginRedirectHash)
42
+ }
43
+ this.session.events.on(EVENTS.SESSION_RESTORED, (url) => {
44
+ debug.log(`Session restored to ${url}`)
45
+ if (document.location.toString() !== url) history.replaceState(null, '', url)
46
+ })
47
+
48
+ /**
49
+ * Handle a successful authentication redirect
50
+ */
51
+ const redirectUrl = new URL(window.location.href)
52
+ redirectUrl.hash = ''
53
+ await this.session
54
+ .handleIncomingRedirect({
55
+ restorePreviousSession: true,
56
+ url: redirectUrl.href
57
+ })
58
+
59
+ // Check to see if a hash was stored in local storage
60
+ const postLoginRedirectHash = window.localStorage.getItem('preLoginRedirectHash')
61
+ if (postLoginRedirectHash) {
62
+ const curUrl = new URL(window.location.href)
63
+ if (curUrl.hash !== postLoginRedirectHash) {
64
+ if (history.pushState) {
65
+ // debug.log('Setting window.location.has using pushState')
66
+ history.pushState(null, document.title, postLoginRedirectHash)
67
+ } else {
68
+ // debug.warn('Setting window.location.has using location.hash')
69
+ location.hash = postLoginRedirectHash
70
+ }
71
+ curUrl.hash = postLoginRedirectHash
72
+ }
73
+ // See https://stackoverflow.com/questions/3870057/how-can-i-update-window-location-hash-without-jumping-the-document
74
+ // window.location.href = curUrl.toString()// @@ See https://developer.mozilla.org/en-US/docs/Web/API/Window/location
75
+ window.localStorage.setItem('preLoginRedirectHash', '')
76
+ }
77
+
78
+ // Check to see if already logged in / have the WebID
79
+ let me = offlineTestID()
80
+ if (me) {
81
+ return Promise.resolve(setUserCallback ? setUserCallback(me) : me)
82
+ }
83
+
84
+ const webId = this.webIdFromSession(this.session.info)
85
+ if (webId) {
86
+ me = this.saveUser(webId)
87
+ }
88
+
89
+ if (me) {
90
+ debug.log(`(Logged in as ${me} by authentication)`)
91
+ }
92
+
93
+ return Promise.resolve(setUserCallback ? setUserCallback(me) : me)
94
+ }
95
+
96
+ /**
97
+ * Saves `webId` in `context.me`
98
+ * @param webId
99
+ * @param context
100
+ *
101
+ * @returns Returns the WebID, after setting it
102
+ */
103
+ saveUser (
104
+ webId: NamedNode | string | null,
105
+ context?: AuthenticationContext
106
+ ): NamedNode | null {
107
+ let webIdUri: string
108
+ if (webId) {
109
+ webIdUri = (typeof webId === 'string') ? webId : webId.uri
110
+ const me = namedNode(webIdUri)
111
+ if (context) {
112
+ context.me = me
113
+ }
114
+ return me
115
+ }
116
+ return null
117
+ }
118
+
119
+ /**
120
+ * @returns {Promise<string|null>} Resolves with WebID URI or null
121
+ */
122
+ webIdFromSession (session?: { webId?: string, isLoggedIn: boolean }): string | null {
123
+ const webId = session?.webId && session.isLoggedIn ? session.webId : null
124
+ return webId
125
+ }
126
+
127
+ }
@@ -0,0 +1,70 @@
1
+ import { NamedNode, sym } from "rdflib"
2
+ import * as debug from '../util/debug'
3
+
4
+ /**
5
+ * find a user or app's context as set in window.SolidAppContext
6
+ * this is a const, not a function, because we have problems to jest mock it otherwise
7
+ * see: https://github.com/facebook/jest/issues/936#issuecomment-545080082 for more
8
+ * @return {any} - an appContext object
9
+ */
10
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
+ export const appContext = ():any => {
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ let { SolidAppContext }: any = window
14
+ SolidAppContext ||= {}
15
+ SolidAppContext.viewingNoAuthPage = false
16
+ if (SolidAppContext.noAuth && window.document) {
17
+ const currentPage = window.document.location.href
18
+ if (currentPage.startsWith(SolidAppContext.noAuth)) {
19
+ SolidAppContext.viewingNoAuthPage = true
20
+ const params = new URLSearchParams(window.document.location.search)
21
+ if (params) {
22
+ let viewedPage = SolidAppContext.viewedPage = params.get('uri') || null
23
+ if (viewedPage) {
24
+ viewedPage = decodeURI(viewedPage)
25
+ if (!viewedPage.startsWith(SolidAppContext.noAuth)) {
26
+ const ary = viewedPage.split(/\//)
27
+ SolidAppContext.idp = ary[0] + '//' + ary[2]
28
+ SolidAppContext.viewingNoAuthPage = false
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+ return SolidAppContext
35
+ }
36
+
37
+ /**
38
+ * Returns `sym($SolidTestEnvironment.username)` if
39
+ * `$SolidTestEnvironment.username` is defined as a global
40
+ * or
41
+ * returns testID defined in the HTML page
42
+ * @returns {NamedNode|null}
43
+ */
44
+ export function offlineTestID (): NamedNode | null {
45
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
+ const { $SolidTestEnvironment }: any = window
47
+ if (
48
+ typeof $SolidTestEnvironment !== 'undefined' &&
49
+ $SolidTestEnvironment.username
50
+ ) {
51
+ // Test setup
52
+ debug.log('Assuming the user is ' + $SolidTestEnvironment.username)
53
+ return sym($SolidTestEnvironment.username)
54
+ }
55
+ // hack that makes SolidOS work in offline mode by adding the webId directly in html
56
+ // example usage: https://github.com/solidos/mashlib/blob/29b8b53c46bf02e0e219f0bacd51b0e9951001dd/test/contact/local.html#L37
57
+ if (
58
+ typeof document !== 'undefined' &&
59
+ document.location &&
60
+ ('' + document.location).slice(0, 16) === 'http://localhost'
61
+ ) {
62
+ const div = document.getElementById('appTarget')
63
+ if (!div) return null
64
+ const id = div.getAttribute('testID')
65
+ if (!id) return null
66
+ debug.log('Assuming user is ' + id)
67
+ return sym(id)
68
+ }
69
+ return null
70
+ }
@@ -0,0 +1,226 @@
1
+ import { NamedNode, Node, st, term } from "rdflib"
2
+ import { ChatLogic, CreatedPaneOptions, NewPaneOptions, Chat } from "../types"
3
+ import { ns as namespace } from "../util/ns";
4
+ import { determineChatContainer, newThing } from "../util/utils"
5
+
6
+ const CHAT_LOCATION_IN_CONTAINER = "index.ttl#this"
7
+
8
+ export function createChatLogic(store, profileLogic): ChatLogic {
9
+ const ns = namespace
10
+
11
+ async function setAcl(
12
+ chatContainer: NamedNode,
13
+ me: NamedNode,
14
+ invitee: NamedNode
15
+ ): Promise<void> {
16
+ // Some servers don't present a Link http response header
17
+ // if the container doesn't exist yet, so refetch the container
18
+ // now that it has been created:
19
+ await store.fetcher.load(chatContainer);
20
+
21
+ // FIXME: check the Why value on this quad:
22
+ const chatAclDoc = store.any(
23
+ chatContainer,
24
+ new NamedNode("http://www.iana.org/assignments/link-relations/acl")
25
+ );
26
+ if (!chatAclDoc) {
27
+ throw new Error("Chat ACL doc not found!");
28
+ }
29
+
30
+ const aclBody = `
31
+ @prefix acl: <http://www.w3.org/ns/auth/acl#>.
32
+ <#owner>
33
+ a acl:Authorization;
34
+ acl:agent <${me.value}>;
35
+ acl:accessTo <.>;
36
+ acl:default <.>;
37
+ acl:mode
38
+ acl:Read, acl:Write, acl:Control.
39
+ <#invitee>
40
+ a acl:Authorization;
41
+ acl:agent <${invitee.value}>;
42
+ acl:accessTo <.>;
43
+ acl:default <.>;
44
+ acl:mode
45
+ acl:Read, acl:Append.
46
+ `;
47
+ await store.fetcher.webOperation("PUT", chatAclDoc.value, {
48
+ data: aclBody,
49
+ contentType: "text/turtle",
50
+ });
51
+ }
52
+
53
+ async function addToPrivateTypeIndex(chatThing, me) {
54
+ // Add to private type index
55
+ const privateTypeIndex = store.any(
56
+ me,
57
+ ns.solid("privateTypeIndex")
58
+ ) as NamedNode | null;
59
+ if (!privateTypeIndex) {
60
+ throw new Error("Private type index not found!");
61
+ }
62
+ await store.fetcher.load(privateTypeIndex);
63
+ const reg = newThing(privateTypeIndex);
64
+ const ins = [
65
+ st(
66
+ reg,
67
+ ns.rdf("type"),
68
+ ns.solid("TypeRegistration"),
69
+ privateTypeIndex.doc()
70
+ ),
71
+ st(
72
+ reg,
73
+ ns.solid("forClass"),
74
+ ns.meeting("LongChat"),
75
+ privateTypeIndex.doc()
76
+ ),
77
+ st(reg, ns.solid("instance"), chatThing, privateTypeIndex.doc()),
78
+ ];
79
+ await new Promise((resolve, reject) => {
80
+ store.updater.update([], ins, function (_uri, ok, errm) {
81
+ if (!ok) {
82
+ reject(new Error(errm));
83
+ } else {
84
+ resolve(null);
85
+ }
86
+ });
87
+ });
88
+ }
89
+
90
+ async function findChat(invitee: NamedNode): Promise<Chat> {
91
+ const me = await profileLogic.loadMe();
92
+ const podRoot = await profileLogic.getPodRoot(me);
93
+ const chatContainer = determineChatContainer(invitee, podRoot);
94
+ let exists = true;
95
+ try {
96
+ await store.fetcher.load(
97
+ new NamedNode(chatContainer.value + "index.ttl#this")
98
+ );
99
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
100
+ } catch (e) {
101
+ exists = false;
102
+ }
103
+ return { me, chatContainer, exists };
104
+ }
105
+
106
+ async function createChatThing(
107
+ chatContainer: NamedNode,
108
+ me: NamedNode
109
+ ): Promise<NamedNode> {
110
+ const created = await mintNew({
111
+ me,
112
+ newBase: chatContainer.value,
113
+ });
114
+ return created.newInstance;
115
+ }
116
+
117
+ function mintNew(newPaneOptions: NewPaneOptions): Promise<CreatedPaneOptions> {
118
+ const kb = store;
119
+ const updater = kb.updater;
120
+ if (newPaneOptions.me && !newPaneOptions.me.uri) {
121
+ throw new Error("chat mintNew: Invalid userid " + newPaneOptions.me);
122
+ }
123
+
124
+ const newInstance = (newPaneOptions.newInstance =
125
+ newPaneOptions.newInstance ||
126
+ kb.sym(newPaneOptions.newBase + CHAT_LOCATION_IN_CONTAINER));
127
+ const newChatDoc = newInstance.doc();
128
+
129
+ kb.add(
130
+ newInstance,
131
+ ns.rdf("type"),
132
+ ns.meeting("LongChat"),
133
+ newChatDoc
134
+ );
135
+ kb.add(newInstance, ns.dc("title"), "Chat channel", newChatDoc);
136
+ kb.add(
137
+ newInstance,
138
+ ns.dc("created"),
139
+ term<Node>(new Date(Date.now())),
140
+ newChatDoc
141
+ );
142
+ if (newPaneOptions.me) {
143
+ kb.add(newInstance, ns.dc("author"), newPaneOptions.me, newChatDoc);
144
+ }
145
+
146
+ return new Promise(function (resolve, reject) {
147
+ updater?.put(
148
+ newChatDoc,
149
+ kb.statementsMatching(undefined, undefined, undefined, newChatDoc),
150
+ "text/turtle",
151
+ function (uri2, ok, message) {
152
+ if (ok) {
153
+ resolve({
154
+ ...newPaneOptions,
155
+ newInstance,
156
+ });
157
+ } else {
158
+ reject(
159
+ new Error(
160
+ "FAILED to save new chat channel at: " + uri2 + " : " + message
161
+ )
162
+ );
163
+ }
164
+ }
165
+ );
166
+ });
167
+ }
168
+
169
+ /**
170
+ * Find (and optionally create) an individual chat between the current user and the given invitee
171
+ * @param invitee - The person to chat with
172
+ * @param createIfMissing - Whether the chat should be created, if missing
173
+ * @returns null if missing, or a node referring to an already existing chat, or the newly created chat
174
+ */
175
+ async function getChat(
176
+ invitee: NamedNode,
177
+ createIfMissing = true
178
+ ): Promise<NamedNode | null> {
179
+ const { me, chatContainer, exists } = await findChat(invitee);
180
+ if (exists) {
181
+ return new NamedNode(chatContainer.value + CHAT_LOCATION_IN_CONTAINER);
182
+ }
183
+
184
+ if (createIfMissing) {
185
+ const chatThing = await createChatThing(chatContainer, me);
186
+ await sendInvite(invitee, chatThing);
187
+ await setAcl(chatContainer, me, invitee);
188
+ await addToPrivateTypeIndex(chatThing, me);
189
+ return chatThing;
190
+ }
191
+ return null;
192
+ }
193
+
194
+ async function sendInvite(invitee: NamedNode, chatThing: NamedNode) {
195
+ await store.fetcher.load(invitee.doc());
196
+ const inviteeInbox = store.any(
197
+ invitee,
198
+ ns.ldp("inbox"),
199
+ undefined,
200
+ invitee.doc()
201
+ );
202
+ if (!inviteeInbox) {
203
+ throw new Error(`Invitee inbox not found! ${invitee.value}`);
204
+ }
205
+ const inviteBody = `
206
+ <> a <http://www.w3.org/ns/pim/meeting#LongChatInvite> ;
207
+ ${ns.rdf("seeAlso")} <${chatThing.value}> .
208
+ `;
209
+
210
+ const inviteResponse = await store.fetcher?.webOperation(
211
+ "POST",
212
+ inviteeInbox.value,
213
+ {
214
+ data: inviteBody,
215
+ contentType: "text/turtle",
216
+ }
217
+ );
218
+ const locationStr = inviteResponse?.headers.get("location");
219
+ if (!locationStr) {
220
+ throw new Error(`Invite sending returned a ${inviteResponse?.status}`);
221
+ }
222
+ }
223
+ return {
224
+ setAcl, addToPrivateTypeIndex, findChat, createChatThing, getChat, sendInvite, mintNew
225
+ }
226
+ }
@@ -0,0 +1,59 @@
1
+ import { NamedNode } from "rdflib";
2
+ import { InboxLogic } from "../types";
3
+ import { getArchiveUrl } from "../util/utils";
4
+
5
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
6
+ export function createInboxLogic(store, profileLogic, utilityLogic, containerLogic, aclLogic): InboxLogic {
7
+
8
+ async function createInboxFor(peerWebId: string, nick: string) {
9
+ const myWebId: NamedNode = await profileLogic.loadMe();
10
+ const podRoot: NamedNode = await profileLogic.getPodRoot(myWebId);
11
+ const ourInbox = `${podRoot.value}p2p-inboxes/${encodeURIComponent(nick)}/`;
12
+ await containerLogic.createContainer(ourInbox);
13
+ // const aclDocUrl = await aclLogic.findAclDocUrl(ourInbox);
14
+ await utilityLogic.setSinglePeerAccess({
15
+ ownerWebId: myWebId.value,
16
+ peerWebId,
17
+ accessToModes: 'acl:Append',
18
+ target: ourInbox
19
+ });
20
+ return ourInbox;
21
+ }
22
+
23
+ async function getNewMessages(
24
+ user?: NamedNode
25
+ ): Promise<NamedNode[]> {
26
+ if (!user) {
27
+ user = await profileLogic.loadMe();
28
+ }
29
+ const inbox = await profileLogic.getMainInbox(user);
30
+ const urls = await containerLogic.getContainerMembers(inbox);
31
+ return urls.filter(url => !containerLogic.isContainer(url));
32
+ }
33
+
34
+ async function markAsRead(url: string, date: Date) {
35
+ const downloaded = await store.fetcher._fetch(url);
36
+ if (downloaded.status !== 200) {
37
+ throw new Error(`Not OK! ${url}`);
38
+ }
39
+ const archiveUrl = getArchiveUrl(url, date);
40
+ const options = {
41
+ method: 'PUT',
42
+ body: await downloaded.text(),
43
+ headers: [
44
+ ['Content-Type', downloaded.headers.get('Content-Type') || 'application/octet-stream']
45
+ ]
46
+ };
47
+ const uploaded = await store.fetcher._fetch(archiveUrl, options);
48
+ if (uploaded.status.toString()[0] === '2') {
49
+ await store.fetcher._fetch(url, {
50
+ method: 'DELETE'
51
+ });
52
+ }
53
+ }
54
+ return {
55
+ createInboxFor,
56
+ getNewMessages,
57
+ markAsRead
58
+ }
59
+ }
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ // Make these variables directly accessible as it is what you need most of the time
2
+ // This also makes these variable globaly accesible in mashlib
3
+ import { solidLogicSingleton } from './logic/solidLogicSingleton'
4
+
5
+ const authn = solidLogicSingleton.authn
6
+ const authSession = solidLogicSingleton.authn.authSession
7
+ const store = solidLogicSingleton.store
8
+
9
+ export { ACL_LINK } from './acl/aclLogic'
10
+ export { offlineTestID, appContext } from './authn/authUtil'
11
+ export { getSuggestedIssuers } from './issuer/issuerLogic'
12
+ export { createTypeIndexLogic } from './typeIndex/typeIndexLogic'
13
+ export { AppDetails, SolidNamespace, AuthenticationContext, SolidLogic } from './types'
14
+ export { UnauthorizedError, CrossOriginForbiddenError, SameOriginForbiddenError, NotFoundError, FetchError, NotEditableError, WebOperationError } from './logic/CustomError'
15
+
16
+ export {
17
+ solidLogicSingleton, // solidLogicSingleton is exported entirely because it is used in solid-panes
18
+ store,
19
+ authn,
20
+ authSession
21
+ }
22
+
@@ -0,0 +1,40 @@
1
+ const DEFAULT_ISSUERS = [
2
+ {
3
+ name: 'Solid Community',
4
+ uri: 'https://solidcommunity.net'
5
+ },
6
+ {
7
+ name: 'Solid Web',
8
+ uri: 'https://solidweb.org'
9
+ },
10
+ {
11
+ name: 'Solid Web ME',
12
+ uri: 'https://solidweb.me'
13
+ },
14
+ {
15
+ name: 'Inrupt.com',
16
+ uri: 'https://login.inrupt.com'
17
+ }
18
+ ]
19
+
20
+ /**
21
+ * @returns - A list of suggested OIDC issuers
22
+ */
23
+ export function getSuggestedIssuers (): { name: string, uri: string }[] {
24
+ // Suggest a default list of OIDC issuers
25
+ const issuers = [...DEFAULT_ISSUERS]
26
+
27
+ // Suggest the current host if not already included
28
+ const { host, origin } = new URL(location.href)
29
+ const hosts = issuers.map(({ uri }) => new URL(uri).host)
30
+ if (!hosts.includes(host) && !hosts.some(existing => isSubdomainOf(host, existing))) {
31
+ issuers.unshift({ name: host, uri: origin })
32
+ }
33
+
34
+ return issuers
35
+ }
36
+
37
+ function isSubdomainOf (subdomain: string, domain: string): boolean {
38
+ const dot = subdomain.length - domain.length - 1
39
+ return dot > 0 && subdomain[dot] === '.' && subdomain.endsWith(domain)
40
+ }
@@ -0,0 +1,29 @@
1
+ class CustomError extends Error {
2
+ constructor(message?: string) {
3
+ super(message);
4
+ // see: typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html
5
+ Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
6
+ this.name = new.target.name; // stack traces display correctly now
7
+ }
8
+ }
9
+
10
+ export class UnauthorizedError extends CustomError {}
11
+
12
+ export class CrossOriginForbiddenError extends CustomError {}
13
+
14
+ export class SameOriginForbiddenError extends CustomError {}
15
+
16
+ export class NotFoundError extends CustomError {}
17
+
18
+ export class NotEditableError extends CustomError { }
19
+
20
+ export class WebOperationError extends CustomError {}
21
+
22
+ export class FetchError extends CustomError {
23
+ status: number;
24
+
25
+ constructor(status: number, message?: string) {
26
+ super(message);
27
+ this.status = status;
28
+ }
29
+ }
@@ -0,0 +1,76 @@
1
+ import { Session } from "@inrupt/solid-client-authn-browser";
2
+ import * as rdf from "rdflib";
3
+ import { LiveStore, NamedNode, Statement } from "rdflib";
4
+ import { createAclLogic } from "../acl/aclLogic";
5
+ import { SolidAuthnLogic } from "../authn/SolidAuthnLogic";
6
+ import { createChatLogic } from "../chat/chatLogic";
7
+ import { createInboxLogic } from "../inbox/inboxLogic";
8
+ import { createProfileLogic } from "../profile/profileLogic";
9
+ import { createTypeIndexLogic } from "../typeIndex/typeIndexLogic";
10
+ import { createContainerLogic } from "../util/containerLogic";
11
+ import { createUtilityLogic } from "../util/utilityLogic";
12
+ import { AuthnLogic, SolidLogic } from "../types";
13
+ import * as debug from "../util/debug";
14
+ /*
15
+ ** It is important to distinquish `fetch`, a function provided by the browser
16
+ ** and `Fetcher`, a helper object for the rdflib Store which turns it
17
+ ** into a `ConnectedStore` or a `LiveStore`. A Fetcher object is
18
+ ** available at store.fetcher, and `fetch` function at `store.fetcher._fetch`,
19
+ */
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ export function createSolidLogic(specialFetch: { fetch: (url: any, requestInit: any) => any }, session: Session): SolidLogic {
22
+
23
+ debug.log("SolidLogic: Unique instance created. There should only be one of these.")
24
+ const store: LiveStore = rdf.graph() as LiveStore
25
+ rdf.fetcher(store, {fetch: specialFetch.fetch}); // Attach a web I/O module, store.fetcher
26
+ store.updater = new rdf.UpdateManager(store); // Add real-time live updates store.updater
27
+ store.features = [] // disable automatic node merging on store load
28
+
29
+ const authn: AuthnLogic = new SolidAuthnLogic(session)
30
+
31
+ const acl = createAclLogic(store)
32
+ const containerLogic = createContainerLogic(store)
33
+ const utilityLogic = createUtilityLogic(store, acl, containerLogic)
34
+ const profile = createProfileLogic(store, authn, utilityLogic)
35
+ const chat = createChatLogic(store, profile)
36
+ const inbox = createInboxLogic(store, profile, utilityLogic, containerLogic, acl)
37
+ const typeIndex = createTypeIndexLogic(store, authn, profile, utilityLogic)
38
+ debug.log('SolidAuthnLogic initialized')
39
+
40
+ function load(doc: NamedNode | NamedNode[] | string) {
41
+ return store.fetcher.load(doc);
42
+ }
43
+
44
+ // @@@@ use the one in rdflib.js when it is available and delete this
45
+ function updatePromise(
46
+ del: Array<Statement>,
47
+ ins: Array<Statement> = []
48
+ ): Promise<void> {
49
+ return new Promise((resolve, reject) => {
50
+ store.updater.update(del, ins, function (_uri, ok, errorBody) {
51
+ if (!ok) {
52
+ reject(new Error(errorBody));
53
+ } else {
54
+ resolve();
55
+ }
56
+ }); // callback
57
+ }); // promise
58
+ }
59
+
60
+ function clearStore() {
61
+ store.statements.slice().forEach(store.remove.bind(store));
62
+ }
63
+
64
+ return {
65
+ store,
66
+ authn,
67
+ acl,
68
+ inbox,
69
+ chat,
70
+ profile,
71
+ typeIndex,
72
+ load,
73
+ updatePromise,
74
+ clearStore
75
+ }
76
+ }
@@ -0,0 +1,20 @@
1
+ import * as debug from "../util/debug"
2
+ import { authSession } from "../authSession/authSession"
3
+ import { createSolidLogic } from "./solidLogic"
4
+
5
+ const _fetch = async (url, requestInit) => {
6
+ const omitCreds = requestInit && requestInit.credentials && requestInit.credentials == 'omit'
7
+ if (authSession.info.webId && !omitCreds) { // see https://github.com/solidos/solidos/issues/114
8
+ // In fact fetch should respect credentials omit itself
9
+ return authSession.fetch(url, requestInit)
10
+ } else {
11
+ return window.fetch(url, requestInit)
12
+ }
13
+ }
14
+
15
+ //this const makes solidLogicSingleton global accessible in mashlib
16
+ const solidLogicSingleton = createSolidLogic({ fetch: _fetch }, authSession)
17
+
18
+ debug.log('Unique quadstore initialized.')
19
+
20
+ export { solidLogicSingleton }