@streamlayer/sdk-web-anonymous-auth 1.3.3 → 1.3.4
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/lib/dist/index.d.ts +10 -0
- package/lib/dist/index.js +75 -0
- package/package.json +9 -5
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StreamLayerPlugin } from '@streamlayer/sdk-web-interfaces';
|
|
2
|
+
declare module '@streamlayer/sdk-web-interfaces' {
|
|
3
|
+
interface StreamLayerSDK {
|
|
4
|
+
anonymousAuthorization: () => Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export declare const anonymous: StreamLayerPlugin<{
|
|
8
|
+
schemaName?: string;
|
|
9
|
+
issuer?: string;
|
|
10
|
+
}>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { queries } from '@streamlayer/sdk-web-api';
|
|
2
|
+
import { importJWK, EncryptJWT } from 'jose';
|
|
3
|
+
import { storage } from '@streamlayer/sdk-web-core/auth';
|
|
4
|
+
const login = async (instance, opts = {}) => {
|
|
5
|
+
const { schemaName, issuer } = {
|
|
6
|
+
schemaName: 'slra',
|
|
7
|
+
issuer: 'sdk-web',
|
|
8
|
+
...opts,
|
|
9
|
+
};
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
instance.sdk.withAuth = true;
|
|
13
|
+
const organization = await instance.stores.organizationSettings.getValue();
|
|
14
|
+
if (!organization) {
|
|
15
|
+
throw new Error('unknown organization');
|
|
16
|
+
}
|
|
17
|
+
const userKey = `${organization.pub?.kid}`;
|
|
18
|
+
const schema = `${schemaName}:${organization.id}`;
|
|
19
|
+
const anonymous = await queries.bypassAuth(instance.transport, { userKey, schema, init: true });
|
|
20
|
+
const token = anonymous.meta?.token;
|
|
21
|
+
const pubKey = organization.pub;
|
|
22
|
+
const pub = await importJWK({ ...pubKey });
|
|
23
|
+
const deviceId = window.crypto.randomUUID();
|
|
24
|
+
const jwe = await new EncryptJWT({ ['device-id']: deviceId, totp: token })
|
|
25
|
+
.setProtectedHeader({ alg: pubKey.alg || '', enc: 'A256CBC-HS512', kid: pubKey.kid })
|
|
26
|
+
.setIssuedAt()
|
|
27
|
+
.setIssuer(issuer)
|
|
28
|
+
.setAudience(organization.id)
|
|
29
|
+
.setExpirationTime('2m')
|
|
30
|
+
.encrypt(pub);
|
|
31
|
+
await instance.auth.login(schema, jwe);
|
|
32
|
+
storage.setSchema('streamlayer:streamlayer');
|
|
33
|
+
storage.setExternalToken(jwe);
|
|
34
|
+
};
|
|
35
|
+
const reLogin = async (instance, opts = {}) => {
|
|
36
|
+
try {
|
|
37
|
+
const prevUser = await instance.auth.reLogin({ skipLogin: true });
|
|
38
|
+
if (!prevUser) {
|
|
39
|
+
await login(instance, opts).catch((e) => {
|
|
40
|
+
console.error(e, 'fail anon login');
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
console.log(e, 'fail anon re-login');
|
|
46
|
+
await login(instance, opts);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const anonymousAuth = (instance, opts = {}) => {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const timeout = setTimeout(() => {
|
|
52
|
+
reject(new Error('timeout'));
|
|
53
|
+
}, 30000);
|
|
54
|
+
instance.stores.status.subscribe(async (status) => {
|
|
55
|
+
if (status === 'ready') {
|
|
56
|
+
try {
|
|
57
|
+
await reLogin(instance, opts);
|
|
58
|
+
resolve(true);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
reject(e);
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
clearTimeout(timeout);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
export const anonymous = (instance, opts, done) => {
|
|
71
|
+
instance.sdk.anonymousAuthorization = async () => {
|
|
72
|
+
await anonymousAuth(instance, opts);
|
|
73
|
+
};
|
|
74
|
+
done();
|
|
75
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamlayer/sdk-web-anonymous-auth",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.4",
|
|
4
4
|
"author": "StreamLayer, Inc (https://streamlayer.io)",
|
|
5
5
|
"maintainers": [
|
|
6
6
|
{
|
|
@@ -20,12 +20,16 @@
|
|
|
20
20
|
"module": "./lib/es/index.js",
|
|
21
21
|
"require": "./lib/cjs/index.js",
|
|
22
22
|
"types": "./lib/index.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"./dist": {
|
|
25
|
+
"module": "./lib/dist/index.js",
|
|
26
|
+
"types": "./lib/dist/index.d.ts"
|
|
23
27
|
}
|
|
24
28
|
},
|
|
25
29
|
"peerDependencies": {
|
|
26
30
|
"@streamlayer/sl-eslib": "^5.225.0",
|
|
27
31
|
"jose": "^5.9.3",
|
|
28
|
-
"@streamlayer/sdk-web": "^1.16.
|
|
32
|
+
"@streamlayer/sdk-web": "^1.16.4"
|
|
29
33
|
},
|
|
30
34
|
"devDependencies": {
|
|
31
35
|
"@nx/devkit": "20.4.2",
|
|
@@ -40,8 +44,8 @@
|
|
|
40
44
|
"vite-plugin-node-polyfills": "^0.22.0",
|
|
41
45
|
"vite-tsconfig-paths": "^5.0.1",
|
|
42
46
|
"webpack": "^5.95.0",
|
|
43
|
-
"@streamlayer/sdk-web-api": "^1.14.
|
|
44
|
-
"@streamlayer/sdk-web-core": "^1.17.
|
|
45
|
-
"@streamlayer/sdk-web-interfaces": "^1.8.
|
|
47
|
+
"@streamlayer/sdk-web-api": "^1.14.4",
|
|
48
|
+
"@streamlayer/sdk-web-core": "^1.17.4",
|
|
49
|
+
"@streamlayer/sdk-web-interfaces": "^1.8.4"
|
|
46
50
|
}
|
|
47
51
|
}
|