@opencampus/ocid-connect-js 1.2.5 → 2.0.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.
Files changed (45) hide show
  1. package/README.md +76 -3
  2. package/dist/ocid-connect-js.js +783 -128
  3. package/dist/ocid-connect-js.js.map +1 -1
  4. package/lib/index.d.ts +2 -0
  5. package/lib/react/LoginButton.d.ts +6 -0
  6. package/lib/react/LoginButton.js +1 -1
  7. package/lib/react/LoginCallBack.d.ts +7 -0
  8. package/lib/react/LoginCallBack.js +22 -9
  9. package/lib/react/OCConnect.d.ts +7 -0
  10. package/lib/react/OCConnect.js +7 -7
  11. package/lib/react/OCContext.d.ts +3 -0
  12. package/lib/react/OCSpinner.css +25 -0
  13. package/lib/react/OCSpinner.d.ts +6 -0
  14. package/lib/react/OCSpinner.js +39 -0
  15. package/lib/react/index.d.ts +4 -0
  16. package/lib/sdk/auth.d.ts +32 -0
  17. package/lib/sdk/auth.js +26 -19
  18. package/lib/sdk/crypto/base64.d.ts +5 -0
  19. package/lib/sdk/crypto/index.d.ts +3 -0
  20. package/lib/sdk/crypto/verifyToken.d.ts +1 -0
  21. package/lib/sdk/crypto/verifyToken.js +1 -1
  22. package/lib/sdk/crypto/webcrypto.d.ts +13 -0
  23. package/lib/sdk/endpoints/buildAuthEndpointUrl.d.ts +1 -0
  24. package/lib/sdk/endpoints/buildAuthEndpointUrl.js +8 -7
  25. package/lib/sdk/endpoints/index.d.ts +1 -0
  26. package/lib/sdk/index.d.ts +2 -0
  27. package/lib/sdk/lib/AuthInfoManager.d.ts +11 -0
  28. package/lib/sdk/lib/CookieStorageProvider.d.ts +8 -0
  29. package/lib/sdk/lib/CookieStorageProvider.js +16 -5
  30. package/lib/sdk/lib/StorageManager.d.ts +23 -0
  31. package/lib/sdk/lib/StorageManager.js +1 -1
  32. package/lib/sdk/lib/TokenManager.d.ts +16 -0
  33. package/lib/sdk/lib/TokenManager.js +2 -2
  34. package/lib/sdk/lib/TransactionManager.d.ts +9 -0
  35. package/lib/sdk/lib/index.d.ts +3 -0
  36. package/lib/sdk/lib/pkce.d.ts +11 -0
  37. package/lib/sdk/utils/createPkceMeta.d.ts +5 -0
  38. package/lib/sdk/utils/errors.d.ts +15 -0
  39. package/lib/sdk/utils/errors.js +2 -2
  40. package/lib/sdk/utils/index.d.ts +4 -0
  41. package/lib/sdk/utils/jwtParser.d.ts +1 -0
  42. package/lib/sdk/utils/prepareTokenParams.d.ts +9 -0
  43. package/lib/sdk/utils/prepareTokenParams.js +11 -10
  44. package/lib/sdk/utils/urlParser.d.ts +5 -0
  45. package/package.json +6 -2
package/README.md CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  ## Table of Contents
3
2
 
