@stamhoofd/models 2.144.0 → 2.146.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/constants/sessions.d.ts +30 -0
- package/dist/constants/sessions.d.ts.map +1 -0
- package/dist/constants/sessions.js +42 -0
- package/dist/constants/sessions.js.map +1 -0
- package/dist/migrations/1785491700-tokens-delete-expired.sql +1 -0
- package/dist/migrations/1785491701-user-sessions-table.sql +19 -0
- package/dist/migrations/1785491702-tokens-session-columns.sql +3 -0
- package/dist/migrations/1785491703-tokens-session-id-backfill.sql +1 -0
- package/dist/migrations/1785491704-user-sessions-backfill.sql +3 -0
- package/dist/migrations/1785491705-tokens-session-required.sql +6 -0
- package/dist/migrations/1785491706-mfa-tokens-login-method.sql +2 -0
- package/dist/migrations/1785824341-user-sessions-impersonated-user-id.sql +4 -0
- package/dist/migrations/1785824342-create-impersonation-tokens-table.sql +15 -0
- package/dist/migrations/1787055465-vies-cached-results.sql +6 -0
- package/dist/models/ImpersonationToken.d.ts +63 -0
- package/dist/models/ImpersonationToken.d.ts.map +1 -0
- package/dist/models/ImpersonationToken.js +135 -0
- package/dist/models/ImpersonationToken.js.map +1 -0
- package/dist/models/MFAToken.d.ts +11 -1
- package/dist/models/MFAToken.d.ts.map +1 -1
- package/dist/models/MFAToken.js +12 -1
- package/dist/models/MFAToken.js.map +1 -1
- package/dist/models/STInvoice.d.ts +4 -3
- package/dist/models/STInvoice.d.ts.map +1 -1
- package/dist/models/STInvoice.js +4 -45
- package/dist/models/STInvoice.js.map +1 -1
- package/dist/models/Token.d.ts +9 -46
- package/dist/models/Token.d.ts.map +1 -1
- package/dist/models/Token.js +19 -140
- package/dist/models/Token.js.map +1 -1
- package/dist/models/UserSession.d.ts +28 -0
- package/dist/models/UserSession.d.ts.map +1 -0
- package/dist/models/UserSession.js +76 -0
- package/dist/models/UserSession.js.map +1 -0
- package/dist/models/ViesCachedResult.d.ts +9 -0
- package/dist/models/ViesCachedResult.d.ts.map +1 -0
- package/dist/models/ViesCachedResult.js +22 -0
- package/dist/models/ViesCachedResult.js.map +1 -0
- package/dist/models/_relations.js +4 -0
- package/dist/models/_relations.js.map +1 -1
- package/dist/models/index.d.ts +2 -0
- package/dist/models/index.d.ts.map +1 -1
- package/dist/models/index.js +2 -0
- package/dist/models/index.js.map +1 -1
- package/package.json +3 -4
- package/src/constants/sessions.ts +59 -0
- package/src/migrations/1785491700-tokens-delete-expired.sql +1 -0
- package/src/migrations/1785491701-user-sessions-table.sql +19 -0
- package/src/migrations/1785491702-tokens-session-columns.sql +3 -0
- package/src/migrations/1785491703-tokens-session-id-backfill.sql +1 -0
- package/src/migrations/1785491704-user-sessions-backfill.sql +3 -0
- package/src/migrations/1785491705-tokens-session-required.sql +6 -0
- package/src/migrations/1785491706-mfa-tokens-login-method.sql +2 -0
- package/src/migrations/1785824341-user-sessions-impersonated-user-id.sql +4 -0
- package/src/migrations/1785824342-create-impersonation-tokens-table.sql +15 -0
- package/src/migrations/1787055465-vies-cached-results.sql +6 -0
- package/src/models/ImpersonationToken.ts +133 -0
- package/src/models/MFAToken.ts +11 -1
- package/src/models/STInvoice.ts +4 -48
- package/src/models/Token.test.ts +15 -27
- package/src/models/Token.ts +22 -167
- package/src/models/UserSession.ts +60 -0
- package/src/models/ViesCachedResult.ts +22 -0
- package/src/models/_relations.ts +4 -0
- package/src/models/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stamhoofd/models",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.146.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -32,7 +32,6 @@
|
|
|
32
32
|
"@aws-sdk/client-s3": "3.1090.0",
|
|
33
33
|
"@aws-sdk/client-sesv2": "3.1090.0",
|
|
34
34
|
"@bwip-js/node": "4.11.2",
|
|
35
|
-
"@mollie/api-client": "4.6.0",
|
|
36
35
|
"@simonbackx/simple-encoding": "2.26.11",
|
|
37
36
|
"@simonbackx/simple-endpoints": "1.22.0",
|
|
38
37
|
"@simonbackx/simple-errors": "1.5.0",
|
|
@@ -45,7 +44,7 @@
|
|
|
45
44
|
},
|
|
46
45
|
"devDependencies": {
|
|
47
46
|
"@simonbackx/simple-database": "1.37.1",
|
|
48
|
-
"@stamhoofd/test-utils": "2.
|
|
47
|
+
"@stamhoofd/test-utils": "2.146.0",
|
|
49
48
|
"@types/luxon": "3.7.2",
|
|
50
49
|
"@types/uuid": "8.3.4",
|
|
51
50
|
"vitest": "4.1.10"
|
|
@@ -64,5 +63,5 @@
|
|
|
64
63
|
"publishConfig": {
|
|
65
64
|
"access": "public"
|
|
66
65
|
},
|
|
67
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "6594424261f48331f2403f002340769ccbe44c9a"
|
|
68
67
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { SessionClientType } from '@stamhoofd/structures';
|
|
2
|
+
|
|
3
|
+
const MINUTE = 60 * 1000;
|
|
4
|
+
const HOUR = 60 * MINUTE;
|
|
5
|
+
const DAY = 24 * HOUR;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Kept short: this is the window in which a leaked access token is still worth something.
|
|
9
|
+
*/
|
|
10
|
+
export const ACCESS_TOKEN_DURATION = 15 * MINUTE;
|
|
11
|
+
export const SSO_HANDOFF_TOKEN_DURATION = 5 * MINUTE;
|
|
12
|
+
|
|
13
|
+
export type SessionType = 'sso' | 'platformAdmin' | 'admin' | 'user';
|
|
14
|
+
|
|
15
|
+
export type SessionDurations = {
|
|
16
|
+
/**
|
|
17
|
+
* Maximum length of the session, counted from the login. Renewing never extends it, so
|
|
18
|
+
* this is when the user has to sign in again however active they were.
|
|
19
|
+
*/
|
|
20
|
+
session: number | null;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* How long the session may go unused before it ends. Every renewal restarts it.
|
|
24
|
+
*/
|
|
25
|
+
refreshToken: number;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* How long a session lives, per kind of session and client.
|
|
30
|
+
*
|
|
31
|
+
* Sessions in a browser are the shortest: it is not a place we can trust to keep a token
|
|
32
|
+
* safe (shared computers, extensions, synced profiles), while the native app keeps its
|
|
33
|
+
* tokens in the secure storage of the device and renews them in the background. Sessions
|
|
34
|
+
* that give access to the data of other people are shorter than those of a member, and an
|
|
35
|
+
* SSO session is the shortest of all: access is granted and taken away at the identity
|
|
36
|
+
* provider, so we go back to it often.
|
|
37
|
+
*/
|
|
38
|
+
export const SESSION_DURATIONS: Record<SessionType, Record<SessionClientType, SessionDurations>> = {
|
|
39
|
+
sso: {
|
|
40
|
+
[SessionClientType.Browser]: { session: 12 * HOUR, refreshToken: 8 * HOUR },
|
|
41
|
+
[SessionClientType.iOS]: { session: 7 * DAY, refreshToken: 36 * HOUR },
|
|
42
|
+
[SessionClientType.Android]: { session: 7 * DAY, refreshToken: 36 * HOUR },
|
|
43
|
+
},
|
|
44
|
+
platformAdmin: {
|
|
45
|
+
[SessionClientType.Browser]: { session: 12 * HOUR, refreshToken: 8 * HOUR },
|
|
46
|
+
[SessionClientType.iOS]: { session: 7 * DAY, refreshToken: 36 * HOUR },
|
|
47
|
+
[SessionClientType.Android]: { session: 7 * DAY, refreshToken: 36 * HOUR },
|
|
48
|
+
},
|
|
49
|
+
admin: {
|
|
50
|
+
[SessionClientType.Browser]: { session: 180 * DAY, refreshToken: 90 * DAY },
|
|
51
|
+
[SessionClientType.iOS]: { session: null, refreshToken: 90 * DAY },
|
|
52
|
+
[SessionClientType.Android]: { session: null, refreshToken: 90 * DAY },
|
|
53
|
+
},
|
|
54
|
+
user: {
|
|
55
|
+
[SessionClientType.Browser]: { session: 180 * DAY, refreshToken: 180 * DAY },
|
|
56
|
+
[SessionClientType.iOS]: { session: null, refreshToken: 180 * DAY },
|
|
57
|
+
[SessionClientType.Android]: { session: null, refreshToken: 180 * DAY },
|
|
58
|
+
},
|
|
59
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
DELETE FROM `tokens` WHERE `accessTokenValidUntil` < NOW() AND `refreshTokenValidUntil` < NOW();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
CREATE TABLE `user_sessions` (
|
|
2
|
+
`id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
3
|
+
`userId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
4
|
+
`clientType` varchar(32) NOT NULL,
|
|
5
|
+
`startedAt` datetime NOT NULL,
|
|
6
|
+
`loginMethod` varchar(32) NOT NULL,
|
|
7
|
+
`deviceType` varchar(32) NOT NULL,
|
|
8
|
+
`deviceName` varchar(255) NULL,
|
|
9
|
+
`osName` varchar(32) NULL,
|
|
10
|
+
`osVersion` varchar(64) NULL,
|
|
11
|
+
`appVersion` varchar(64) NULL,
|
|
12
|
+
`nativeAppVersion` varchar(64) NULL,
|
|
13
|
+
`browserName` varchar(64) NULL,
|
|
14
|
+
`lastUsedTokenId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
15
|
+
`lastActiveTokenId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
16
|
+
PRIMARY KEY (`id`),
|
|
17
|
+
KEY `user_sessions_userId_index` (`userId`),
|
|
18
|
+
CONSTRAINT `user_sessions_userId_foreign` FOREIGN KEY (`userId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
|
19
|
+
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
UPDATE `tokens` SET `id` = UUID(), `sessionId` = UUID();
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
ALTER TABLE `tokens`
|
|
2
|
+
MODIFY COLUMN `id` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
3
|
+
MODIFY COLUMN `sessionId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
4
|
+
ADD UNIQUE KEY `tokens_id_unique` (`id`),
|
|
5
|
+
ADD KEY `tokens_sessionId_index` (`sessionId`),
|
|
6
|
+
ADD CONSTRAINT `tokens_sessionId_foreign` FOREIGN KEY (`sessionId`) REFERENCES `user_sessions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
ALTER TABLE `user_sessions`
|
|
2
|
+
ADD COLUMN `impersonatedUserId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL AFTER `userId`,
|
|
3
|
+
ADD KEY `user_sessions_impersonatedUserId_index` (`impersonatedUserId`),
|
|
4
|
+
ADD CONSTRAINT `user_sessions_impersonatedUserId_foreign` FOREIGN KEY (`impersonatedUserId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
CREATE TABLE `impersonation_tokens` (
|
|
2
|
+
`token` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
3
|
+
`userId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
4
|
+
`impersonatedUserId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
5
|
+
`organizationId` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
|
|
6
|
+
`createdIp` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
7
|
+
`expiresAt` datetime NOT NULL,
|
|
8
|
+
`createdAt` datetime NOT NULL,
|
|
9
|
+
`updatedAt` datetime NOT NULL,
|
|
10
|
+
PRIMARY KEY (`token`),
|
|
11
|
+
KEY `idx_userId` (`userId`) USING BTREE,
|
|
12
|
+
KEY `idx_impersonatedUserId` (`impersonatedUserId`) USING BTREE,
|
|
13
|
+
CONSTRAINT `impersonation_tokens_ibfk_1` FOREIGN KEY (`userId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
|
14
|
+
CONSTRAINT `impersonation_tokens_ibfk_2` FOREIGN KEY (`impersonatedUserId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
|
15
|
+
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
CREATE TABLE `vies_cached_results` (
|
|
2
|
+
`VATNumber` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
|
|
3
|
+
`checkedAt` datetime NOT NULL,
|
|
4
|
+
`result` tinyint(1) NOT NULL,
|
|
5
|
+
PRIMARY KEY (`VATNumber`)
|
|
6
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { column } from '@simonbackx/simple-database';
|
|
2
|
+
import { QueryableModel, SQLWhereSign } from '@stamhoofd/sql';
|
|
3
|
+
import crypto from 'crypto';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A one-time ticket an administrator trades for a session that impersonates another user.
|
|
7
|
+
*
|
|
8
|
+
* The ticket travels through a link that has to be opened in a fresh (private) browser
|
|
9
|
+
* session, so it is handled as a credential: single use, short lived, and only valid from
|
|
10
|
+
* the address that asked for it.
|
|
11
|
+
*/
|
|
12
|
+
export class ImpersonationToken extends QueryableModel {
|
|
13
|
+
static table = 'impersonation_tokens';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Long enough for the administrator to open a private window and confirm, short enough
|
|
17
|
+
* that a leaked link is worthless by the time it is read.
|
|
18
|
+
*/
|
|
19
|
+
static EXPIRY_MS = 5 * 60 * 1000;
|
|
20
|
+
|
|
21
|
+
@column({ primary: true, type: 'string' })
|
|
22
|
+
token: string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The administrator that will act through the session.
|
|
26
|
+
*/
|
|
27
|
+
@column({ type: 'string' })
|
|
28
|
+
userId: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The account the session will present itself as.
|
|
32
|
+
*/
|
|
33
|
+
@column({ type: 'string' })
|
|
34
|
+
impersonatedUserId: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The organization the impersonation was started from, used to scope the audit log.
|
|
38
|
+
*/
|
|
39
|
+
@column({ type: 'string', nullable: true })
|
|
40
|
+
organizationId: string | null = null;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The address the ticket was requested from. The link only works from that same
|
|
44
|
+
* address, so a link that is forwarded to someone else is useless to them.
|
|
45
|
+
*
|
|
46
|
+
* Trustworthy because Caddy overwrites X-Forwarded-For in production: a client cannot
|
|
47
|
+
* claim an address of its own choosing.
|
|
48
|
+
*/
|
|
49
|
+
@column({ type: 'string' })
|
|
50
|
+
createdIp: string;
|
|
51
|
+
|
|
52
|
+
@column({ type: 'datetime' })
|
|
53
|
+
expiresAt: Date;
|
|
54
|
+
|
|
55
|
+
@column({
|
|
56
|
+
type: 'datetime', beforeSave(old?: any) {
|
|
57
|
+
if (old !== undefined) {
|
|
58
|
+
return old;
|
|
59
|
+
}
|
|
60
|
+
const date = new Date();
|
|
61
|
+
date.setMilliseconds(0);
|
|
62
|
+
return date;
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
createdAt: Date;
|
|
66
|
+
|
|
67
|
+
@column({
|
|
68
|
+
type: 'datetime', beforeSave() {
|
|
69
|
+
const date = new Date();
|
|
70
|
+
date.setMilliseconds(0);
|
|
71
|
+
return date;
|
|
72
|
+
},
|
|
73
|
+
skipUpdate: true,
|
|
74
|
+
})
|
|
75
|
+
updatedAt: Date;
|
|
76
|
+
|
|
77
|
+
static async createFor(data: { userId: string; impersonatedUserId: string; organizationId: string | null; ip: string }): Promise<ImpersonationToken> {
|
|
78
|
+
// Only one outstanding ticket per administrator: asking for a new link invalidates
|
|
79
|
+
// the previous one, so an abandoned link cannot be picked up later.
|
|
80
|
+
await this.delete().where('userId', data.userId).delete();
|
|
81
|
+
|
|
82
|
+
const model = new ImpersonationToken();
|
|
83
|
+
model.token = crypto.randomBytes(48).toString('base64url');
|
|
84
|
+
model.userId = data.userId;
|
|
85
|
+
model.impersonatedUserId = data.impersonatedUserId;
|
|
86
|
+
model.organizationId = data.organizationId;
|
|
87
|
+
model.createdIp = data.ip;
|
|
88
|
+
model.expiresAt = new Date(Date.now() + ImpersonationToken.EXPIRY_MS);
|
|
89
|
+
await model.save();
|
|
90
|
+
return model;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
isExpired(): boolean {
|
|
94
|
+
return this.expiresAt < new Date();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Claim this ticket. Returns false when it was already used: a ticket may only ever
|
|
99
|
+
* hand out one session, even when two requests arrive at the same time.
|
|
100
|
+
*/
|
|
101
|
+
async consume(): Promise<boolean> {
|
|
102
|
+
const { affectedRows } = await ImpersonationToken.delete().where('token', this.token).delete();
|
|
103
|
+
return affectedRows === 1;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Look up a ticket that is still valid, deleting it when it turns out to be expired.
|
|
108
|
+
* Does not consume it: call consume() once the request is allowed to proceed.
|
|
109
|
+
*/
|
|
110
|
+
static async getValid(token: string): Promise<ImpersonationToken | undefined> {
|
|
111
|
+
if (!token) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
const model = await this.select().where('token', token).first(false);
|
|
115
|
+
if (!model) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
if (model.isExpired()) {
|
|
119
|
+
await model.delete();
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
return model;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Remove every expired ticket. Tickets are also dropped when they are looked up after
|
|
127
|
+
* expiring, but one that is never opened would stay behind forever.
|
|
128
|
+
*/
|
|
129
|
+
static async deleteExpired(): Promise<number> {
|
|
130
|
+
const { affectedRows } = await this.delete().where('expiresAt', SQLWhereSign.Less, new Date()).delete();
|
|
131
|
+
return affectedRows;
|
|
132
|
+
}
|
|
133
|
+
}
|
package/src/models/MFAToken.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { column } from '@simonbackx/simple-database';
|
|
2
2
|
import { QueryableModel, SQLCalculation, SQLColumnExpression, SQLScalar, SQLWhereSign } from '@stamhoofd/sql';
|
|
3
|
+
import { SessionLoginMethod } from '@stamhoofd/structures';
|
|
3
4
|
import crypto from 'crypto';
|
|
4
5
|
|
|
5
6
|
export type MFATokenPurpose = 'login' | 'setup';
|
|
@@ -22,6 +23,14 @@ export class MFAToken extends QueryableModel {
|
|
|
22
23
|
@column({ type: 'string' })
|
|
23
24
|
purpose: MFATokenPurpose = 'login';
|
|
24
25
|
|
|
26
|
+
/**
|
|
27
|
+
* The primary credential that was accepted before this challenge was created. The
|
|
28
|
+
* session is only created once the challenge is completed, and it has to know how the
|
|
29
|
+
* user got there.
|
|
30
|
+
*/
|
|
31
|
+
@column({ type: 'string' })
|
|
32
|
+
loginMethod = SessionLoginMethod.Password;
|
|
33
|
+
|
|
25
34
|
@column({ type: 'integer' })
|
|
26
35
|
tries = 0;
|
|
27
36
|
|
|
@@ -56,7 +65,7 @@ export class MFAToken extends QueryableModel {
|
|
|
56
65
|
})
|
|
57
66
|
updatedAt: Date;
|
|
58
67
|
|
|
59
|
-
static async createFor(userId: string, purpose: MFATokenPurpose, webauthnChallenge: string | null =
|
|
68
|
+
static async createFor(userId: string, purpose: MFATokenPurpose, { webauthnChallenge = null, loginMethod = SessionLoginMethod.Password }: { webauthnChallenge?: string | null; loginMethod?: SessionLoginMethod } = {}): Promise<MFAToken> {
|
|
60
69
|
// A new attempt supersedes the previous one. Without this, every single login
|
|
61
70
|
// attempt of a user with 2FA leaves a row behind (anyone who knows the password
|
|
62
71
|
// can create them at will), and the abandoned tokens stay usable until they
|
|
@@ -67,6 +76,7 @@ export class MFAToken extends QueryableModel {
|
|
|
67
76
|
model.token = crypto.randomBytes(48).toString('base64url');
|
|
68
77
|
model.userId = userId;
|
|
69
78
|
model.purpose = purpose;
|
|
79
|
+
model.loginMethod = loginMethod;
|
|
70
80
|
model.webauthnChallenge = webauthnChallenge;
|
|
71
81
|
model.expiresAt = new Date(Date.now() + MFAToken.EXPIRY_MS);
|
|
72
82
|
await model.save();
|
package/src/models/STInvoice.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { createMollieClient } from '@mollie/api-client';
|
|
2
1
|
import { column } from '@simonbackx/simple-database';
|
|
3
|
-
import { SimpleError } from '@simonbackx/simple-errors';
|
|
4
2
|
import { QueryableModel } from '@stamhoofd/sql';
|
|
5
|
-
import {
|
|
3
|
+
import { STInvoiceMeta } from '@stamhoofd/structures';
|
|
6
4
|
import { v4 as uuidv4 } from 'uuid';
|
|
7
|
-
import type { Organization } from './Organization.js';
|
|
8
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated
|
|
8
|
+
*/
|
|
9
9
|
export class STInvoice extends QueryableModel {
|
|
10
10
|
static table = 'stamhoofd_invoices';
|
|
11
11
|
|
|
@@ -91,48 +91,4 @@ export class STInvoice extends QueryableModel {
|
|
|
91
91
|
|
|
92
92
|
@column({ type: 'boolean' })
|
|
93
93
|
didSendPeppol = false;
|
|
94
|
-
|
|
95
|
-
static async getMollieMandates(organization: Organization) {
|
|
96
|
-
// Poll mollie status
|
|
97
|
-
// Mollie payment is required
|
|
98
|
-
const mandates: OrganizationPaymentMandate[] = [];
|
|
99
|
-
|
|
100
|
-
try {
|
|
101
|
-
const apiKey = STAMHOOFD.MOLLIE_API_KEY;
|
|
102
|
-
if (!apiKey) {
|
|
103
|
-
throw new SimpleError({
|
|
104
|
-
code: '',
|
|
105
|
-
message: 'Mollie niet correct gekoppeld',
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const mollieClient = createMollieClient({ apiKey });
|
|
110
|
-
|
|
111
|
-
if (organization.serverMeta.mollieCustomerId) {
|
|
112
|
-
const m = await mollieClient.customerMandates.page({ customerId: organization.serverMeta.mollieCustomerId, limit: 250 });
|
|
113
|
-
for (const mandate of m) {
|
|
114
|
-
try {
|
|
115
|
-
const details = mandate.details;
|
|
116
|
-
mandates.push(OrganizationPaymentMandate.create({
|
|
117
|
-
...mandate,
|
|
118
|
-
isDefault: mandate.id === organization.serverMeta.mollieMandateId,
|
|
119
|
-
createdAt: new Date(mandate.createdAt),
|
|
120
|
-
details: OrganizationPaymentMandateDetails.create({
|
|
121
|
-
consumerName: ('consumerName' in details ? details.consumerName : details.cardHolder) ?? undefined,
|
|
122
|
-
consumerAccount: ('consumerAccount' in details ? details.consumerAccount : details.cardNumber) ?? undefined,
|
|
123
|
-
consumerBic: ('consumerBic' in details ? details.consumerBic : details.cardExpiryDate) ?? undefined,
|
|
124
|
-
cardExpiryDate: ('cardExpiryDate' in details ? details.cardExpiryDate : null),
|
|
125
|
-
cardLabel: ('cardLabel' in details ? details.cardLabel : null),
|
|
126
|
-
}),
|
|
127
|
-
}));
|
|
128
|
-
} catch (e) {
|
|
129
|
-
console.error(e);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
} catch (e) {
|
|
134
|
-
console.error(e);
|
|
135
|
-
}
|
|
136
|
-
return mandates;
|
|
137
|
-
}
|
|
138
94
|
}
|
package/src/models/Token.test.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { Database } from '@simonbackx/simple-database';
|
|
2
|
+
import { SessionClientType, SessionDeviceType, SessionLoginMethod } from '@stamhoofd/structures';
|
|
3
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
4
|
|
|
3
5
|
import { OrganizationFactory } from '../factories/OrganizationFactory.js';
|
|
4
6
|
import { UserFactory } from '../factories/UserFactory.js';
|
|
5
7
|
import type { Organization } from './Organization.js';
|
|
6
8
|
import { Token } from './Token.js';
|
|
7
9
|
import type { User } from './User.js';
|
|
10
|
+
import { UserSession } from './UserSession.js';
|
|
8
11
|
|
|
9
12
|
describe('Model.Token', () => {
|
|
10
13
|
const existingToken = 'ABCDEFG';
|
|
@@ -14,9 +17,21 @@ describe('Model.Token', () => {
|
|
|
14
17
|
beforeAll(async () => {
|
|
15
18
|
organization = await new OrganizationFactory({}).create();
|
|
16
19
|
user = await new UserFactory({ organization }).create();
|
|
20
|
+
const tokenId = uuidv4();
|
|
21
|
+
const session = new UserSession();
|
|
22
|
+
session.userId = user.id;
|
|
23
|
+
session.clientType = SessionClientType.Browser;
|
|
24
|
+
session.loginMethod = SessionLoginMethod.Password;
|
|
25
|
+
session.deviceType = SessionDeviceType.Desktop;
|
|
26
|
+
session.startedAt = new Date();
|
|
27
|
+
session.lastUsedTokenId = tokenId;
|
|
28
|
+
session.lastActiveTokenId = tokenId;
|
|
29
|
+
await session.save();
|
|
17
30
|
|
|
18
31
|
await Database.insert('INSERT INTO ' + Token.table + ' SET ?', [
|
|
19
32
|
{
|
|
33
|
+
id: tokenId,
|
|
34
|
+
sessionId: session.id,
|
|
20
35
|
accessToken: existingToken,
|
|
21
36
|
refreshToken: 'refreshtoken',
|
|
22
37
|
|
|
@@ -39,31 +54,4 @@ describe('Model.Token', () => {
|
|
|
39
54
|
expect(token.userId).toEqual(user.id);
|
|
40
55
|
});
|
|
41
56
|
|
|
42
|
-
test('Create a token', async () => {
|
|
43
|
-
const token = await Token.createToken(user);
|
|
44
|
-
expect(token).toBeDefined();
|
|
45
|
-
if (!token) return;
|
|
46
|
-
expect(token).toBeInstanceOf(Token);
|
|
47
|
-
expect(token.user.id).toEqual(user.id);
|
|
48
|
-
expect(token.accessToken).toHaveLength(256);
|
|
49
|
-
expect(token.refreshToken).toHaveLength(256);
|
|
50
|
-
expect(token.accessTokenValidUntil.getTime()).toBeGreaterThan(new Date().getTime() + (3600 * 1000) / 2 - 1);
|
|
51
|
-
expect(token.accessTokenValidUntil.getTime()).toBeLessThan(new Date().getTime() + 3600 * 1000 * 24 * 365);
|
|
52
|
-
|
|
53
|
-
expect(token.refreshTokenValidUntil.getTime()).toBeGreaterThan(token.accessTokenValidUntil.getTime());
|
|
54
|
-
expect(token.refreshTokenValidUntil.getTime()).toBeLessThan(new Date().getTime() + 3600 * 1000 * 24 * 365);
|
|
55
|
-
|
|
56
|
-
expect(token.userId).toEqual(user.id);
|
|
57
|
-
|
|
58
|
-
const search = await Token.getByAccessToken(token.accessToken);
|
|
59
|
-
// Make sure we do not compare the organization, since that won't be loaded now, but is loaded on user, and on token
|
|
60
|
-
|
|
61
|
-
expect(search).toMatchObject({
|
|
62
|
-
accessToken: token.accessToken,
|
|
63
|
-
refreshToken: token.refreshToken,
|
|
64
|
-
userId: token.userId,
|
|
65
|
-
accessTokenValidUntil: token.accessTokenValidUntil,
|
|
66
|
-
refreshTokenValidUntil: token.refreshTokenValidUntil,
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
57
|
});
|