@ziqx/auth 0.0.4 → 1.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 +78 -15
- package/dist/index.d.mts +41 -17
- package/dist/index.d.ts +41 -17
- package/dist/index.js +60 -16
- package/dist/index.mjs +60 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,6 +27,11 @@ import { ZAuthClient } from "@ziqx/auth";
|
|
|
27
27
|
|
|
28
28
|
const auth = new ZAuthClient({
|
|
29
29
|
authKey: "YOUR_AUTH_KEY",
|
|
30
|
+
redirectUrl: "YOUR_REDIRECT_URL",
|
|
31
|
+
codeChallenge: "YOUR_CODE_CHALLENGE",
|
|
32
|
+
// Optional parameters
|
|
33
|
+
// codeChallengeMethod: "S256",
|
|
34
|
+
// state: "YOUR_STATE_STRING"
|
|
30
35
|
});
|
|
31
36
|
```
|
|
32
37
|
|
|
@@ -41,11 +46,29 @@ auth.login(); // Redirects to live auth environment
|
|
|
41
46
|
If you're working in a development environment, enable dev mode:
|
|
42
47
|
|
|
43
48
|
```typescript
|
|
44
|
-
auth.login(true); // Redirects to dev auth environment
|
|
49
|
+
auth.login(true); // Redirects to dev auth environment (http://localhost:3000/zauth)
|
|
45
50
|
```
|
|
46
51
|
|
|
47
52
|
---
|
|
48
53
|
|
|
54
|
+
## Access Token Exchange
|
|
55
|
+
|
|
56
|
+
You can use the `ZAuthTokenService` class to exchange an authorization code for an access token.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { ZAuthTokenService } from "@ziqx/auth";
|
|
60
|
+
|
|
61
|
+
const tokenService = new ZAuthTokenService();
|
|
62
|
+
|
|
63
|
+
const tokenData = await tokenService.getAuthToken({
|
|
64
|
+
authAppKey: "your-app-key",
|
|
65
|
+
authSecret: "your-app-secret",
|
|
66
|
+
code: "authorization-code",
|
|
67
|
+
codeVerifier: "code-verifier",
|
|
68
|
+
redirectUri: "redirect-uri",
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
49
72
|
## Token Validation
|
|
50
73
|
|
|
51
74
|
You can use the `ZAuthTokenService` class to validate authentication tokens issued by ZIQX.
|
|
@@ -73,7 +96,10 @@ if (isValid) {
|
|
|
73
96
|
#### Constructor
|
|
74
97
|
|
|
75
98
|
```typescript
|
|
76
|
-
new ZAuthClient(options: {
|
|
99
|
+
new ZAuthClient(options: {
|
|
100
|
+
authKey: string;
|
|
101
|
+
|
|
102
|
+
});
|
|
77
103
|
```
|
|
78
104
|
|
|
79
105
|
| Parameter | Type | Required | Description |
|
|
@@ -82,19 +108,26 @@ new ZAuthClient(options: { authKey: string });
|
|
|
82
108
|
|
|
83
109
|
#### Methods
|
|
84
110
|
|
|
85
|
-
##### `login(
|
|
111
|
+
##### `login(): void`
|
|
86
112
|
|
|
87
113
|
Redirects the user to the ZIQX authentication portal.
|
|
88
114
|
|
|
89
|
-
| Parameter
|
|
90
|
-
|
|
|
91
|
-
| `
|
|
115
|
+
| Parameter | Type | Required | Description |
|
|
116
|
+
| --------------------- | ------ | -------- | ----------------------------------------------------------------- |
|
|
117
|
+
| `redirectUrl` | string | ✅ Yes | The URL where the user should be redirected after authentication. |
|
|
118
|
+
| `codeChallenge` | string | ✅ Yes | The code challenge for PKCE flow. |
|
|
119
|
+
| `codeChallengeMethod` | string | ❌ No | (Optional) The code challenge method (e.g., "S256"). |
|
|
120
|
+
| `state` | string | ❌ No | (Optional) An opaque value used to maintain state. |
|
|
92
121
|
|
|
93
122
|
**Example:**
|
|
94
123
|
|
|
95
124
|
```typescript
|
|
96
|
-
auth.login(
|
|
97
|
-
|
|
125
|
+
auth.login({
|
|
126
|
+
redirectUrl: "";
|
|
127
|
+
codeChallenge: "";
|
|
128
|
+
codeChallengeMethod: "";
|
|
129
|
+
state: "";
|
|
130
|
+
}); // Redirects to production portal
|
|
98
131
|
```
|
|
99
132
|
|
|
100
133
|
---
|
|
@@ -105,21 +138,47 @@ auth.login(true); // Redirects to development portal
|
|
|
105
138
|
|
|
106
139
|
##### `validate(token: string): Promise<boolean>`
|
|
107
140
|
|
|
108
|
-
Validates a given authentication token with the ZIQX Auth API.
|
|
141
|
+
Validates a given authentication token with the ZIQX Auth API (V1).
|
|
109
142
|
|
|
110
143
|
| Parameter | Type | Required | Description |
|
|
111
144
|
| --------- | ------ | -------- | ------------------------------------------ |
|
|
112
145
|
| `token` | string | ✅ Yes | The authentication token (JWT or similar). |
|
|
113
146
|
|
|
147
|
+
##### `validateV2(token: string): Promise<boolean>`
|
|
148
|
+
|
|
149
|
+
Validates a given authentication token with the ZIQX Auth API (V2).
|
|
150
|
+
|
|
151
|
+
| Parameter | Type | Required | Description |
|
|
152
|
+
| --------- | ------ | -------- | ------------------------------------- |
|
|
153
|
+
| `token` | string | ✅ Yes | The authentication token to validate. |
|
|
154
|
+
|
|
155
|
+
##### `getAuthToken(params: GetAuthTokenParams): Promise<any>`
|
|
156
|
+
|
|
157
|
+
Exchanges an authorization code for an access token.
|
|
158
|
+
|
|
159
|
+
**Parameters:**
|
|
160
|
+
|
|
161
|
+
| Parameter | Type | Required | Description |
|
|
162
|
+
| -------------- | ------ | -------- | -------------------------------------------------- |
|
|
163
|
+
| `authAppKey` | string | ✅ Yes | The application key. |
|
|
164
|
+
| `authSecret` | string | ✅ Yes | The application secret. |
|
|
165
|
+
| `code` | string | ✅ Yes | The authorization code received from the callback. |
|
|
166
|
+
| `codeVerifier` | string | ✅ Yes | The code verifier used in PKCE. |
|
|
167
|
+
| `redirectUri` | string | ✅ Yes | The redirect URI used in the initial request. |
|
|
168
|
+
|
|
114
169
|
**Example:**
|
|
115
170
|
|
|
116
171
|
```typescript
|
|
117
172
|
const tokenService = new ZAuthTokenService();
|
|
118
|
-
const
|
|
173
|
+
const tokenData = await tokenService.getAuthToken({
|
|
174
|
+
authAppKey: "your-app-key",
|
|
175
|
+
authSecret: "your-app-secret",
|
|
176
|
+
code: "authorization-code",
|
|
177
|
+
codeVerifier: "code-verifier",
|
|
178
|
+
redirectUri: "redirect-uri",
|
|
179
|
+
});
|
|
119
180
|
```
|
|
120
181
|
|
|
121
|
-
Returns `true` if the token is valid and authorized, otherwise `false`.
|
|
122
|
-
|
|
123
182
|
---
|
|
124
183
|
|
|
125
184
|
## Example Implementation
|
|
@@ -127,7 +186,11 @@ Returns `true` if the token is valid and authorized, otherwise `false`.
|
|
|
127
186
|
```typescript
|
|
128
187
|
import { ZAuthClient, ZAuthTokenService } from "@ziqx/auth";
|
|
129
188
|
|
|
130
|
-
const auth = new ZAuthClient({
|
|
189
|
+
const auth = new ZAuthClient({
|
|
190
|
+
authKey: "12345-xyz",
|
|
191
|
+
redirectUrl: "https://myapp.com/callback",
|
|
192
|
+
codeChallenge: "pkce-challenge-string",
|
|
193
|
+
});
|
|
131
194
|
|
|
132
195
|
// Login
|
|
133
196
|
const loginBtn = document.getElementById("loginBtn");
|
|
@@ -140,7 +203,7 @@ const tokenService = new ZAuthTokenService();
|
|
|
140
203
|
const token = localStorage.getItem("ziqx_token");
|
|
141
204
|
|
|
142
205
|
if (token) {
|
|
143
|
-
const isValid = await tokenService.
|
|
206
|
+
const isValid = await tokenService.validateV2(token);
|
|
144
207
|
console.log("Token valid:", isValid);
|
|
145
208
|
}
|
|
146
209
|
```
|
|
@@ -151,7 +214,7 @@ if (token) {
|
|
|
151
214
|
|
|
152
215
|
- Make sure your `authKey` is valid and corresponds to your environment.
|
|
153
216
|
- Use `auth.login(true)` in development mode (e.g., on localhost or staging).
|
|
154
|
-
- The `ZAuthTokenService` uses `https://ziqx.cc/api/auth/validate-token` internally.
|
|
217
|
+
- The `ZAuthTokenService` uses `https://ziqx.cc/api/auth/validate-token` internally for V1 and `https://ziqx.cc/zauth/validate` for V2.
|
|
155
218
|
- This SDK is for **client-side use** only.
|
|
156
219
|
|
|
157
220
|
---
|
package/dist/index.d.mts
CHANGED
|
@@ -1,49 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ZAuthClient - A lightweight authentication client for ZIQX Auth.
|
|
3
3
|
*
|
|
4
|
-
* This class
|
|
4
|
+
* This class facilitates redirection to the ZIQX authentication gateway.
|
|
5
5
|
*
|
|
6
6
|
* Example:
|
|
7
7
|
* ```ts
|
|
8
8
|
* import { ZAuthClient } from "@ziqx/auth";
|
|
9
9
|
*
|
|
10
10
|
* const auth = new ZAuthClient({
|
|
11
|
-
* authKey: "your-auth-key"
|
|
11
|
+
* authKey: "your-auth-key",
|
|
12
12
|
* });
|
|
13
13
|
*
|
|
14
14
|
* // Redirects user to ZIQX Auth
|
|
15
|
-
* auth.login(
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
15
|
+
* auth.login({
|
|
16
|
+
* redirectUrl:"",
|
|
17
|
+
* codeChallenge:"",
|
|
18
|
+
* codeChallengeMethod:"",
|
|
19
|
+
* state:"",
|
|
20
|
+
* });
|
|
19
21
|
* ```
|
|
20
22
|
*/
|
|
21
23
|
declare class ZAuthClient {
|
|
22
24
|
private authKey;
|
|
25
|
+
private codeChallengeMethod?;
|
|
26
|
+
private state?;
|
|
23
27
|
/**
|
|
24
28
|
* Creates a new ZAuthClient instance.
|
|
25
|
-
*
|
|
26
29
|
* @param options - The configuration options.
|
|
27
30
|
* @param options.authKey - The authentication key provided by ZIQX.
|
|
31
|
+
* @throws Will throw an error if `authKey` is missing.
|
|
28
32
|
*/
|
|
29
33
|
constructor({ authKey }: {
|
|
30
34
|
authKey: string;
|
|
31
35
|
});
|
|
32
36
|
/**
|
|
33
37
|
* Redirects the user to the ZIQX authentication page.
|
|
34
|
-
*
|
|
35
|
-
* @param
|
|
36
|
-
*
|
|
37
|
-
* @
|
|
38
|
-
* ```ts
|
|
39
|
-
* const client = new ZAuthClient({ authKey: "your-auth-key" });
|
|
40
|
-
* client.login(); // Redirects to production auth page
|
|
41
|
-
* client.login(true); // Redirects to development auth page
|
|
42
|
-
* ```
|
|
38
|
+
* @param options.redirectUrl - The URL where the user should be redirected after authentication.
|
|
39
|
+
* @param options.codeChallenge - The code challenge for PKCE flow.
|
|
40
|
+
* @param options.codeChallengeMethod - (Optional) The code challenge method (e.g., "S256"). Default is "plaintext".
|
|
41
|
+
* @param options.state - (Optional) An opaque value used to maintain state between the request and the callback.
|
|
43
42
|
*/
|
|
44
|
-
login(
|
|
43
|
+
login({ redirectUrl, codeChallenge, codeChallengeMethod, state, }: {
|
|
44
|
+
redirectUrl?: string;
|
|
45
|
+
codeChallenge?: string;
|
|
46
|
+
codeChallengeMethod?: string;
|
|
47
|
+
state?: string;
|
|
48
|
+
}): void;
|
|
45
49
|
}
|
|
46
50
|
|
|
51
|
+
type GetAuthTokenParams = {
|
|
52
|
+
authAppKey: string;
|
|
53
|
+
authSecret: string;
|
|
54
|
+
code: string;
|
|
55
|
+
codeVerifier: string;
|
|
56
|
+
redirectUri: string;
|
|
57
|
+
};
|
|
58
|
+
|
|
47
59
|
/**
|
|
48
60
|
* ZAuthTokenService - A simple token validation service for ZIQX Auth.
|
|
49
61
|
*
|
|
@@ -77,6 +89,18 @@ declare class ZAuthTokenService {
|
|
|
77
89
|
* ```
|
|
78
90
|
*/
|
|
79
91
|
validate(token: string): Promise<boolean>;
|
|
92
|
+
/**
|
|
93
|
+
* Exchanges an authorization code for an access token.
|
|
94
|
+
*
|
|
95
|
+
* @param params - The parameters required to exchange the code for a token.
|
|
96
|
+
* @param params.authAppKey - The application key.
|
|
97
|
+
* @param params.authSecret - The application secret.
|
|
98
|
+
* @param params.code - The authorization code received from the callback.
|
|
99
|
+
* @param params.codeVerifier - The code verifier used in PKCE.
|
|
100
|
+
* @param params.redirectUri - The redirect URI used in the initial request.
|
|
101
|
+
* @returns A Promise that resolves to the token response data.
|
|
102
|
+
*/
|
|
103
|
+
getAuthToken({ authAppKey, authSecret, code, codeVerifier, redirectUri, }: GetAuthTokenParams): Promise<any>;
|
|
80
104
|
}
|
|
81
105
|
|
|
82
106
|
export { ZAuthClient, ZAuthTokenService };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,49 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ZAuthClient - A lightweight authentication client for ZIQX Auth.
|
|
3
3
|
*
|
|
4
|
-
* This class
|
|
4
|
+
* This class facilitates redirection to the ZIQX authentication gateway.
|
|
5
5
|
*
|
|
6
6
|
* Example:
|
|
7
7
|
* ```ts
|
|
8
8
|
* import { ZAuthClient } from "@ziqx/auth";
|
|
9
9
|
*
|
|
10
10
|
* const auth = new ZAuthClient({
|
|
11
|
-
* authKey: "your-auth-key"
|
|
11
|
+
* authKey: "your-auth-key",
|
|
12
12
|
* });
|
|
13
13
|
*
|
|
14
14
|
* // Redirects user to ZIQX Auth
|
|
15
|
-
* auth.login(
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
15
|
+
* auth.login({
|
|
16
|
+
* redirectUrl:"",
|
|
17
|
+
* codeChallenge:"",
|
|
18
|
+
* codeChallengeMethod:"",
|
|
19
|
+
* state:"",
|
|
20
|
+
* });
|
|
19
21
|
* ```
|
|
20
22
|
*/
|
|
21
23
|
declare class ZAuthClient {
|
|
22
24
|
private authKey;
|
|
25
|
+
private codeChallengeMethod?;
|
|
26
|
+
private state?;
|
|
23
27
|
/**
|
|
24
28
|
* Creates a new ZAuthClient instance.
|
|
25
|
-
*
|
|
26
29
|
* @param options - The configuration options.
|
|
27
30
|
* @param options.authKey - The authentication key provided by ZIQX.
|
|
31
|
+
* @throws Will throw an error if `authKey` is missing.
|
|
28
32
|
*/
|
|
29
33
|
constructor({ authKey }: {
|
|
30
34
|
authKey: string;
|
|
31
35
|
});
|
|
32
36
|
/**
|
|
33
37
|
* Redirects the user to the ZIQX authentication page.
|
|
34
|
-
*
|
|
35
|
-
* @param
|
|
36
|
-
*
|
|
37
|
-
* @
|
|
38
|
-
* ```ts
|
|
39
|
-
* const client = new ZAuthClient({ authKey: "your-auth-key" });
|
|
40
|
-
* client.login(); // Redirects to production auth page
|
|
41
|
-
* client.login(true); // Redirects to development auth page
|
|
42
|
-
* ```
|
|
38
|
+
* @param options.redirectUrl - The URL where the user should be redirected after authentication.
|
|
39
|
+
* @param options.codeChallenge - The code challenge for PKCE flow.
|
|
40
|
+
* @param options.codeChallengeMethod - (Optional) The code challenge method (e.g., "S256"). Default is "plaintext".
|
|
41
|
+
* @param options.state - (Optional) An opaque value used to maintain state between the request and the callback.
|
|
43
42
|
*/
|
|
44
|
-
login(
|
|
43
|
+
login({ redirectUrl, codeChallenge, codeChallengeMethod, state, }: {
|
|
44
|
+
redirectUrl?: string;
|
|
45
|
+
codeChallenge?: string;
|
|
46
|
+
codeChallengeMethod?: string;
|
|
47
|
+
state?: string;
|
|
48
|
+
}): void;
|
|
45
49
|
}
|
|
46
50
|
|
|
51
|
+
type GetAuthTokenParams = {
|
|
52
|
+
authAppKey: string;
|
|
53
|
+
authSecret: string;
|
|
54
|
+
code: string;
|
|
55
|
+
codeVerifier: string;
|
|
56
|
+
redirectUri: string;
|
|
57
|
+
};
|
|
58
|
+
|
|
47
59
|
/**
|
|
48
60
|
* ZAuthTokenService - A simple token validation service for ZIQX Auth.
|
|
49
61
|
*
|
|
@@ -77,6 +89,18 @@ declare class ZAuthTokenService {
|
|
|
77
89
|
* ```
|
|
78
90
|
*/
|
|
79
91
|
validate(token: string): Promise<boolean>;
|
|
92
|
+
/**
|
|
93
|
+
* Exchanges an authorization code for an access token.
|
|
94
|
+
*
|
|
95
|
+
* @param params - The parameters required to exchange the code for a token.
|
|
96
|
+
* @param params.authAppKey - The application key.
|
|
97
|
+
* @param params.authSecret - The application secret.
|
|
98
|
+
* @param params.code - The authorization code received from the callback.
|
|
99
|
+
* @param params.codeVerifier - The code verifier used in PKCE.
|
|
100
|
+
* @param params.redirectUri - The redirect URI used in the initial request.
|
|
101
|
+
* @returns A Promise that resolves to the token response data.
|
|
102
|
+
*/
|
|
103
|
+
getAuthToken({ authAppKey, authSecret, code, codeVerifier, redirectUri, }: GetAuthTokenParams): Promise<any>;
|
|
80
104
|
}
|
|
81
105
|
|
|
82
106
|
export { ZAuthClient, ZAuthTokenService };
|
package/dist/index.js
CHANGED
|
@@ -27,38 +27,44 @@ module.exports = __toCommonJS(src_exports);
|
|
|
27
27
|
|
|
28
28
|
// src/constants.ts
|
|
29
29
|
var ZAUTH_BASE_URL = "https://ziqx.cc/zauth";
|
|
30
|
-
var
|
|
30
|
+
var TOKEN_URL = "https://ziqx.cc/zauth/token";
|
|
31
|
+
var VALIDATION_URL_V2 = "https://ziqx.cc/zauth/validate";
|
|
31
32
|
|
|
32
33
|
// src/ZAuthClient.ts
|
|
33
34
|
var ZAuthClient = class {
|
|
34
35
|
authKey;
|
|
36
|
+
codeChallengeMethod;
|
|
37
|
+
state;
|
|
35
38
|
/**
|
|
36
39
|
* Creates a new ZAuthClient instance.
|
|
37
|
-
*
|
|
38
40
|
* @param options - The configuration options.
|
|
39
41
|
* @param options.authKey - The authentication key provided by ZIQX.
|
|
42
|
+
* @throws Will throw an error if `authKey` is missing.
|
|
40
43
|
*/
|
|
41
44
|
constructor({ authKey }) {
|
|
42
45
|
if (!authKey) {
|
|
43
|
-
throw new Error(
|
|
46
|
+
throw new Error(
|
|
47
|
+
"ZAuthClient: Missing required parameters. `authKey` is required."
|
|
48
|
+
);
|
|
44
49
|
}
|
|
45
50
|
this.authKey = authKey;
|
|
46
51
|
}
|
|
47
52
|
/**
|
|
48
53
|
* Redirects the user to the ZIQX authentication page.
|
|
49
|
-
*
|
|
50
|
-
* @param
|
|
51
|
-
*
|
|
52
|
-
* @
|
|
53
|
-
* ```ts
|
|
54
|
-
* const client = new ZAuthClient({ authKey: "your-auth-key" });
|
|
55
|
-
* client.login(); // Redirects to production auth page
|
|
56
|
-
* client.login(true); // Redirects to development auth page
|
|
57
|
-
* ```
|
|
54
|
+
* @param options.redirectUrl - The URL where the user should be redirected after authentication.
|
|
55
|
+
* @param options.codeChallenge - The code challenge for PKCE flow.
|
|
56
|
+
* @param options.codeChallengeMethod - (Optional) The code challenge method (e.g., "S256"). Default is "plaintext".
|
|
57
|
+
* @param options.state - (Optional) An opaque value used to maintain state between the request and the callback.
|
|
58
58
|
*/
|
|
59
|
-
login(
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
login({
|
|
60
|
+
redirectUrl,
|
|
61
|
+
codeChallenge,
|
|
62
|
+
codeChallengeMethod,
|
|
63
|
+
state
|
|
64
|
+
}) {
|
|
65
|
+
const stateQuery = state ? `&state=${state}` : "";
|
|
66
|
+
const codeChallengeMethodQuery = codeChallengeMethod ? `&challenge_method=${codeChallengeMethod}` : "";
|
|
67
|
+
const loginUrl = `${ZAUTH_BASE_URL}?key=${this.authKey}&redir=${redirectUrl}&code_challenge=${codeChallenge}${codeChallengeMethodQuery}${stateQuery}`;
|
|
62
68
|
window.location.href = loginUrl;
|
|
63
69
|
}
|
|
64
70
|
};
|
|
@@ -82,7 +88,7 @@ var ZAuthTokenService = class {
|
|
|
82
88
|
throw new Error("ZAuthTokenService: token is required for validation.");
|
|
83
89
|
}
|
|
84
90
|
try {
|
|
85
|
-
const response = await fetch(
|
|
91
|
+
const response = await fetch(VALIDATION_URL_V2, {
|
|
86
92
|
method: "GET",
|
|
87
93
|
headers: {
|
|
88
94
|
Authorization: token
|
|
@@ -97,6 +103,44 @@ var ZAuthTokenService = class {
|
|
|
97
103
|
return false;
|
|
98
104
|
}
|
|
99
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Exchanges an authorization code for an access token.
|
|
108
|
+
*
|
|
109
|
+
* @param params - The parameters required to exchange the code for a token.
|
|
110
|
+
* @param params.authAppKey - The application key.
|
|
111
|
+
* @param params.authSecret - The application secret.
|
|
112
|
+
* @param params.code - The authorization code received from the callback.
|
|
113
|
+
* @param params.codeVerifier - The code verifier used in PKCE.
|
|
114
|
+
* @param params.redirectUri - The redirect URI used in the initial request.
|
|
115
|
+
* @returns A Promise that resolves to the token response data.
|
|
116
|
+
*/
|
|
117
|
+
async getAuthToken({
|
|
118
|
+
authAppKey,
|
|
119
|
+
authSecret,
|
|
120
|
+
code,
|
|
121
|
+
codeVerifier,
|
|
122
|
+
redirectUri
|
|
123
|
+
}) {
|
|
124
|
+
if (!authAppKey || !authSecret || !code || !codeVerifier || !redirectUri) {
|
|
125
|
+
throw new Error("ZAuthTokenService: All parameters are required");
|
|
126
|
+
}
|
|
127
|
+
const response = await fetch(TOKEN_URL, {
|
|
128
|
+
method: "POST",
|
|
129
|
+
body: JSON.stringify({
|
|
130
|
+
code,
|
|
131
|
+
grant_type: "authorization_code",
|
|
132
|
+
code_verifier: codeVerifier,
|
|
133
|
+
redirect_uri: redirectUri
|
|
134
|
+
}),
|
|
135
|
+
headers: {
|
|
136
|
+
"Content-Type": "application/json",
|
|
137
|
+
"x-app-key": authAppKey,
|
|
138
|
+
"x-app-secret": authSecret
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
const data = await response.json();
|
|
142
|
+
return data;
|
|
143
|
+
}
|
|
100
144
|
};
|
|
101
145
|
// Annotate the CommonJS export names for ESM import in node:
|
|
102
146
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -1,37 +1,43 @@
|
|
|
1
1
|
// src/constants.ts
|
|
2
2
|
var ZAUTH_BASE_URL = "https://ziqx.cc/zauth";
|
|
3
|
-
var
|
|
3
|
+
var TOKEN_URL = "https://ziqx.cc/zauth/token";
|
|
4
|
+
var VALIDATION_URL_V2 = "https://ziqx.cc/zauth/validate";
|
|
4
5
|
|
|
5
6
|
// src/ZAuthClient.ts
|
|
6
7
|
var ZAuthClient = class {
|
|
7
8
|
authKey;
|
|
9
|
+
codeChallengeMethod;
|
|
10
|
+
state;
|
|
8
11
|
/**
|
|
9
12
|
* Creates a new ZAuthClient instance.
|
|
10
|
-
*
|
|
11
13
|
* @param options - The configuration options.
|
|
12
14
|
* @param options.authKey - The authentication key provided by ZIQX.
|
|
15
|
+
* @throws Will throw an error if `authKey` is missing.
|
|
13
16
|
*/
|
|
14
17
|
constructor({ authKey }) {
|
|
15
18
|
if (!authKey) {
|
|
16
|
-
throw new Error(
|
|
19
|
+
throw new Error(
|
|
20
|
+
"ZAuthClient: Missing required parameters. `authKey` is required."
|
|
21
|
+
);
|
|
17
22
|
}
|
|
18
23
|
this.authKey = authKey;
|
|
19
24
|
}
|
|
20
25
|
/**
|
|
21
26
|
* Redirects the user to the ZIQX authentication page.
|
|
22
|
-
*
|
|
23
|
-
* @param
|
|
24
|
-
*
|
|
25
|
-
* @
|
|
26
|
-
* ```ts
|
|
27
|
-
* const client = new ZAuthClient({ authKey: "your-auth-key" });
|
|
28
|
-
* client.login(); // Redirects to production auth page
|
|
29
|
-
* client.login(true); // Redirects to development auth page
|
|
30
|
-
* ```
|
|
27
|
+
* @param options.redirectUrl - The URL where the user should be redirected after authentication.
|
|
28
|
+
* @param options.codeChallenge - The code challenge for PKCE flow.
|
|
29
|
+
* @param options.codeChallengeMethod - (Optional) The code challenge method (e.g., "S256"). Default is "plaintext".
|
|
30
|
+
* @param options.state - (Optional) An opaque value used to maintain state between the request and the callback.
|
|
31
31
|
*/
|
|
32
|
-
login(
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
login({
|
|
33
|
+
redirectUrl,
|
|
34
|
+
codeChallenge,
|
|
35
|
+
codeChallengeMethod,
|
|
36
|
+
state
|
|
37
|
+
}) {
|
|
38
|
+
const stateQuery = state ? `&state=${state}` : "";
|
|
39
|
+
const codeChallengeMethodQuery = codeChallengeMethod ? `&challenge_method=${codeChallengeMethod}` : "";
|
|
40
|
+
const loginUrl = `${ZAUTH_BASE_URL}?key=${this.authKey}&redir=${redirectUrl}&code_challenge=${codeChallenge}${codeChallengeMethodQuery}${stateQuery}`;
|
|
35
41
|
window.location.href = loginUrl;
|
|
36
42
|
}
|
|
37
43
|
};
|
|
@@ -55,7 +61,7 @@ var ZAuthTokenService = class {
|
|
|
55
61
|
throw new Error("ZAuthTokenService: token is required for validation.");
|
|
56
62
|
}
|
|
57
63
|
try {
|
|
58
|
-
const response = await fetch(
|
|
64
|
+
const response = await fetch(VALIDATION_URL_V2, {
|
|
59
65
|
method: "GET",
|
|
60
66
|
headers: {
|
|
61
67
|
Authorization: token
|
|
@@ -70,6 +76,44 @@ var ZAuthTokenService = class {
|
|
|
70
76
|
return false;
|
|
71
77
|
}
|
|
72
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Exchanges an authorization code for an access token.
|
|
81
|
+
*
|
|
82
|
+
* @param params - The parameters required to exchange the code for a token.
|
|
83
|
+
* @param params.authAppKey - The application key.
|
|
84
|
+
* @param params.authSecret - The application secret.
|
|
85
|
+
* @param params.code - The authorization code received from the callback.
|
|
86
|
+
* @param params.codeVerifier - The code verifier used in PKCE.
|
|
87
|
+
* @param params.redirectUri - The redirect URI used in the initial request.
|
|
88
|
+
* @returns A Promise that resolves to the token response data.
|
|
89
|
+
*/
|
|
90
|
+
async getAuthToken({
|
|
91
|
+
authAppKey,
|
|
92
|
+
authSecret,
|
|
93
|
+
code,
|
|
94
|
+
codeVerifier,
|
|
95
|
+
redirectUri
|
|
96
|
+
}) {
|
|
97
|
+
if (!authAppKey || !authSecret || !code || !codeVerifier || !redirectUri) {
|
|
98
|
+
throw new Error("ZAuthTokenService: All parameters are required");
|
|
99
|
+
}
|
|
100
|
+
const response = await fetch(TOKEN_URL, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
body: JSON.stringify({
|
|
103
|
+
code,
|
|
104
|
+
grant_type: "authorization_code",
|
|
105
|
+
code_verifier: codeVerifier,
|
|
106
|
+
redirect_uri: redirectUri
|
|
107
|
+
}),
|
|
108
|
+
headers: {
|
|
109
|
+
"Content-Type": "application/json",
|
|
110
|
+
"x-app-key": authAppKey,
|
|
111
|
+
"x-app-secret": authSecret
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const data = await response.json();
|
|
115
|
+
return data;
|
|
116
|
+
}
|
|
73
117
|
};
|
|
74
118
|
export {
|
|
75
119
|
ZAuthClient,
|