@cloudbase/oauth 0.1.1-alpha.4 → 0.1.1-alpha.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/oauth",
3
- "version": "0.1.1-alpha.3",
3
+ "version": "0.1.1-alpha.4",
4
4
  "description": "auth apis for cloudbase",
5
5
  "author": "xbase",
6
6
  "main": "./dist/index.js",
@@ -1,3 +1,4 @@
1
- export declare const weBtoa: (string: string) => string;
1
+ export declare function weBtoa(string: string): string;
2
2
  export declare const weAtob: (string: string) => string;
3
- export default function weappJwtDecode(token: string, options?: any): any;
3
+ export declare function base64_url_decode(str: string): string;
4
+ export declare function weappJwtDecode(token: string, options?: any): any;
@@ -1,15 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.weAtob = exports.weBtoa = void 0;
3
+ exports.weappJwtDecode = exports.base64_url_decode = exports.weAtob = exports.weBtoa = void 0;
4
4
  // weapp jwt-decode
5
- const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
5
+ const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
6
6
  const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
7
7
  // btoa
8
- const weBtoa = function (string) {
8
+ function weBtoa(string) {
9
9
  string = String(string);
10
- var bitmap, a, b, c, result = '', i = 0, rest = string.length % 3;
10
+ var bitmap, a, b, c, result = "", i = 0, rest = string.length % 3;
11
11
  for (; i < string.length;) {
12
- if ((a = string.charCodeAt(i++)) > 255 || (b = string.charCodeAt(i++)) > 255 || (c = string.charCodeAt(i++)) > 255)
12
+ if ((a = string.charCodeAt(i++)) > 255 ||
13
+ (b = string.charCodeAt(i++)) > 255 ||
14
+ (c = string.charCodeAt(i++)) > 255)
13
15
  throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");
14
16
  bitmap = (a << 16) | (b << 8) | c;
15
17
  result +=
@@ -18,16 +20,16 @@ const weBtoa = function (string) {
18
20
  b64.charAt((bitmap >> 6) & 63) +
19
21
  b64.charAt(bitmap & 63);
20
22
  }
21
- return rest ? result.slice(0, rest - 3) + '==='.substring(rest) : result;
22
- };
23
+ return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
24
+ }
23
25
  exports.weBtoa = weBtoa;
24
26
  // atob
25
27
  const weAtob = function (string) {
26
- string = String(string).replace(/[\t\n\f\r ]+/g, '');
28
+ string = String(string).replace(/[\t\n\f\r ]+/g, "");
27
29
  if (!b64re.test(string))
28
30
  throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
29
- string += '=='.slice(2 - (string.length & 3));
30
- var bitmap, result = '', r1, r2, i = 0;
31
+ string += "==".slice(2 - (string.length & 3));
32
+ var bitmap, result = "", r1, r2, i = 0;
31
33
  for (; i < string.length;) {
32
34
  bitmap =
33
35
  (b64.indexOf(string.charAt(i++)) << 18) |
@@ -44,29 +46,28 @@ const weAtob = function (string) {
44
46
  return result;
45
47
  };
46
48
  exports.weAtob = weAtob;
47
- // @quote https://github.com/auth0/jwt-decode
48
49
  function b64DecodeUnicode(str) {
49
50
  return decodeURIComponent((0, exports.weAtob)(str).replace(/(.)/g, function (p) {
50
51
  var code = p.charCodeAt(0).toString(16).toUpperCase();
51
52
  if (code.length < 2) {
52
- code = '0' + code;
53
+ code = "0" + code;
53
54
  }
54
- return '%' + code;
55
+ return "%" + code;
55
56
  }));
56
57
  }
57
58
  function base64_url_decode(str) {
58
- var output = str.replace(/-/g, '+').replace(/_/g, '/');
59
+ var output = str.replace(/-/g, "+").replace(/_/g, "/");
59
60
  switch (output.length % 4) {
60
61
  case 0:
61
62
  break;
62
63
  case 2:
63
- output += '==';
64
+ output += "==";
64
65
  break;
65
66
  case 3:
66
- output += '=';
67
+ output += "=";
67
68
  break;
68
69
  default:
69
- throw 'Illegal base64url string!';
70
+ throw new Error("Illegal base64url string!");
70
71
  }
71
72
  try {
72
73
  return b64DecodeUnicode(output);
@@ -75,18 +76,19 @@ function base64_url_decode(str) {
75
76
  return (0, exports.weAtob)(output);
76
77
  }
77
78
  }
79
+ exports.base64_url_decode = base64_url_decode;
78
80
  function weappJwtDecode(token, options) {
79
- if (typeof token !== 'string') {
80
- throw 'Invalid token specified';
81
+ if (typeof token !== "string") {
82
+ throw new Error("Invalid token specified");
81
83
  }
82
84
  options = options || {};
83
85
  var pos = options.header === true ? 0 : 1;
84
86
  try {
85
- return JSON.parse(base64_url_decode(token.split('.')[pos]));
87
+ return JSON.parse(base64_url_decode(token.split(".")[pos]));
86
88
  }
87
89
  catch (e) {
88
- throw 'Invalid token specified: ' + e?.message;
90
+ throw new Error("Invalid token specified: " + e ? e.message : "");
89
91
  }
90
92
  }
91
- exports.default = weappJwtDecode;
93
+ exports.weappJwtDecode = weappJwtDecode;
92
94
  //# sourceMappingURL=base64.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"base64.js","sourceRoot":"","sources":["../../src/utils/base64.ts"],"names":[],"mappings":";;;AAAA,mBAAmB;AACnB,MAAM,GAAG,GAAG,mEAAmE,CAAC;AAChF,MAAM,KAAK,GAAG,yEAAyE,CAAC;AAExF,OAAO;AACA,MAAM,MAAM,GAAG,UAAU,MAAc;IAC5C,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IACxB,IAAI,MAAM,EACR,CAAC,EACD,CAAC,EACD,CAAC,EACD,MAAM,GAAG,EAAE,EACX,CAAC,GAAG,CAAC,EACL,IAAI,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;IAE3B,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,GAAI;QAC1B,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,U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CAAC;YACJ,MAAM;QACR,KAAK,CAAC;YACJ,MAAM,IAAI,IAAI,CAAC;YACf,MAAM;QACR,KAAK,CAAC;YACJ,MAAM,IAAI,GAAG,CAAC;YACd,MAAM;QACR;YACE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;KAChD;IAED,IAAI;QACF,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC;KACjC;IAAC,OAAO,GAAG,EAAE;QACZ,OAAO,IAAA,cAAM,EAAC,MAAM,CAAC,CAAC;KACvB;AACH,CAAC;AApBD,8CAoBC;AAED,SAAgB,cAAc,CAAC,KAAa,EAAE,OAAa;IACzD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QAC7B,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;KAC5C;IACD,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;IACxB,IAAI,GAAG,GAAG,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1C,IAAI;QACF,OAAO,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;KAC7D;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,IAAI,KAAK,CAAC,2BAA2B,GAAG,CAAC,CAAC,CAAC,CAAE,CAAS,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;KAC5E;AACH,CAAC;AAXD,wCAWC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/oauth",
3
- "version": "0.1.1-alpha.4",
3
+ "version": "0.1.1-alpha.5",
4
4
  "description": "auth apis for cloudbase",
5
5
  "author": "xbase",
6
6
  "main": "./dist/index.js",
@@ -1,22 +1,26 @@
1
1
  // weapp jwt-decode
2
- const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
2
+ const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
3
3
  const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
4
4
 
5
5
  // btoa
6
- export const weBtoa = function (string: string) {
6
+ export function weBtoa(string: string) {
7
7
  string = String(string);
8
8
  var bitmap,
9
9
  a,
10
10
  b,
11
11
  c,
12
- result = '',
12
+ result = "",
13
13
  i = 0,
14
14
  rest = string.length % 3;
15
15
 
16
16
  for (; i < string.length; ) {
17
- if ((a = string.charCodeAt(i++)) > 255 || (b = string.charCodeAt(i++)) > 255 || (c = string.charCodeAt(i++)) > 255)
17
+ if (
18
+ (a = string.charCodeAt(i++)) > 255 ||
19
+ (b = string.charCodeAt(i++)) > 255 ||
20
+ (c = string.charCodeAt(i++)) > 255
21
+ )
18
22
  throw new TypeError(
19
- "Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.",
23
+ "Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range."
20
24
  );
21
25
 
22
26
  bitmap = (a << 16) | (b << 8) | c;
@@ -27,16 +31,18 @@ export const weBtoa = function (string: string) {
27
31
  b64.charAt(bitmap & 63);
28
32
  }
29
33
 
30
- return rest ? result.slice(0, rest - 3) + '==='.substring(rest) : result;
31
- };
34
+ return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
35
+ }
32
36
  // atob
33
37
  export const weAtob = function (string: string) {
34
- string = String(string).replace(/[\t\n\f\r ]+/g, '');
38
+ string = String(string).replace(/[\t\n\f\r ]+/g, "");
35
39
  if (!b64re.test(string))
36
- throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
37
- string += '=='.slice(2 - (string.length & 3));
40
+ throw new TypeError(
41
+ "Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded."
42
+ );
43
+ string += "==".slice(2 - (string.length & 3));
38
44
  var bitmap,
39
- result = '',
45
+ result = "",
40
46
  r1,
41
47
  r2,
42
48
  i = 0;
@@ -52,37 +58,40 @@ export const weAtob = function (string: string) {
52
58
  ? String.fromCharCode((bitmap >> 16) & 255)
53
59
  : r2 === 64
54
60
  ? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
55
- : String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
61
+ : String.fromCharCode(
62
+ (bitmap >> 16) & 255,
63
+ (bitmap >> 8) & 255,
64
+ bitmap & 255
65
+ );
56
66
  }
57
67
  return result;
58
68
  };
59
69
 
60
- // @quote https://github.com/auth0/jwt-decode
61
70
  function b64DecodeUnicode(str: string) {
62
71
  return decodeURIComponent(
63
72
  weAtob(str).replace(/(.)/g, function (p) {
64
73
  var code = p.charCodeAt(0).toString(16).toUpperCase();
65
74
  if (code.length < 2) {
66
- code = '0' + code;
75
+ code = "0" + code;
67
76
  }
68
- return '%' + code;
69
- }),
77
+ return "%" + code;
78
+ })
70
79
  );
71
80
  }
72
81
 
73
- function base64_url_decode(str: string) {
74
- var output = str.replace(/-/g, '+').replace(/_/g, '/');
82
+ export function base64_url_decode(str: string) {
83
+ var output = str.replace(/-/g, "+").replace(/_/g, "/");
75
84
  switch (output.length % 4) {
76
85
  case 0:
77
86
  break;
78
87
  case 2:
79
- output += '==';
88
+ output += "==";
80
89
  break;
81
90
  case 3:
82
- output += '=';
91
+ output += "=";
83
92
  break;
84
93
  default:
85
- throw 'Illegal base64url string!';
94
+ throw new Error("Illegal base64url string!");
86
95
  }
87
96
 
88
97
  try {
@@ -92,16 +101,15 @@ function base64_url_decode(str: string) {
92
101
  }
93
102
  }
94
103
 
95
- export default function weappJwtDecode(token: string, options?: any) {
96
- if (typeof token !== 'string') {
97
- throw 'Invalid token specified';
104
+ export function weappJwtDecode(token: string, options?: any) {
105
+ if (typeof token !== "string") {
106
+ throw new Error("Invalid token specified");
98
107
  }
99
-
100
108
  options = options || {};
101
109
  var pos = options.header === true ? 0 : 1;
102
110
  try {
103
- return JSON.parse(base64_url_decode(token.split('.')[pos]));
111
+ return JSON.parse(base64_url_decode(token.split(".")[pos]));
104
112
  } catch (e) {
105
- throw 'Invalid token specified: ' + (e as any)?.message;
113
+ throw new Error("Invalid token specified: " + e ? (e as any).message : "");
106
114
  }
107
115
  }