@intelicity/gates-sdk 0.1.6 → 0.2.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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-auth.d.ts","sourceRoot":"","sources":["../../src/services/client-auth.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,uBAAuB,GAAG;IACpC,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAW;IAClC,OAAO,CAAC,WAAW,CAAqD;gBAE5D,MAAM,EAAE,uBAAuB;IAkBrC,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IAiEvC,UAAU,IAAI,IAAI;CAGnB"}
@@ -0,0 +1,75 @@
1
+ import { MissingParameterError, ClientCredentialsError, } from "../errors/error.js";
2
+ export class GatesClientAuth {
3
+ tokenEndpoint;
4
+ clientId;
5
+ clientSecret;
6
+ scopes;
7
+ cachedToken = null;
8
+ constructor(config) {
9
+ if (!config.domain || config.domain.trim().length === 0) {
10
+ throw new MissingParameterError("domain");
11
+ }
12
+ if (!config.clientId || config.clientId.trim().length === 0) {
13
+ throw new MissingParameterError("clientId");
14
+ }
15
+ if (!config.clientSecret || config.clientSecret.trim().length === 0) {
16
+ throw new MissingParameterError("clientSecret");
17
+ }
18
+ const domain = config.domain.replace(/\/$/, "");
19
+ this.tokenEndpoint = `https://${domain}/oauth2/token`;
20
+ this.clientId = config.clientId;
21
+ this.clientSecret = config.clientSecret;
22
+ this.scopes = config.scopes ?? [];
23
+ }
24
+ async getAccessToken() {
25
+ if (this.cachedToken && Date.now() < this.cachedToken.expiresAt) {
26
+ return this.cachedToken.token;
27
+ }
28
+ const body = new URLSearchParams({
29
+ grant_type: "client_credentials",
30
+ });
31
+ if (this.scopes.length > 0) {
32
+ body.set("scope", this.scopes.join(" "));
33
+ }
34
+ const credentials = Buffer.from(`${this.clientId}:${this.clientSecret}`).toString("base64");
35
+ let response;
36
+ try {
37
+ response = await fetch(this.tokenEndpoint, {
38
+ method: "POST",
39
+ headers: {
40
+ "Content-Type": "application/x-www-form-urlencoded",
41
+ Authorization: `Basic ${credentials}`,
42
+ },
43
+ body: body.toString(),
44
+ });
45
+ }
46
+ catch (error) {
47
+ const message = error instanceof Error ? error.message : "Unknown network error";
48
+ throw new ClientCredentialsError(`Failed to reach token endpoint: ${message}`);
49
+ }
50
+ if (!response.ok) {
51
+ let detail;
52
+ try {
53
+ const errorBody = (await response.json());
54
+ detail = errorBody.error ?? response.statusText;
55
+ }
56
+ catch {
57
+ detail = response.statusText;
58
+ }
59
+ throw new ClientCredentialsError(`Token request failed (${response.status}): ${detail}`);
60
+ }
61
+ const data = (await response.json());
62
+ if (!data.access_token) {
63
+ throw new ClientCredentialsError("Token response missing access_token field");
64
+ }
65
+ const bufferMs = 30_000;
66
+ this.cachedToken = {
67
+ token: data.access_token,
68
+ expiresAt: Date.now() + data.expires_in * 1000 - bufferMs,
69
+ };
70
+ return data.access_token;
71
+ }
72
+ clearCache() {
73
+ this.cachedToken = null;
74
+ }
75
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intelicity/gates-sdk",
3
- "version": "0.1.6",
3
+ "version": "0.2.0",
4
4
  "description": "Simple SDK for authenticating users with AWS Cognito JWT tokens",
5
5
  "type": "module",
6
6
  "exports": "./dist/index.js",
@@ -18,6 +18,8 @@
18
18
  "scripts": {
19
19
  "build": "tsc && tsc-alias",
20
20
  "typecheck": "tsc --noEmit",
21
+ "test": "vitest run",
22
+ "test:watch": "vitest",
21
23
  "test:token": "tsx scripts/test-token.ts",
22
24
  "prepublishOnly": "npm run build"
23
25
  },
@@ -37,7 +39,8 @@
37
39
  "@types/node": "^24.1.0",
38
40
  "tsc-alias": "^1.8.16",
39
41
  "tsx": "^4.20.3",
40
- "typescript": "^5.9.2"
42
+ "typescript": "^5.9.2",
43
+ "vitest": "^4.1.0"
41
44
  },
42
45
  "dependencies": {
43
46
  "jose": "^6.1.1"