@opencampus/ocid-connect-js 1.2.6 → 2.0.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.
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,20 +55,22 @@ 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. |
62
- | domain | Domain to store cookie. Leave it blank to tell the browser to use the current domain |
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. |
63
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 |
64
75
 
65
76
  Setup LoginCallBack to handle flow's result
@@ -187,6 +198,7 @@ export default function RootLayout({
187
198
  children,
188
199
  }) {
189
200
  const opts = {
201
+ clientId: '<Does_Not_Matter_For_Sandbox_mode>',
190
202
  redirectUri: 'http://localhost:3000/redirect', // Adjust this URL
191
203
  referralCode: 'PARTNER6', // Assign partner code
192
204
  };
@@ -289,17 +301,17 @@ import LoginButton from '../components/LoginButton';
289
301
  import { useOCAuth } from '@opencampus/ocid-connect-js';
290
302
 
291
303
  export default function Home() {
292
- const { authState, ocAuth } = useOCAuth();
293
-
294
- if (authState.error) {
295
- return <div>Error: {authState.error.message}</div>;
296
- }
304
+ const { isInitialized, authState, ocAuth } = useOCAuth();
297
305
 
298
306
  // Add a loading state
299
- if (authState.isLoading) {
307
+ if (!isInitialized) {
300
308
  return <div>Loading...</div>;
301
309
  }
302
310
 
311
+ if (authState.error) {
312
+ return <div>Error: {authState.error.message}</div>;
313
+ }
314
+
303
315
  return (
304
316
  <div>
305
317
  <h1>Welcome to My App</h1>
@@ -328,6 +340,15 @@ import { OCAuthSandbox } from '@opencampus/ocid-connect-js';
328
340
  const authSdk = new OCAuthSandbox();
329
341
  ```
330
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
+
331
352
  Main Methods of Auth SDK
332
353
 
333
354
  | Method | Description |
@@ -336,6 +357,7 @@ Main Methods of Auth SDK
336
357
  | handleLoginRedirect | Return the auth state of the login process |
337
358
  | getAuthState | Return auth state data { accessToken, idToken, OCId, ethAddress, isAuthenticated } |
338
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 |
339
361
 
340
362
  Sample usage
341
363
 
@@ -384,3 +406,52 @@ Access OCId info of Auth SDK
384
406
 
385
407
  ### License
386
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
+