@artemis-apps/app-server 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/dist/account-deletion/account-deletion.service.d.ts +18 -0
- package/dist/account-deletion/account-deletion.service.js +18 -0
- package/dist/activity/core-activity.client.d.ts +20 -0
- package/dist/activity/core-activity.client.js +32 -0
- package/dist/core-api/core-api.client.d.ts +25 -0
- package/dist/core-api/core-api.client.js +63 -0
- package/dist/enrollment/enrollment.service.d.ts +30 -0
- package/dist/enrollment/enrollment.service.js +103 -0
- package/dist/entity-grants/entity-grant.service.d.ts +22 -0
- package/dist/entity-grants/entity-grant.service.js +49 -0
- package/dist/events/security-event.routes.d.ts +24 -0
- package/dist/events/security-event.routes.js +28 -0
- package/dist/handlers/handler.routes.d.ts +32 -0
- package/dist/handlers/handler.routes.js +74 -0
- package/dist/icon/app-icon.routes.d.ts +15 -0
- package/dist/icon/app-icon.routes.js +18 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +4 -0
- package/dist/keys/app-key.entity.d.ts +67 -0
- package/dist/keys/app-key.entity.js +10 -0
- package/dist/keys/app-key.repository.d.ts +14 -0
- package/dist/keys/app-key.repository.js +38 -0
- package/dist/manifest/manifest.routes.d.ts +115 -0
- package/dist/manifest/manifest.routes.js +9 -0
- package/dist/oidc/account-claims.d.ts +5 -0
- package/dist/oidc/account-claims.js +22 -0
- package/dist/oidc/oidc.client.d.ts +64 -0
- package/dist/oidc/oidc.client.js +205 -0
- package/dist/platform.config.d.ts +22 -0
- package/dist/platform.config.js +0 -0
- package/dist/platform.d.ts +387 -0
- package/dist/platform.js +125 -0
- package/dist/platform.tables.d.ts +448 -0
- package/dist/platform.tables.js +9 -0
- package/dist/providers/provider.routes.d.ts +78 -0
- package/dist/providers/provider.routes.js +208 -0
- package/dist/relay/core-relay.routes.d.ts +26 -0
- package/dist/relay/core-relay.routes.js +64 -0
- package/dist/session/app-session.entity.d.ts +301 -0
- package/dist/session/app-session.entity.js +26 -0
- package/dist/session/app-session.repository.d.ts +34 -0
- package/dist/session/app-session.repository.js +87 -0
- package/dist/session/app-sign-in-attempt.entity.d.ts +82 -0
- package/dist/session/app-sign-in-attempt.entity.js +8 -0
- package/dist/session/app-sign-in-attempt.repository.d.ts +15 -0
- package/dist/session/app-sign-in-attempt.repository.js +29 -0
- package/dist/session/app.cookies.d.ts +22 -0
- package/dist/session/app.cookies.js +18 -0
- package/dist/session/require-permissions.middleware.d.ts +36 -0
- package/dist/session/require-permissions.middleware.js +34 -0
- package/dist/session/session.routes.d.ts +204 -0
- package/dist/session/session.routes.js +92 -0
- package/dist/session/session.service.d.ts +50 -0
- package/dist/session/session.service.js +137 -0
- package/dist/storage/core-storage.client.d.ts +27 -0
- package/dist/storage/core-storage.client.js +71 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/testing/index.js +3 -0
- package/dist/testing/platform-fixtures.d.ts +24 -0
- package/dist/testing/platform-fixtures.js +25 -0
- package/dist/testing/test-core.d.ts +73 -0
- package/dist/testing/test-core.js +134 -0
- package/package.json +45 -3
- package/README.md +0 -3
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { APP_PROTOCOL_PATHS, ENTITY_PARAMS, fillPath, operationPermission, PROVIDER_TOKEN_TYPE, ProviderTokenClaimsSchema, parseScope, REQUIRED_OPERATIONS, typePermission, } from '@artemis-apps/contracts';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
|
+
import { createMiddleware } from 'hono/factory';
|
|
4
|
+
import { HTTPException } from 'hono/http-exception';
|
|
5
|
+
const BEARER_PATTERN = /^Bearer\s+(\S+)$/i;
|
|
6
|
+
const WRITE_OPERATIONS = ['create', 'update', 'delete'];
|
|
7
|
+
/**
|
|
8
|
+
* The manifest's `provides`: by type, the operations its handlers implement. Throws when a type
|
|
9
|
+
* is provided without being declared, or its parent type isn't provided too.
|
|
10
|
+
*/
|
|
11
|
+
export function providedTypes(types, providers) {
|
|
12
|
+
const provided = {};
|
|
13
|
+
for (const [typeId, provider] of Object.entries(providers)) {
|
|
14
|
+
const type = types.find((candidate) => candidate.id === typeId);
|
|
15
|
+
if (!type)
|
|
16
|
+
throw new Error(`Declare the type "${typeId}" in the manifest to provide it.`);
|
|
17
|
+
if (type.parent && !providers[type.parent]) {
|
|
18
|
+
throw new Error(`Provide "${type.parent}" too: "${typeId}" lives in it.`);
|
|
19
|
+
}
|
|
20
|
+
provided[typeId] = {
|
|
21
|
+
operations: [
|
|
22
|
+
...REQUIRED_OPERATIONS,
|
|
23
|
+
...WRITE_OPERATIONS.filter((operation) => provider[operation] !== undefined),
|
|
24
|
+
...Object.keys(provider.operations ?? {}),
|
|
25
|
+
],
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return provided;
|
|
29
|
+
}
|
|
30
|
+
function inputOf(context) {
|
|
31
|
+
return {
|
|
32
|
+
contentType: context.req.header('content-type') ?? null,
|
|
33
|
+
json: () => context.req.json().catch(() => {
|
|
34
|
+
throw new HTTPException(400, { message: 'Expected a JSON body.' });
|
|
35
|
+
}),
|
|
36
|
+
formData: () => context.req.formData().catch(() => {
|
|
37
|
+
throw new HTTPException(400, { message: 'Expected a form.' });
|
|
38
|
+
}),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const outOfReach = () => new HTTPException(404, { message: 'Not found.' });
|
|
42
|
+
/** The entity a call is about no longer exists: the core drops its shares on this answer only. */
|
|
43
|
+
const gone = () => new HTTPException(410, { message: 'It no longer exists.' });
|
|
44
|
+
const isNotFound = (error) => error instanceof HTTPException && error.status === 404;
|
|
45
|
+
/**
|
|
46
|
+
* The routes the core calls on what the app provides (`APP_PROTOCOL_PATHS.providedEntit…`),
|
|
47
|
+
* each with a token the core minted for that one call: its scope holds the permission the
|
|
48
|
+
* operation needs, its `sub` the owner, its `within` the shared entity the call stays in.
|
|
49
|
+
*/
|
|
50
|
+
export function createProviderRoutes({ types, providers, oidcClient }) {
|
|
51
|
+
const routes = new Hono();
|
|
52
|
+
for (const [typeId, provider] of Object.entries(providers)) {
|
|
53
|
+
const declared = types.find((candidate) => candidate.id === typeId);
|
|
54
|
+
if (!declared)
|
|
55
|
+
throw new Error(`Declare the type "${typeId}" in the manifest to provide it.`);
|
|
56
|
+
const type = declared;
|
|
57
|
+
const entities = fillPath(APP_PROTOCOL_PATHS.providedEntities, { type: typeId });
|
|
58
|
+
// Hono's own parameters for the entity and the operation: `fillPath` would encode them.
|
|
59
|
+
const entity = `${entities}/:entityId`;
|
|
60
|
+
const operation = `${entity}/operations/:operation`;
|
|
61
|
+
const requireCore = (operationName) => createMiddleware(async (context, next) => {
|
|
62
|
+
const permission = operationPermission(type, operationName);
|
|
63
|
+
if (!permission)
|
|
64
|
+
throw new HTTPException(404, { message: 'No such operation.' });
|
|
65
|
+
const token = context.req.header('Authorization')?.match(BEARER_PATTERN)?.[1];
|
|
66
|
+
if (!token)
|
|
67
|
+
throw new HTTPException(401, { message: 'A core token is required.' });
|
|
68
|
+
const claims = ProviderTokenClaimsSchema.safeParse(await oidcClient.verifyCoreToken(token, PROVIDER_TOKEN_TYPE));
|
|
69
|
+
const required = typePermission(typeId, permission);
|
|
70
|
+
if (!claims.success || !parseScope(claims.data.scope).includes(required)) {
|
|
71
|
+
throw new HTTPException(403, { message: `This token does not allow ${required}.` });
|
|
72
|
+
}
|
|
73
|
+
const { sub, act, client_id: appId, within } = claims.data;
|
|
74
|
+
context.set('call', { ownerId: sub, actorId: act?.sub ?? sub, appId, within });
|
|
75
|
+
await next();
|
|
76
|
+
});
|
|
77
|
+
/** Whether `entityId`, of `entityType`, lies within the call's shared entity. */
|
|
78
|
+
async function isReachable(call, entityType, entityId) {
|
|
79
|
+
const { within } = call;
|
|
80
|
+
if (!within || (within.type === entityType && within.id === entityId))
|
|
81
|
+
return true;
|
|
82
|
+
const contains = providers[entityType]?.contains;
|
|
83
|
+
return contains ? contains(call, within, entityId) : false;
|
|
84
|
+
}
|
|
85
|
+
async function reachable(context) {
|
|
86
|
+
const entityId = context.req.param('entityId') ?? '';
|
|
87
|
+
if (!(await isReachable(context.get('call'), typeId, entityId)))
|
|
88
|
+
throw outOfReach();
|
|
89
|
+
return entityId;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Whether the owner still has the entity, by its type's `read` (which throws a 404 when it
|
|
93
|
+
* doesn't); any other failure says nothing.
|
|
94
|
+
*/
|
|
95
|
+
async function isMissing(call, entityType, entityId) {
|
|
96
|
+
const read = providers[entityType]?.read;
|
|
97
|
+
if (!read)
|
|
98
|
+
return false;
|
|
99
|
+
try {
|
|
100
|
+
await read(call, entityId);
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
return isNotFound(error);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Runs a handler about one entity. Its 404 may be about something else (a parent, a
|
|
109
|
+
* thumbnail): it becomes a 410 only when the entity itself is missing, so that the core
|
|
110
|
+
* drops shares of what is really gone, and of nothing else.
|
|
111
|
+
*/
|
|
112
|
+
async function aboutEntity(call, entityId, handle) {
|
|
113
|
+
try {
|
|
114
|
+
return await handle();
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (isNotFound(error) && (await isMissing(call, typeId, entityId)))
|
|
118
|
+
throw gone();
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** The parent asked for (`?parent=`), checked within the call's reach. */
|
|
123
|
+
async function parentOf(context) {
|
|
124
|
+
const parentId = context.req.query(ENTITY_PARAMS.parent) || undefined;
|
|
125
|
+
const call = context.get('call');
|
|
126
|
+
if (parentId === undefined) {
|
|
127
|
+
// Only the owner creates at the top: a share reaches inside what it shares.
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
if (!type.parent || !(await isReachable(call, type.parent, parentId)))
|
|
131
|
+
throw outOfReach();
|
|
132
|
+
return parentId;
|
|
133
|
+
}
|
|
134
|
+
routes
|
|
135
|
+
.get(entities, requireCore('list'), async (context) => {
|
|
136
|
+
const { [ENTITY_PARAMS.parent]: _parent, ...params } = context.req.query();
|
|
137
|
+
const call = context.get('call');
|
|
138
|
+
try {
|
|
139
|
+
const list = await provider.list(call, params);
|
|
140
|
+
return context.json({ entities: list });
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
// Within a share: the shared entity may be what is gone.
|
|
144
|
+
const { within } = call;
|
|
145
|
+
if (within && isNotFound(error) && (await isMissing(call, within.type, within.id))) {
|
|
146
|
+
throw gone();
|
|
147
|
+
}
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
.get(entity, requireCore('read'), async (context) => {
|
|
152
|
+
const call = context.get('call');
|
|
153
|
+
const entityId = await reachable(context);
|
|
154
|
+
try {
|
|
155
|
+
return context.json((await provider.read(call, entityId)));
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
// `read` answers 404 for an entity the owner doesn't have: it is gone.
|
|
159
|
+
if (isNotFound(error))
|
|
160
|
+
throw gone();
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
const { create, update, delete: remove } = provider;
|
|
165
|
+
if (create) {
|
|
166
|
+
routes.post(entities, requireCore('create'), async (context) => {
|
|
167
|
+
const call = context.get('call');
|
|
168
|
+
const parentId = await parentOf(context);
|
|
169
|
+
if (parentId === undefined && call.within)
|
|
170
|
+
throw outOfReach();
|
|
171
|
+
if (parentId === undefined && type.parent && type.parent !== typeId) {
|
|
172
|
+
throw new HTTPException(400, { message: `It is created in a ${type.parent}.` });
|
|
173
|
+
}
|
|
174
|
+
const created = await create(call, parentId ?? null, inputOf(context));
|
|
175
|
+
return context.json(created, 201);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
if (update) {
|
|
179
|
+
routes.patch(entity, requireCore('update'), async (context) => {
|
|
180
|
+
const call = context.get('call');
|
|
181
|
+
const entityId = await reachable(context);
|
|
182
|
+
const parentId = await parentOf(context);
|
|
183
|
+
// A move alone sends no body, hence no content type.
|
|
184
|
+
const updated = await aboutEntity(call, entityId, () => update(call, entityId, {
|
|
185
|
+
parentId,
|
|
186
|
+
input: context.req.header('content-type') ? inputOf(context) : null,
|
|
187
|
+
}));
|
|
188
|
+
return context.json(updated);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
if (remove) {
|
|
192
|
+
routes.delete(entity, requireCore('delete'), async (context) => {
|
|
193
|
+
const call = context.get('call');
|
|
194
|
+
const entityId = await reachable(context);
|
|
195
|
+
await aboutEntity(call, entityId, () => remove(call, entityId));
|
|
196
|
+
return context.body(null, 204);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
for (const [name, handler] of Object.entries(provider.operations ?? {})) {
|
|
200
|
+
routes.on(['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], operation.replace(':operation', name), requireCore(name), async (context) => {
|
|
201
|
+
const call = context.get('call');
|
|
202
|
+
const entityId = await reachable(context);
|
|
203
|
+
return aboutEntity(call, entityId, () => handler(call, entityId, context.req.raw));
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return routes;
|
|
208
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type HttpOnlyCookie } from '@artemis-apps/server-kit';
|
|
2
|
+
import type { CoreApiClient } from '../core-api/core-api.client.ts';
|
|
3
|
+
import type { SessionService } from '../session/session.service.ts';
|
|
4
|
+
export interface CoreRelayRoutesOptions {
|
|
5
|
+
/** The app's base path (`/apps/drive`), `''` at the root of its origin. */
|
|
6
|
+
basePath: string;
|
|
7
|
+
coreApi: Pick<CoreApiClient, 'forAccount'>;
|
|
8
|
+
sessionService: Pick<SessionService, 'resolve'>;
|
|
9
|
+
sessionCookie: HttpOnlyCookie;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The relay: the app's web client reaches the core's API (entities, entity types, directory,
|
|
13
|
+
* launcher: `CORE_RELAYED_PATHS`) at `APP_PROTOCOL_PATHS.coreRelay`, its server adding the
|
|
14
|
+
* session's access token. The browser never holds a core token, and the core needs no CORS.
|
|
15
|
+
* Requests and answers pass through as they are; nothing is read or rewritten.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createCoreRelayRoutes({ basePath, coreApi, sessionService, sessionCookie, }: CoreRelayRoutesOptions): import("hono/hono-base").HonoBase<import("hono/types").BlankEnv, {
|
|
18
|
+
"/api/core/*": {
|
|
19
|
+
$all: {
|
|
20
|
+
output: {};
|
|
21
|
+
outputFormat: string;
|
|
22
|
+
status: import("hono/utils/http-status").StatusCode;
|
|
23
|
+
input: {};
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
}, "/", "/api/core/*">;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { APP_PROTOCOL_PATHS, CORE_RELAYED_PATHS, MAX_ENTITY_BODY_BYTES, } from '@artemis-apps/contracts';
|
|
2
|
+
import { limitUploadSize } from '@artemis-apps/server-kit';
|
|
3
|
+
import { Hono } from 'hono';
|
|
4
|
+
import { HTTPException } from 'hono/http-exception';
|
|
5
|
+
/** Request headers the core reads, passed on as they are. */
|
|
6
|
+
const FORWARDED_REQUEST_HEADERS = ['accept', 'content-type'];
|
|
7
|
+
/** Answer headers the web client needs: what the answer is, and how to treat it. */
|
|
8
|
+
const FORWARDED_RESPONSE_HEADERS = [
|
|
9
|
+
'cache-control',
|
|
10
|
+
'content-disposition',
|
|
11
|
+
'content-length',
|
|
12
|
+
'content-security-policy',
|
|
13
|
+
'content-type',
|
|
14
|
+
'etag',
|
|
15
|
+
'last-modified',
|
|
16
|
+
'x-content-type-options',
|
|
17
|
+
];
|
|
18
|
+
const BODYLESS_METHODS = ['GET', 'HEAD'];
|
|
19
|
+
/**
|
|
20
|
+
* The relay: the app's web client reaches the core's API (entities, entity types, directory,
|
|
21
|
+
* launcher: `CORE_RELAYED_PATHS`) at `APP_PROTOCOL_PATHS.coreRelay`, its server adding the
|
|
22
|
+
* session's access token. The browser never holds a core token, and the core needs no CORS.
|
|
23
|
+
* Requests and answers pass through as they are; nothing is read or rewritten.
|
|
24
|
+
*/
|
|
25
|
+
export function createCoreRelayRoutes({ basePath, coreApi, sessionService, sessionCookie, }) {
|
|
26
|
+
const prefix = APP_PROTOCOL_PATHS.coreRelay;
|
|
27
|
+
return new Hono().all(`${prefix}/*`, limitUploadSize(MAX_ENTITY_BODY_BYTES, 'The request is too large.'), async (context) => {
|
|
28
|
+
const url = new URL(context.req.url);
|
|
29
|
+
const path = `/api${url.pathname.slice(basePath.length + prefix.length)}`;
|
|
30
|
+
const relayed = CORE_RELAYED_PATHS.some((allowed) => path === allowed || path.startsWith(`${allowed}/`));
|
|
31
|
+
if (!relayed)
|
|
32
|
+
throw new HTTPException(404, { message: 'Not found.' });
|
|
33
|
+
const session = await sessionService.resolve(sessionCookie.read(context));
|
|
34
|
+
if (!session)
|
|
35
|
+
throw new HTTPException(401, { message: 'Sign in to continue.' });
|
|
36
|
+
const headers = new Headers();
|
|
37
|
+
for (const name of FORWARDED_REQUEST_HEADERS) {
|
|
38
|
+
const value = context.req.header(name);
|
|
39
|
+
if (value)
|
|
40
|
+
headers.set(name, value);
|
|
41
|
+
}
|
|
42
|
+
const method = context.req.method;
|
|
43
|
+
// Buffered: a refused token is renewed and the request sent again, body included.
|
|
44
|
+
const body = BODYLESS_METHODS.includes(method) ? undefined : await context.req.arrayBuffer();
|
|
45
|
+
const response = await coreApi.forAccount(session, `${path}${url.search}`, {
|
|
46
|
+
method,
|
|
47
|
+
headers,
|
|
48
|
+
body,
|
|
49
|
+
});
|
|
50
|
+
const answer = new Headers();
|
|
51
|
+
for (const name of FORWARDED_RESPONSE_HEADERS) {
|
|
52
|
+
const value = response.headers.get(name);
|
|
53
|
+
if (value)
|
|
54
|
+
answer.set(name, value);
|
|
55
|
+
}
|
|
56
|
+
// `fetch` decompresses a compressed answer (a proxy in front of the core) but keeps its
|
|
57
|
+
// compressed length: the body relayed is longer.
|
|
58
|
+
if (response.headers.has('content-encoding'))
|
|
59
|
+
answer.delete('content-length');
|
|
60
|
+
if (!answer.has('cache-control'))
|
|
61
|
+
answer.set('cache-control', 'no-store');
|
|
62
|
+
return new Response(response.body, { status: response.status, headers: answer });
|
|
63
|
+
});
|
|
64
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import type { DelegablePermission } from '@artemis-apps/contracts';
|
|
2
|
+
/** Server-side app sessions: the browser only holds an opaque token whose hash is the id. */
|
|
3
|
+
export declare const appSessions: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
4
|
+
name: "app_sessions";
|
|
5
|
+
schema: undefined;
|
|
6
|
+
columns: {
|
|
7
|
+
tokenHash: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
8
|
+
name: "tokenHash";
|
|
9
|
+
tableName: "app_sessions";
|
|
10
|
+
dataType: "string";
|
|
11
|
+
columnType: "SQLiteText";
|
|
12
|
+
data: string;
|
|
13
|
+
driverParam: string;
|
|
14
|
+
notNull: true;
|
|
15
|
+
hasDefault: false;
|
|
16
|
+
isPrimaryKey: true;
|
|
17
|
+
isAutoincrement: false;
|
|
18
|
+
hasRuntimeDefault: false;
|
|
19
|
+
enumValues: [string, ...string[]];
|
|
20
|
+
baseColumn: never;
|
|
21
|
+
identity: undefined;
|
|
22
|
+
generated: undefined;
|
|
23
|
+
}, {}, {
|
|
24
|
+
length: undefined;
|
|
25
|
+
}>;
|
|
26
|
+
accountId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
27
|
+
name: "accountId";
|
|
28
|
+
tableName: "app_sessions";
|
|
29
|
+
dataType: "string";
|
|
30
|
+
columnType: "SQLiteText";
|
|
31
|
+
data: string;
|
|
32
|
+
driverParam: string;
|
|
33
|
+
notNull: true;
|
|
34
|
+
hasDefault: false;
|
|
35
|
+
isPrimaryKey: false;
|
|
36
|
+
isAutoincrement: false;
|
|
37
|
+
hasRuntimeDefault: false;
|
|
38
|
+
enumValues: [string, ...string[]];
|
|
39
|
+
baseColumn: never;
|
|
40
|
+
identity: undefined;
|
|
41
|
+
generated: undefined;
|
|
42
|
+
}, {}, {
|
|
43
|
+
length: undefined;
|
|
44
|
+
}>;
|
|
45
|
+
accountEmail: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
46
|
+
name: "accountEmail";
|
|
47
|
+
tableName: "app_sessions";
|
|
48
|
+
dataType: "string";
|
|
49
|
+
columnType: "SQLiteText";
|
|
50
|
+
data: string;
|
|
51
|
+
driverParam: string;
|
|
52
|
+
notNull: true;
|
|
53
|
+
hasDefault: false;
|
|
54
|
+
isPrimaryKey: false;
|
|
55
|
+
isAutoincrement: false;
|
|
56
|
+
hasRuntimeDefault: false;
|
|
57
|
+
enumValues: [string, ...string[]];
|
|
58
|
+
baseColumn: never;
|
|
59
|
+
identity: undefined;
|
|
60
|
+
generated: undefined;
|
|
61
|
+
}, {}, {
|
|
62
|
+
length: undefined;
|
|
63
|
+
}>;
|
|
64
|
+
accountName: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
65
|
+
name: "accountName";
|
|
66
|
+
tableName: "app_sessions";
|
|
67
|
+
dataType: "string";
|
|
68
|
+
columnType: "SQLiteText";
|
|
69
|
+
data: string;
|
|
70
|
+
driverParam: string;
|
|
71
|
+
notNull: true;
|
|
72
|
+
hasDefault: false;
|
|
73
|
+
isPrimaryKey: false;
|
|
74
|
+
isAutoincrement: false;
|
|
75
|
+
hasRuntimeDefault: false;
|
|
76
|
+
enumValues: [string, ...string[]];
|
|
77
|
+
baseColumn: never;
|
|
78
|
+
identity: undefined;
|
|
79
|
+
generated: undefined;
|
|
80
|
+
}, {}, {
|
|
81
|
+
length: undefined;
|
|
82
|
+
}>;
|
|
83
|
+
accountAvatar: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
84
|
+
name: "accountAvatar";
|
|
85
|
+
tableName: "app_sessions";
|
|
86
|
+
dataType: "json";
|
|
87
|
+
columnType: "SQLiteTextJson";
|
|
88
|
+
data: {
|
|
89
|
+
kind: "generated";
|
|
90
|
+
seed: string;
|
|
91
|
+
} | {
|
|
92
|
+
kind: "file";
|
|
93
|
+
fileId: string;
|
|
94
|
+
url: string;
|
|
95
|
+
};
|
|
96
|
+
driverParam: string;
|
|
97
|
+
notNull: true;
|
|
98
|
+
hasDefault: false;
|
|
99
|
+
isPrimaryKey: false;
|
|
100
|
+
isAutoincrement: false;
|
|
101
|
+
hasRuntimeDefault: false;
|
|
102
|
+
enumValues: undefined;
|
|
103
|
+
baseColumn: never;
|
|
104
|
+
identity: undefined;
|
|
105
|
+
generated: undefined;
|
|
106
|
+
}, {}, {
|
|
107
|
+
$type: {
|
|
108
|
+
kind: "generated";
|
|
109
|
+
seed: string;
|
|
110
|
+
} | {
|
|
111
|
+
kind: "file";
|
|
112
|
+
fileId: string;
|
|
113
|
+
url: string;
|
|
114
|
+
};
|
|
115
|
+
}>;
|
|
116
|
+
accountLocale: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
117
|
+
name: "accountLocale";
|
|
118
|
+
tableName: "app_sessions";
|
|
119
|
+
dataType: "string";
|
|
120
|
+
columnType: "SQLiteText";
|
|
121
|
+
data: "en" | "fr";
|
|
122
|
+
driverParam: string;
|
|
123
|
+
notNull: true;
|
|
124
|
+
hasDefault: true;
|
|
125
|
+
isPrimaryKey: false;
|
|
126
|
+
isAutoincrement: false;
|
|
127
|
+
hasRuntimeDefault: false;
|
|
128
|
+
enumValues: [string, ...string[]];
|
|
129
|
+
baseColumn: never;
|
|
130
|
+
identity: undefined;
|
|
131
|
+
generated: undefined;
|
|
132
|
+
}, {}, {
|
|
133
|
+
length: undefined;
|
|
134
|
+
$type: "en" | "fr";
|
|
135
|
+
}>;
|
|
136
|
+
administrator: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
137
|
+
name: "administrator";
|
|
138
|
+
tableName: "app_sessions";
|
|
139
|
+
dataType: "boolean";
|
|
140
|
+
columnType: "SQLiteBoolean";
|
|
141
|
+
data: boolean;
|
|
142
|
+
driverParam: number;
|
|
143
|
+
notNull: true;
|
|
144
|
+
hasDefault: true;
|
|
145
|
+
isPrimaryKey: false;
|
|
146
|
+
isAutoincrement: false;
|
|
147
|
+
hasRuntimeDefault: false;
|
|
148
|
+
enumValues: undefined;
|
|
149
|
+
baseColumn: never;
|
|
150
|
+
identity: undefined;
|
|
151
|
+
generated: undefined;
|
|
152
|
+
}, {}, {}>;
|
|
153
|
+
permissions: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
154
|
+
name: "permissions";
|
|
155
|
+
tableName: "app_sessions";
|
|
156
|
+
dataType: "json";
|
|
157
|
+
columnType: "SQLiteTextJson";
|
|
158
|
+
data: DelegablePermission[];
|
|
159
|
+
driverParam: string;
|
|
160
|
+
notNull: true;
|
|
161
|
+
hasDefault: false;
|
|
162
|
+
isPrimaryKey: false;
|
|
163
|
+
isAutoincrement: false;
|
|
164
|
+
hasRuntimeDefault: false;
|
|
165
|
+
enumValues: undefined;
|
|
166
|
+
baseColumn: never;
|
|
167
|
+
identity: undefined;
|
|
168
|
+
generated: undefined;
|
|
169
|
+
}, {}, {
|
|
170
|
+
$type: DelegablePermission[];
|
|
171
|
+
}>;
|
|
172
|
+
idToken: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
173
|
+
name: "idToken";
|
|
174
|
+
tableName: "app_sessions";
|
|
175
|
+
dataType: "string";
|
|
176
|
+
columnType: "SQLiteText";
|
|
177
|
+
data: string;
|
|
178
|
+
driverParam: string;
|
|
179
|
+
notNull: true;
|
|
180
|
+
hasDefault: false;
|
|
181
|
+
isPrimaryKey: false;
|
|
182
|
+
isAutoincrement: false;
|
|
183
|
+
hasRuntimeDefault: false;
|
|
184
|
+
enumValues: [string, ...string[]];
|
|
185
|
+
baseColumn: never;
|
|
186
|
+
identity: undefined;
|
|
187
|
+
generated: undefined;
|
|
188
|
+
}, {}, {
|
|
189
|
+
length: undefined;
|
|
190
|
+
}>;
|
|
191
|
+
sid: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
192
|
+
name: "sid";
|
|
193
|
+
tableName: "app_sessions";
|
|
194
|
+
dataType: "string";
|
|
195
|
+
columnType: "SQLiteText";
|
|
196
|
+
data: string;
|
|
197
|
+
driverParam: string;
|
|
198
|
+
notNull: true;
|
|
199
|
+
hasDefault: false;
|
|
200
|
+
isPrimaryKey: false;
|
|
201
|
+
isAutoincrement: false;
|
|
202
|
+
hasRuntimeDefault: false;
|
|
203
|
+
enumValues: [string, ...string[]];
|
|
204
|
+
baseColumn: never;
|
|
205
|
+
identity: undefined;
|
|
206
|
+
generated: undefined;
|
|
207
|
+
}, {}, {
|
|
208
|
+
length: undefined;
|
|
209
|
+
}>;
|
|
210
|
+
accessToken: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
211
|
+
name: "accessToken";
|
|
212
|
+
tableName: "app_sessions";
|
|
213
|
+
dataType: "string";
|
|
214
|
+
columnType: "SQLiteText";
|
|
215
|
+
data: string;
|
|
216
|
+
driverParam: string;
|
|
217
|
+
notNull: true;
|
|
218
|
+
hasDefault: false;
|
|
219
|
+
isPrimaryKey: false;
|
|
220
|
+
isAutoincrement: false;
|
|
221
|
+
hasRuntimeDefault: false;
|
|
222
|
+
enumValues: [string, ...string[]];
|
|
223
|
+
baseColumn: never;
|
|
224
|
+
identity: undefined;
|
|
225
|
+
generated: undefined;
|
|
226
|
+
}, {}, {
|
|
227
|
+
length: undefined;
|
|
228
|
+
}>;
|
|
229
|
+
refreshToken: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
230
|
+
name: "refreshToken";
|
|
231
|
+
tableName: "app_sessions";
|
|
232
|
+
dataType: "string";
|
|
233
|
+
columnType: "SQLiteText";
|
|
234
|
+
data: string;
|
|
235
|
+
driverParam: string;
|
|
236
|
+
notNull: false;
|
|
237
|
+
hasDefault: false;
|
|
238
|
+
isPrimaryKey: false;
|
|
239
|
+
isAutoincrement: false;
|
|
240
|
+
hasRuntimeDefault: false;
|
|
241
|
+
enumValues: [string, ...string[]];
|
|
242
|
+
baseColumn: never;
|
|
243
|
+
identity: undefined;
|
|
244
|
+
generated: undefined;
|
|
245
|
+
}, {}, {
|
|
246
|
+
length: undefined;
|
|
247
|
+
}>;
|
|
248
|
+
accessTokenExpiresAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
249
|
+
name: "accessTokenExpiresAt";
|
|
250
|
+
tableName: "app_sessions";
|
|
251
|
+
dataType: "date";
|
|
252
|
+
columnType: "SQLiteTimestamp";
|
|
253
|
+
data: Date;
|
|
254
|
+
driverParam: number;
|
|
255
|
+
notNull: true;
|
|
256
|
+
hasDefault: false;
|
|
257
|
+
isPrimaryKey: false;
|
|
258
|
+
isAutoincrement: false;
|
|
259
|
+
hasRuntimeDefault: false;
|
|
260
|
+
enumValues: undefined;
|
|
261
|
+
baseColumn: never;
|
|
262
|
+
identity: undefined;
|
|
263
|
+
generated: undefined;
|
|
264
|
+
}, {}, {}>;
|
|
265
|
+
expiresAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
266
|
+
name: "expiresAt";
|
|
267
|
+
tableName: "app_sessions";
|
|
268
|
+
dataType: "date";
|
|
269
|
+
columnType: "SQLiteTimestamp";
|
|
270
|
+
data: Date;
|
|
271
|
+
driverParam: number;
|
|
272
|
+
notNull: true;
|
|
273
|
+
hasDefault: false;
|
|
274
|
+
isPrimaryKey: false;
|
|
275
|
+
isAutoincrement: false;
|
|
276
|
+
hasRuntimeDefault: false;
|
|
277
|
+
enumValues: undefined;
|
|
278
|
+
baseColumn: never;
|
|
279
|
+
identity: undefined;
|
|
280
|
+
generated: undefined;
|
|
281
|
+
}, {}, {}>;
|
|
282
|
+
createdAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
283
|
+
name: "createdAt";
|
|
284
|
+
tableName: "app_sessions";
|
|
285
|
+
dataType: "date";
|
|
286
|
+
columnType: "SQLiteTimestamp";
|
|
287
|
+
data: Date;
|
|
288
|
+
driverParam: number;
|
|
289
|
+
notNull: true;
|
|
290
|
+
hasDefault: false;
|
|
291
|
+
isPrimaryKey: false;
|
|
292
|
+
isAutoincrement: false;
|
|
293
|
+
hasRuntimeDefault: false;
|
|
294
|
+
enumValues: undefined;
|
|
295
|
+
baseColumn: never;
|
|
296
|
+
identity: undefined;
|
|
297
|
+
generated: undefined;
|
|
298
|
+
}, {}, {}>;
|
|
299
|
+
};
|
|
300
|
+
dialect: 'sqlite';
|
|
301
|
+
}>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
|
|
2
|
+
/** Server-side app sessions: the browser only holds an opaque token whose hash is the id. */
|
|
3
|
+
export const appSessions = sqliteTable('app_sessions', {
|
|
4
|
+
tokenHash: text().primaryKey(),
|
|
5
|
+
accountId: text().notNull(),
|
|
6
|
+
accountEmail: text().notNull(),
|
|
7
|
+
accountName: text().notNull(),
|
|
8
|
+
/** As the ID token carried it (`picture` or `avatar_seed`), refreshed with it. */
|
|
9
|
+
accountAvatar: text({ mode: 'json' }).$type().notNull(),
|
|
10
|
+
/** The account's language (`AccountPreferences`), refreshed with the ID token. */
|
|
11
|
+
accountLocale: text().$type().notNull().default('en'),
|
|
12
|
+
/** Whether the account administers the platform's apps, as the ID token says. */
|
|
13
|
+
administrator: integer({ mode: 'boolean' }).notNull().default(false),
|
|
14
|
+
permissions: text({ mode: 'json' }).$type().notNull(),
|
|
15
|
+
idToken: text().notNull(),
|
|
16
|
+
/** The core's OIDC session (`sid`): back-channel logout ends the app sessions it holds. */
|
|
17
|
+
sid: text().notNull(),
|
|
18
|
+
accessToken: text().notNull(),
|
|
19
|
+
refreshToken: text(),
|
|
20
|
+
accessTokenExpiresAt: integer({ mode: 'timestamp_ms' }).notNull(),
|
|
21
|
+
expiresAt: integer({ mode: 'timestamp_ms' }).notNull(),
|
|
22
|
+
createdAt: integer({ mode: 'timestamp_ms' }).notNull(),
|
|
23
|
+
}, (table) => [
|
|
24
|
+
index('app_sessions_expires_at_index').on(table.expiresAt),
|
|
25
|
+
index('app_sessions_sid_index').on(table.sid),
|
|
26
|
+
]);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Account, AccountPreferences, DelegablePermission } from '@artemis-apps/contracts';
|
|
2
|
+
import type { PlatformDatabase } from '../platform.tables.ts';
|
|
3
|
+
/** What the core granted to the app for an account: identity, permissions and tokens. */
|
|
4
|
+
export interface SessionGrant {
|
|
5
|
+
account: Account;
|
|
6
|
+
preferences: AccountPreferences;
|
|
7
|
+
/** The account administers the platform's apps (`APP_ADMINISTRATOR_CLAIM`): this one's settings. */
|
|
8
|
+
administrator: boolean;
|
|
9
|
+
permissions: DelegablePermission[];
|
|
10
|
+
idToken: string;
|
|
11
|
+
/** The core's OIDC session the grant belongs to. */
|
|
12
|
+
sid: string;
|
|
13
|
+
accessToken: string;
|
|
14
|
+
refreshToken: string | null;
|
|
15
|
+
accessTokenExpiresAt: Date;
|
|
16
|
+
}
|
|
17
|
+
export interface AppSession extends SessionGrant {
|
|
18
|
+
/** sha256 of the opaque token held by the browser. */
|
|
19
|
+
tokenHash: string;
|
|
20
|
+
expiresAt: Date;
|
|
21
|
+
}
|
|
22
|
+
export declare function createAppSessionRepository(database: PlatformDatabase): {
|
|
23
|
+
find(tokenHash: string): AppSession | undefined;
|
|
24
|
+
create(session: AppSession): void;
|
|
25
|
+
/** Replaces the grant (tokens, permissions) of a session after a refresh. */
|
|
26
|
+
update(tokenHash: string, grant: SessionGrant): AppSession | undefined;
|
|
27
|
+
delete(tokenHash: string): void;
|
|
28
|
+
deleteExpired(now: Date): void;
|
|
29
|
+
/** Ends the app sessions of one core OIDC session (back-channel logout). */
|
|
30
|
+
deleteBySid(sid: string): void;
|
|
31
|
+
deleteByAccount(accountId: string): void;
|
|
32
|
+
deleteAll(): void;
|
|
33
|
+
};
|
|
34
|
+
export type AppSessionRepository = ReturnType<typeof createAppSessionRepository>;
|