4
3
  - [Setup](#setup)
@@ -7,6 +6,15 @@
7
6
  - [Javascript Integration](#javascript-integration)
8
7
  - [License](#license)
9
8
 
9
+ ## Pre-Requisites
10
+
11
+ An Auth Client ID is required to use OCID Connect in Live (Production) mode. Please contact your Open Campus Ambassador to request access to an Open Campus Developer Account and Auth Client ID.
12
+ For Live mode integration, you will need to configure the Redirect URIs for you Auth Client and only configured Redirect URIs are allowed to be passed to the SDK.
13
+
14
+ You **do not need a Client ID when testing integration in Sandbox mode**. Sandbox mode connect to the sandbox OCID environment which is separate from the production environment. An OCID registered in the sandbox environment does not exist in the production environment and vice versa. Sandbox mode has no restriction for Redirect URIs and hence does not require a Client ID at the moment. Client ID can be passed to the SDK in sandbox mode, but **does not have any effect**.
15
+
16
+ > If you were onboarded to live integration before Apr 2025 and did not have an Open Campus Developer Account, you would **need to** use the V1.x SDK. Please get in touch with your Open Campus point of contact to get your Open Campus Developer Account and migrate to the V2 SDK. Thanks!
17
+
10
18
  ## Setup
11
19
 
12
20
  **yarn**
@@ -33,6 +41,7 @@ Setup Context to hook up state variables and override configuration
33
41
  import { OCConnect } from '@opencampus/ocid-connect-js';
34
42
 
35
43
  const opts = {
44
+ clientId: '<Does_Not_Matter_For_Sandbox_mode>',
36
45
  redirectUri: 'http://localhost:3001/redirect',
37
46
  referralCode: 'PARTNER6'
38
47
  }
@@ -46,19 +55,23 @@ return (
46
55
  )
47
56
  ```
48
57
 
49
- OCConnect Property
58
+ OCConnect Props
50
59
 
51
60
  | Property | Description |
52
61
  | --- | --- |
53
62
  | opts | Authentication's properties that can be overriden |
54
63
  | sandboxMode | Connect to sandbox if it is set, default to live mode |
55
64
 
56
- Opts Property
65
+ opts Properties
57
66
 
58
67
  | Property | Description |
59
68
  | --- | --- |
69
+ | clientId | Your Auth Client ID. Required for live mode, optional for sandbox mode |
60
70
  | redirectUri | URL to return after the login process is completed |
61
71
  | referralCode | Unique identifiers assigned to partners for tracking during OCID account's registration. |
72
+ | storageType | Storage type to store the auth state. Use cookie if specified as `cookie`. Otherwise if not defined, local storage is used. |
73
+ | domain | Domain to store cookie. Only meaningful if `cookie` type storaged is used. Leave it blank to tell the browser to use the current domain. |
74
+ | sameSite | Specify the SameSite behavior when using cookie as storage. When `true` - SameSite: strict; when `false` - SameSite: None, when not set - default SameSite behavior browser dependent |
62
75
 
63
76
  Setup LoginCallBack to handle flow's result
64
77
 
@@ -185,6 +198,7 @@ export default function RootLayout({
185
198
  children,
186
199
  }) {
187
200
  const opts = {
201
+ clientId: '<Does_Not_Matter_For_Sandbox_mode>',
188
202
  redirectUri: 'http://localhost:3000/redirect', // Adjust this URL
189
203
  referralCode: 'PARTNER6', // Assign partner code
190
204
  };
@@ -326,6 +340,15 @@ import { OCAuthSandbox } from '@opencampus/ocid-connect-js';
326
340
  const authSdk = new OCAuthSandbox();
327
341
  ```
328
342
 
343
+ In live mode, we need to provide the client id.
344
+
345
+ ```js
346
+ import { OCAuthLive } from '@opencampus/ocid-connect-js';
347
+ const authSdk = new OCAuthLive({
348
+ clientId: 'your_client_id',
349
+ });
350
+ ```
351
+
329
352
  Main Methods of Auth SDK
330
353
 
331
354
  | Method | Description |
@@ -334,6 +357,7 @@ Main Methods of Auth SDK
334
357
  | handleLoginRedirect | Return the auth state of the login process |
335
358
  | getAuthState | Return auth state data { accessToken, idToken, OCId, ethAddress, isAuthenticated } |
336
359
  | getStateParameter() | Return the state that was initialized in signin process |
360
+ | logout() | Logout the current user. Accept "returnUrl" as an input so user can be redirected to the app after logout |
337
361
 
338
362
  Sample usage
339
363
 
@@ -382,3 +406,52 @@ Access OCId info of Auth SDK
382
406
 
383
407
  ### License
384
408
  ocid-connect-js is released under the MIT license.
409
+
410
+ ## JWT Verification Example
411
+
412
+ Below is a sample code snippet demonstrating how to fetch the JSON Web Key Set (JWKS) from a remote URL and verify a JWT. Depending on the environment, it will choose either the Sandbox or Live JWKS URL.
413
+
414
+ Sandbox:
415
+ https://static.opencampus.xyz/jwks/jwks-sandbox.json
416
+
417
+ Live:
418
+ https://static.opencampus.xyz/jwks/jwks-live.json
419
+
420
+
421
+ ### This is just an example, you can use any library to verify the JWT. Do not use this code in production.
422
+
423
+ ```js
424
+ import * as jose from 'jose';
425
+
426
+ const fetchJWKS = async (jwkUrl) => {
427
+ const resp = await fetch(jwkUrl);
428
+ json = await resp.json();
429
+ return await jose.createLocalJWKSet(json);
430
+ };
431
+
432
+ const verifyJwt = async (jwt, jwkUrl) => {
433
+ const JWK = await fetchJWKS(jwkUrl);
434
+ const { payload } = await jose.jwtVerify(jwt, JWK);
435
+ return payload;
436
+ };
437
+
438
+ // Example usage
439
+ const verifyTokenExample = async (jwt) => {
440
+ try {
441
+ // Choose the JWKS URL based on the environment
442
+ const jwkUrl = process.env.NODE_ENV === 'production'
443
+ ? 'https://static.opencampus.xyz/jwks/jwks-live.json'
444
+ : 'https://static.opencampus.xyz/certs/jwks-sandbox.json';
445
+
446
+ const payload = await verifyJwt(jwt, jwkUrl);
447
+ console.log('JWT verified successfully:', payload);
448
+ } catch (error) {
449
+ console.error('JWT verification failed:', error);
450
+ }
451
+ };
452
+
453
+ // Replace 'your_jwt_here' with your actual JWT token
454
+ verifyTokenExample('your_jwt_here');
455
+ ```
456
+
457
+