@octap/browser-sdk 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +107 -2
- package/dist/auth.d.ts +118 -0
- package/dist/auth.js +263 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +48 -0
- package/dist/client.js +110 -0
- package/dist/client.js.map +1 -0
- package/dist/core-api.d.ts +13 -0
- package/dist/core-api.js +64 -0
- package/dist/core-api.js.map +1 -0
- package/dist/errors.d.ts +32 -0
- package/dist/errors.js +13 -0
- package/dist/errors.js.map +1 -0
- package/dist/google-provider.d.ts +13 -0
- package/dist/google-provider.js +104 -0
- package/dist/google-provider.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/session-store.d.ts +25 -0
- package/dist/session-store.js +79 -0
- package/dist/session-store.js.map +1 -0
- package/dist/zklogin-signer.d.ts +25 -0
- package/dist/zklogin-signer.js +111 -0
- package/dist/zklogin-signer.js.map +1 -0
- package/package.json +27 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OctaPointees
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,108 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @octap/browser-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Customer sign-in and transaction signing for a merchant's website, added the way you would add
|
|
4
|
+
Firebase Auth. You never handle OAuth nonces, ephemeral keys, ZK proofs or Enoki.
|
|
5
|
+
|
|
6
|
+
This package holds no merchant credential. Balance, history, voucher lists and claims need your API
|
|
7
|
+
key, so they stay on your backend through `@octap/sdk`.
|
|
8
|
+
|
|
9
|
+
## Sign a customer in
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createOctapAuth } from '@octap/browser-sdk';
|
|
13
|
+
|
|
14
|
+
const auth = createOctapAuth({ apiBaseUrl, organizationId, googleClientId });
|
|
15
|
+
|
|
16
|
+
auth.onAuthStateChanged((user) => {
|
|
17
|
+
// { endUserId, memberId, suiAddress } or null. Also called once on load, after a reload restores.
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
await auth.renderGoogleButton(document.getElementById('sign-in'), {
|
|
21
|
+
onError: (error) => showMessage(error.message),
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Signing in also enrolls the customer in your organization. The button renders nothing while a
|
|
26
|
+
customer is signed in, and returns after they sign out or their session expires. Google's token
|
|
27
|
+
lasts about an hour: when `getIdToken` finds it expired, the customer is signed out, so listeners get
|
|
28
|
+
`null` and the button returns.
|
|
29
|
+
|
|
30
|
+
## Prove the customer to your backend
|
|
31
|
+
|
|
32
|
+
Send the token to your backend, and check it there with `verifyCustomer` from `@octap/sdk`. Never
|
|
33
|
+
trust a member ID your frontend sends on its own.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const response = await fetch('/api/claim', {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
headers: { authorization: `Bearer ${await auth.getIdToken()}` },
|
|
39
|
+
body: JSON.stringify({ voucherId }),
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Claim a paid voucher
|
|
44
|
+
|
|
45
|
+
Your backend calls `claimVoucher` and returns the result. The customer's device then signs it:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
const claim = await response.json();
|
|
49
|
+
if (claim.status === 'RESERVED') {
|
|
50
|
+
await auth.signAndFinalize(claim);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Follow these rules, whatever your interface looks like:
|
|
55
|
+
|
|
56
|
+
- A RESERVED voucher is not owned yet. Show it as pending until finalize succeeds.
|
|
57
|
+
- `signAndFinalize` never retries by itself. On `outcome-unknown`, the transaction may have failed
|
|
58
|
+
or may still land. Read the claim from your backend, or claim again with the **same** idempotency
|
|
59
|
+
key. `status` and `body` carry what the server answered, if anything.
|
|
60
|
+
- Use a new idempotency key only after the claim is CANCELLED.
|
|
61
|
+
|
|
62
|
+
`auth.signTransaction(transactionBytes)` is the lower-level call: it returns the zkLogin signature
|
|
63
|
+
and sends nothing. It refuses bytes whose sender is not the signed-in customer.
|
|
64
|
+
|
|
65
|
+
## Errors
|
|
66
|
+
|
|
67
|
+
Every failure is an `OctapAuthError` with a `code`. API errors also have `status` and `body`.
|
|
68
|
+
|
|
69
|
+
| Code | Meaning | What to do |
|
|
70
|
+
| ---------------------- | ------------------------------------------------------- | -------------------------------- |
|
|
71
|
+
| `sign-in-canceled` | The customer closed Google's window | Nothing; the button still works |
|
|
72
|
+
| `nonce-mismatch` | Google's token was not issued for this attempt | Ask them to sign in again |
|
|
73
|
+
| `provider-unavailable` | Google's script could not load | Check for blocked scripts |
|
|
74
|
+
| `not-signed-in` | No customer is signed in | Show the button |
|
|
75
|
+
| `session-expired` | The signing session passed `maxEpoch` | Ask them to sign in again |
|
|
76
|
+
| `token-expired` | Google's token expired (after about an hour) | Ask them to sign in again |
|
|
77
|
+
| `wrong-account` | The transaction belongs to a different customer | Ask them to use that account |
|
|
78
|
+
| `invalid-claim` | The claim has nothing to sign, or its bytes are invalid | Check the claim's status |
|
|
79
|
+
| `outcome-unknown` | Finalize may have reached the chain | Read the claim; do not re-create |
|
|
80
|
+
| `api-error` | OctaP answered with an error | See `status` and `message` |
|
|
81
|
+
| `network-error` | OctaP could not be reached | Retry later |
|
|
82
|
+
| `timeout` | The request took too long | Retry later |
|
|
83
|
+
| `aborted` | Your `AbortSignal` fired | Nothing |
|
|
84
|
+
| `invalid-response` | OctaP's response could not be read | Report it |
|
|
85
|
+
|
|
86
|
+
`sign-in-canceled` is a best guess from the page regaining focus, because Google's button reports no
|
|
87
|
+
cancellation itself. It can fire while the popup is still open if the customer clicks back to the
|
|
88
|
+
page, and may not fire at all. Use it only to show a message.
|
|
89
|
+
|
|
90
|
+
## Security
|
|
91
|
+
|
|
92
|
+
The session holds the ephemeral signing key, the proof and the id_token until `maxEpoch` or sign-out.
|
|
93
|
+
|
|
94
|
+
- By default these are in `sessionStorage`, so they survive a reload in this tab. Any script on your
|
|
95
|
+
page can read them, so an XSS flaw lets an attacker sign as this customer until the session
|
|
96
|
+
expires. Use a strict Content Security Policy, and avoid third-party scripts on these pages.
|
|
97
|
+
- Pass `persistence: 'memory'` to keep them out of browser storage. A reload then signs the customer
|
|
98
|
+
out.
|
|
99
|
+
- The private key is never sent anywhere. Only its public half reaches OctaP.
|
|
100
|
+
|
|
101
|
+
## Google client ID
|
|
102
|
+
|
|
103
|
+
Use the Google client ID OctaP gives you, and ask OctaP to add your site's origin to it. All merchants
|
|
104
|
+
share this client for now, so moving to per-merchant clients later would change customer wallets.
|
|
105
|
+
|
|
106
|
+
## Local example
|
|
107
|
+
|
|
108
|
+
See [the browser auth example](../../examples/README.md#browser-auth-example) for a runnable page.
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { type RequestOptions } from './core-api.js';
|
|
2
|
+
import { OctapAuthError } from './errors.js';
|
|
3
|
+
import { type OctapUser, type Persistence } from './session-store.js';
|
|
4
|
+
export interface OctapAuthOptions {
|
|
5
|
+
apiBaseUrl: string;
|
|
6
|
+
organizationId: string;
|
|
7
|
+
googleClientId: string;
|
|
8
|
+
/**
|
|
9
|
+
* Where the signing key, proof and id_token are kept until `maxEpoch` or sign-out. `session` (the
|
|
10
|
+
* default) survives a reload in this tab; `memory` does not. Any script running on your page can
|
|
11
|
+
* read a `session` value, so an XSS flaw exposes it until it expires.
|
|
12
|
+
*/
|
|
13
|
+
persistence?: Persistence;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
fetch?: typeof fetch;
|
|
16
|
+
}
|
|
17
|
+
export interface RenderOptions {
|
|
18
|
+
/** Sign-in failures, including a customer closing Google's window. */
|
|
19
|
+
onError?: (error: OctapAuthError) => void;
|
|
20
|
+
/** Passed through to Google's button, such as `theme`, `size`, `text` or `shape`. */
|
|
21
|
+
button?: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
/** The merchant backend's claim result, as `@octap/sdk`'s `claimVoucher` returns it. */
|
|
24
|
+
export interface SignableClaim {
|
|
25
|
+
status: string;
|
|
26
|
+
digest?: string | null;
|
|
27
|
+
transactionBytes?: string | null;
|
|
28
|
+
}
|
|
29
|
+
/** What `prepareVoucherUse` returned on the merchant's server. */
|
|
30
|
+
export interface SignableVoucherUse {
|
|
31
|
+
digest?: string | null;
|
|
32
|
+
transactionBytes?: string | null;
|
|
33
|
+
}
|
|
34
|
+
export interface FinalizedVoucherUse {
|
|
35
|
+
voucherOwnershipId: string;
|
|
36
|
+
voucherId: string;
|
|
37
|
+
serialCode: string;
|
|
38
|
+
status: string;
|
|
39
|
+
redeemedAt: string;
|
|
40
|
+
voucherTitle: string;
|
|
41
|
+
discountType?: string | null;
|
|
42
|
+
discountValue?: string | null;
|
|
43
|
+
blockchainDigest: string;
|
|
44
|
+
}
|
|
45
|
+
export interface FinalizedClaim {
|
|
46
|
+
transactionId: string;
|
|
47
|
+
organizationEndUserId: string;
|
|
48
|
+
pointsRedeemed: number;
|
|
49
|
+
newBalance: number;
|
|
50
|
+
status: string;
|
|
51
|
+
blockchainDigest: string;
|
|
52
|
+
}
|
|
53
|
+
export type AuthStateListener = (user: OctapUser | null) => void;
|
|
54
|
+
export declare class OctapAuth {
|
|
55
|
+
private readonly api;
|
|
56
|
+
private readonly store;
|
|
57
|
+
private readonly organizationId;
|
|
58
|
+
private readonly googleClientId;
|
|
59
|
+
private readonly listeners;
|
|
60
|
+
private session;
|
|
61
|
+
private provider?;
|
|
62
|
+
private button?;
|
|
63
|
+
private signingIn;
|
|
64
|
+
/** Changes when the session ends, so a sign-in still running cannot bring it back. */
|
|
65
|
+
private signInGeneration;
|
|
66
|
+
/** Changes on each new button, so only the latest nonce can be rendered or used. */
|
|
67
|
+
private armGeneration;
|
|
68
|
+
private expiryTimer?;
|
|
69
|
+
constructor(options: OctapAuthOptions);
|
|
70
|
+
/** The signed-in customer, restored synchronously after a reload. Null once the session expires. */
|
|
71
|
+
get currentUser(): OctapUser | null;
|
|
72
|
+
/** Calls `listener` with the current customer soon after subscribing, then on every change. */
|
|
73
|
+
onAuthStateChanged(listener: AuthStateListener): () => void;
|
|
74
|
+
/**
|
|
75
|
+
* Renders Google's button. Key, nonce, enrollment and proof are handled when the customer uses it.
|
|
76
|
+
* While a customer is signed in it renders nothing, and appears once they sign out or expire.
|
|
77
|
+
*/
|
|
78
|
+
renderGoogleButton(element: HTMLElement, options?: RenderOptions): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* The id_token for your backend to check with `@octap/sdk`'s `verifyCustomer`. Google's token lasts
|
|
81
|
+
* about an hour. When it has expired the customer is signed out, so the button returns for a new one.
|
|
82
|
+
*/
|
|
83
|
+
getIdToken(): Promise<string>;
|
|
84
|
+
/** Lower-level signing: returns the zkLogin signature over base64 transaction bytes. */
|
|
85
|
+
signTransaction(transactionBytes: string): Promise<string>;
|
|
86
|
+
/**
|
|
87
|
+
* Signs a RESERVED claim and finalizes it. The voucher is not owned until this resolves.
|
|
88
|
+
*
|
|
89
|
+
* Never retried automatically. On `outcome-unknown` the transaction may have failed or may still
|
|
90
|
+
* land, so read the claim from your backend, or retry the claim with the same idempotency key. Use
|
|
91
|
+
* a new key only after the claim is CANCELLED.
|
|
92
|
+
*/
|
|
93
|
+
signAndFinalize(claim: SignableClaim, options?: RequestOptions): Promise<FinalizedClaim>;
|
|
94
|
+
/**
|
|
95
|
+
* Signs the customer's use of a voucher and finalizes it. Show the customer what they are using
|
|
96
|
+
* first: this signature is the only thing that can mark their voucher used. Never retried
|
|
97
|
+
* automatically; on `outcome-unknown`, read the voucher from your backend.
|
|
98
|
+
*/
|
|
99
|
+
signAndFinalizeVoucherUse(use: SignableVoucherUse, options?: RequestOptions): Promise<FinalizedVoucherUse>;
|
|
100
|
+
private submitSigned;
|
|
101
|
+
/** Always ends the session. A button that cannot return is reported through `onError`. */
|
|
102
|
+
signOut(): Promise<void>;
|
|
103
|
+
private requireSession;
|
|
104
|
+
private expireSession;
|
|
105
|
+
private endSession;
|
|
106
|
+
/** Lets an open page notice expiry without waiting for a call to fail. Only runs in a browser. */
|
|
107
|
+
private scheduleExpiry;
|
|
108
|
+
private notify;
|
|
109
|
+
/** One merchant listener's bug must not undo a sign-in, or stop the other listeners. */
|
|
110
|
+
private call;
|
|
111
|
+
private reportError;
|
|
112
|
+
/** Puts a usable button back, if one was rendered, reporting rather than throwing a failure. */
|
|
113
|
+
private rearm;
|
|
114
|
+
/** Prepares a key and nonce and renders the button with them. A newer call replaces an older one. */
|
|
115
|
+
private arm;
|
|
116
|
+
private completeSignIn;
|
|
117
|
+
}
|
|
118
|
+
export declare function createOctapAuth(options: OctapAuthOptions): OctapAuth;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { CoreApi, normalizeBaseUrl } from './core-api.js';
|
|
2
|
+
import { OctapAuthError } from './errors.js';
|
|
3
|
+
import { createGoogleProvider } from './google-provider.js';
|
|
4
|
+
import { createSessionStore, } from './session-store.js';
|
|
5
|
+
import { PendingSignIn, idTokenExpiry, signWithZkLogin } from './zklogin-signer.js';
|
|
6
|
+
const DEFAULT_TIMEOUT_MS = 35_000;
|
|
7
|
+
/** Finalize waits for the chain's verdict, so it gets longer than an ordinary request. */
|
|
8
|
+
const FINALIZE_TIMEOUT_MS = 90_000;
|
|
9
|
+
/** Browsers run a longer `setTimeout` immediately. */
|
|
10
|
+
const MAX_TIMER_MS = 2_147_483_647;
|
|
11
|
+
export class OctapAuth {
|
|
12
|
+
api;
|
|
13
|
+
store;
|
|
14
|
+
organizationId;
|
|
15
|
+
googleClientId;
|
|
16
|
+
listeners = new Set();
|
|
17
|
+
session;
|
|
18
|
+
provider;
|
|
19
|
+
button;
|
|
20
|
+
signingIn = false;
|
|
21
|
+
/** Changes when the session ends, so a sign-in still running cannot bring it back. */
|
|
22
|
+
signInGeneration = 0;
|
|
23
|
+
/** Changes on each new button, so only the latest nonce can be rendered or used. */
|
|
24
|
+
armGeneration = 0;
|
|
25
|
+
expiryTimer;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
const fetchFn = options.fetch ?? ((...args) => globalThis.fetch(...args));
|
|
28
|
+
this.api = new CoreApi(options.apiBaseUrl, fetchFn, options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
29
|
+
this.organizationId = options.organizationId;
|
|
30
|
+
this.googleClientId = options.googleClientId;
|
|
31
|
+
this.store = createSessionStore(options.persistence ?? 'session', {
|
|
32
|
+
apiBaseUrl: normalizeBaseUrl(options.apiBaseUrl),
|
|
33
|
+
googleClientId: options.googleClientId,
|
|
34
|
+
organizationId: options.organizationId,
|
|
35
|
+
});
|
|
36
|
+
this.session = this.store.load();
|
|
37
|
+
if (this.session && Date.now() >= this.session.signer.expiresAt) {
|
|
38
|
+
this.store.clear();
|
|
39
|
+
this.session = null;
|
|
40
|
+
}
|
|
41
|
+
this.scheduleExpiry();
|
|
42
|
+
}
|
|
43
|
+
/** The signed-in customer, restored synchronously after a reload. Null once the session expires. */
|
|
44
|
+
get currentUser() {
|
|
45
|
+
return this.session && Date.now() < this.session.signer.expiresAt ? this.session.user : null;
|
|
46
|
+
}
|
|
47
|
+
/** Calls `listener` with the current customer soon after subscribing, then on every change. */
|
|
48
|
+
onAuthStateChanged(listener) {
|
|
49
|
+
this.listeners.add(listener);
|
|
50
|
+
queueMicrotask(() => {
|
|
51
|
+
if (this.listeners.has(listener))
|
|
52
|
+
this.call(listener);
|
|
53
|
+
});
|
|
54
|
+
return () => {
|
|
55
|
+
this.listeners.delete(listener);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Renders Google's button. Key, nonce, enrollment and proof are handled when the customer uses it.
|
|
60
|
+
* While a customer is signed in it renders nothing, and appears once they sign out or expire.
|
|
61
|
+
*/
|
|
62
|
+
async renderGoogleButton(element, options = {}) {
|
|
63
|
+
this.button = { element, options };
|
|
64
|
+
this.provider = createGoogleProvider(this.googleClientId, options.button);
|
|
65
|
+
if (!this.currentUser)
|
|
66
|
+
await this.arm();
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The id_token for your backend to check with `@octap/sdk`'s `verifyCustomer`. Google's token lasts
|
|
70
|
+
* about an hour. When it has expired the customer is signed out, so the button returns for a new one.
|
|
71
|
+
*/
|
|
72
|
+
async getIdToken() {
|
|
73
|
+
const { idToken } = this.requireSession();
|
|
74
|
+
const expiry = idTokenExpiry(idToken);
|
|
75
|
+
if (expiry !== undefined && Date.now() >= expiry * 1000) {
|
|
76
|
+
this.expireSession();
|
|
77
|
+
throw new OctapAuthError('token-expired', 'The sign-in token expired. Sign in again.');
|
|
78
|
+
}
|
|
79
|
+
return idToken;
|
|
80
|
+
}
|
|
81
|
+
/** Lower-level signing: returns the zkLogin signature over base64 transaction bytes. */
|
|
82
|
+
async signTransaction(transactionBytes) {
|
|
83
|
+
const session = this.requireSession();
|
|
84
|
+
return signWithZkLogin(session.signer, session.user.suiAddress, transactionBytes);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Signs a RESERVED claim and finalizes it. The voucher is not owned until this resolves.
|
|
88
|
+
*
|
|
89
|
+
* Never retried automatically. On `outcome-unknown` the transaction may have failed or may still
|
|
90
|
+
* land, so read the claim from your backend, or retry the claim with the same idempotency key. Use
|
|
91
|
+
* a new key only after the claim is CANCELLED.
|
|
92
|
+
*/
|
|
93
|
+
async signAndFinalize(claim, options = {}) {
|
|
94
|
+
if (claim.status !== 'RESERVED' || !claim.digest || !claim.transactionBytes) {
|
|
95
|
+
throw new OctapAuthError('invalid-claim', 'Only a RESERVED claim with transaction bytes can be signed.');
|
|
96
|
+
}
|
|
97
|
+
return this.submitSigned('/v1/credits/redeem/finalize', claim.digest, claim.transactionBytes, options);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Signs the customer's use of a voucher and finalizes it. Show the customer what they are using
|
|
101
|
+
* first: this signature is the only thing that can mark their voucher used. Never retried
|
|
102
|
+
* automatically; on `outcome-unknown`, read the voucher from your backend.
|
|
103
|
+
*/
|
|
104
|
+
async signAndFinalizeVoucherUse(use, options = {}) {
|
|
105
|
+
if (!use.digest || !use.transactionBytes) {
|
|
106
|
+
throw new OctapAuthError('invalid-claim', 'Only a voucher use with transaction bytes can be signed.');
|
|
107
|
+
}
|
|
108
|
+
return this.submitSigned('/v1/rewards/vouchers/use/finalize', use.digest, use.transactionBytes, options);
|
|
109
|
+
}
|
|
110
|
+
async submitSigned(path, digest, transactionBytes, options) {
|
|
111
|
+
const signature = await this.signTransaction(transactionBytes);
|
|
112
|
+
try {
|
|
113
|
+
return await this.api.post(path, { digest, signature }, { ...options, timeoutMs: options.timeoutMs ?? FINALIZE_TIMEOUT_MS });
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
// Core answers 400 for a bad signature and for an Enoki failure after sending alike, and 404 for
|
|
117
|
+
// a repeat of a finalize that already succeeded. Only a rate limit proves nothing was attempted.
|
|
118
|
+
throw err instanceof OctapAuthError && err.status === 429
|
|
119
|
+
? err
|
|
120
|
+
: new OctapAuthError('outcome-unknown', 'The result of this transaction is not known yet. Check the claim before trying again.', err instanceof OctapAuthError ? { status: err.status, body: err.body } : {});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Always ends the session. A button that cannot return is reported through `onError`. */
|
|
124
|
+
async signOut() {
|
|
125
|
+
this.provider?.signOut();
|
|
126
|
+
this.endSession();
|
|
127
|
+
// The button's nonce belongs to a key this session may have used, so issue a fresh one.
|
|
128
|
+
await this.rearm();
|
|
129
|
+
}
|
|
130
|
+
requireSession() {
|
|
131
|
+
if (!this.session) {
|
|
132
|
+
throw new OctapAuthError('not-signed-in', 'No customer is signed in.');
|
|
133
|
+
}
|
|
134
|
+
if (Date.now() >= this.session.signer.expiresAt) {
|
|
135
|
+
this.expireSession();
|
|
136
|
+
throw new OctapAuthError('session-expired', 'The signing session expired. Sign in again.');
|
|
137
|
+
}
|
|
138
|
+
return this.session;
|
|
139
|
+
}
|
|
140
|
+
expireSession() {
|
|
141
|
+
this.endSession();
|
|
142
|
+
void this.rearm();
|
|
143
|
+
}
|
|
144
|
+
endSession() {
|
|
145
|
+
const hadSession = this.session !== null;
|
|
146
|
+
this.signInGeneration++;
|
|
147
|
+
this.signingIn = false;
|
|
148
|
+
this.store.clear();
|
|
149
|
+
this.session = null;
|
|
150
|
+
this.scheduleExpiry();
|
|
151
|
+
if (hadSession)
|
|
152
|
+
this.notify();
|
|
153
|
+
}
|
|
154
|
+
/** Lets an open page notice expiry without waiting for a call to fail. Only runs in a browser. */
|
|
155
|
+
scheduleExpiry() {
|
|
156
|
+
if (typeof window === 'undefined')
|
|
157
|
+
return;
|
|
158
|
+
window.clearTimeout(this.expiryTimer);
|
|
159
|
+
if (!this.session)
|
|
160
|
+
return;
|
|
161
|
+
const remaining = this.session.signer.expiresAt - Date.now();
|
|
162
|
+
this.expiryTimer = window.setTimeout(() => {
|
|
163
|
+
if (this.session && Date.now() >= this.session.signer.expiresAt)
|
|
164
|
+
this.expireSession();
|
|
165
|
+
else
|
|
166
|
+
this.scheduleExpiry();
|
|
167
|
+
}, Math.min(Math.max(remaining, 0), MAX_TIMER_MS));
|
|
168
|
+
}
|
|
169
|
+
notify() {
|
|
170
|
+
for (const listener of this.listeners)
|
|
171
|
+
this.call(listener);
|
|
172
|
+
}
|
|
173
|
+
/** One merchant listener's bug must not undo a sign-in, or stop the other listeners. */
|
|
174
|
+
call(listener) {
|
|
175
|
+
try {
|
|
176
|
+
listener(this.currentUser);
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
console.error('An onAuthStateChanged listener threw:', err);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
reportError(err) {
|
|
183
|
+
const error = err instanceof OctapAuthError
|
|
184
|
+
? err
|
|
185
|
+
: new OctapAuthError('provider-unavailable', err instanceof Error ? err.message : String(err));
|
|
186
|
+
try {
|
|
187
|
+
this.button?.options.onError?.(error);
|
|
188
|
+
}
|
|
189
|
+
catch (handlerError) {
|
|
190
|
+
console.error('The onError handler threw:', handlerError);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** Puts a usable button back, if one was rendered, reporting rather than throwing a failure. */
|
|
194
|
+
async rearm() {
|
|
195
|
+
if (!this.button)
|
|
196
|
+
return;
|
|
197
|
+
await this.arm().catch((err) => this.reportError(err));
|
|
198
|
+
}
|
|
199
|
+
/** Prepares a key and nonce and renders the button with them. A newer call replaces an older one. */
|
|
200
|
+
async arm() {
|
|
201
|
+
const generation = ++this.armGeneration;
|
|
202
|
+
const pending = await PendingSignIn.start(this.api);
|
|
203
|
+
if (generation !== this.armGeneration)
|
|
204
|
+
return;
|
|
205
|
+
await this.provider.render(this.button.element, pending.nonce, {
|
|
206
|
+
onIdToken: (idToken) => {
|
|
207
|
+
if (generation === this.armGeneration)
|
|
208
|
+
void this.completeSignIn(pending, idToken);
|
|
209
|
+
},
|
|
210
|
+
onCanceled: () => {
|
|
211
|
+
if (generation === this.armGeneration) {
|
|
212
|
+
this.reportError(new OctapAuthError('sign-in-canceled', 'Google sign-in was canceled.'));
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
async completeSignIn(pending, idToken) {
|
|
218
|
+
if (this.signingIn)
|
|
219
|
+
return;
|
|
220
|
+
this.signingIn = true;
|
|
221
|
+
const generation = this.signInGeneration;
|
|
222
|
+
const superseded = () => generation !== this.signInGeneration;
|
|
223
|
+
try {
|
|
224
|
+
pending.assertIssuedForThisAttempt(idToken);
|
|
225
|
+
const { suiAddress } = await this.api.post('/v1/identity/google-login', { idToken });
|
|
226
|
+
if (superseded())
|
|
227
|
+
return;
|
|
228
|
+
const { endUserId, organizationEndUserId } = await this.api.post(`/v1/identity/organizations/${encodeURIComponent(this.organizationId)}/enroll`, { idToken });
|
|
229
|
+
if (superseded())
|
|
230
|
+
return;
|
|
231
|
+
if (typeof suiAddress !== 'string' ||
|
|
232
|
+
typeof endUserId !== 'string' ||
|
|
233
|
+
typeof organizationEndUserId !== 'string') {
|
|
234
|
+
throw new OctapAuthError('invalid-response', 'OctaP returned an incomplete customer.');
|
|
235
|
+
}
|
|
236
|
+
const signer = await pending.prove(this.api, idToken);
|
|
237
|
+
if (superseded())
|
|
238
|
+
return;
|
|
239
|
+
this.session = { user: { endUserId, memberId: organizationEndUserId, suiAddress }, idToken, signer };
|
|
240
|
+
this.store.save(this.session);
|
|
241
|
+
this.scheduleExpiry();
|
|
242
|
+
// A button armed for another attempt must not outlive this session's sign-in.
|
|
243
|
+
this.armGeneration++;
|
|
244
|
+
this.button?.element.replaceChildren();
|
|
245
|
+
this.notify();
|
|
246
|
+
}
|
|
247
|
+
catch (err) {
|
|
248
|
+
if (superseded())
|
|
249
|
+
return;
|
|
250
|
+
this.reportError(err);
|
|
251
|
+
// A nonce is good for one attempt, so a failure must leave a usable button behind.
|
|
252
|
+
await this.rearm();
|
|
253
|
+
}
|
|
254
|
+
finally {
|
|
255
|
+
if (!superseded())
|
|
256
|
+
this.signingIn = false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
export function createOctapAuth(options) {
|
|
261
|
+
return new OctapAuth(options);
|
|
262
|
+
}
|
|
263
|
+
//# sourceMappingURL=auth.js.map
|
package/dist/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAuB,MAAM,eAAe,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,oBAAoB,EAAwB,MAAM,sBAAsB,CAAC;AAClF,OAAO,EACL,kBAAkB,GAKnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AA2DpF,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAClC,0FAA0F;AAC1F,MAAM,mBAAmB,GAAG,MAAM,CAAC;AACnC,sDAAsD;AACtD,MAAM,YAAY,GAAG,aAAa,CAAC;AAEnC,MAAM,OAAO,SAAS;IACH,GAAG,CAAU;IACb,KAAK,CAAe;IACpB,cAAc,CAAS;IACvB,cAAc,CAAS;IACvB,SAAS,GAAG,IAAI,GAAG,EAAqB,CAAC;IAClD,OAAO,CAAuB;IAC9B,QAAQ,CAAmB;I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YACD,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YACtD,IAAI,UAAU,EAAE;gBAAE,OAAO;YAEzB,IAAI,CAAC,OAAO,GAAG,EAAE,IAAI,EAAE,EAAE,SAAS,EAAE,QAAQ,EAAE,qBAAqB,EAAE,UAAU,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;YACrG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC9B,IAAI,CAAC,cAAc,EAAE,CAAC;YACtB,8EAA8E;YAC9E,IAAI,CAAC,aAAa,EAAE,CAAC;YACrB,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,eAAe,EAAE,CAAC;YACvC,IAAI,CAAC,MAAM,EAAE,CAAC;QAChB,CAAC;QAAC,OAAO,GAAY,EAAE,CAAC;YACtB,IAAI,UAAU,EAAE;gBAAE,OAAO;YACzB,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;YACtB,mFAAmF;YACnF,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;QACrB,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,UAAU,EAAE;gBAAE,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;QAC5C,CAAC;IACH,CAAC;CACF;AAED,MAAM,UAAU,eAAe,CAAC,OAAyB;IACvD,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;AAChC,CAAC"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { CreditBalanceSnapshot, LoyaltyTransactionDto, VoucherDto, ClaimVoucherResponse, VoucherOwnershipDto, GoogleLoginResponse, EnrollMemberResponse } from '@octap/contracts';
|
|
2
|
+
export interface BrowserClientOptions {
|
|
3
|
+
apiBaseUrl: string;
|
|
4
|
+
googleClientId?: string;
|
|
5
|
+
organizationId?: string;
|
|
6
|
+
getAuthToken?: () => Promise<string | null> | string | null;
|
|
7
|
+
}
|
|
8
|
+
export declare class OctaBrowserClient {
|
|
9
|
+
private readonly apiBaseUrl;
|
|
10
|
+
private readonly defaultOrganizationId?;
|
|
11
|
+
private readonly getAuthToken?;
|
|
12
|
+
constructor(options: BrowserClientOptions);
|
|
13
|
+
private request;
|
|
14
|
+
/**
|
|
15
|
+
* Get credit balance for a member.
|
|
16
|
+
*/
|
|
17
|
+
getBalance(memberId: string): Promise<CreditBalanceSnapshot>;
|
|
18
|
+
/**
|
|
19
|
+
* List loyalty transactions for a member.
|
|
20
|
+
*/
|
|
21
|
+
listTransactions(memberId: string, limit?: number): Promise<LoyaltyTransactionDto[]>;
|
|
22
|
+
/**
|
|
23
|
+
* List active vouchers in the organization.
|
|
24
|
+
*/
|
|
25
|
+
listVouchers(organizationId?: string): Promise<VoucherDto[]>;
|
|
26
|
+
/**
|
|
27
|
+
* Get details of a specific voucher.
|
|
28
|
+
*/
|
|
29
|
+
getVoucher(voucherId: string): Promise<VoucherDto>;
|
|
30
|
+
/**
|
|
31
|
+
* Claim a voucher using loyalty points.
|
|
32
|
+
*/
|
|
33
|
+
claimVoucher(voucherId: string, organizationEndUserId: string, idempotencyKey?: string, organizationId?: string): Promise<ClaimVoucherResponse>;
|
|
34
|
+
/**
|
|
35
|
+
* List all vouchers claimed/owned by a member.
|
|
36
|
+
*/
|
|
37
|
+
listMemberVouchers(memberId: string): Promise<VoucherOwnershipDto[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Resolves a Google id_token (obtained from the host app's own Google Sign-In; this SDK does
|
|
40
|
+
* not render a sign-in button) to a zkLogin Sui address and a global end-user identity.
|
|
41
|
+
*/
|
|
42
|
+
loginWithGoogle(idToken: string): Promise<GoogleLoginResponse>;
|
|
43
|
+
/**
|
|
44
|
+
* Enrolls an already-logged-in end user into one organization's loyalty program.
|
|
45
|
+
*/
|
|
46
|
+
enroll(endUserId: string, organizationId?: string, externalMemberId?: string): Promise<EnrollMemberResponse>;
|
|
47
|
+
}
|
|
48
|
+
export declare function createBrowserClient(options: BrowserClientOptions): OctaBrowserClient;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
export class OctaBrowserClient {
|
|
2
|
+
apiBaseUrl;
|
|
3
|
+
defaultOrganizationId;
|
|
4
|
+
getAuthToken;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.apiBaseUrl = options.apiBaseUrl.replace(/\/+$/, '');
|
|
7
|
+
this.defaultOrganizationId = options.organizationId;
|
|
8
|
+
this.getAuthToken = options.getAuthToken;
|
|
9
|
+
}
|
|
10
|
+
async request(path, method = 'GET', body, orgId) {
|
|
11
|
+
const url = `${this.apiBaseUrl}${path.startsWith('/') ? path : `/${path}`}`;
|
|
12
|
+
const headers = {
|
|
13
|
+
accept: 'application/json',
|
|
14
|
+
};
|
|
15
|
+
if (body !== undefined) {
|
|
16
|
+
headers['content-type'] = 'application/json';
|
|
17
|
+
}
|
|
18
|
+
const organizationId = orgId ?? this.defaultOrganizationId;
|
|
19
|
+
if (organizationId) {
|
|
20
|
+
headers['x-organization-id'] = organizationId;
|
|
21
|
+
}
|
|
22
|
+
if (this.getAuthToken) {
|
|
23
|
+
const token = await this.getAuthToken();
|
|
24
|
+
if (token) {
|
|
25
|
+
headers['authorization'] = `Bearer ${token}`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const response = await fetch(url, {
|
|
29
|
+
method,
|
|
30
|
+
headers,
|
|
31
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
32
|
+
});
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
let errorBody = null;
|
|
35
|
+
try {
|
|
36
|
+
errorBody = await response.json();
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
errorBody = await response.text();
|
|
40
|
+
}
|
|
41
|
+
throw new Error(typeof errorBody === 'object' && errorBody !== null && 'message' in errorBody
|
|
42
|
+
? String(errorBody.message)
|
|
43
|
+
: `Octa Browser SDK request failed: ${response.status} ${response.statusText}`);
|
|
44
|
+
}
|
|
45
|
+
return response.json();
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get credit balance for a member.
|
|
49
|
+
*/
|
|
50
|
+
async getBalance(memberId) {
|
|
51
|
+
return this.request(`/v1/credits/members/${memberId}/balance`, 'GET');
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* List loyalty transactions for a member.
|
|
55
|
+
*/
|
|
56
|
+
async listTransactions(memberId, limit = 50) {
|
|
57
|
+
return this.request(`/v1/credits/members/${memberId}/transactions?limit=${limit}`, 'GET');
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* List active vouchers in the organization.
|
|
61
|
+
*/
|
|
62
|
+
async listVouchers(organizationId) {
|
|
63
|
+
return this.request('/v1/rewards/vouchers', 'GET', undefined, organizationId);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get details of a specific voucher.
|
|
67
|
+
*/
|
|
68
|
+
async getVoucher(voucherId) {
|
|
69
|
+
return this.request(`/v1/rewards/vouchers/${voucherId}`, 'GET');
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Claim a voucher using loyalty points.
|
|
73
|
+
*/
|
|
74
|
+
async claimVoucher(voucherId, organizationEndUserId, idempotencyKey, organizationId) {
|
|
75
|
+
return this.request(`/v1/rewards/vouchers/${voucherId}/claim`, 'POST', {
|
|
76
|
+
organizationEndUserId,
|
|
77
|
+
idempotencyKey: idempotencyKey ?? `claim-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`,
|
|
78
|
+
}, organizationId);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* List all vouchers claimed/owned by a member.
|
|
82
|
+
*/
|
|
83
|
+
async listMemberVouchers(memberId) {
|
|
84
|
+
return this.request(`/v1/rewards/members/${memberId}/vouchers`, 'GET');
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Resolves a Google id_token (obtained from the host app's own Google Sign-In; this SDK does
|
|
88
|
+
* not render a sign-in button) to a zkLogin Sui address and a global end-user identity.
|
|
89
|
+
*/
|
|
90
|
+
async loginWithGoogle(idToken) {
|
|
91
|
+
return this.request('/v1/identity/google-login', 'POST', { idToken });
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Enrolls an already-logged-in end user into one organization's loyalty program.
|
|
95
|
+
*/
|
|
96
|
+
async enroll(endUserId, organizationId, externalMemberId) {
|
|
97
|
+
const orgId = organizationId ?? this.defaultOrganizationId;
|
|
98
|
+
if (!orgId) {
|
|
99
|
+
throw new Error('organizationId is required (pass it or set BrowserClientOptions.organizationId)');
|
|
100
|
+
}
|
|
101
|
+
return this.request(`/v1/identity/organizations/${orgId}/enroll`, 'POST', {
|
|
102
|
+
endUserId,
|
|
103
|
+
externalMemberId,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export function createBrowserClient(options) {
|
|
108
|
+
return new OctaBrowserClient(options);
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAiBA,MAAM,OAAO,iBAAiB;IACX,UAAU,CAAS;IACnB,qBAAqB,CAAU;IAC/B,YAAY,CAAgD;IAE7E,YAAY,OAA6B;QACvC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QACzD,IAAI,CAAC,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC;QACpD,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAC3C,CAAC;IAEO,KAAK,CAAC,OAAO,CAAI,IAAY,EAAE,SAAyB,KAAK,EAAE,IAAc,EAAE,KAAc;QACnG,MAAM,GAAG,GAAG,GAAG,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,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,kBAAkB,CAAC,QAAgB;QACvC,OAAO,IAAI,CAAC,OAAO,CAAwB,uBAAuB,QAAQ,WAAW,EAAE,KAAK,CAAC,CAAC;IAChG,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,eAAe,CAAC,OAAe;QACnC,OAAO,IAAI,CAAC,OAAO,CAAsB,2BAA2B,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;IAC7F,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM,CAAC,SAAiB,EAAE,cAAuB,EAAE,gBAAyB;QAChF,MAAM,KAAK,GAAG,cAAc,IAAI,IAAI,CAAC,qBAAqB,CAAC;QAC3D,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,iFAAiF,CAAC,CAAC;QACrG,CAAC;QACD,OAAO,IAAI,CAAC,OAAO,CAAuB,8BAA8B,KAAK,SAAS,EAAE,MAAM,EAAE;YAC9F,SAAS;YACT,gBAAgB;SACjB,CAAC,CAAC;IACL,CAAC;CACF;AAED,MAAM,UAAU,mBAAmB,CAAC,OAA6B;IAC/D,OAAO,IAAI,iBAAiB,CAAC,OAAO,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface RequestOptions {
|
|
2
|
+
signal?: AbortSignal;
|
|
3
|
+
timeoutMs?: number;
|
|
4
|
+
}
|
|
5
|
+
export declare const normalizeBaseUrl: (baseUrl: string) => string;
|
|
6
|
+
/** The public Core routes a customer's device may call. It holds no credential of any kind. */
|
|
7
|
+
export declare class CoreApi {
|
|
8
|
+
private readonly fetchFn;
|
|
9
|
+
private readonly defaultTimeoutMs;
|
|
10
|
+
private readonly baseUrl;
|
|
11
|
+
constructor(baseUrl: string, fetchFn: typeof fetch, defaultTimeoutMs: number);
|
|
12
|
+
post<T>(path: string, payload: unknown, options?: RequestOptions): Promise<T>;
|
|
13
|
+
}
|
package/dist/core-api.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { OctapAuthError } from './errors.js';
|
|
2
|
+
export const normalizeBaseUrl = (baseUrl) => baseUrl.replace(/\/+$/, '');
|
|
3
|
+
/** Reads a body once, whatever its type: an error page must not surface as a `TypeError`. */
|
|
4
|
+
async function readBody(response) {
|
|
5
|
+
const text = await response.text();
|
|
6
|
+
try {
|
|
7
|
+
return JSON.parse(text);
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return text;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function errorMessage(response, body) {
|
|
14
|
+
if (typeof body === 'object' && body !== null && 'message' in body) {
|
|
15
|
+
return String(body.message);
|
|
16
|
+
}
|
|
17
|
+
return `OctaP request failed: ${response.status} ${response.statusText}`.trim();
|
|
18
|
+
}
|
|
19
|
+
/** The public Core routes a customer's device may call. It holds no credential of any kind. */
|
|
20
|
+
export class CoreApi {
|
|
21
|
+
fetchFn;
|
|
22
|
+
defaultTimeoutMs;
|
|
23
|
+
baseUrl;
|
|
24
|
+
constructor(baseUrl, fetchFn, defaultTimeoutMs) {
|
|
25
|
+
this.fetchFn = fetchFn;
|
|
26
|
+
this.defaultTimeoutMs = defaultTimeoutMs;
|
|
27
|
+
this.baseUrl = normalizeBaseUrl(baseUrl);
|
|
28
|
+
}
|
|
29
|
+
async post(path, payload, options = {}) {
|
|
30
|
+
const timeout = AbortSignal.timeout(options.timeoutMs ?? this.defaultTimeoutMs);
|
|
31
|
+
const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
|
|
32
|
+
let response;
|
|
33
|
+
let body;
|
|
34
|
+
try {
|
|
35
|
+
response = await this.fetchFn(`${this.baseUrl}${path}`, {
|
|
36
|
+
method: 'POST',
|
|
37
|
+
headers: { accept: 'application/json', 'content-type': 'application/json' },
|
|
38
|
+
body: JSON.stringify(payload),
|
|
39
|
+
signal,
|
|
40
|
+
});
|
|
41
|
+
body = await readBody(response);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
if (options.signal?.aborted) {
|
|
45
|
+
throw new OctapAuthError('aborted', 'The request was aborted.');
|
|
46
|
+
}
|
|
47
|
+
if (timeout.aborted) {
|
|
48
|
+
throw new OctapAuthError('timeout', 'The request timed out.');
|
|
49
|
+
}
|
|
50
|
+
throw new OctapAuthError('network-error', `Could not reach OctaP: ${err?.message ?? String(err)}`);
|
|
51
|
+
}
|
|
52
|
+
if (!response.ok) {
|
|
53
|
+
throw new OctapAuthError('api-error', errorMessage(response, body), { status: response.status, body });
|
|
54
|
+
}
|
|
55
|
+
if (typeof body !== 'object' || body === null) {
|
|
56
|
+
throw new OctapAuthError('invalid-response', 'OctaP returned a response this SDK cannot read.', {
|
|
57
|
+
status: response.status,
|
|
58
|
+
body,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return body;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=core-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-api.js","sourceRoot":"","sources":["../src/core-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAO7C,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,OAAe,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAEjF,6FAA6F;AAC7F,KAAK,UAAU,QAAQ,CAAC,QAAkB;IACxC,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IACnC,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAY,CAAC;IACrC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,SAAS,YAAY,CAAC,QAAkB,EAAE,IAAa;IACrD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,SAAS,IAAI,IAA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B,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;gBAC5B,MAAM,IAAI,cAAc,CAAC,SAAS,EAAE,0BAA0B,CAAC,CAAC;YAClE,CAAC;YACD,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;gBACpB,MAAM,IAAI,cAAc,CAAC,SAAS,EAAE,wBAAwB,CAAC,CAAC;YAChE,CAAC;YACD,MAAM,IAAI,cAAc,CAAC,eAAe,EAAE,0BAA2B,GAAa,EAAE,OAAO,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAChH,CAAC;QAED,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,cAAc,CAAC,WAAW,EAAE,YAAY,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;QACzG,CAAC;QACD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YAC9C,MAAM,IAAI,cAAc,CAAC,kBAAkB,EAAE,iDAAiD,EAAE;gBAC9F,MAAM,EAAE,QAAQ,CAAC,MAAM;gBACvB,IAAI;aACL,CAAC,CAAC;QACL,CAAC;QACD,OAAO,IAAS,CAAC;IACnB,CAAC;CACF"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export type OctapAuthErrorCode =
|
|
2
|
+
/** The customer closed Google's sign-in window. */
|
|
3
|
+
'sign-in-canceled'
|
|
4
|
+
/** Google's token was not issued for this sign-in attempt. */
|
|
5
|
+
| 'nonce-mismatch'
|
|
6
|
+
/** Google's sign-in script could not load, or the SDK ran outside a browser. */
|
|
7
|
+
| 'provider-unavailable'
|
|
8
|
+
/** No customer is signed in. */
|
|
9
|
+
| 'not-signed-in'
|
|
10
|
+
/** The signing session passed its `maxEpoch`, so the customer must sign in again. */
|
|
11
|
+
| 'session-expired'
|
|
12
|
+
/** Google's id_token expired. The signing session may still work, but the backend will refuse the token. */
|
|
13
|
+
| 'token-expired'
|
|
14
|
+
/** The transaction is for a different address than the signed-in customer's. */
|
|
15
|
+
| 'wrong-account'
|
|
16
|
+
/** The claim has nothing to sign, such as a free reward that is already AVAILABLE. */
|
|
17
|
+
| 'invalid-claim'
|
|
18
|
+
/** Finalize may have reached the chain. Read the claim's state; do not sign again with a new key. */
|
|
19
|
+
| 'outcome-unknown'
|
|
20
|
+
/** The server answered with an error; see `status` and `body`. */
|
|
21
|
+
| 'api-error' | 'network-error' | 'timeout' | 'aborted'
|
|
22
|
+
/** The server answered successfully, with a body this SDK cannot use. */
|
|
23
|
+
| 'invalid-response';
|
|
24
|
+
export declare class OctapAuthError extends Error {
|
|
25
|
+
readonly code: OctapAuthErrorCode;
|
|
26
|
+
readonly status?: number;
|
|
27
|
+
readonly body?: unknown;
|
|
28
|
+
constructor(code: OctapAuthErrorCode, message: string, details?: {
|
|
29
|
+
status?: number;
|
|
30
|
+
body?: unknown;
|
|
31
|
+
});
|
|
32
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export class OctapAuthError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
status;
|
|
4
|
+
body;
|
|
5
|
+
constructor(code, message, details = {}) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'OctapAuthError';
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.status = details.status;
|
|
10
|
+
this.body = details.body;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AA2BA,MAAM,OAAO,cAAe,SAAQ,KAAK;IAC9B,IAAI,CAAqB;IACzB,MAAM,CAAU;IAChB,IAAI,CAAW;IAExB,YAAY,IAAwB,EAAE,OAAe,EAAE,UAA+C,EAAE;QACtG,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;QAC7B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAC3B,CAAC;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider sign-in: turns a click into an id_token bound to a nonce. Nothing outside this file knows
|
|
3
|
+
* it is Google, which keeps a later provider a change here, not in the public API.
|
|
4
|
+
*/
|
|
5
|
+
export interface SignInHandlers {
|
|
6
|
+
onIdToken(idToken: string): void;
|
|
7
|
+
onCanceled(): void;
|
|
8
|
+
}
|
|
9
|
+
export interface IdTokenProvider {
|
|
10
|
+
render(element: HTMLElement, nonce: string, handlers: SignInHandlers): Promise<void>;
|
|
11
|
+
signOut(): void;
|
|
12
|
+
}
|
|
13
|
+
export declare function createGoogleProvider(clientId: string, buttonOptions?: Record<string, unknown>): IdTokenProvider;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { OctapAuthError } from './errors.js';
|
|
2
|
+
const SCRIPT_URL = 'https://accounts.google.com/gsi/client';
|
|
3
|
+
const SCRIPT_TIMEOUT_MS = 20_000;
|
|
4
|
+
/** A credential can arrive just after the popup closes, so wait before calling it a cancellation. */
|
|
5
|
+
const CANCEL_GRACE_MS = 1_500;
|
|
6
|
+
const CANCEL_POLL_MS = 250;
|
|
7
|
+
const CANCEL_WATCH_LIMIT_MS = 5 * 60_000;
|
|
8
|
+
let loading;
|
|
9
|
+
function loadGoogleIdentity() {
|
|
10
|
+
if (typeof window === 'undefined' || typeof document === 'undefined') {
|
|
11
|
+
return Promise.reject(new OctapAuthError('provider-unavailable', 'Google sign-in needs a browser.'));
|
|
12
|
+
}
|
|
13
|
+
const loaded = window.google?.accounts?.id;
|
|
14
|
+
if (loaded)
|
|
15
|
+
return Promise.resolve(loaded);
|
|
16
|
+
if (loading)
|
|
17
|
+
return loading;
|
|
18
|
+
loading = new Promise((resolve, reject) => {
|
|
19
|
+
const script = document.createElement('script');
|
|
20
|
+
script.src = SCRIPT_URL;
|
|
21
|
+
script.async = true;
|
|
22
|
+
const fail = () => {
|
|
23
|
+
window.clearTimeout(timer);
|
|
24
|
+
script.remove();
|
|
25
|
+
loading = undefined;
|
|
26
|
+
reject(new OctapAuthError('provider-unavailable', 'Google sign-in could not load.'));
|
|
27
|
+
};
|
|
28
|
+
const timer = window.setTimeout(fail, SCRIPT_TIMEOUT_MS);
|
|
29
|
+
script.onerror = fail;
|
|
30
|
+
script.onload = () => {
|
|
31
|
+
const google = window.google?.accounts?.id;
|
|
32
|
+
if (!google) {
|
|
33
|
+
fail();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
window.clearTimeout(timer);
|
|
37
|
+
resolve(google);
|
|
38
|
+
};
|
|
39
|
+
document.head.appendChild(script);
|
|
40
|
+
});
|
|
41
|
+
return loading;
|
|
42
|
+
}
|
|
43
|
+
export function createGoogleProvider(clientId, buttonOptions = {}) {
|
|
44
|
+
let google;
|
|
45
|
+
let stopWatching;
|
|
46
|
+
return {
|
|
47
|
+
async render(element, nonce, handlers) {
|
|
48
|
+
google = await loadGoogleIdentity();
|
|
49
|
+
google.initialize({
|
|
50
|
+
client_id: clientId,
|
|
51
|
+
auto_select: false,
|
|
52
|
+
nonce,
|
|
53
|
+
callback: ({ credential }) => {
|
|
54
|
+
stopWatching?.();
|
|
55
|
+
handlers.onIdToken(credential);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
google.renderButton(element, {
|
|
59
|
+
theme: 'outline',
|
|
60
|
+
size: 'large',
|
|
61
|
+
...buttonOptions,
|
|
62
|
+
click_listener: () => {
|
|
63
|
+
stopWatching?.();
|
|
64
|
+
stopWatching = watchForCancel(handlers.onCanceled);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
signOut() {
|
|
69
|
+
stopWatching?.();
|
|
70
|
+
google?.disableAutoSelect();
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Google's button reports no cancellation. Its popup takes focus from this document and gives it back
|
|
76
|
+
* when it closes, so focus returning with no credential after a short wait means it was closed. This
|
|
77
|
+
* polls because Google's button is a cross-origin iframe, which makes `blur` and `focus` events on the
|
|
78
|
+
* window fire unreliably. A customer who clicks back while the popup is open also looks like this,
|
|
79
|
+
* and a credential arriving afterwards still completes the sign-in.
|
|
80
|
+
*/
|
|
81
|
+
function watchForCancel(onCanceled) {
|
|
82
|
+
const startedAt = Date.now();
|
|
83
|
+
let popupSeen = false;
|
|
84
|
+
let focusedSince;
|
|
85
|
+
const timer = window.setInterval(() => {
|
|
86
|
+
if (Date.now() - startedAt >= CANCEL_WATCH_LIMIT_MS) {
|
|
87
|
+
stop();
|
|
88
|
+
}
|
|
89
|
+
else if (!document.hasFocus()) {
|
|
90
|
+
popupSeen = true;
|
|
91
|
+
focusedSince = undefined;
|
|
92
|
+
}
|
|
93
|
+
else if (popupSeen) {
|
|
94
|
+
focusedSince ??= Date.now();
|
|
95
|
+
if (Date.now() - focusedSince >= CANCEL_GRACE_MS) {
|
|
96
|
+
stop();
|
|
97
|
+
onCanceled();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}, CANCEL_POLL_MS);
|
|
101
|
+
const stop = () => window.clearInterval(timer);
|
|
102
|
+
return stop;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=google-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-provider.js","sourceRoot":"","sources":["../src/google-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AA6B7C,MAAM,UAAU,GAAG,wCAAwC,CAAC;AAC5D,MAAM,iBAAi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ACF,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,cAAc,CAAC,UAAsB;IAC5C,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC7B,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,IAAI,YAAgC,CAAC;IACrC,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE;QACpC,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,IAAI,qBAAqB,EAAE,CAAC;YACpD,IAAI,EAAE,CAAC;QACT,CAAC;aAAM,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,CAAC;YAChC,SAAS,GAAG,IAAI,CAAC;YACjB,YAAY,GAAG,SAAS,CAAC;QAC3B,CAAC;aAAM,IAAI,SAAS,EAAE,CAAC;YACrB,YAAY,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC;YAC5B,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,YAAY,IAAI,eAAe,EAAE,CAAC;gBACjD,IAAI,EAAE,CAAC;gBACP,UAAU,EAAE,CAAC;YACf,CAAC;QACH,CAAC;IACH,CAAC,EAAE,cAAc,CAAC,CAAC;IACnB,MAAM,IAAI,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IAC/C,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { OctapAuth, createOctapAuth, type AuthStateListener, type FinalizedClaim, type FinalizedVoucherUse, type OctapAuthOptions, type RenderOptions, type SignableClaim, type SignableVoucherUse, } from './auth.js';
|
|
2
|
+
export type { RequestOptions } from './core-api.js';
|
|
3
|
+
export { OctapAuthError, type OctapAuthErrorCode } from './errors.js';
|
|
4
|
+
export type { OctapUser, Persistence } from './session-store.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,eAAe,GAQhB,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,cAAc,EAA2B,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type SignerState } from './zklogin-signer.js';
|
|
2
|
+
export interface OctapUser {
|
|
3
|
+
endUserId: string;
|
|
4
|
+
/** The customer's `organizationEndUserId` in this organization. */
|
|
5
|
+
memberId: string;
|
|
6
|
+
suiAddress: string;
|
|
7
|
+
}
|
|
8
|
+
export interface StoredSession {
|
|
9
|
+
user: OctapUser;
|
|
10
|
+
idToken: string;
|
|
11
|
+
signer: SignerState;
|
|
12
|
+
}
|
|
13
|
+
export type Persistence = 'session' | 'memory';
|
|
14
|
+
export interface SessionStore {
|
|
15
|
+
load(): StoredSession | null;
|
|
16
|
+
save(session: StoredSession): void;
|
|
17
|
+
clear(): void;
|
|
18
|
+
}
|
|
19
|
+
/** A session belongs to one configuration: another API or Google client must not restore it. */
|
|
20
|
+
export interface SessionScope {
|
|
21
|
+
apiBaseUrl: string;
|
|
22
|
+
googleClientId: string;
|
|
23
|
+
organizationId: string;
|
|
24
|
+
}
|
|
25
|
+
export declare function createSessionStore(persistence: Persistence, scope: SessionScope): SessionStore;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { isZkProof } from './zklogin-signer.js';
|
|
2
|
+
function memoryStore() {
|
|
3
|
+
let held = null;
|
|
4
|
+
return {
|
|
5
|
+
load: () => held,
|
|
6
|
+
save: (session) => {
|
|
7
|
+
held = session;
|
|
8
|
+
},
|
|
9
|
+
clear: () => {
|
|
10
|
+
held = null;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function isStoredSession(value) {
|
|
15
|
+
const session = value;
|
|
16
|
+
return (typeof session?.user?.endUserId === 'string' &&
|
|
17
|
+
typeof session.user.memberId === 'string' &&
|
|
18
|
+
typeof session.user.suiAddress === 'string' &&
|
|
19
|
+
typeof session.idToken === 'string' &&
|
|
20
|
+
typeof session.signer?.secretKey === 'string' &&
|
|
21
|
+
typeof session.signer.maxEpoch === 'number' &&
|
|
22
|
+
typeof session.signer.expiresAt === 'number' &&
|
|
23
|
+
isZkProof(session.signer.proof));
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Browser storage can be missing or throw (private windows, blocked site data). Losing it must only
|
|
27
|
+
* cost a sign-in after reload, so every access falls back to the copy held in memory.
|
|
28
|
+
*/
|
|
29
|
+
function browserStore(storage, key) {
|
|
30
|
+
const memory = memoryStore();
|
|
31
|
+
return {
|
|
32
|
+
load() {
|
|
33
|
+
try {
|
|
34
|
+
const raw = storage.getItem(key);
|
|
35
|
+
const parsed = raw ? JSON.parse(raw) : null;
|
|
36
|
+
if (isStoredSession(parsed))
|
|
37
|
+
return parsed;
|
|
38
|
+
if (raw)
|
|
39
|
+
storage.removeItem(key);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// Unreadable storage is the same as no stored session.
|
|
43
|
+
}
|
|
44
|
+
return memory.load();
|
|
45
|
+
},
|
|
46
|
+
save(session) {
|
|
47
|
+
memory.save(session);
|
|
48
|
+
try {
|
|
49
|
+
storage.setItem(key, JSON.stringify(session));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// Kept in memory only: the session lasts until reload.
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
clear() {
|
|
56
|
+
memory.clear();
|
|
57
|
+
try {
|
|
58
|
+
storage.removeItem(key);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Nothing could have been written either.
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export function createSessionStore(persistence, scope) {
|
|
67
|
+
if (persistence === 'memory')
|
|
68
|
+
return memoryStore();
|
|
69
|
+
let storage;
|
|
70
|
+
try {
|
|
71
|
+
storage = globalThis.sessionStorage;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
storage = undefined;
|
|
75
|
+
}
|
|
76
|
+
const key = `octap.auth.${JSON.stringify([scope.apiBaseUrl, scope.googleClientId, scope.organizationId])}`;
|
|
77
|
+
return storage ? browserStore(storage, key) : memoryStore();
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=session-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-store.js","sourceRoot":"","sources":["../src/session-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAoB,MAAM,qBAAqB,CAAC;AAuBlE,SAAS,WAAW;IAClB,IAAI,IAAI,GAAyB,IAAI,CAAC;IACtC,OAAO;QACL,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI;QAChB,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE;YAChB,IAAI,GAAG,OAAO,CAAC;QACjB,CAAC;QACD,KAAK,EAAE,GAAG,EAAE;YACV,IAAI,GAAG,IAAI,CAAC;QACd,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,eAAe,CAAC,KAAc;IACrC,MAAM,OAAO,GAAG,KAAsC,CAAC;IACvD,OAAO,CACL,OAAO,OAAO,EAAE,IAAI,EAAE,SAAS,KAAK,QAAQ;QAC5C,OAAO,OAAO,CAAC,IAAI,CAAC,QAAQ,KAAK,QAAQ;QACzC,OAAO,OAAO,CAAC,IAAI,CAAC,UAAU,KAAK,QAAQ;QAC3C,OAAO,OAAO,CAAC,OAAO,KAAK,QAAQ;QACnC,OAAO,OAAO,CAAC,MAAM,EAAE,SAAS,KAAK,QAAQ;QAC7C,OAAO,OAAO,CAAC,MAAM,CAAC,QAAQ,KAAK,QAAQ;QAC3C,OAAO,OAAO,CAAC,MAAM,CAAC,SAAS,KAAK,QAAQ;QAC5C,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAChC,CAAC;AACJ,CAAC;AAED;;;GAGG;A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AO,GAAG,SAAS,CAAC;IACtB,CAAC;IACD,MAAM,GAAG,GAAG,cAAc,IAAI,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,cAAc,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC;IAC3G,OAAO,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AAC9D,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
|
|
2
|
+
import type { CoreApi } from './core-api.js';
|
|
3
|
+
export interface SignerState {
|
|
4
|
+
secretKey: string;
|
|
5
|
+
maxEpoch: number;
|
|
6
|
+
/** Core's estimate of when `maxEpoch` ends, in milliseconds since the epoch. */
|
|
7
|
+
expiresAt: number;
|
|
8
|
+
proof: ZkLoginSignatureInputs;
|
|
9
|
+
}
|
|
10
|
+
export declare function isZkProof(value: unknown): value is ZkLoginSignatureInputs;
|
|
11
|
+
/** A key and nonce waiting for the customer to sign in. */
|
|
12
|
+
export declare class PendingSignIn {
|
|
13
|
+
private readonly keypair;
|
|
14
|
+
private readonly params;
|
|
15
|
+
private constructor();
|
|
16
|
+
static start(api: CoreApi): Promise<PendingSignIn>;
|
|
17
|
+
get nonce(): string;
|
|
18
|
+
/** A flow check, not token validation: Enoki validates the token's signature. */
|
|
19
|
+
assertIssuedForThisAttempt(idToken: string): void;
|
|
20
|
+
prove(api: CoreApi, idToken: string): Promise<SignerState>;
|
|
21
|
+
}
|
|
22
|
+
/** Seconds since the epoch at which an id_token stops being valid, or undefined if unreadable. */
|
|
23
|
+
export declare function idTokenExpiry(idToken: string): number | undefined;
|
|
24
|
+
/** Signs for the signed-in customer, refusing bytes that name a different sender. */
|
|
25
|
+
export declare function signWithZkLogin(state: SignerState, suiAddress: string, transactionBytes: string): Promise<string>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
2
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
3
|
+
import { fromBase64 } from '@mysten/sui/utils';
|
|
4
|
+
import { getZkLoginSignature } from '@mysten/sui/zklogin';
|
|
5
|
+
import { OctapAuthError } from './errors.js';
|
|
6
|
+
export function isZkProof(value) {
|
|
7
|
+
const proof = value;
|
|
8
|
+
return (typeof proof?.addressSeed === 'string' &&
|
|
9
|
+
typeof proof.headerBase64 === 'string' &&
|
|
10
|
+
typeof proof.issBase64Details?.value === 'string' &&
|
|
11
|
+
Array.isArray(proof.proofPoints?.a) &&
|
|
12
|
+
Array.isArray(proof.proofPoints.b) &&
|
|
13
|
+
Array.isArray(proof.proofPoints.c));
|
|
14
|
+
}
|
|
15
|
+
/** A key and nonce waiting for the customer to sign in. */
|
|
16
|
+
export class PendingSignIn {
|
|
17
|
+
keypair;
|
|
18
|
+
params;
|
|
19
|
+
constructor(keypair, params) {
|
|
20
|
+
this.keypair = keypair;
|
|
21
|
+
this.params = params;
|
|
22
|
+
}
|
|
23
|
+
static async start(api) {
|
|
24
|
+
const keypair = new Ed25519Keypair();
|
|
25
|
+
const params = await api.post('/v1/identity/zklogin/nonce', {
|
|
26
|
+
ephemeralPublicKey: keypair.getPublicKey().toSuiPublicKey(),
|
|
27
|
+
});
|
|
28
|
+
if (typeof params.nonce !== 'string' ||
|
|
29
|
+
!params.nonce ||
|
|
30
|
+
typeof params.randomness !== 'string' ||
|
|
31
|
+
!Number.isFinite(params.maxEpoch) ||
|
|
32
|
+
!Number.isFinite(params.estimatedExpiration)) {
|
|
33
|
+
throw new OctapAuthError('invalid-response', 'OctaP returned an invalid sign-in nonce.', { body: params });
|
|
34
|
+
}
|
|
35
|
+
return new PendingSignIn(keypair, params);
|
|
36
|
+
}
|
|
37
|
+
get nonce() {
|
|
38
|
+
return this.params.nonce;
|
|
39
|
+
}
|
|
40
|
+
/** A flow check, not token validation: Enoki validates the token's signature. */
|
|
41
|
+
assertIssuedForThisAttempt(idToken) {
|
|
42
|
+
if (Date.now() >= this.params.estimatedExpiration) {
|
|
43
|
+
throw new OctapAuthError('session-expired', 'This sign-in attempt expired. Sign in again.');
|
|
44
|
+
}
|
|
45
|
+
let nonce;
|
|
46
|
+
try {
|
|
47
|
+
nonce = decodePayload(idToken).nonce;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
throw new OctapAuthError('nonce-mismatch', 'Google returned an unreadable token. Sign in again.');
|
|
51
|
+
}
|
|
52
|
+
if (nonce !== this.params.nonce) {
|
|
53
|
+
throw new OctapAuthError('nonce-mismatch', 'Google sign-in did not match this attempt. Sign in again.');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async prove(api, idToken) {
|
|
57
|
+
const proof = await api.post('/v1/identity/zklogin/proof', {
|
|
58
|
+
idToken,
|
|
59
|
+
ephemeralPublicKey: this.keypair.getPublicKey().toSuiPublicKey(),
|
|
60
|
+
randomness: this.params.randomness,
|
|
61
|
+
maxEpoch: this.params.maxEpoch,
|
|
62
|
+
});
|
|
63
|
+
if (!isZkProof(proof)) {
|
|
64
|
+
throw new OctapAuthError('invalid-response', 'OctaP returned an invalid signing proof.', { body: proof });
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
secretKey: this.keypair.getSecretKey(),
|
|
68
|
+
maxEpoch: this.params.maxEpoch,
|
|
69
|
+
expiresAt: this.params.estimatedExpiration,
|
|
70
|
+
proof,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function decodePayload(idToken) {
|
|
75
|
+
const payload = idToken.split('.')[1];
|
|
76
|
+
const bytes = Uint8Array.from(atob(payload.replace(/-/g, '+').replace(/_/g, '/')), (c) => c.charCodeAt(0));
|
|
77
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
78
|
+
}
|
|
79
|
+
/** Seconds since the epoch at which an id_token stops being valid, or undefined if unreadable. */
|
|
80
|
+
export function idTokenExpiry(idToken) {
|
|
81
|
+
try {
|
|
82
|
+
const { exp } = decodePayload(idToken);
|
|
83
|
+
return typeof exp === 'number' ? exp : undefined;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Signs for the signed-in customer, refusing bytes that name a different sender. */
|
|
90
|
+
export async function signWithZkLogin(state, suiAddress, transactionBytes) {
|
|
91
|
+
let bytes;
|
|
92
|
+
let sender;
|
|
93
|
+
try {
|
|
94
|
+
bytes = fromBase64(transactionBytes);
|
|
95
|
+
sender = Transaction.from(bytes).getData().sender;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
throw new OctapAuthError('invalid-claim', 'These transaction bytes cannot be read.');
|
|
99
|
+
}
|
|
100
|
+
if (sender !== suiAddress) {
|
|
101
|
+
throw new OctapAuthError('wrong-account', 'This transaction belongs to a different account than the one signed in. Sign in with that account.');
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const { signature: userSignature } = await Ed25519Keypair.fromSecretKey(state.secretKey).signTransaction(bytes);
|
|
105
|
+
return getZkLoginSignature({ inputs: state.proof, maxEpoch: state.maxEpoch, userSignature });
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
throw new OctapAuthError('session-expired', 'The signing session cannot be used. Sign in again.');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=zklogin-signer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zklogin-signer.js","sourceRoot":"","sources":["../src/zklogin-signer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,mBAAmB,EAA+B,MAAM,qBA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AAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC,MAAM,CAAC;IACpD,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,cAAc,CAAC,eAAe,EAAE,yCAAyC,CAAC,CAAC;IACvF,CAAC;IACD,IAAI,MAAM,KAAK,UAAU,EAAE,CAAC;QAC1B,MAAM,IAAI,cAAc,CACtB,eAAe,EACf,oGAAoG,CACrG,CAAC;IACJ,CAAC;IACD,IAAI,CAAC;QACH,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,GAAG,MAAM,cAAc,CAAC,aAAa,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QAChH,OAAO,mBAAmB,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,aAAa,EAAE,CAAC,CAAC;IAC/F,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,cAAc,CAAC,iBAAiB,EAAE,oDAAoD,CAAC,CAAC;IACpG,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octap/browser-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Firebase-style customer sign-in and transaction signing for OctaP loyalty programs.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc -p tsconfig.json",
|
|
17
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@mysten/sui": "^2.38.0"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md"
|
|
28
|
+
]
|
|
29
|
+
}
|