@knowlearning/agents 0.6.0 → 0.6.2

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.
@@ -1,73 +1,26 @@
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
- }
1
+ const AUTH_HOST = 'https://auth.knowlearning.systems'
28
2
 
29
3
  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
- )
4
+ const provider = 'google'
5
+ const state = Math.random().toString(36).substring(2)
6
+ localStorage.setItem('state', state)
51
7
 
52
- window.location.href = `${authEndpoint}?${queryString}`
8
+ window.location.href = `${AUTH_HOST}/${provider}/${state}/${encodeURIComponent(window.location.href)}`
53
9
  }
54
10
 
55
11
  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()
12
+ localStorage.removeItem('token')
13
+ window.location.reload()
61
14
  }
62
15
 
63
16
  async function getToken() {
64
- const token = localStorage.getItem('id_token')
17
+ const token = localStorage.getItem('token')
65
18
  if (token) return token
66
19
  else {
67
20
  const randArr = new Uint32Array(16)
68
21
  crypto.getRandomValues(randArr)
69
22
  const anonymousCredential = [...randArr].map(x => x.toString(16).padStart(2, '0')).join('')
70
- localStorage.setItem('id_token', anonymousCredential)
23
+ localStorage.setItem('token', anonymousCredential)
71
24
  return anonymousCredential
72
25
  }
73
26
  }
@@ -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, state) {
138
+ postMessage({ type: 'auth', token, state })
139
+ }
140
+
134
141
  return {
142
+ auth,
135
143
  remove,
136
144
  on
137
145
  }
@@ -32,7 +32,14 @@ export default function EmbeddedAgent() {
32
32
  }
33
33
 
34
34
  addEventListener('message', async ({ data }) => {
35
- if (data.type === 'setup') resolveSession(data.session)
35
+ if (data.type === 'auth') {
36
+ // TODO: switch to access_token
37
+ if (localStorage.getItem('state') === data.state) {
38
+ localStorage.setItem('token', data.token)
39
+ }
40
+ send({ type: 'close' })
41
+ }
42
+ else if (data.type === 'setup') resolveSession(data.session)
36
43
  else if (responses[data.requestId]) {
37
44
  const { resolve, reject } = responses[data.requestId]
38
45
  if (data.error) reject(data.error)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",