-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccountController.ts
207 lines (167 loc) · 6.68 KB
/
accountController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import shortUuid from 'short-uuid';
import { AuthressClient } from '@authress/sdk';
import express, { NextFunction, Request, Response } from 'express';
import accountsRepository, { Account } from './accountsRepository';
import authressPermissionsWrapper from '../authressPermissionsWrapper';
const accountsController = express.Router();
export default accountsController;
function formatAccount(account: { accountId: string }) {
if (!account) {
return null;
}
return {
accountId: account.accountId,
links: {
self: { href: `http://localhost:8080/accounts/${account.accountId}` }
}
};
}
accountsController.post('/', async (request: Request, response: Response, next: NextFunction) => {
const userId = response.locals.userId;
const newAccountId = `acc_${shortUuid('abcdefghijklmnopqrstuvwxyz0123456789').generate()}`;
const existingAccounts = await authressPermissionsWrapper.getUserResources('accounts', 'accounts:read');
if (existingAccounts.resources?.length) {
console.log({ title: 'User creating a second account with the same identity.', level: 'TRACK', request, existingAccounts, count: existingAccounts.resources.length });
if (existingAccounts.resources.length >= 3) {
response.status(429).json({
errorCode: 'TooManyAccounts',
title: 'Accounts per user is limited, for more information, please contact support@rhosys.ch'
});
return;
}
}
const account = await accountsRepository.createAccount(newAccountId, { accountCreatorId: userId });
await authressPermissionsWrapper.setRoleForUser(newAccountId, userId, `accounts/${newAccountId}`, ['AccountOwner']);
response.status(200).json(formatAccount(account));
});
accountsController.get('/:accountId/sso', async (request: Request, response: Response, next: NextFunction) => {
const userId = response.locals.userId;
const accountId = request.params.accountId;
if (!accountId) {
response.status(403).json({
errorCode: 'Forbidden',
title: 'Cannot update any account without having a user account that is linked to an existing account.'
});
return;
}
const hasAccess = await authressPermissionsWrapper.hasAccessToResource(userId, `accounts/${accountId}/sso`, 'sso:read');
if (!hasAccess) {
response.status(403).json({});
return;
}
const sso = await authressPermissionsWrapper.getSsoConfiguration(accountId);
response.status(200).json({
domain: sso?.tenant?.tenantLookupIdentifier,
connection: sso?.connection
});
});
accountsController.put('/:accountId/sso', async (request: Request, response: Response, next: NextFunction) => {
const userId = response.locals.userId;
const accountId = request.params.accountId;
if (!accountId) {
response.status(403).json({
errorCode: 'Forbidden',
title: 'Cannot update any account without having a user account that is linked to an existing account.'
});
}
const hasAccess = await authressPermissionsWrapper.hasAccessToResource(userId, `accounts/${accountId}/sso`, 'sso:update');
if (!hasAccess) {
response.status(403);
return;
}
const account = await accountsRepository.getAccount(accountId);
if (!account) {
response.status(404);
return;
}
if (request.body.domain) {
await authressPermissionsWrapper.updateSsoConfiguration(accountId, request.body.domain, request.body.connection);
} else {
await authressPermissionsWrapper.deleteSsoConfiguration(accountId);
}
response.status(202).json({});
});
accountsController.put('/:accountId', async (request: Request, response: Response, next: NextFunction) => {
const userId = response.locals.userId;
const accountId = request.params.accountId;
if (!accountId) {
response.status(403).json({
errorCode: 'Forbidden',
title: 'Cannot update any account without having a user account that is linked to an existing account.'
});
return;
}
const hasAccess = await authressPermissionsWrapper.hasAccessToResource(userId, `accounts/${accountId}`, 'accounts:update');
if (!hasAccess) {
response.status(403);
return;
}
const account = await accountsRepository.getAccount(accountId);
if (!account) {
response.status(404);
return;
}
const company = request.body.company;
const updatedAccount = await accountsRepository.updateAccount(accountId, { company });
response.status(200).json(formatAccount(updatedAccount));
return;
});
accountsController.get('/:accountId', async (request: Request, response: Response, next: NextFunction) => {
const userId = response.locals.userId;
const accountId = request.params.accountId;
if (!accountId) {
response.status(404);
return;
}
const hasAccess = await authressPermissionsWrapper.hasAccessToResource(userId, `accounts/${accountId}`, 'accounts:read');
if (!hasAccess) {
response.status(403);
return;
}
const account = await accountsRepository.getAccount(accountId);
if (!account) {
response.status(404);
return;
}
response.status(200).json(formatAccount(account));
return;
});
accountsController.get('/', async (request: Request, response: Response, next: NextFunction) => {
const userId = response.locals.userId;
const resourcePermissions = await authressPermissionsWrapper.getUserResources('accounts', 'accounts:read');
if (resourcePermissions.accessToAllSubResources) {
const accounts = await accountsRepository.getAllAccounts();
response.status(200).json({
accounts: accounts.map(a => formatAccount(a))
});
return;
}
const accounts: Account[] = await Promise.all(resourcePermissions.resources?.map(s => accountsRepository.getAccount(s.resourceUri.replace('accounts/', ''))) || []);
if (accounts.length) {
response.status(200).json({
accounts: accounts.filter(a => a).map(a => formatAccount(a))
});
return;
}
const usersTenantAccountId = response.locals.tenantId;
if (!usersTenantAccountId) {
response.status(200).json({
accounts: []
});
return;
}
// The user's came from an SSO connection, we know that because their JWT access token has a `tenantId` in the `aud` claim.
// * We'll assume that our service automatically gives READ access to everything in the account if they work at that company
const account = await accountsRepository.getAccount(usersTenantAccountId);
if (!account) {
response.status(200).json({
accounts: []
});
return;
}
// Since we assume that everyone in the tenant should have READ permissions even if they never got an invite, set the role for this user
await authressPermissionsWrapper.setRoleForUser(usersTenantAccountId, userId, `accounts/${usersTenantAccountId}`, ['Authress:ReadResource']);
response.status(200).json({
accounts: [formatAccount(account)]
});
});