@hautechai/sdk 0.0.14 → 0.0.15

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/dist/sdk/index.js CHANGED
@@ -3,7 +3,7 @@ import balances from './balances';
3
3
  import collections from './collections';
4
4
  import groups from './groups';
5
5
  import images from './images';
6
- import { jwtDecode } from 'jwt-decode';
6
+ import { decodeJwt } from 'jose';
7
7
  import operations from './operations';
8
8
  import pipelines from './pipelines';
9
9
  import stacks from './stacks';
@@ -13,7 +13,7 @@ export const createSDK = (options) => {
13
13
  let token = undefined;
14
14
  const authToken = async () => {
15
15
  if (token) {
16
- const decoded = jwtDecode(token);
16
+ const decoded = decodeJwt(token);
17
17
  const currentTime = Math.floor(Date.now() / 1000);
18
18
  if (decoded.exp && decoded.exp > currentTime)
19
19
  return token;
@@ -8,8 +8,8 @@ export declare const createTokenSigner: (options: {
8
8
  accountId: string;
9
9
  expiresInSeconds: number;
10
10
  permissions?: Partial<MethodsPermissions>;
11
- }) => any;
11
+ }) => Promise<string>;
12
12
  createRootToken: (props: {
13
13
  expiresInSeconds: number;
14
- }) => any;
14
+ }) => Promise<string>;
15
15
  };
@@ -1,4 +1,5 @@
1
1
  import { defaultPermissions, defaultRootPermissions } from './permissions';
2
+ import * as jose from 'jose';
2
3
  const createPrivateKey = (key) => {
3
4
  const header = `-----BEGIN PRIVATE KEY-----\n`;
4
5
  const footer = `\n-----END PRIVATE KEY-----`;
@@ -6,15 +7,15 @@ const createPrivateKey = (key) => {
6
7
  const keyBody = key.match(/.{1,64}/g).join('\n');
7
8
  return header + keyBody + footer;
8
9
  };
9
- // This function will not work in the browser
10
- const createToken = (props) => {
11
- const jwt = require('jsonwebtoken');
12
- const privateKey = createPrivateKey(props.appKeySecret);
13
- return jwt.sign(props.payload, privateKey, {
14
- algorithm: 'RS256',
15
- expiresIn: props.expiresInSeconds,
16
- keyid: props.appKeyId,
17
- });
10
+ const createToken = async (props) => {
11
+ const alg = 'RS256';
12
+ const pkcs8 = createPrivateKey(props.appKeySecret);
13
+ const privateKey = await jose.importPKCS8(pkcs8, alg);
14
+ return await new jose.SignJWT(props.payload)
15
+ .setIssuedAt()
16
+ .setExpirationTime(`${props.expiresInSeconds}s`)
17
+ .setProtectedHeader({ alg, kid: props.appKeyId })
18
+ .sign(privateKey);
18
19
  };
19
20
  const serializePermissions = (permissions) => {
20
21
  const result = [];
@@ -32,7 +33,7 @@ const serializePermissions = (permissions) => {
32
33
  return result;
33
34
  };
34
35
  export const createTokenSigner = (options) => ({
35
- createAccountToken: (props) => createToken({
36
+ createAccountToken: async (props) => createToken({
36
37
  appKeyId: options.appKeyId,
37
38
  appKeySecret: options.appKeySecret,
38
39
  expiresInSeconds: props.expiresInSeconds,
@@ -42,7 +43,7 @@ export const createTokenSigner = (options) => ({
42
43
  sub: props.accountId,
43
44
  },
44
45
  }),
45
- createRootToken: (props) => createToken({
46
+ createRootToken: async (props) => createToken({
46
47
  appKeyId: options.appKeyId,
47
48
  appKeySecret: options.appKeySecret,
48
49
  expiresInSeconds: props.expiresInSeconds ?? 3600,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hautechai/sdk",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "license": "MIT",
5
5
  "keywords": [],
6
6
  "repository": {
@@ -19,8 +19,7 @@
19
19
  "dependencies": {
20
20
  "@hautechai/pipelines": "0.1.0",
21
21
  "axios": "1.6.1",
22
- "jsonwebtoken": "8.5.1",
23
- "jwt-decode": "4.0.0",
22
+ "jose": "5.9.6",
24
23
  "pusher-js": "8.4.0-rc2"
25
24
  },
26
25
  "devDependencies": {
package/jest.setup.js DELETED
@@ -1,5 +0,0 @@
1
- const originalError = console.error;
2
-
3
- console.error = (...args) => {
4
- originalError(...args);
5
- };