@soulcode/bexio-oauth 0.2.0 → 0.3.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 +8 -4
- package/dist/claims.d.ts +2 -2
- package/dist/claims.d.ts.map +1 -1
- package/dist/claims.js +3 -3
- package/dist/claims.js.map +1 -1
- package/dist/crypto.d.ts +2 -2
- package/dist/crypto.js +5 -5
- package/dist/crypto.js.map +1 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/grants.d.ts +20 -11
- package/dist/grants.d.ts.map +1 -1
- package/dist/grants.js +67 -39
- package/dist/grants.js.map +1 -1
- package/dist/idle-sweep.d.ts +1 -1
- package/dist/idle-sweep.d.ts.map +1 -1
- package/dist/idle-sweep.js +5 -5
- package/dist/idle-sweep.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/migrations.generated.d.ts.map +1 -1
- package/dist/migrations.generated.js +26 -10
- package/dist/migrations.generated.js.map +1 -1
- package/dist/refresh.d.ts +7 -7
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +38 -30
- package/dist/refresh.js.map +1 -1
- package/dist/store/memory.d.ts.map +1 -1
- package/dist/store/memory.js +82 -46
- package/dist/store/memory.js.map +1 -1
- package/dist/store/postgres.d.ts +3 -3
- package/dist/store/postgres.d.ts.map +1 -1
- package/dist/store/postgres.js +128 -59
- package/dist/store/postgres.js.map +1 -1
- package/dist/store/types.d.ts +61 -21
- package/dist/store/types.d.ts.map +1 -1
- package/dist/types.d.ts +24 -10
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/sql/0001_bexio_oauth.sql +48 -0
- package/sql/0001_bexio_grants.sql +0 -32
package/README.md
CHANGED
|
@@ -11,12 +11,14 @@ It is **not** an auth framework: your app keeps its own login. For "Sign in with
|
|
|
11
11
|
|
|
12
12
|
```bash
|
|
13
13
|
npm install @soulcode/bexio-oauth pg
|
|
14
|
-
npx @soulcode/bexio-oauth sql > migrations/
|
|
14
|
+
npx @soulcode/bexio-oauth sql > migrations/0001_bexio_oauth.sql # then apply it
|
|
15
15
|
npx @soulcode/bexio-oauth keygen # encryption key
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
Requires Node ≥ 24 and PostgreSQL.
|
|
19
19
|
|
|
20
|
+
Use one Bexio OAuth client per app: Bexio keeps one refresh token chain per user, company and client, so apps sharing a client id invalidate each other's grants. Owners of your app that connect the same Bexio user and company share one connection, refreshed once for all of them ([guide §2](https://github.com/SoulcodeAgency/bexio-oauth/blob/main/docs/integration.md#why-owners-of-the-same-identity-share-a-connection)).
|
|
21
|
+
|
|
20
22
|
## Quickstart
|
|
21
23
|
|
|
22
24
|
```ts
|
|
@@ -37,14 +39,16 @@ export const bexio = createBexioOAuth({
|
|
|
37
39
|
```
|
|
38
40
|
|
|
39
41
|
```ts
|
|
40
|
-
//
|
|
42
|
+
// app/api/bexio/connect/route.ts
|
|
41
43
|
export const GET = async (req: Request) =>
|
|
42
44
|
bexio.start({
|
|
43
45
|
ownerId: await currentWorkspaceId(req),
|
|
44
46
|
returnTo: "/settings",
|
|
45
47
|
});
|
|
48
|
+
```
|
|
46
49
|
|
|
47
|
-
|
|
50
|
+
```ts
|
|
51
|
+
// app/api/bexio/callback/route.ts (your redirectUri)
|
|
48
52
|
export const GET = async (req: Request) =>
|
|
49
53
|
bexio.callback(req, { ownerId: await currentWorkspaceId(req) });
|
|
50
54
|
```
|
|
@@ -60,7 +64,7 @@ await fetch("https://api.bexio.com/2.0/contact", {
|
|
|
60
64
|
});
|
|
61
65
|
```
|
|
62
66
|
|
|
63
|
-
Also: `listGrants`, `disconnect`, `refreshIdle` (keep idle
|
|
67
|
+
Also: `listGrants`, `disconnect`, `refreshIdle` (keep idle connections alive), `saveGrant` (tokens from your auth framework). Every error is a `BexioOAuthError` with a stable `code`.
|
|
64
68
|
|
|
65
69
|
## License
|
|
66
70
|
|
package/dist/claims.d.ts
CHANGED
|
@@ -26,6 +26,6 @@ export interface BexioIdentity {
|
|
|
26
26
|
* missing.
|
|
27
27
|
*/
|
|
28
28
|
export declare function mapClaims(claims: ClaimBag): BexioIdentity;
|
|
29
|
-
/** The value a
|
|
30
|
-
export declare function
|
|
29
|
+
/** The value a connection's ciphertext is bound to (see `TokenCipher`). */
|
|
30
|
+
export declare function connectionBinding(companyId: string, userId: string): string;
|
|
31
31
|
//# sourceMappingURL=claims.d.ts.map
|
package/dist/claims.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claims.d.ts","sourceRoot":"","sources":["../src/claims.ts"],"names":[],"mappings":"AAEA,uDAAuD;AACvD,eAAO,MAAM,YAAY,wCAAwC,CAAC;AAClE,uCAAuC;AACvC,eAAO,MAAM,kBAAkB,0BAA0B,CAAC;AAC1D;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,SAAS,MAAM,EAMvC,CAAC;AAEH,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE/C,oFAAoF;AACpF,MAAM,WAAW,aAAa;IAC5B,kBAAkB;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,4CAA4C;IAC5C,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,0EAA0E;IAC1E,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAaD;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,QAAQ,GAAG,aAAa,CAwBzD;AAED,
|
|
1
|
+
{"version":3,"file":"claims.d.ts","sourceRoot":"","sources":["../src/claims.ts"],"names":[],"mappings":"AAEA,uDAAuD;AACvD,eAAO,MAAM,YAAY,wCAAwC,CAAC;AAClE,uCAAuC;AACvC,eAAO,MAAM,kBAAkB,0BAA0B,CAAC;AAC1D;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,SAAS,MAAM,EAMvC,CAAC;AAEH,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE/C,oFAAoF;AACpF,MAAM,WAAW,aAAa;IAC5B,kBAAkB;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,4CAA4C;IAC5C,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,0EAA0E;IAC1E,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAaD;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,QAAQ,GAAG,aAAa,CAwBzD;AAED,2EAA2E;AAC3E,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAE3E"}
|
package/dist/claims.js
CHANGED
|
@@ -51,8 +51,8 @@ export function mapClaims(claims) {
|
|
|
51
51
|
userEmail: text(claims.email),
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
|
-
/** The value a
|
|
55
|
-
export function
|
|
56
|
-
return `${
|
|
54
|
+
/** The value a connection's ciphertext is bound to (see `TokenCipher`). */
|
|
55
|
+
export function connectionBinding(companyId, userId) {
|
|
56
|
+
return `${companyId}\u0000${userId}`;
|
|
57
57
|
}
|
|
58
58
|
//# sourceMappingURL=claims.js.map
|
package/dist/claims.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claims.js","sourceRoot":"","sources":["../src/claims.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,uDAAuD;AACvD,MAAM,CAAC,MAAM,YAAY,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AACJ,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,iBAAiB,CAAC,SAAiB,EAAE,MAAc;IACjE,OAAO,GAAG,SAAS,SAAS,MAAM,EAAE,CAAC;AACvC,CAAC"}
|
package/dist/crypto.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ export interface Keyring {
|
|
|
12
12
|
export declare function generateKey(): string;
|
|
13
13
|
/**
|
|
14
14
|
* Encrypts tokens at rest as compact JWE (`dir` + `A256GCM`) and seals the
|
|
15
|
-
* short-lived flow cookie. The `
|
|
16
|
-
*
|
|
15
|
+
* short-lived flow cookie. The `bxc` protected header binds a token to the
|
|
16
|
+
* connection it belongs to, so ciphertext copied to another row fails to decrypt.
|
|
17
17
|
*/
|
|
18
18
|
export declare class TokenCipher {
|
|
19
19
|
#private;
|
package/dist/crypto.js
CHANGED
|
@@ -16,8 +16,8 @@ function hash(value) {
|
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Encrypts tokens at rest as compact JWE (`dir` + `A256GCM`) and seals the
|
|
19
|
-
* short-lived flow cookie. The `
|
|
20
|
-
*
|
|
19
|
+
* short-lived flow cookie. The `bxc` protected header binds a token to the
|
|
20
|
+
* connection it belongs to, so ciphertext copied to another row fails to decrypt.
|
|
21
21
|
*/
|
|
22
22
|
export class TokenCipher {
|
|
23
23
|
#current;
|
|
@@ -48,20 +48,20 @@ export class TokenCipher {
|
|
|
48
48
|
alg: "dir",
|
|
49
49
|
enc: "A256GCM",
|
|
50
50
|
kid: this.#current,
|
|
51
|
-
|
|
51
|
+
bxc: hash(binding),
|
|
52
52
|
})
|
|
53
53
|
.encrypt(this.#key(this.#current).key);
|
|
54
54
|
}
|
|
55
55
|
async decrypt(jwe, binding) {
|
|
56
56
|
try {
|
|
57
57
|
const { plaintext, protectedHeader } = await compactDecrypt(jwe, (header) => this.#key(header.kid).key, JWE_ALGORITHMS);
|
|
58
|
-
if (protectedHeader.
|
|
58
|
+
if (protectedHeader.bxc !== hash(binding)) {
|
|
59
59
|
throw new Error("binding mismatch");
|
|
60
60
|
}
|
|
61
61
|
return new TextDecoder().decode(plaintext);
|
|
62
62
|
}
|
|
63
63
|
catch {
|
|
64
|
-
throw new BexioOAuthError("decryption_failed", "stored token could not be decrypted (unknown key id, or it belongs to another
|
|
64
|
+
throw new BexioOAuthError("decryption_failed", "stored token could not be decrypted (unknown key id, or it belongs to another connection)");
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
async seal(payload, ttlSeconds, now) {
|
package/dist/crypto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAE9E,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAY9C,MAAM,cAAc,GAAG;IACrB,uBAAuB,EAAE,CAAC,KAAK,CAAC;IAChC,2BAA2B,EAAE,CAAC,SAAS,CAAC;CACzC,CAAC;AAEF,uFAAuF;AACvF,MAAM,UAAU,GAAG,qBAAqB,CAAC;AAEzC,2DAA2D;AAC3D,MAAM,UAAU,WAAW;IACzB,OAAO,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AAC/C,CAAC;AAED,SAAS,IAAI,CAAC,KAAa;IACzB,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAChE,CAAC;AAED;;;;GAIG;AACH,MAAM,OAAO,WAAW;IACb,QAAQ,CAAS;IACjB,KAAK,CAA0B;IAC/B,SAAS,GAAG,IAAI,GAAG,EAAsB,CAAC;IAEnD,YAAY,OAAgB;QAC1B,IAAI,CAAC,O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AAO,EAAE,CAAC;YACb,OAAO,GAAG,IAAI,UAAU,CACtB,QAAQ,CACN,QAAQ,EACR,IAAI,EACJ,IAAI,UAAU,CAAC,CAAC,CAAC,EACjB,yBAAyB,EACzB,EAAE,CACH,CACF,CAAC;YACF,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
|
package/dist/errors.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export type BexioOAuthErrorCode =
|
|
|
20
20
|
| "ambiguous_grant"
|
|
21
21
|
/** The refresh token is dead; the user has to reconnect. */
|
|
22
22
|
| "reauth_required"
|
|
23
|
-
/** A stored token can't be decrypted (unknown key id, or it belongs to another
|
|
23
|
+
/** A stored token can't be decrypted (unknown key id, or it belongs to another connection). */
|
|
24
24
|
| "decryption_failed"
|
|
25
25
|
/** The library was configured incorrectly. */
|
|
26
26
|
| "invalid_config";
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,mBAAmB;AAC7B,+DAA+D;AAC7D,gBAAgB;AAClB,wEAAwE;GACtE,gBAAgB;AAClB,mCAAmC;GACjC,eAAe;AACjB,2EAA2E;GACzE,kBAAkB;AACpB,6DAA6D;GAC3D,WAAW;AACb,4DAA4D;GAC1D,eAAe;AACjB,mEAAmE;GACjE,iBAAiB;AACnB,4DAA4D;GAC1D,iBAAiB;AACnB
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,mBAAmB;AAC7B,+DAA+D;AAC7D,gBAAgB;AAClB,wEAAwE;GACtE,gBAAgB;AAClB,mCAAmC;GACjC,eAAe;AACjB,2EAA2E;GACzE,kBAAkB;AACpB,6DAA6D;GAC3D,WAAW;AACb,4DAA4D;GAC1D,eAAe;AACjB,mEAAmE;GACjE,iBAAiB;AACnB,4DAA4D;GAC1D,iBAAiB;AACnB,+FAA+F;GAC7F,mBAAmB;AACrB,8CAA8C;GAC5C,gBAAgB,CAAC;AAErB,qBAAa,eAAgB,SAAQ,KAAK;IACxC,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;gBAGjC,IAAI,EAAE,mBAAmB,EACzB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,KAAK,CAAC,EAAE,MAAM,CAAC;YAAC,MAAM,CAAC,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE;CAM1E;AAED,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,OAAO,EACd,IAAI,CAAC,EAAE,mBAAmB,GACzB,KAAK,IAAI,eAAe,CAK1B"}
|
package/dist/grants.d.ts
CHANGED
|
@@ -1,25 +1,34 @@
|
|
|
1
1
|
import { type ClaimBag } from "./claims.js";
|
|
2
2
|
import type { ClientContext } from "./config.js";
|
|
3
3
|
import type { TokenCipher } from "./crypto.js";
|
|
4
|
-
import type { GrantRecord, GrantStore } from "./store/types.js";
|
|
4
|
+
import type { ConnectionRecord, GrantRecord, GrantStore } from "./store/types.js";
|
|
5
5
|
import type { AccessToken, Grant, GrantSelector } from "./types.js";
|
|
6
|
-
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export declare function
|
|
11
|
-
/**
|
|
12
|
-
export declare function
|
|
6
|
+
/**
|
|
7
|
+
* The app-facing view of a stored grant, with `c` (default: the grant's own)
|
|
8
|
+
* as its connection: no token material.
|
|
9
|
+
*/
|
|
10
|
+
export declare function toGrant(grant: GrantRecord, c?: ConnectionRecord): Grant;
|
|
11
|
+
/** The binding a connection's ciphertexts are encrypted under. */
|
|
12
|
+
export declare function bindingOf(connection: ConnectionRecord): string;
|
|
13
|
+
export declare function decryptFor(cipher: TokenCipher, connection: ConnectionRecord, value: string): Promise<string>;
|
|
14
|
+
/** Decrypts the connection's access token for the owner of `grant`. */
|
|
15
|
+
export declare function issue(cipher: TokenCipher, grant: GrantRecord, connection: ConnectionRecord): Promise<AccessToken>;
|
|
13
16
|
/** Exactly one grant of the owner, or `not_connected` / `ambiguous_grant`. */
|
|
14
17
|
export declare function resolveGrant(store: GrantStore, selector: GrantSelector): Promise<GrantRecord>;
|
|
15
18
|
/** The token's claims if it is a JWT, else none (Bexio access tokens usually are). */
|
|
16
19
|
export declare function decodeIfJwt(token: string): ClaimBag;
|
|
17
20
|
/**
|
|
18
|
-
* Adds userinfo claims
|
|
19
|
-
*
|
|
21
|
+
* Adds userinfo claims when the tokens lack the profile claims. Bexio's access
|
|
22
|
+
* token carries `company_id` but no company name, email or person name; the
|
|
23
|
+
* ID token has them, so the extra call only happens without one (`saveGrant`
|
|
24
|
+
* with an access token alone). Token claims win over userinfo; a userinfo
|
|
25
|
+
* failure keeps the token claims.
|
|
20
26
|
*/
|
|
21
27
|
export declare function withUserInfo(ctx: ClientContext, claims: ClaimBag, accessToken: string): Promise<ClaimBag>;
|
|
22
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* Encrypts the tokens, reads the role, and upserts the connection for the
|
|
30
|
+
* identity in `claims` and the owner's grant on it.
|
|
31
|
+
*/
|
|
23
32
|
export declare function saveTokens(ctx: ClientContext, input: {
|
|
24
33
|
ownerId: string;
|
|
25
34
|
claims: ClaimBag;
|
package/dist/grants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grants.d.ts","sourceRoot":"","sources":["../src/grants.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"grants.d.ts","sourceRoot":"","sources":["../src/grants.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgC,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AA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AAM,CAAC;IACpB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,eAAe,EAAE,IAAI,GAAG,IAAI,CAAC;IAC7B,gBAAgB,EAAE,IAAI,GAAG,IAAI,CAAC;IAC9B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,GACA,OAAO,CAAC,WAAW,CAAC,CAsBtB;AAED,wBAAsB,UAAU,CAC9B,KAAK,EAAE,UAAU,EACjB,KAAK,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,GACzB,OAAO,CAAC,KAAK,EAAE,CAAC,CAMlB;AAED,wBAAsB,UAAU,CAC9B,GAAG,EAAE,aAAa,EAClB,QAAQ,EAAE,aAAa,GACtB,OAAO,CAAC,IAAI,CAAC,CA8Bf"}
|
package/dist/grants.js
CHANGED
|
@@ -1,41 +1,45 @@
|
|
|
1
1
|
import * as oidc from "openid-client";
|
|
2
2
|
import { decodeJwt } from "jose";
|
|
3
|
-
import {
|
|
3
|
+
import { connectionBinding, mapClaims } from "./claims.js";
|
|
4
4
|
import { BexioOAuthError } from "./errors.js";
|
|
5
5
|
import { fetchRole } from "./permissions.js";
|
|
6
|
-
/**
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* The app-facing view of a stored grant, with `c` (default: the grant's own)
|
|
8
|
+
* as its connection: no token material.
|
|
9
|
+
*/
|
|
10
|
+
export function toGrant(grant, c = grant.connection) {
|
|
8
11
|
return {
|
|
9
|
-
id:
|
|
10
|
-
ownerId:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
id: grant.id,
|
|
13
|
+
ownerId: grant.ownerId,
|
|
14
|
+
connectionId: c.id,
|
|
15
|
+
companyId: c.companyId,
|
|
16
|
+
companyName: c.companyName,
|
|
17
|
+
userId: c.userId,
|
|
18
|
+
userName: c.userName,
|
|
19
|
+
userEmail: c.userEmail,
|
|
20
|
+
companyUserId: c.companyUserId,
|
|
21
|
+
role: c.role,
|
|
22
|
+
status: c.status,
|
|
23
|
+
scope: c.scope,
|
|
24
|
+
accessExpiresAt: c.accessExpiresAt,
|
|
25
|
+
lastRefreshedAt: c.lastRefreshedAt,
|
|
26
|
+
createdAt: grant.createdAt,
|
|
27
|
+
updatedAt: c.updatedAt,
|
|
24
28
|
};
|
|
25
29
|
}
|
|
26
|
-
/** The binding a
|
|
27
|
-
export function bindingOf(
|
|
28
|
-
return
|
|
30
|
+
/** The binding a connection's ciphertexts are encrypted under. */
|
|
31
|
+
export function bindingOf(connection) {
|
|
32
|
+
return connectionBinding(connection.companyId, connection.userId);
|
|
29
33
|
}
|
|
30
|
-
export function decryptFor(cipher,
|
|
31
|
-
return cipher.decrypt(value, bindingOf(
|
|
34
|
+
export function decryptFor(cipher, connection, value) {
|
|
35
|
+
return cipher.decrypt(value, bindingOf(connection));
|
|
32
36
|
}
|
|
33
|
-
/** Decrypts the
|
|
34
|
-
export async function issue(cipher,
|
|
37
|
+
/** Decrypts the connection's access token for the owner of `grant`. */
|
|
38
|
+
export async function issue(cipher, grant, connection) {
|
|
35
39
|
return {
|
|
36
|
-
accessToken: await decryptFor(cipher,
|
|
37
|
-
expiresAt:
|
|
38
|
-
grant: toGrant(
|
|
40
|
+
accessToken: await decryptFor(cipher, connection, connection.accessToken),
|
|
41
|
+
expiresAt: connection.accessExpiresAt,
|
|
42
|
+
grant: toGrant(grant, connection),
|
|
39
43
|
};
|
|
40
44
|
}
|
|
41
45
|
/** Exactly one grant of the owner, or `not_connected` / `ambiguous_grant`. */
|
|
@@ -59,12 +63,17 @@ export function decodeIfJwt(token) {
|
|
|
59
63
|
return {};
|
|
60
64
|
}
|
|
61
65
|
}
|
|
66
|
+
/** Claims a connection needs beyond the ids (see `mapClaims`). */
|
|
67
|
+
const PROFILE_CLAIMS = ["company_id", "company_name", "email"];
|
|
62
68
|
/**
|
|
63
|
-
* Adds userinfo claims
|
|
64
|
-
*
|
|
69
|
+
* Adds userinfo claims when the tokens lack the profile claims. Bexio's access
|
|
70
|
+
* token carries `company_id` but no company name, email or person name; the
|
|
71
|
+
* ID token has them, so the extra call only happens without one (`saveGrant`
|
|
72
|
+
* with an access token alone). Token claims win over userinfo; a userinfo
|
|
73
|
+
* failure keeps the token claims.
|
|
65
74
|
*/
|
|
66
75
|
export async function withUserInfo(ctx, claims, accessToken) {
|
|
67
|
-
if (
|
|
76
|
+
if (PROFILE_CLAIMS.every((claim) => claims[claim] !== undefined))
|
|
68
77
|
return claims;
|
|
69
78
|
try {
|
|
70
79
|
const info = await oidc.fetchUserInfo(await ctx.oidc.config(), accessToken, typeof claims.sub === "string" ? claims.sub : oidc.skipSubjectCheck);
|
|
@@ -74,12 +83,16 @@ export async function withUserInfo(ctx, claims, accessToken) {
|
|
|
74
83
|
return claims;
|
|
75
84
|
}
|
|
76
85
|
}
|
|
77
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* Encrypts the tokens, reads the role, and upserts the connection for the
|
|
88
|
+
* identity in `claims` and the owner's grant on it.
|
|
89
|
+
*/
|
|
78
90
|
export async function saveTokens(ctx, input) {
|
|
79
91
|
const identity = mapClaims(input.claims);
|
|
80
|
-
const binding =
|
|
92
|
+
const binding = connectionBinding(identity.companyId, identity.userId);
|
|
81
93
|
const role = (await fetchRole(ctx.fetchImpl, ctx.apiBaseUrl, input.accessToken)) ??
|
|
82
94
|
"member";
|
|
95
|
+
const issuedAt = decodeIfJwt(input.accessToken).iat;
|
|
83
96
|
return ctx.store.upsertGrant({
|
|
84
97
|
ownerId: input.ownerId,
|
|
85
98
|
...identity,
|
|
@@ -91,25 +104,40 @@ export async function saveTokens(ctx, input) {
|
|
|
91
104
|
accessExpiresAt: input.accessExpiresAt,
|
|
92
105
|
refreshExpiresAt: input.refreshExpiresAt,
|
|
93
106
|
scope: input.scope,
|
|
107
|
+
authorizedAt: typeof issuedAt === "number" ? new Date(issuedAt * 1000) : ctx.clock(),
|
|
94
108
|
now: ctx.clock(),
|
|
95
109
|
});
|
|
96
110
|
}
|
|
97
111
|
export async function listGrants(store, query) {
|
|
98
112
|
if (!query?.ownerId)
|
|
99
113
|
throw new BexioOAuthError("invalid_config", "ownerId is required");
|
|
100
|
-
return (await store.findGrants({ ownerId: query.ownerId })).map(toGrant);
|
|
114
|
+
return (await store.findGrants({ ownerId: query.ownerId })).map((g) => toGrant(g));
|
|
101
115
|
}
|
|
102
116
|
export async function disconnect(ctx, selector) {
|
|
103
|
-
const
|
|
117
|
+
const grant = await resolveGrant(ctx.store, selector);
|
|
118
|
+
// Delete first, revoke after: revoking ends the one refresh token chain
|
|
119
|
+
// all owners of this identity share, so it may only happen once the store
|
|
120
|
+
// has confirmed that no grant (not even a concurrent one) uses it.
|
|
121
|
+
await ctx.store.deleteGrant(grant.id);
|
|
122
|
+
const deleted = await ctx.store.deleteUnusedConnection(grant.connection.id);
|
|
123
|
+
if (!deleted)
|
|
124
|
+
return;
|
|
125
|
+
const { connection, userHasOtherConnections } = deleted;
|
|
126
|
+
// Revoking ends all of the user's chains at Bexio, whichever token it
|
|
127
|
+
// names: a stale (reauth_required) token would still end the chain the user
|
|
128
|
+
// authorized since, and any token the user's other company's connection.
|
|
129
|
+
if (!connection.refreshToken ||
|
|
130
|
+
connection.status !== "active" ||
|
|
131
|
+
userHasOtherConnections)
|
|
132
|
+
return;
|
|
104
133
|
try {
|
|
105
134
|
const c = await ctx.oidc.config();
|
|
106
|
-
if (
|
|
107
|
-
await oidc.tokenRevocation(c, await decryptFor(ctx.cipher,
|
|
135
|
+
if (c.serverMetadata().revocation_endpoint) {
|
|
136
|
+
await oidc.tokenRevocation(c, await decryptFor(ctx.cipher, connection, connection.refreshToken), { token_type_hint: "refresh_token" });
|
|
108
137
|
}
|
|
109
138
|
}
|
|
110
139
|
catch {
|
|
111
|
-
// Revocation is best effort; the
|
|
140
|
+
// Revocation is best effort; the connection is deleted either way.
|
|
112
141
|
}
|
|
113
|
-
await ctx.store.deleteGrant(record.id);
|
|
114
142
|
}
|
|
115
143
|
//# sourceMappingURL=grants.js.map
|
package/dist/grants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grants.js","sourceRoot":"","sources":["../src/grants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"grants.js","sourceRoot":"","sources":["../src/grants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAiB,MAAM,aAAa,CAAC;AAG1E,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAQ7C;;;GAGG;AACH,MAAM,UAAU,OAAO,CACrB,KAAkB,EAClB,IAAsB,KAAK,CAAC,UAAU;IAEtC,OAAO;QACL,EAAE,EAAE,KAAK,CAAC,EAAE;QACZ,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,YAAY,EAAE,CAAC,CAAC,EAAE;QAClB,SAAS,EAAE,CAAC,CAAC,SAAS;QACtB,WAAW,EAAE,CAAC,CAAC,WAAW;QAC1B,MAAM,EAAE,CAAC,CAAC,MAAM;QAChB,QAAQ,EAAE,CAAC,CAAC,QAAQ;QACpB,SAAS,EAAE,CAAC,CAAC,SAAS;QACtB,aAAa,EAAE,CAAC,CAAC,aAAa;QAC9B,IAAI,EAAE,CAAC,CAAC,IAAI;QACZ,MAAM,EAAE,CAAC,CAAC,MAAM;QAChB,KAAK,EAAE,CAAC,CAAC,KAAK;QACd,eAAe,EAAE,CAAC,CAAC,eAAe;QAClC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AC,KAAK,UAAU,UAAU,CAC9B,GAAkB,EAClB,QAAuB;IAEvB,MAAM,KAAK,GAAG,MAAM,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IACtD,wEAAwE;IACxE,0EAA0E;IAC1E,mEAAmE;IACnE,MAAM,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACtC,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,KAAK,CAAC,sBAAsB,CAAC,KAAK,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAC5E,IAAI,CAAC,OAAO;QAAE,OAAO;IACrB,MAAM,EAAE,UAAU,EAAE,uBAAuB,EAAE,GAAG,OAAO,CAAC;IACxD,sEAAsE;IACtE,4EAA4E;IAC5E,yEAAyE;IACzE,IACE,CAAC,UAAU,CAAC,YAAY;QACxB,UAAU,CAAC,MAAM,KAAK,QAAQ;QAC9B,uBAAuB;QAEvB,OAAO;IACT,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;QAClC,IAAI,CAAC,CAAC,cAAc,EAAE,CAAC,mBAAmB,EAAE,CAAC;YAC3C,MAAM,IAAI,CAAC,eAAe,CACxB,CAAC,EACD,MAAM,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,UAAU,CAAC,YAAY,CAAC,EACjE,EAAE,eAAe,EAAE,eAAe,EAAE,CACrC,CAAC;QACJ,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,mEAAmE;IACrE,CAAC;AACH,CAAC"}
|
package/dist/idle-sweep.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ClientContext } from "./config.js";
|
|
|
2
2
|
import { type Duration } from "./duration.js";
|
|
3
3
|
import type { RefreshIdleResult } from "./types.js";
|
|
4
4
|
export declare const DEFAULT_IDLE_LIMIT = 100;
|
|
5
|
-
/** `BexioOAuth.refreshIdle`: refreshes idle
|
|
5
|
+
/** `BexioOAuth.refreshIdle`: refreshes idle connections one after another. */
|
|
6
6
|
export declare function refreshIdle(ctx: ClientContext, options: {
|
|
7
7
|
idleFor: Duration;
|
|
8
8
|
limit?: number;
|
package/dist/idle-sweep.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"idle-sweep.d.ts","sourceRoot":"","sources":["../src/idle-sweep.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAQ,KAAK,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEpD,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAEtC,
|
|
1
|
+
{"version":3,"file":"idle-sweep.d.ts","sourceRoot":"","sources":["../src/idle-sweep.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAQ,KAAK,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEpD,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAEtC,8EAA8E;AAC9E,wBAAsB,WAAW,CAC/B,GAAG,EAAE,aAAa,EAClB,OAAO,EAAE;IAAE,OAAO,EAAE,QAAQ,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,QAAQ,CAAA;CAAE,GACpE,OAAO,CAAC,iBAAiB,CAAC,CA2D5B"}
|
package/dist/idle-sweep.js
CHANGED
|
@@ -2,7 +2,7 @@ import { toMs } from "./duration.js";
|
|
|
2
2
|
import { isBexioOAuthError } from "./errors.js";
|
|
3
3
|
import { lockWindow, refreshLocked } from "./refresh.js";
|
|
4
4
|
export const DEFAULT_IDLE_LIMIT = 100;
|
|
5
|
-
/** `BexioOAuth.refreshIdle`: refreshes idle
|
|
5
|
+
/** `BexioOAuth.refreshIdle`: refreshes idle connections one after another. */
|
|
6
6
|
export async function refreshIdle(ctx, options) {
|
|
7
7
|
const { store, clock } = ctx;
|
|
8
8
|
const idleMs = toMs(options?.idleFor, "idleFor");
|
|
@@ -18,7 +18,7 @@ export async function refreshIdle(ctx, options) {
|
|
|
18
18
|
remaining: 0,
|
|
19
19
|
};
|
|
20
20
|
const refreshedBefore = new Date(startedAt - idleMs);
|
|
21
|
-
const idle = await store.
|
|
21
|
+
const idle = await store.listIdleConnections({
|
|
22
22
|
refreshedBefore,
|
|
23
23
|
limit: options.limit ?? DEFAULT_IDLE_LIMIT,
|
|
24
24
|
});
|
|
@@ -32,9 +32,9 @@ export async function refreshIdle(ctx, options) {
|
|
|
32
32
|
result.failed = idle.length;
|
|
33
33
|
return result;
|
|
34
34
|
}
|
|
35
|
-
for (const [index,
|
|
35
|
+
for (const [index, connection] of idle.entries()) {
|
|
36
36
|
// Stop before taking the next lock, so a function timeout can't cut a
|
|
37
|
-
// refresh in half. The oldest
|
|
37
|
+
// refresh in half. The oldest connections come first, so the next run
|
|
38
38
|
// continues where this one stopped.
|
|
39
39
|
if (clock().getTime() - startedAt >= budgetMs) {
|
|
40
40
|
result.remaining = idle.length - index;
|
|
@@ -42,7 +42,7 @@ export async function refreshIdle(ctx, options) {
|
|
|
42
42
|
}
|
|
43
43
|
const window = lockWindow(ctx, clock());
|
|
44
44
|
const locked = await store.acquireRefreshLock({
|
|
45
|
-
id:
|
|
45
|
+
id: connection.id,
|
|
46
46
|
...window,
|
|
47
47
|
refreshedBefore,
|
|
48
48
|
});
|
package/dist/idle-sweep.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"idle-sweep.js","sourceRoot":"","sources":["../src/idle-sweep.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAiB,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,iBAA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AM,CAAC,MAAM,EAAE,CAAC;YAClB,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,5 +8,5 @@ export { roleFromPermissions, type Role } from "./permissions.js";
|
|
|
8
8
|
export { migrations, type Migration } from "./sql.js";
|
|
9
9
|
export { memoryStore } from "./store/memory.js";
|
|
10
10
|
export { postgresStore, type SqlClient } from "./store/postgres.js";
|
|
11
|
-
export type {
|
|
11
|
+
export type { ConnectionRecord, ConnectionRefresh, GrantRecord, GrantStatus, GrantStore, GrantUpsert, LockRequest, } from "./store/types.js";
|
|
12
12
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EACV,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,cAAc,EACd,cAAc,EACd,KAAK,EACL,aAAa,EACb,iBAAiB,GAClB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,YAAY,EACZ,SAAS,EACT,KAAK,aAAa,GACnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AACxD,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,KAAK,mBAAmB,GACzB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,mBAAmB,EAAE,KAAK,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,KAAK,SAAS,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,KAAK,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACpE,YAAY,EACV,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EACV,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,eAAe,EACf,cAAc,EACd,cAAc,EACd,KAAK,EACL,aAAa,EACb,iBAAiB,GAClB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,YAAY,EACZ,SAAS,EACT,KAAK,aAAa,GACnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AACxD,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,KAAK,mBAAmB,GACzB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,mBAAmB,EAAE,KAAK,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,KAAK,SAAS,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,KAAK,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACpE,YAAY,EACV,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACX,WAAW,EACX,UAAU,EACV,WAAW,EACX,WAAW,GACZ,MAAM,kBAAkB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrations.generated.d.ts","sourceRoot":"","sources":["../src/migrations.generated.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAE1C,eAAO,MAAM,UAAU,EAAE,SAAS,SAAS,
|
|
1
|
+
{"version":3,"file":"migrations.generated.d.ts","sourceRoot":"","sources":["../src/migrations.generated.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAE1C,eAAO,MAAM,UAAU,EAAE,SAAS,SAAS,EAsD1C,CAAC"}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
export const MIGRATIONS = [
|
|
2
2
|
{
|
|
3
3
|
number: 1,
|
|
4
|
-
name: "
|
|
5
|
-
sql: `-- @soulcode/bexio-oauth 0001:
|
|
6
|
-
--
|
|
7
|
-
--
|
|
4
|
+
name: "0001_bexio_oauth.sql",
|
|
5
|
+
sql: `-- @soulcode/bexio-oauth 0001: connections and grants.
|
|
6
|
+
-- Token columns hold JWE ciphertext; only your server-side database role
|
|
7
|
+
-- should be able to read these tables.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
-- One row per Bexio identity (company, user): the tokens of its one refresh
|
|
10
|
+
-- token chain, shared by every owner that connected this identity.
|
|
11
|
+
CREATE TABLE IF NOT EXISTS bexio_connections (
|
|
10
12
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
11
|
-
owner_id text NOT NULL,
|
|
12
13
|
bexio_company_id text NOT NULL,
|
|
13
14
|
bexio_user_id text NOT NULL,
|
|
14
15
|
bexio_company_user_id integer,
|
|
@@ -24,16 +25,31 @@ CREATE TABLE IF NOT EXISTS bexio_grants (
|
|
|
24
25
|
scope text,
|
|
25
26
|
refresh_locked_until timestamptz,
|
|
26
27
|
refresh_lock_token text,
|
|
28
|
+
authorized_at timestamptz NOT NULL,
|
|
27
29
|
last_refreshed_at timestamptz NOT NULL DEFAULT now(),
|
|
28
30
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
29
31
|
updated_at timestamptz NOT NULL DEFAULT now(),
|
|
30
|
-
CONSTRAINT
|
|
32
|
+
CONSTRAINT bexio_connections_company_user_key UNIQUE (bexio_company_id, bexio_user_id)
|
|
31
33
|
);
|
|
32
34
|
|
|
33
|
-
-- refreshIdle(): active
|
|
34
|
-
CREATE INDEX IF NOT EXISTS
|
|
35
|
-
ON
|
|
35
|
+
-- refreshIdle(): active connections by last refresh.
|
|
36
|
+
CREATE INDEX IF NOT EXISTS bexio_connections_idle_idx
|
|
37
|
+
ON bexio_connections (last_refreshed_at)
|
|
36
38
|
WHERE status = 'active';
|
|
39
|
+
|
|
40
|
+
-- One row per (owner, connection): an owner's access to a connection. A
|
|
41
|
+
-- connection is deleted only once no grant references it (RESTRICT).
|
|
42
|
+
CREATE TABLE IF NOT EXISTS bexio_grants (
|
|
43
|
+
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
44
|
+
owner_id text NOT NULL,
|
|
45
|
+
connection_id uuid NOT NULL REFERENCES bexio_connections (id) ON DELETE RESTRICT,
|
|
46
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
47
|
+
CONSTRAINT bexio_grants_owner_connection_key UNIQUE (owner_id, connection_id)
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
-- Disconnect checks whether a connection has grants left.
|
|
51
|
+
CREATE INDEX IF NOT EXISTS bexio_grants_connection_idx
|
|
52
|
+
ON bexio_grants (connection_id);
|
|
37
53
|
`,
|
|
38
54
|
},
|
|
39
55
|
];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrations.generated.js","sourceRoot":"","sources":["../src/migrations.generated.ts"],"names":[],"mappings":"AAIA,MAAM,CAAC,MAAM,UAAU,GAAyB;IAC9C;QACE,MAAM,EAAE,CAAC;QACT,IAAI,EAAE,
|
|
1
|
+
{"version":3,"file":"migrations.generated.js","sourceRoot":"","sources":["../src/migrations.generated.ts"],"names":[],"mappings":"AAIA,MAAM,CAAC,MAAM,UAAU,GAAyB;IAC9C;QACE,MAAM,EAAE,CAAC;QACT,IAAI,EAAE,sBAAsB;QAC5B,GAAG,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgDR;KACE;CACF,CAAC"}
|
package/dist/refresh.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as oidc from "openid-client";
|
|
2
2
|
import type { ClientContext } from "./config.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { ConnectionRecord } from "./store/types.js";
|
|
4
4
|
import type { AccessToken, GrantSelector } from "./types.js";
|
|
5
5
|
/** Refresh a little before the access token really expires. */
|
|
6
6
|
export declare const EXPIRY_SKEW_MS = 30000;
|
|
@@ -11,7 +11,7 @@ export declare function tokenExpiry(tokens: TokenResponse, now: Date): {
|
|
|
11
11
|
refreshExpiresAt: Date | null;
|
|
12
12
|
};
|
|
13
13
|
/** True while the access token is valid for longer than the skew. */
|
|
14
|
-
export declare function isFresh(
|
|
14
|
+
export declare function isFresh(connection: ConnectionRecord, now: Date): boolean;
|
|
15
15
|
/** A new lock holder's token and lease, starting at `now`. */
|
|
16
16
|
export declare function lockWindow(ctx: ClientContext, now: Date): {
|
|
17
17
|
now: Date;
|
|
@@ -19,12 +19,12 @@ export declare function lockWindow(ctx: ClientContext, now: Date): {
|
|
|
19
19
|
lockToken: `${string}-${string}-${string}-${string}-${string}`;
|
|
20
20
|
};
|
|
21
21
|
/**
|
|
22
|
-
* Refreshes a
|
|
23
|
-
* is fenced by the token: if the lock was lost (expired and taken over,
|
|
24
|
-
* the
|
|
25
|
-
* returns `null`: the caller re-reads the
|
|
22
|
+
* Refreshes a connection whose lock the caller holds (`lockToken`). Every
|
|
23
|
+
* write is fenced by the token: if the lock was lost (expired and taken over,
|
|
24
|
+
* or the connection was reconnected meanwhile), the write changes nothing and
|
|
25
|
+
* this returns `null`: the caller re-reads the connection.
|
|
26
26
|
*/
|
|
27
|
-
export declare function refreshLocked(ctx: ClientContext,
|
|
27
|
+
export declare function refreshLocked(ctx: ClientContext, connection: ConnectionRecord, lockToken: string): Promise<ConnectionRecord | null>;
|
|
28
28
|
/** `BexioOAuth.getAccessToken`. */
|
|
29
29
|
export declare function getAccessToken(ctx: ClientContext, selector: GrantSelector): Promise<AccessToken>;
|
|
30
30
|
//# sourceMappingURL=refresh.d.ts.map
|