@pagopa/io-react-native-jwt 0.4.1

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.
Files changed (113) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +145 -0
  3. package/android/build.gradle +97 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +4 -0
  6. package/android/src/main/java/com/pagopa/ioreactnativejwt/IoReactNativeJwtModule.kt +114 -0
  7. package/android/src/main/java/com/pagopa/ioreactnativejwt/IoReactNativeJwtPackage.kt +17 -0
  8. package/android/src/main/java/com/pagopa/ioreactnativejwt/Utils.kt +95 -0
  9. package/ios/ASN1DERParsing.swift +226 -0
  10. package/ios/IoReactNativeJwt-Bridging-Header.h +2 -0
  11. package/ios/IoReactNativeJwt.mm +28 -0
  12. package/ios/IoReactNativeJwt.swift +156 -0
  13. package/ios/IoReactNativeJwt.xcodeproj/project.pbxproj +283 -0
  14. package/lib/commonjs/algorithms.js +39 -0
  15. package/lib/commonjs/algorithms.js.map +1 -0
  16. package/lib/commonjs/hash.js +17 -0
  17. package/lib/commonjs/hash.js.map +1 -0
  18. package/lib/commonjs/index.js +194 -0
  19. package/lib/commonjs/index.js.map +1 -0
  20. package/lib/commonjs/jwt_claims_set.js +112 -0
  21. package/lib/commonjs/jwt_claims_set.js.map +1 -0
  22. package/lib/commonjs/produce.js +140 -0
  23. package/lib/commonjs/produce.js.map +1 -0
  24. package/lib/commonjs/sign.js +159 -0
  25. package/lib/commonjs/sign.js.map +1 -0
  26. package/lib/commonjs/types.js +2 -0
  27. package/lib/commonjs/types.js.map +1 -0
  28. package/lib/commonjs/unsecured.js +118 -0
  29. package/lib/commonjs/unsecured.js.map +1 -0
  30. package/lib/commonjs/utils/asn1.js +37 -0
  31. package/lib/commonjs/utils/asn1.js.map +1 -0
  32. package/lib/commonjs/utils/base64.js +43 -0
  33. package/lib/commonjs/utils/base64.js.map +1 -0
  34. package/lib/commonjs/utils/errors.js +214 -0
  35. package/lib/commonjs/utils/errors.js.map +1 -0
  36. package/lib/commonjs/utils/is_object.js +23 -0
  37. package/lib/commonjs/utils/is_object.js.map +1 -0
  38. package/lib/commonjs/utils/proxy.js +17 -0
  39. package/lib/commonjs/utils/proxy.js.map +1 -0
  40. package/lib/commonjs/utils/secs.js +61 -0
  41. package/lib/commonjs/utils/secs.js.map +1 -0
  42. package/lib/module/algorithms.js +29 -0
  43. package/lib/module/algorithms.js.map +1 -0
  44. package/lib/module/hash.js +11 -0
  45. package/lib/module/hash.js.map +1 -0
  46. package/lib/module/index.js +115 -0
  47. package/lib/module/index.js.map +1 -0
  48. package/lib/module/jwt_claims_set.js +103 -0
  49. package/lib/module/jwt_claims_set.js.map +1 -0
  50. package/lib/module/produce.js +131 -0
  51. package/lib/module/produce.js.map +1 -0
  52. package/lib/module/sign.js +153 -0
  53. package/lib/module/sign.js.map +1 -0
  54. package/lib/module/types.js +2 -0
  55. package/lib/module/types.js.map +1 -0
  56. package/lib/module/unsecured.js +110 -0
  57. package/lib/module/unsecured.js.map +1 -0
  58. package/lib/module/utils/asn1.js +31 -0
  59. package/lib/module/utils/asn1.js.map +1 -0
  60. package/lib/module/utils/base64.js +36 -0
  61. package/lib/module/utils/base64.js.map +1 -0
  62. package/lib/module/utils/errors.js +200 -0
  63. package/lib/module/utils/errors.js.map +1 -0
  64. package/lib/module/utils/is_object.js +17 -0
  65. package/lib/module/utils/is_object.js.map +1 -0
  66. package/lib/module/utils/proxy.js +10 -0
  67. package/lib/module/utils/proxy.js.map +1 -0
  68. package/lib/module/utils/secs.js +53 -0
  69. package/lib/module/utils/secs.js.map +1 -0
  70. package/lib/typescript/algorithms.d.ts +5 -0
  71. package/lib/typescript/algorithms.d.ts.map +1 -0
  72. package/lib/typescript/hash.d.ts +5 -0
  73. package/lib/typescript/hash.d.ts.map +1 -0
  74. package/lib/typescript/index.d.ts +93 -0
  75. package/lib/typescript/index.d.ts.map +1 -0
  76. package/lib/typescript/jwt_claims_set.d.ts +4 -0
  77. package/lib/typescript/jwt_claims_set.d.ts.map +1 -0
  78. package/lib/typescript/produce.d.ts +55 -0
  79. package/lib/typescript/produce.d.ts.map +1 -0
  80. package/lib/typescript/sign.d.ts +61 -0
  81. package/lib/typescript/sign.d.ts.map +1 -0
  82. package/lib/typescript/types.d.ts +172 -0
  83. package/lib/typescript/types.d.ts.map +1 -0
  84. package/lib/typescript/unsecured.d.ts +64 -0
  85. package/lib/typescript/unsecured.d.ts.map +1 -0
  86. package/lib/typescript/utils/asn1.d.ts +2 -0
  87. package/lib/typescript/utils/asn1.d.ts.map +1 -0
  88. package/lib/typescript/utils/base64.d.ts +4 -0
  89. package/lib/typescript/utils/base64.d.ts.map +1 -0
  90. package/lib/typescript/utils/errors.d.ts +157 -0
  91. package/lib/typescript/utils/errors.d.ts.map +1 -0
  92. package/lib/typescript/utils/is_object.d.ts +2 -0
  93. package/lib/typescript/utils/is_object.d.ts.map +1 -0
  94. package/lib/typescript/utils/proxy.d.ts +2 -0
  95. package/lib/typescript/utils/proxy.d.ts.map +1 -0
  96. package/lib/typescript/utils/secs.d.ts +4 -0
  97. package/lib/typescript/utils/secs.d.ts.map +1 -0
  98. package/package.json +170 -0
  99. package/pagopa-io-react-native-jwt.podspec +44 -0
  100. package/src/algorithms.ts +46 -0
  101. package/src/hash.ts +10 -0
  102. package/src/index.tsx +120 -0
  103. package/src/jwt_claims_set.ts +194 -0
  104. package/src/produce.ts +109 -0
  105. package/src/sign.ts +180 -0
  106. package/src/types.ts +203 -0
  107. package/src/unsecured.ts +127 -0
  108. package/src/utils/asn1.ts +36 -0
  109. package/src/utils/base64.ts +37 -0
  110. package/src/utils/errors.ts +206 -0
  111. package/src/utils/is_object.ts +20 -0
  112. package/src/utils/proxy.ts +15 -0
  113. package/src/utils/secs.ts +58 -0
