@privateaim/server-realtime-kit 0.7.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.
Files changed (62) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/LICENSE +202 -0
  3. package/README.md +6 -0
  4. package/dist/helpers/index.d.ts +2 -0
  5. package/dist/helpers/index.d.ts.map +1 -0
  6. package/dist/helpers/index.js +24 -0
  7. package/dist/helpers/index.js.map +1 -0
  8. package/dist/helpers/room-subscriptions.d.ts +4 -0
  9. package/dist/helpers/room-subscriptions.d.ts.map +1 -0
  10. package/dist/helpers/room-subscriptions.js +37 -0
  11. package/dist/helpers/room-subscriptions.js.map +1 -0
  12. package/dist/index.d.ts +5 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +27 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/middlewares/force-logged-in.d.ts +3 -0
  17. package/dist/middlewares/force-logged-in.d.ts.map +1 -0
  18. package/dist/middlewares/force-logged-in.js +20 -0
  19. package/dist/middlewares/force-logged-in.js.map +1 -0
  20. package/dist/middlewares/index.d.ts +2 -0
  21. package/dist/middlewares/index.d.ts.map +1 -0
  22. package/dist/middlewares/index.js +24 -0
  23. package/dist/middlewares/index.js.map +1 -0
  24. package/dist/services/authup/index.d.ts +3 -0
  25. package/dist/services/authup/index.d.ts.map +1 -0
  26. package/dist/services/authup/index.js +25 -0
  27. package/dist/services/authup/index.js.map +1 -0
  28. package/dist/services/authup/middleware.d.ts +5 -0
  29. package/dist/services/authup/middleware.d.ts.map +1 -0
  30. package/dist/services/authup/middleware.js +65 -0
  31. package/dist/services/authup/middleware.js.map +1 -0
  32. package/dist/services/authup/types.d.ts +11 -0
  33. package/dist/services/authup/types.d.ts.map +1 -0
  34. package/dist/services/authup/types.js +9 -0
  35. package/dist/services/authup/types.js.map +1 -0
  36. package/dist/services/authup/utils.d.ts +7 -0
  37. package/dist/services/authup/utils.d.ts.map +1 -0
  38. package/dist/services/authup/utils.js +53 -0
  39. package/dist/services/authup/utils.js.map +1 -0
  40. package/dist/services/index.d.ts +2 -0
  41. package/dist/services/index.d.ts.map +1 -0
  42. package/dist/services/index.js +24 -0
  43. package/dist/services/index.js.map +1 -0
  44. package/dist/types.d.ts +25 -0
  45. package/dist/types.d.ts.map +1 -0
  46. package/dist/types.js +9 -0
  47. package/dist/types.js.map +1 -0
  48. package/package.json +36 -0
  49. package/src/helpers/index.ts +8 -0
  50. package/src/helpers/room-subscriptions.ts +46 -0
  51. package/src/index.ts +11 -0
  52. package/src/middlewares/force-logged-in.ts +20 -0
  53. package/src/middlewares/index.ts +8 -0
  54. package/src/services/authup/index.ts +9 -0
  55. package/src/services/authup/middleware.ts +80 -0
  56. package/src/services/authup/types.ts +18 -0
  57. package/src/services/authup/utils.ts +72 -0
  58. package/src/services/index.ts +8 -0
  59. package/src/types.ts +69 -0
  60. package/tsconfig.build.json +11 -0
  61. package/tsconfig.json +13 -0
  62. package/writable/.gitkeep +0 -0
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024.
4
+ * Author Peter Placzek (tada5hi)
5
+ * For the full copyright and license information,
6
+ * view the LICENSE file that was distributed with this source code.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.createFakeTokenVerificationData = createFakeTokenVerificationData;
10
+ exports.applyTokenVerificationData = applyTokenVerificationData;
11
+ const kit_1 = require("@authup/kit");
12
+ const core_kit_1 = require("@authup/core-kit");
13
+ const kit_2 = require("@privateaim/kit");
14
+ function generateAbilities() {
15
+ return Object.values(kit_2.PermissionID).map((name) => ({
16
+ name,
17
+ }));
18
+ }
19
+ function createFakeTokenVerificationData() {
20
+ return {
21
+ realm_id: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee',
22
+ realm_name: core_kit_1.REALM_MASTER_NAME,
23
+ sub_kind: 'user',
24
+ sub: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee',
25
+ sub_name: 'admin',
26
+ permissions: generateAbilities(),
27
+ };
28
+ }
29
+ function applyTokenVerificationData(socket, data, fakeAbilities) {
30
+ let abilities;
31
+ if (fakeAbilities) {
32
+ abilities = generateAbilities();
33
+ }
34
+ else {
35
+ abilities = data.permissions;
36
+ }
37
+ socket.data.realmId = data.realm_id;
38
+ socket.data.realmName = data.realm_name;
39
+ socket.data.abilities = new kit_1.Abilities(abilities);
40
+ switch (data.sub_kind) {
41
+ case kit_1.OAuth2SubKind.USER: {
42
+ socket.data.userId = data.sub;
43
+ socket.data.userName = data.sub_name;
44
+ break;
45
+ }
46
+ case kit_1.OAuth2SubKind.ROBOT: {
47
+ socket.data.robotId = data.sub;
48
+ socket.data.robotName = data.sub_name;
49
+ break;
50
+ }
51
+ }
52
+ }
53
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/services/authup/utils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAyBH,0EAWC;AAED,gEA4BC;AA/DD,qCAAuD;AACvD,+CAAqD;AAErD,yCAA+C;AAa/C,SAAS,iBAAiB;IACtB,OAAO,MAAM,CAAC,MAAM,CAAC,kBAAY,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC9C,IAAI;KACY,CAAA,CAAC,CAAC;AAC1B,CAAC;AAED,SAAgB,+BAA+B;IAC3C,OAAO;QACH,QAAQ,EAAE,sCAAsC;QAChD,UAAU,EAAE,4BAAiB;QAE7B,QAAQ,EAAE,MAAM;QAChB,GAAG,EAAE,sCAAsC;QAC3C,QAAQ,EAAE,OAAO;QAEjB,WAAW,EAAE,iBAAiB,EAAE;KACnC,CAAC;AACN,CAAC;AAED,SAAgB,0BAA0B,CACtC,MAAc,EACd,IAAkC,EAClC,aAAuB;IAEvB,IAAI,SAAoB,CAAC;IACzB,IAAI,aAAa,EAAE,CAAC;QAChB,SAAS,GAAG,iBAAiB,EAAE,CAAC;IACpC,CAAC;SAAM,CAAC;QACJ,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC;IACjC,CAAC;IAED,MAAM,CAAC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC;IACpC,MAAM,CAAC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC;IACxC,MAAM,CAAC,IAAI,CAAC,SAAS,GAAG,IAAI,eAAS,CAAC,SAAS,CAAC,CAAC;IAEjD,QAAQ,IAAI,CAAC,QAAQ,EAAE,CAAC;QACpB,KAAK,mBAAa,CAAC,IAAI,CAAC,CAAC,CAAC;YACtB,MAAM,CAAC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC;YAC9B,MAAM,CAAC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;YACrC,MAAM;QACV,CAAC;QACD,KAAK,mBAAa,CAAC,KAAK,CAAC,CAAC,CAAC;YACvB,MAAM,CAAC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC;YAC/B,MAAM,CAAC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,QAAQ,CAAC;YACtC,MAAM;QACV,CAAC;IACL,CAAC;AACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './authup';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAOA,cAAc,UAAU,CAAC"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024.
4
+ * Author Peter Placzek (tada5hi)
5
+ * For the full copyright and license information,
6
+ * view the LICENSE file that was distributed with this source code.
7
+ */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
20
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ __exportStar(require("./authup"), exports);
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;AAEH,2CAAyB"}
@@ -0,0 +1,25 @@
1
+ import type { Realm, Robot, User } from '@authup/core-kit';
2
+ import type { Abilities } from '@authup/kit';
3
+ import type { Namespace as _Namespace, Server as _Server, Socket as _Socket } from 'socket.io';
4
+ export type SocketData = {
5
+ abilities?: Abilities;
6
+ realmId?: Realm['id'];
7
+ realmName?: Realm['name'];
8
+ userId?: User['id'];
9
+ userName?: User['name'];
10
+ robotId?: Robot['id'];
11
+ robotName?: Robot['name'];
12
+ namespaceId?: string;
13
+ roomSubscriptions: Record<string, number>;
14
+ };
15
+ export interface EventsMap {
16
+ [p: string]: any;
17
+ }
18
+ export interface DefaultEventsMap {
19
+ [p: string]: (...args: any[]) => void;
20
+ }
21
+ export type Server<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, Data extends SocketData = SocketData> = _Server<ListenEvents, EmitEvents, ServerSideEvents, Data>;
22
+ export type Socket<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, Data extends SocketData = SocketData> = _Socket<ListenEvents, EmitEvents, ServerSideEvents, Data>;
23
+ export type Namespace<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, Data extends SocketData = SocketData> = _Namespace<ListenEvents, EmitEvents, ServerSideEvents, Data>;
24
+ export type Middleware<ListenEvents extends EventsMap = DefaultEventsMap, EmitEvents extends EventsMap = ListenEvents, ServerSideEvents extends EventsMap = DefaultEventsMap, Data extends SocketData = SocketData> = (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, Data>, next: (err?: Error) => void) => void | Promise<void>;
25
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,KAAK,EACR,SAAS,IAAI,UAAU,EACvB,MAAM,IAAI,OAAO,EACjB,MAAM,IAAI,OAAO,EACpB,MAAM,WAAW,CAAC;AAEnB,MAAM,MAAM,UAAU,GAAG;IACrB,SAAS,CAAC,EAAE,SAAS,CAAC;IAEtB,OAAO,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IACtB,SAAS,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAE1B,MAAM,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;IACpB,QAAQ,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAExB,OAAO,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IACtB,SAAS,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAE1B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC7C,CAAC;AAEF,MAAM,WAAW,SAAS;IACtB,CAAC,CAAC,EAAE,MAAM,GAAG,GAAG,CAAA;CACnB;AAED,MAAM,WAAW,gBAAgB;IAC7B,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;CACxC;AAED,MAAM,MAAM,MAAM,CACd,YAAY,SAAS,SAAS,GAAG,gBAAgB,EACjD,UAAU,SAAS,SAAS,GAAG,YAAY,EAC3C,gBAAgB,SAAS,SAAS,GAAG,gBAAgB,EACrD,IAAI,SAAS,UAAU,GAAG,UAAU,IACpC,OAAO,CAAC,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,IAAI,CAAC,CAAC;AAE9D,MAAM,MAAM,MAAM,CACd,YAAY,SAAS,SAAS,GAAG,gBAAgB,EACjD,UAAU,SAAS,SAAS,GAAG,YAAY,EAC3C,gBAAgB,SAAS,SAAS,GAAG,gBAAgB,EACrD,IAAI,SAAS,UAAU,GAAG,UAAU,IACpC,OAAO,CAAC,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,IAAI,CAAC,CAAC;AAE9D,MAAM,MAAM,SAAS,CACjB,YAAY,SAAS,SAAS,GAAG,gBAAgB,EACjD,UAAU,SAAS,SAAS,GAAG,YAAY,EAC3C,gBAAgB,SAAS,SAAS,GAAG,gBAAgB,EACrD,IAAI,SAAS,UAAU,GAAG,UAAU,IACpC,UAAU,CAAC,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,IAAI,CAAC,CAAC;AAEjE,MAAM,MAAM,UAAU,CAClB,YAAY,SAAS,SAAS,GAAG,gBAAgB,EACjD,UAAU,SAAS,SAAS,GAAG,YAAY,EAC3C,gBAAgB,SAAS,SAAS,GAAG,gBAAgB,EACrD,IAAI,SAAS,UAAU,GAAG,UAAU,IACpC,CACA,MAAM,EAAE,MAAM,CAAC,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,IAAI,CAAC,EAChE,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,KAC1B,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024.
4
+ * Author Peter Placzek (tada5hi)
5
+ * For the full copyright and license information,
6
+ * view the LICENSE file that was distributed with this source code.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA;;;;;GAKG"}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@privateaim/server-realtime-kit",
3
+ "version": "0.7.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "author": {
7
+ "name": "Peter Placzek",
8
+ "email": "contact@tada5hi.net",
9
+ "url": "https://tada5hi.net"
10
+ },
11
+ "license": "Apache-2.0",
12
+ "description": "This package contains the realtime application which connects the API with socket based clients.",
13
+ "dependencies": {
14
+ "@authup/kit": "^1.0.0-beta.18",
15
+ "@authup/core-kit": "^1.0.0-beta.18",
16
+ "@authup/core-realtime-kit": "^1.0.0-beta.18",
17
+ "@authup/server-core-plugin-kit": "^1.0.0-beta.18",
18
+ "@authup/server-core-plugin-socket-io": "^1.0.0-beta.18",
19
+ "@privateaim/kit": "^0.7.0",
20
+ "@privateaim/server-kit": "^0.7.0",
21
+ "@ebec/http": "^2.3.0"
22
+ },
23
+ "devDependencies": {
24
+ "redis-extension": "^1.5.0",
25
+ "socket.io": "^4.7.5"
26
+ },
27
+ "peerDependencies": {
28
+ "redis-extension": "^1.5.0",
29
+ "socket.io": "^4.7.5"
30
+ },
31
+ "scripts": {
32
+ "dev": "ts-node src/index.ts",
33
+ "build": "rimraf dist && tsc -p tsconfig.build.json",
34
+ "start": "node dist/index.js"
35
+ }
36
+ }
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './room-subscriptions';
@@ -0,0 +1,46 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { Socket } from '../types';
9
+
10
+ export function subscribeSocketRoom(
11
+ socket: Socket,
12
+ roomName: string,
13
+ ) {
14
+ if (!socket.data.roomSubscriptions) {
15
+ socket.data.roomSubscriptions = {};
16
+ }
17
+
18
+ if (!socket.data.roomSubscriptions[roomName]) {
19
+ socket.data.roomSubscriptions[roomName] = 0;
20
+ }
21
+
22
+ socket.data.roomSubscriptions[roomName]++;
23
+
24
+ socket.join(roomName);
25
+ }
26
+
27
+ export function unsubscribeSocketRoom(
28
+ socket: Socket,
29
+ roomName: string,
30
+ ) {
31
+ if (!socket.data.roomSubscriptions) {
32
+ return;
33
+ }
34
+
35
+ if (socket.data.roomSubscriptions[roomName]) {
36
+ if (socket.data.roomSubscriptions[roomName] > 1) {
37
+ socket.data.roomSubscriptions[roomName]--;
38
+ } else {
39
+ delete socket.data.roomSubscriptions[roomName];
40
+ }
41
+ }
42
+
43
+ if (!socket.data.roomSubscriptions[roomName]) {
44
+ socket.leave(roomName);
45
+ }
46
+ }
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './helpers';
9
+ export * from './middlewares';
10
+ export * from './services';
11
+ export * from './types';
@@ -0,0 +1,20 @@
1
+ /*
2
+ * Copyright (c) 2021-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { UnauthorizedError } from '@ebec/http';
9
+ import type { Namespace, Server } from '../types';
10
+
11
+ export function mountForceLoggedInMiddleware(input: Namespace | Server) {
12
+ input.use((socket, next) => {
13
+ if (socket.data.userId || socket.data.robotId) {
14
+ next();
15
+ return;
16
+ }
17
+
18
+ next(new UnauthorizedError());
19
+ });
20
+ }
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Copyright (c) 2022-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './force-logged-in';
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './middleware';
9
+ export * from './types';
@@ -0,0 +1,80 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { TokenCreatorOptions } from '@authup/core-http-kit';
9
+ import type { TokenVerifierRedisCacheOptions } from '@authup/server-core-plugin-kit';
10
+ import { createMiddleware } from '@authup/server-core-plugin-socket-io';
11
+ import type {
12
+ Middleware, Namespace, Server, Socket,
13
+ } from '../../types';
14
+ import type { AuthupMiddlewareRegistrationOptions } from './types';
15
+ import { applyTokenVerificationData, createFakeTokenVerificationData } from './utils';
16
+
17
+ export function createAuthupMiddleware(
18
+ options: AuthupMiddlewareRegistrationOptions,
19
+ ) : Middleware {
20
+ let baseURL : string | undefined;
21
+ if (options.baseURL) {
22
+ baseURL = options.baseURL;
23
+ } else if (options.client) {
24
+ baseURL = options.client.getBaseURL();
25
+ }
26
+
27
+ if (!baseURL) {
28
+ const data = createFakeTokenVerificationData();
29
+
30
+ return (socket, next) => {
31
+ applyTokenVerificationData(socket, data, options.fakeAbilities);
32
+ next();
33
+ };
34
+ }
35
+
36
+ let tokenCreator : TokenCreatorOptions;
37
+ if (options.vault) {
38
+ tokenCreator = {
39
+ type: 'robotInVault',
40
+ name: 'system',
41
+ vault: options.vault,
42
+ baseURL,
43
+ };
44
+ } else {
45
+ tokenCreator = {
46
+ type: 'user',
47
+ name: 'admin',
48
+ password: 'start123',
49
+ baseURL,
50
+ };
51
+ }
52
+
53
+ let tokenCache : TokenVerifierRedisCacheOptions | undefined;
54
+ if (options.redis) {
55
+ tokenCache = {
56
+ type: 'redis',
57
+ client: options.redis,
58
+ };
59
+ }
60
+
61
+ return createMiddleware({
62
+ tokenVerifier: {
63
+ baseURL,
64
+ creator: tokenCreator,
65
+ cache: tokenCache,
66
+ },
67
+ tokenVerifierHandler: (
68
+ socket: Socket,
69
+ data,
70
+ ) => applyTokenVerificationData(socket, data, options.fakeAbilities),
71
+ });
72
+ }
73
+
74
+ export function mountAuthupMiddleware(
75
+ nsp: Namespace | Server,
76
+ options: AuthupMiddlewareRegistrationOptions,
77
+ ) {
78
+ const middleware = createAuthupMiddleware(options);
79
+ nsp.use(middleware);
80
+ }
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { VaultClient } from '@hapic/vault';
9
+ import type { Client as RedisClient } from 'redis-extension';
10
+ import type { Client as AuthupClient } from '@authup/core-http-kit';
11
+
12
+ export type AuthupMiddlewareRegistrationOptions = {
13
+ baseURL?: string,
14
+ client?: AuthupClient,
15
+ vault?: VaultClient | string,
16
+ redis?: RedisClient | string,
17
+ fakeAbilities?: boolean
18
+ };
@@ -0,0 +1,72 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { Ability } from '@authup/kit';
9
+ import { Abilities, OAuth2SubKind } from '@authup/kit';
10
+ import { REALM_MASTER_NAME } from '@authup/core-kit';
11
+ import type { TokenVerificationData } from '@authup/server-core-plugin-kit';
12
+ import { PermissionID } from '@privateaim/kit';
13
+ import type { Socket } from '../../types';
14
+
15
+ type TokenVerificationDataMinimal = Pick<
16
+ TokenVerificationData,
17
+ 'permissions' |
18
+ 'realm_id' |
19
+ 'realm_name' |
20
+ 'sub' |
21
+ 'sub_kind' |
22
+ 'sub_name'
23
+ >;
24
+
25
+ function generateAbilities(): Ability[] {
26
+ return Object.values(PermissionID).map((name) => ({
27
+ name,
28
+ } satisfies Ability));
29
+ }
30
+
31
+ export function createFakeTokenVerificationData(): TokenVerificationDataMinimal {
32
+ return {
33
+ realm_id: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee',
34
+ realm_name: REALM_MASTER_NAME,
35
+
36
+ sub_kind: 'user',
37
+ sub: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee',
38
+ sub_name: 'admin',
39
+
40
+ permissions: generateAbilities(),
41
+ };
42
+ }
43
+
44
+ export function applyTokenVerificationData(
45
+ socket: Socket,
46
+ data: TokenVerificationDataMinimal,
47
+ fakeAbilities?: boolean,
48
+ ) {
49
+ let abilities: Ability[];
50
+ if (fakeAbilities) {
51
+ abilities = generateAbilities();
52
+ } else {
53
+ abilities = data.permissions;
54
+ }
55
+
56
+ socket.data.realmId = data.realm_id;
57
+ socket.data.realmName = data.realm_name;
58
+ socket.data.abilities = new Abilities(abilities);
59
+
60
+ switch (data.sub_kind) {
61
+ case OAuth2SubKind.USER: {
62
+ socket.data.userId = data.sub;
63
+ socket.data.userName = data.sub_name;
64
+ break;
65
+ }
66
+ case OAuth2SubKind.ROBOT: {
67
+ socket.data.robotId = data.sub;
68
+ socket.data.robotName = data.sub_name;
69
+ break;
70
+ }
71
+ }
72
+ }
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './authup';
package/src/types.ts ADDED
@@ -0,0 +1,69 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { Realm, Robot, User } from '@authup/core-kit';
9
+ import type { Abilities } from '@authup/kit';
10
+ import type {
11
+ Namespace as _Namespace,
12
+ Server as _Server,
13
+ Socket as _Socket,
14
+ } from 'socket.io';
15
+
16
+ export type SocketData = {
17
+ abilities?: Abilities,
18
+
19
+ realmId?: Realm['id'],
20
+ realmName?: Realm['name'],
21
+
22
+ userId?: User['id'],
23
+ userName?: User['name'],
24
+
25
+ robotId?: Robot['id'],
26
+ robotName?: Robot['name'],
27
+
28
+ namespaceId?: string,
29
+ roomSubscriptions: Record<string, number>,
30
+ };
31
+
32
+ export interface EventsMap {
33
+ [p: string]: any
34
+ }
35
+
36
+ export interface DefaultEventsMap {
37
+ [p: string]: (...args: any[]) => void
38
+ }
39
+
40
+ export type Server<
41
+ ListenEvents extends EventsMap = DefaultEventsMap,
42
+ EmitEvents extends EventsMap = ListenEvents,
43
+ ServerSideEvents extends EventsMap = DefaultEventsMap,
44
+ Data extends SocketData = SocketData,
45
+ > = _Server<ListenEvents, EmitEvents, ServerSideEvents, Data>;
46
+
47
+ export type Socket<
48
+ ListenEvents extends EventsMap = DefaultEventsMap,
49
+ EmitEvents extends EventsMap = ListenEvents,
50
+ ServerSideEvents extends EventsMap = DefaultEventsMap,
51
+ Data extends SocketData = SocketData,
52
+ > = _Socket<ListenEvents, EmitEvents, ServerSideEvents, Data>;
53
+
54
+ export type Namespace<
55
+ ListenEvents extends EventsMap = DefaultEventsMap,
56
+ EmitEvents extends EventsMap = ListenEvents,
57
+ ServerSideEvents extends EventsMap = DefaultEventsMap,
58
+ Data extends SocketData = SocketData,
59
+ > = _Namespace<ListenEvents, EmitEvents, ServerSideEvents, Data>;
60
+
61
+ export type Middleware<
62
+ ListenEvents extends EventsMap = DefaultEventsMap,
63
+ EmitEvents extends EventsMap = ListenEvents,
64
+ ServerSideEvents extends EventsMap = DefaultEventsMap,
65
+ Data extends SocketData = SocketData,
66
+ > = (
67
+ socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, Data>,
68
+ next: (err?: Error) => void
69
+ ) => void | Promise<void>;
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../tsconfig.build.json",
3
+
4
+ "compilerOptions": {
5
+ "outDir": "./dist"
6
+ },
7
+
8
+ "include": [
9
+ "src/**/*.ts"
10
+ ]
11
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "paths": {
5
+ "@privateaim/kit": [
6
+ "./packages/kit/src"
7
+ ],
8
+ "@privateaim/server-kit": [
9
+ "./packages/server-kit/src"
10
+ ]
11
+ }
12
+ }
13
+ }
File without changes