@knowlearning/agents 0.5.14 → 0.6.1

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.
@@ -0,0 +1,32 @@
1
+ const AUTH_HOST = 'https://auth.knowlearning.systems'
2
+
3
+ function login() {
4
+ const provider = 'google'
5
+ const state = Math.random().toString(36).substring(2)
6
+
7
+ window.location.href = `${AUTH_HOST}/${provider}/${state}/${encodeURIComponent(window.location.href)}`
8
+ }
9
+
10
+ function logout() {
11
+ localStorage.removeItem('token')
12
+ window.location.reload()
13
+ }
14
+
15
+ async function getToken() {
16
+ const token = localStorage.getItem('token')
17
+ if (token) return token
18
+ else {
19
+ const randArr = new Uint32Array(16)
20
+ crypto.getRandomValues(randArr)
21
+ const anonymousCredential = [...randArr].map(x => x.toString(16).padStart(2, '0')).join('')
22
+ localStorage.setItem('token', anonymousCredential)
23
+ return anonymousCredential
24
+ }
25
+ }
26
+
27
+ export {
28
+ getToken,
29
+ login,
30
+ logout
31
+ }
32
+
@@ -38,6 +38,9 @@ export default function embed(environment, iframe) {
38
38
  console.error(message)
39
39
  sendDown({})
40
40
  }
41
+ else if (type === 'close') {
42
+ if (listeners.close) listeners.close()
43
+ }
41
44
  else if (type === 'environment') {
42
45
  sendDown(await Agent.environment())
43
46
  }
@@ -131,7 +134,12 @@ export default function embed(environment, iframe) {
131
134
  listeners[event] = fn
132
135
  }
133
136
 
137
+ function auth(token) {
138
+ postMessage({ type: 'auth', token })
139
+ }
140
+
134
141
  return {
142
+ auth,
135
143
  remove,
136
144
  on
137
145
  }
@@ -32,7 +32,13 @@ export default function EmbeddedAgent() {
32
32
  }
33
33
 
34
34
  addEventListener('message', async ({ data }) => {
35
- if (data.type === 'setup') resolveSession(data.session)
35
+ console.log('embedded message', data)
36
+ if (data.type === 'auth') {
37
+ // TODO: switch to access_token
38
+ localStorage.setItem('token', data.token)
39
+ send({ type: 'close' })
40
+ }
41
+ else if (data.type === 'setup') resolveSession(data.session)
36
42
  else if (responses[data.requestId]) {
37
43
  const { resolve, reject } = responses[data.requestId]
38
44
  if (data.error) reject(data.error)
@@ -1,6 +1,6 @@
1
1
  import { v1 as uuid } from 'uuid'
2
2
  import { applyPatch } from 'fast-json-patch'
3
- import { onAuth, login, logout } from './firebase-auth.js'
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: () => getIdTokenPromise.then(f => f(true)),
17
+ token: getToken,
20
18
  WebSocket,
21
19
  uuid,
22
20
  fetch,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.5.14",
3
+ "version": "0.6.1",
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 }