@knowlearning/agents 0.5.13 → 0.6.0
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.
- package/agents/browser/auth.js +80 -0
- package/agents/browser/root.js +2 -4
- package/agents/generic.js +1 -1
- package/package.json +1 -4
- package/agents/browser/firebase-auth.js +0 -60
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const client_id = '603559646501-i29ma3u7c590ijimgp809vpdhg52jibd.apps.googleusercontent.com'
|
|
2
|
+
const redirect_uri = 'https://localhost:5173/callback'
|
|
3
|
+
const authEndpoint = 'https://accounts.google.com/o/oauth2/auth'
|
|
4
|
+
const tokenEndpoint = 'https://oauth2.googleapis.com/token'
|
|
5
|
+
|
|
6
|
+
if (window.location.pathname === '/callback') {
|
|
7
|
+
const hashParams = parseHash(window.location.hash)
|
|
8
|
+
|
|
9
|
+
if (hashParams.access_token && hashParams.id_token) {
|
|
10
|
+
localStorage.setItem('access_token', hashParams.access_token)
|
|
11
|
+
localStorage.setItem('id_token', hashParams.id_token)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
window.location = '/'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
// Functions
|
|
19
|
+
function parseHash(hash) {
|
|
20
|
+
const params = hash.substr(1).split('&')
|
|
21
|
+
const result = {}
|
|
22
|
+
params.forEach(param => {
|
|
23
|
+
const parts = param.split('=')
|
|
24
|
+
result[parts[0]] = parts[1]
|
|
25
|
+
})
|
|
26
|
+
return result
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function login() {
|
|
30
|
+
const nonce = Math.random().toString(36).substring(2)
|
|
31
|
+
const state = Math.random().toString(36).substring(2)
|
|
32
|
+
localStorage.setItem('oidc_nonce', nonce)
|
|
33
|
+
localStorage.setItem('oidc_state', state)
|
|
34
|
+
|
|
35
|
+
const query = {
|
|
36
|
+
client_id,
|
|
37
|
+
redirect_uri,
|
|
38
|
+
prompt: 'select_account',
|
|
39
|
+
response_type: "id_token token",
|
|
40
|
+
scope: "openid profile",
|
|
41
|
+
nonce,
|
|
42
|
+
state
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const queryString = (
|
|
46
|
+
Object
|
|
47
|
+
.entries(query)
|
|
48
|
+
.map(([k,v]) => `${k}=${v}`)
|
|
49
|
+
.join('&')
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
window.location.href = `${authEndpoint}?${queryString}`
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function logout() {
|
|
56
|
+
localStorage.removeItem('access_token')
|
|
57
|
+
localStorage.removeItem('id_token')
|
|
58
|
+
localStorage.removeItem('oidc_nonce')
|
|
59
|
+
localStorage.removeItem('oidc_state')
|
|
60
|
+
window.location.reload()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function getToken() {
|
|
64
|
+
const token = localStorage.getItem('id_token')
|
|
65
|
+
if (token) return token
|
|
66
|
+
else {
|
|
67
|
+
const randArr = new Uint32Array(16)
|
|
68
|
+
crypto.getRandomValues(randArr)
|
|
69
|
+
const anonymousCredential = [...randArr].map(x => x.toString(16).padStart(2, '0')).join('')
|
|
70
|
+
localStorage.setItem('id_token', anonymousCredential)
|
|
71
|
+
return anonymousCredential
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export {
|
|
76
|
+
getToken,
|
|
77
|
+
login,
|
|
78
|
+
logout
|
|
79
|
+
}
|
|
80
|
+
|
package/agents/browser/root.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { v1 as uuid } from 'uuid'
|
|
2
2
|
import { applyPatch } from 'fast-json-patch'
|
|
3
|
-
import {
|
|
3
|
+
import { getToken, login, logout } from './auth.js'
|
|
4
4
|
import GenericAgent from '../generic.js'
|
|
5
5
|
|
|
6
6
|
const DEVELOPMENT_HOST = 'localhost:32001'
|
|
@@ -8,15 +8,13 @@ const REMOTE_HOST = 'api.knowlearning.systems'
|
|
|
8
8
|
|
|
9
9
|
function isLocal() { return localStorage.getItem('api') === 'local' }
|
|
10
10
|
|
|
11
|
-
const getIdTokenPromise = new Promise(r => onAuth(({ getIdToken }) => r(getIdToken)))
|
|
12
|
-
|
|
13
11
|
export default () => {
|
|
14
12
|
const { host, protocol } = window.location
|
|
15
13
|
|
|
16
14
|
const agent = GenericAgent({
|
|
17
15
|
host: isLocal() ? DEVELOPMENT_HOST : REMOTE_HOST,
|
|
18
16
|
protocol: protocol === 'https:' ? 'wss' : 'ws',
|
|
19
|
-
token:
|
|
17
|
+
token: getToken,
|
|
20
18
|
WebSocket,
|
|
21
19
|
uuid,
|
|
22
20
|
fetch,
|
package/agents/generic.js
CHANGED
|
@@ -178,7 +178,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
178
178
|
|
|
179
179
|
if (states[message.scope].active === undefined) states[message.scope].active = {}
|
|
180
180
|
applyPatch(states[message.scope], standardJSONPatch(message.patch.slice(lastResetPatchIndex + 1)))
|
|
181
|
-
watchers[message.scope].forEach(fn => fn({ ...message, state: states[message.scope] }))
|
|
181
|
+
watchers[message.scope].forEach(fn => fn({ ...message, state: states[message.scope].active }))
|
|
182
182
|
if (sub) sub.ii = message.ii
|
|
183
183
|
}
|
|
184
184
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knowlearning/agents",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "API for embedding applications in KnowLearning systems.",
|
|
5
5
|
"main": "node.js",
|
|
6
6
|
"browser": "browser.js",
|
|
@@ -23,10 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://github.com/knowlearning/core#readme",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@firebase/app": "^0.9.14",
|
|
27
|
-
"@firebase/auth": "^1.0.0",
|
|
28
26
|
"fast-json-patch": "^3.1.1",
|
|
29
|
-
"node-fetch": "^3.3.1",
|
|
30
27
|
"uuid": "^8.3.2"
|
|
31
28
|
}
|
|
32
29
|
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { initializeApp } from '@firebase/app'
|
|
2
|
-
import {
|
|
3
|
-
getAuth,
|
|
4
|
-
signOut as firebaseSignOut,
|
|
5
|
-
GoogleAuthProvider,
|
|
6
|
-
OAuthProvider,
|
|
7
|
-
onAuthStateChanged,
|
|
8
|
-
signInWithRedirect,
|
|
9
|
-
signInWithEmailAndPassword,
|
|
10
|
-
signInAnonymously
|
|
11
|
-
} from '@firebase/auth'
|
|
12
|
-
|
|
13
|
-
initializeApp({
|
|
14
|
-
"apiKey": "AIzaSyAxjYuF-2JmXxlXnGgNu2CO4Q41EAtUgrY",
|
|
15
|
-
"authDomain": "opensourcelearningplatform.firebaseapp.com",
|
|
16
|
-
"databaseURL": "https://opensourcelearningplatform.firebaseio.com",
|
|
17
|
-
"projectId": "opensourcelearningplatform",
|
|
18
|
-
"storageBucket": "opensourcelearningplatform.appspot.com",
|
|
19
|
-
"messagingSenderId": "831020253582",
|
|
20
|
-
"appId: 1:831020253582": "web:6e1dfc6e89f5c2107164df",
|
|
21
|
-
"measurementId": "G-8QD6034RJ0"
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
let currentUser
|
|
25
|
-
const authClient = getAuth()
|
|
26
|
-
const onAuth = fn => onAuthStateChanged(authClient, user => {
|
|
27
|
-
if (!user) signInAnonymously(authClient)
|
|
28
|
-
else if (user.uid !== currentUser) {
|
|
29
|
-
const { uid } = user
|
|
30
|
-
currentUser = uid
|
|
31
|
-
const provider = user.isAnonymous ? 'anonymous' : user.providerData[0].providerId
|
|
32
|
-
fn({ user: uid, provider, getIdToken: () => user.getIdToken() })
|
|
33
|
-
}
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
const authProviders = {
|
|
37
|
-
google: () => new GoogleAuthProvider(),
|
|
38
|
-
microsoft: () => new OAuthProvider('microsoft.com')
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const login = async (provider, username, password) => {
|
|
42
|
-
if (authProviders[provider]) {
|
|
43
|
-
signInWithRedirect(authClient, authProviders[provider]())
|
|
44
|
-
return { success: true }
|
|
45
|
-
}
|
|
46
|
-
else if (provider === 'email') {
|
|
47
|
-
try {
|
|
48
|
-
await signInWithEmailAndPassword(authClient, username, password)
|
|
49
|
-
location.reload()
|
|
50
|
-
}
|
|
51
|
-
catch (error) { return { success: false } }
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const logout = async () => {
|
|
56
|
-
await firebaseSignOut(authClient)
|
|
57
|
-
location.reload()
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export { onAuth, login, logout }
|