@@ -0,0 +1,206 @@
1
+ /**
2
+ * A generic Error that all other JOSE specific Error subclasses extend.
3
+ *
4
+ * @example Checking thrown error is a JOSE one
5
+ *
6
+ * ```js
7
+ * if (err instanceof errors.JOSEError) {
8
+ * // ...
9
+ * }
10
+ * ```
11
+ */
12
+ export class JOSEError extends Error {
13
+ /** A unique error code for the particular error subclass. */
14
+ static get code(): string {
15
+ return 'ERR_JOSE_GENERIC';
16
+ }
17
+
18
+ /** A unique error code for the particular error subclass. */
19
+ code: string = 'ERR_JOSE_GENERIC';
20
+
21
+ constructor(message?: string) {
22
+ super(message);
23
+ this.name = this.constructor.name;
24
+ // @ts-ignore
25
+ Error.captureStackTrace?.(this, this.constructor);
26
+ }
27
+ }
28
+ export class JWTClaimValidationFailed extends JOSEError {
29
+ static get code(): 'ERR_JWT_CLAIM_VALIDATION_FAILED' {
30
+ return 'ERR_JWT_CLAIM_VALIDATION_FAILED';
31
+ }
32
+
33
+ code = 'ERR_JWT_CLAIM_VALIDATION_FAILED';
34
+
35
+ /** The Claim for which the validation failed. */
36
+ claim: string;
37
+
38
+ /** Reason code for the validation failure. */
39
+ reason: string;
40
+
41
+ constructor(message: string, claim = 'unspecified', reason = 'unspecified') {
42
+ super(message);
43
+ this.claim = claim;
44
+ this.reason = reason;
45
+ }
46
+ }
47
+
48
+ /**
49
+ * An error subclass thrown when a JWT is expired.
50
+ *
51
+ * @example Checking thrown error is this one using a stable error code
52
+ *
53
+ * ```js
54
+ * if (err.code === 'ERR_JWT_EXPIRED') {
55
+ * // ...
56
+ * }
57
+ * ```
58
+ *
59
+ * @example Checking thrown error is this one using `instanceof`
60
+ *
61
+ * ```js
62
+ * if (err instanceof errors.JWTExpired) {
63
+ * // ...
64
+ * }
65
+ * ```
66
+ */
67
+ export class JWTExpired extends JOSEError implements JWTClaimValidationFailed {
68
+ static get code(): 'ERR_JWT_EXPIRED' {
69
+ return 'ERR_JWT_EXPIRED';
70
+ }
71
+
72
+ code = 'ERR_JWT_EXPIRED';
73
+
74
+ /** The Claim for which the validation failed. */
75
+ claim: string;
76
+
77
+ /** Reason code for the validation failure. */
78
+ reason: string;
79
+
80
+ constructor(message: string, claim = 'unspecified', reason = 'unspecified') {
81
+ super(message);
82
+ this.claim = claim;
83
+ this.reason = reason;
84
+ }
85
+ }
86
+
87
+ /**
88
+ * An error subclass thrown when a JOSE Algorithm is not allowed per developer preference.
89
+ *
90
+ * @example Checking thrown error is this one using a stable error code
91
+ *
92
+ * ```js
93
+ * if (err.code === 'ERR_JOSE_ALG_NOT_ALLOWED') {
94
+ * // ...
95
+ * }
96
+ * ```
97
+ *
98
+ * @example Checking thrown error is this one using `instanceof`
99
+ *
100
+ * ```js
101
+ * if (err instanceof errors.JOSEAlgNotAllowed) {
102
+ * // ...
103
+ * }
104
+ * ```
105
+ */
106
+ export class JOSEAlgNotAllowed extends JOSEError {
107
+ static get code(): 'ERR_JOSE_ALG_NOT_ALLOWED' {
108
+ return 'ERR_JOSE_ALG_NOT_ALLOWED';
109
+ }
110
+
111
+ code = 'ERR_JOSE_ALG_NOT_ALLOWED';
112
+ }
113
+
114
+ /**
115
+ * An error subclass thrown when a particular feature or algorithm is not supported by this
116
+ * implementation or JOSE in general.
117
+ *
118
+ * @example Checking thrown error is this one using a stable error code
119
+ *
120
+ * ```js
121
+ * if (err.code === 'ERR_JOSE_NOT_SUPPORTED') {
122
+ * // ...
123
+ * }
124
+ * ```
125
+ *
126
+ * @example Checking thrown error is this one using `instanceof`
127
+ *
128
+ * ```js
129
+ * if (err instanceof errors.JOSENotSupported) {
130
+ * // ...
131
+ * }
132
+ * ```
133
+ */
134
+ export class JOSENotSupported extends JOSEError {
135
+ static get code(): 'ERR_JOSE_NOT_SUPPORTED' {
136
+ return 'ERR_JOSE_NOT_SUPPORTED';
137
+ }
138
+
139
+ code = 'ERR_JOSE_NOT_SUPPORTED';
140
+ }
141
+
142
+ /**
143
+ * An error subclass thrown when a JWT is invalid.
144
+ *
145
+ * @example Checking thrown error is this one using a stable error code
146
+ *
147
+ * ```js
148
+ * if (err.code === 'ERR_JWT_INVALID') {
149
+ * // ...
150
+ * }
151
+ * ```
152
+ *
153
+ * @example Checking thrown error is this one using `instanceof`
154
+ *
155
+ * ```js
156
+ * if (err instanceof errors.JWTInvalid) {
157
+ * // ...
158
+ * }
159
+ * ```
160
+ */
161
+ export class JWTInvalid extends JOSEError {
162
+ static get code(): 'ERR_JWT_INVALID' {
163
+ return 'ERR_JWT_INVALID';
164
+ }
165
+
166
+ code = 'ERR_JWT_INVALID';
167
+ }
168
+
169
+ /**
170
+ * An error subclass thrown when a JWT signature (JWS) is invalid.
171
+ */
172
+ export class JWSInvalid extends JOSEError {
173
+ static get code(): 'ERR_JWS_INVALID' {
174
+ return 'ERR_JWS_INVALID';
175
+ }
176
+ code = 'ERR_JWS_INVALID';
177
+ }
178
+
179
+ /**
180
+ * An error subclass thrown when JWS signature verification fails.
181
+ *
182
+ * @example Checking thrown error is this one using a stable error code
183
+ *
184
+ * ```js
185
+ * if (err.code === 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED') {
186
+ * // ...
187
+ * }
188
+ * ```
189
+ *
190
+ * @example Checking thrown error is this one using `instanceof`
191
+ *
192
+ * ```js
193
+ * if (err instanceof errors.JWSSignatureVerificationFailed) {
194
+ * // ...
195
+ * }
196
+ * ```
197
+ */
198
+ export class JWSSignatureVerificationFailed extends JOSEError {
199
+ static get code(): 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED' {
200
+ return 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED';
201
+ }
202
+
203
+ code = 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED';
204
+
205
+ message = 'signature verification failed';
206
+ }
@@ -0,0 +1,20 @@
1
+ function isObjectLike(value: unknown) {
2
+ return typeof value === 'object' && value !== null;
3
+ }
4
+
5
+ export default function isObject<T = object>(input: unknown): input is T {
6
+ if (
7
+ !isObjectLike(input) ||
8
+ Object.prototype.toString.call(input) !== '[object Object]'
9
+ ) {
10
+ return false;
11
+ }
12
+ if (Object.getPrototypeOf(input) === null) {
13
+ return true;
14
+ }
15
+ let proto = input;
16
+ while (Object.getPrototypeOf(proto) !== null) {
17
+ proto = Object.getPrototypeOf(proto);
18
+ }
19
+ return Object.getPrototypeOf(input) === proto;
20
+ }
@@ -0,0 +1,15 @@
1
+ import { NativeModules } from 'react-native';
2
+
3
+ export const IoReactNativeJwt = NativeModules.IoReactNativeJwt
4
+ ? NativeModules.IoReactNativeJwt
5
+ : new Proxy(
6
+ {
7
+ //this is a fake implementation to allow local deviceless testing
8
+ unpackBerEncodedASN1: (a: string, _b: number) => a,
9
+ },
10
+ {
11
+ get: function (target, prop) {
12
+ return Reflect.get(target, prop);
13
+ },
14
+ }
15
+ );
@@ -0,0 +1,58 @@
1
+ const minute = 60;
2
+ const hour = minute * 60;
3
+ const day = hour * 24;
4
+ const week = day * 7;
5
+ const year = day * 365.25;
6
+
7
+ /*
8
+ * It allows to extract the value and the unit of measurement for the time.
9
+ * For example, indicating a string like "1s" or "1 second" or "3 hour"
10
+ * the regex extracts a group made up of [1, "s"], [1, "second"] or [3, "hour"]
11
+ */
12
+ const REGEX =
13
+ /^(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)$/i;
14
+
15
+ export default (str: string): number => {
16
+ const matched = REGEX.exec(str);
17
+
18
+ if (!matched) {
19
+ throw new TypeError('Invalid time period format');
20
+ }
21
+
22
+ const value = parseFloat(matched[1]!);
23
+ const unit = matched[2]!.toLowerCase();
24
+
25
+ switch (unit) {
26
+ case 'sec':
27
+ case 'secs':
28
+ case 'second':
29
+ case 'seconds':
30
+ case 's':
31
+ return Math.round(value);
32
+ case 'minute':
33
+ case 'minutes':
34
+ case 'min':
35
+ case 'mins':
36
+ case 'm':
37
+ return Math.round(value * minute);
38
+ case 'hour':
39
+ case 'hours':
40
+ case 'hr':
41
+ case 'hrs':
42
+ case 'h':
43
+ return Math.round(value * hour);
44
+ case 'day':
45
+ case 'days':
46
+ case 'd':
47
+ return Math.round(value * day);
48
+ case 'week':
49
+ case 'weeks':
50
+ case 'w':
51
+ return Math.round(value * week);
52
+ // years matched
53
+ default:
54
+ return Math.round(value * year);
55
+ }
56
+ };
57
+
58
+ export const epoch = (date: Date) => Math.floor(date.getTime() / 1000);