@streamlayer/sdk-web-anonymous-auth 0.1.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/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # sdk-web-anonymous-auth
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build sdk-web-anonymous-auth` to build the library
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@streamlayer/sdk-web-anonymous-auth",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./src/index.js",
6
+ "typings": "./src/index.d.ts",
7
+ "files": [
8
+ "src/"
9
+ ],
10
+ "dependencies": {
11
+ "@nx/devkit": "*",
12
+ "@nx/playwright": "*",
13
+ "@nx/webpack": "*",
14
+ "@playwright/test": "*",
15
+ "@streamlayer/sdk-web": "*",
16
+ "@streamlayer/sdk-web-api": "*",
17
+ "@swc/helpers": "*",
18
+ "jose": "*",
19
+ "webpack": "*"
20
+ },
21
+ "module": "./src/index.js"
22
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { StreamLayerContext } from '@streamlayer/sdk-web';
2
+ declare module '@streamlayer/sdk-web' {
3
+ interface StreamLayerSDK {
4
+ anonymousAuthorization: () => Promise<void>;
5
+ }
6
+ }
7
+ export declare const anonymous: (instance: StreamLayerContext, opts: any, done: any) => void;
package/src/index.js ADDED
@@ -0,0 +1,39 @@
1
+ import { queries } from '@streamlayer/sdk-web-api';
2
+ import { importJWK, EncryptJWT } from 'jose';
3
+ const anonymousAuth = async (instance, opts)=>{
4
+ const { schemaName, issuer } = {
5
+ schemaName: 'slra',
6
+ issuer: 'sdk-web'
7
+ } || opts;
8
+ const organization = await instance.sdk.sdkStore().getStoreItem('organizationSettings').getValue();
9
+ const userKey = `${organization.pub.kid}`;
10
+ const schema = `${schemaName}:${organization.id}`;
11
+ const anonymous = await queries.bypassAuth(instance.sdk.transport, {
12
+ userKey,
13
+ schema,
14
+ init: true
15
+ });
16
+ const token = anonymous.meta.token;
17
+ const pubKey = organization.pub;
18
+ const pub = await importJWK({
19
+ ...pubKey
20
+ });
21
+ const deviceId = window.crypto.randomUUID();
22
+ const jwe = await new EncryptJWT({
23
+ ['device-id']: deviceId,
24
+ totp: token
25
+ }).setProtectedHeader({
26
+ alg: pubKey.alg,
27
+ enc: 'A256CBC-HS512',
28
+ kid: pubKey.kid
29
+ }).setIssuedAt().setIssuer(issuer).setAudience(organization.id).setExpirationTime('2m').encrypt(pub);
30
+ return instance.sdk.authorizationBypass(schema, jwe);
31
+ };
32
+ export const anonymous = (instance, opts, done)=>{
33
+ instance.sdk.anonymousAuthorization = async ()=>{
34
+ await anonymousAuth(instance, opts);
35
+ };
36
+ done();
37
+ };
38
+
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../packages/sdk-web-anonymous-auth/src/index.ts"],"sourcesContent":["import { StreamLayerContext } from '@streamlayer/sdk-web'\nimport { queries } from '@streamlayer/sdk-web-api'\n\nimport { importJWK, JWK, EncryptJWT } from 'jose'\n\ndeclare module '@streamlayer/sdk-web' {\n interface StreamLayerSDK {\n anonymousAuthorization: () => Promise<void>\n }\n}\n\nconst anonymousAuth = async (instance: StreamLayerContext, opts) => {\n const { schemaName, issuer } =\n {\n schemaName: 'slra',\n issuer: 'sdk-web',\n } || opts\n\n const organization = await instance.sdk.sdkStore().getStoreItem('organizationSettings').getValue()\n\n const userKey = `${organization.pub.kid}`\n const schema = `${schemaName}:${organization.id}`\n\n const anonymous = await queries.bypassAuth(instance.sdk.transport, { userKey, schema, init: true })\n const token = anonymous.meta.token\n\n const pubKey = organization.pub as unknown as JWK\n\n const pub = await importJWK({ ...pubKey })\n\n const deviceId = window.crypto.randomUUID()\n const jwe = await new EncryptJWT({ ['device-id']: deviceId, totp: token })\n .setProtectedHeader({ alg: pubKey.alg, enc: 'A256CBC-HS512', kid: pubKey.kid })\n .setIssuedAt()\n .setIssuer(issuer)\n .setAudience(organization.id)\n .setExpirationTime('2m')\n .encrypt(pub)\n\n return instance.sdk.authorizationBypass(schema, jwe)\n}\n\nexport const anonymous = (instance: StreamLayerContext, opts, done) => {\n instance.sdk.anonymousAuthorization = async () => {\n await anonymousAuth(instance, opts)\n }\n\n done()\n}\n"],"names":["queries","importJWK","EncryptJWT","anonymousAuth","instance","opts","schemaName","issuer","organization","sdk","sdkStore","getStoreItem","getValue","userKey","pub","kid","schema","id","anonymous","bypassAuth","transport","init","token","meta","pubKey","deviceId","window","crypto","randomUUID","jwe","totp","setProtectedHeader","alg","enc","setIssuedAt","setIssuer","setAudience","setExpirationTime","encrypt","authorizationBypass","done","anonymousAuthorization"],"mappings":"AACA,SAASA,OAAO,QAAQ,2BAA0B;AAElD,SAASC,SAAS,EAAOC,UAAU,QAAQ,OAAM;AAQjD,MAAMC,gBAAgB,OAAOC,UAA8BC;IACzD,MAAM,EAAEC,UAAU,EAAEC,MAAM,EAAE,GAC1B;QACED,YAAY;QACZC,QAAQ;IACV,KAAKF;IAEP,MAAMG,eAAe,MAAMJ,SAASK,GAAG,CAACC,QAAQ,GAAGC,YAAY,CAAC,wBAAwBC,QAAQ;IAEhG,MAAMC,UAAU,CAAC,EAAEL,aAAaM,GAAG,CAACC,GAAG,CAAC,CAAC;IACzC,MAAMC,SAAS,CAAC,EAAEV,WAAW,CAAC,EAAEE,aAAaS,EAAE,CAAC,CAAC;IAEjD,MAAMC,YAAY,MAAMlB,QAAQmB,UAAU,CAACf,SAASK,GAAG,CAACW,SAAS,EAAE;QAAEP;QAASG;QAAQK,MAAM;IAAK;IACjG,MAAMC,QAAQJ,UAAUK,IAAI,CAACD,KAAK;IAElC,MAAME,SAAShB,aAAaM,GAAG;IAE/B,MAAMA,MAAM,MAAMb,UAAU;QAAE,GAAGuB,MAAM;IAAC;IAExC,MAAMC,WAAWC,OAAOC,MAAM,CAACC,UAAU;IACzC,MAAMC,MAAM,MAAM,IAAI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