@knowlearning/agents 0.9.159 → 0.9.160
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
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
const AUTH_HOST = 'https://auth.knowlearning.systems'
|
|
2
|
-
|
|
2
|
+
const CORE_AUTH_SERVICE_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
|
|
3
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA59Uz6jvBJF3B8/7xMqGo
|
|
4
|
+
XkIhLFvTCHuFIGuCNNZGCJUnSk2ne6Jp1ehUIarliJwzrvfr2HMe0PvzAJyZqQIs
|
|
5
|
+
uz0Lt867TTojCAKJunxbcrwEhzvz0FNjNu1wpgkSHFvd1uTvRSZqauqUmG0HqC17
|
|
6
|
+
HSmBaXivB49B/pviowVJc+mUJJ9MROtOiL4JN5niHnLbt6QVi6NITAJkOwtoRhck
|
|
7
|
+
5j0KLvfrq18R8QrfDOq3v5hWlrA6j1wPvTW1mzFk8MrOZw935mMDdMivFAm/DltM
|
|
8
|
+
NT5I3YnLZpcl1e/fydC+B6zSz2nZfLb2iDBbADDVj2+i9JUEFomg6ng1DjHUGMYc
|
|
9
|
+
ZQIDAQAB
|
|
10
|
+
-----END PUBLIC KEY-----
|
|
11
|
+
`
|
|
3
12
|
// auth token info is sent with pathnames of form /auth/VERIFICATION_STATE/PROVIDER_TOKEN
|
|
4
13
|
if (window.location.pathname.startsWith('/auth/')) {
|
|
5
14
|
const state_token = window.location.pathname.slice(6)
|
|
@@ -12,11 +21,19 @@ if (window.location.pathname.startsWith('/auth/')) {
|
|
|
12
21
|
}
|
|
13
22
|
}
|
|
14
23
|
|
|
15
|
-
function login(provider='google') {
|
|
24
|
+
async function login(provider='google', code) {
|
|
16
25
|
const state = Math.random().toString(36).substring(2)
|
|
17
26
|
window.localStorage.setItem(state, window.location.href)
|
|
18
27
|
|
|
19
|
-
|
|
28
|
+
if (provider === 'code') {
|
|
29
|
+
const tokenContents = { code, provider, domain: window.location.host }
|
|
30
|
+
const token = await encryptString(CORE_AUTH_SERVICE_PUBLIC_KEY, JSON.stringify(tokenContents))
|
|
31
|
+
window.location.href = `/auth/${state}/${token}`
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const redirect = encodeURIComponent(window.location.href)
|
|
35
|
+
window.location.href = `${AUTH_HOST}/${provider}/${state}/${redirect}`
|
|
36
|
+
}
|
|
20
37
|
}
|
|
21
38
|
|
|
22
39
|
function logout() {
|
|
@@ -31,6 +48,57 @@ async function getToken() {
|
|
|
31
48
|
return token
|
|
32
49
|
}
|
|
33
50
|
|
|
51
|
+
|
|
52
|
+
async function encryptString(publicKeyPem, plainText) {
|
|
53
|
+
const publicKey = await crypto.subtle.importKey(
|
|
54
|
+
'spki',
|
|
55
|
+
pemToArrayBuffer(publicKeyPem),
|
|
56
|
+
{ name: 'RSA-OAEP', hash: 'SHA-256' },
|
|
57
|
+
true,
|
|
58
|
+
['encrypt']
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
const symmetricKey = await crypto.subtle.generateKey(
|
|
62
|
+
{ name: 'AES-GCM', length: 256 },
|
|
63
|
+
true,
|
|
64
|
+
['encrypt', 'decrypt']
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const encodedPlainText = new TextEncoder().encode(plainText);
|
|
68
|
+
const iv = crypto.getRandomValues(new Uint8Array(12))
|
|
69
|
+
const encryptedData = await crypto.subtle.encrypt(
|
|
70
|
+
{ name: 'AES-GCM', iv },
|
|
71
|
+
symmetricKey,
|
|
72
|
+
encodedPlainText
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
const exportedSymmetricKey = await crypto.subtle.exportKey('raw', symmetricKey)
|
|
76
|
+
|
|
77
|
+
const encryptedSymmetricKey = await crypto.subtle.encrypt(
|
|
78
|
+
{ name: 'RSA-OAEP' },
|
|
79
|
+
publicKey,
|
|
80
|
+
exportedSymmetricKey
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
const serialized = [
|
|
84
|
+
encodeURIComponent(btoa(String.fromCharCode(...new Uint8Array(iv)))),
|
|
85
|
+
encodeURIComponent(btoa(String.fromCharCode(...new Uint8Array(encryptedData)))),
|
|
86
|
+
encodeURIComponent(btoa(String.fromCharCode(...new Uint8Array(encryptedSymmetricKey))))
|
|
87
|
+
].join(',')
|
|
88
|
+
|
|
89
|
+
return serialized
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function pemToArrayBuffer(pem) {
|
|
93
|
+
const b64 = pem.replace(/-----[^-]+-----/g, '').replace(/\s/g, '')
|
|
94
|
+
const binary = atob(b64)
|
|
95
|
+
const array = new Uint8Array(binary.length)
|
|
96
|
+
for (let i = 0; i < binary.length; i++) {
|
|
97
|
+
array[i] = binary.charCodeAt(i)
|
|
98
|
+
}
|
|
99
|
+
return array.buffer
|
|
100
|
+
}
|
|
101
|
+
|
|
34
102
|
export {
|
|
35
103
|
getToken,
|
|
36
104
|
login,
|
|
@@ -73,8 +73,13 @@ export default function EmbeddedAgent() {
|
|
|
73
73
|
}
|
|
74
74
|
})
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
let variables
|
|
77
|
+
|
|
78
|
+
async function environment(user) {
|
|
79
|
+
const response = await send({ type: 'environment', user })
|
|
80
|
+
// keep copy on initialize symantics for environment variables
|
|
81
|
+
if (!variables) variables = response.variables
|
|
82
|
+
return { ...response, variables }
|
|
78
83
|
}
|
|
79
84
|
|
|
80
85
|
function create({ id=uuid(), active_type, active }) {
|
|
@@ -81,7 +81,7 @@ function embed(environment, iframe) {
|
|
|
81
81
|
}
|
|
82
82
|
else if (type === 'environment') {
|
|
83
83
|
const { user } = message
|
|
84
|
-
const { mode, variables } = environment
|
|
84
|
+
const { mode, variables={} } = environment
|
|
85
85
|
|
|
86
86
|
const env = await (listeners.environment ? listeners.environment(user) : Agent.environment(user))
|
|
87
87
|
|
|
@@ -93,7 +93,7 @@ function embed(environment, iframe) {
|
|
|
93
93
|
],
|
|
94
94
|
variables: {
|
|
95
95
|
...(env.variables || {}),
|
|
96
|
-
...
|
|
96
|
+
...variables
|
|
97
97
|
},
|
|
98
98
|
mode // TODO: deprecate
|
|
99
99
|
})
|