Spaces:
Running
Running
File size: 6,072 Bytes
b39afbe |
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 208 209 210 211 212 213 214 215 216 217 218 |
/**
* Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
* All rights reserved.
*/
// ---------------------------------------------------------------------------------------------
// login.ts
//
// Purpose: Handler for login function
//
// ---------------------------------------------------------------------------------------------
import { type FastifyRequest, type FastifyReply } from 'fastify';
import { type AuthIntegration } from 'integrations/Authentication/AuthIntegration';
import { type User } from 'omni-shared';
import { loadUserPermission, PermissionChecker, setAcceptedTOS } from '../../../helper/permission.js';
const createAcceptTOSHandler = function (integration: AuthIntegration, config: any) {
return {
schema: {
response: {
200: {
type: 'object',
properties: {
username: { type: 'string' },
tosAccepted: { type: 'string' }
}
}
}
},
handler: async function (request: FastifyRequest, reply: FastifyReply) {
const user: User = request.user as User;
user.tosAccepted = await setAcceptedTOS(integration.db, user);
//omnilog.debug('User accepted TOS ' + user.tosAccepted);
if (user) {
return await reply.send({ username: user.username, tosAccepted: user.tosAccepted });
}
return await reply.code(200).send();
}
};
};
const createGetAuthenticatedUserHandler = function (integration: AuthIntegration, config: any) {
return {
schema: {
response: {
200: {
type: 'object',
properties: {
username: { type: 'string' },
isAdmin: { type: 'boolean' },
tosAccepted: { type: 'string' }
}
}
}
},
handler: async function (request: FastifyRequest, reply: FastifyReply) {
const user: User = request.user as User;
if (user) {
// @ts-ignore
const ability = request.session.get('permission');
if (ability == null) {
// @ts-ignore
request.session.set('permission', await loadUserPermission(integration.db, user));
}
return await reply.send({
username: user.username,
isAdmin: await integration.isAdmin(user),
tosAccepted: user.tosAccepted
});
}
return await reply.code(200).send();
}
};
};
const createLoginHandler = function (integration: AuthIntegration, config: any) {
return {
schema: {
response: {
200: {
type: 'object',
properties: {
username: { type: 'string' },
isAdmin: { type: 'boolean' },
tosAccepted: { type: 'string' }
}
}
}
},
handler: async function (request: FastifyRequest, reply: FastifyReply) {
const user = request.user as User;
await integration.login(request);
// @ts-ignore
await reply.send({
username: user.username,
isAdmin: await integration.isAdmin(user),
tosAccepted: user.tosAccepted
});
}
};
};
const createLogoutHandler = function (config: any) {
return {
handler: async function (request: FastifyRequest, reply: FastifyReply) {
try {
// request.logOut()
await request.session.destroy();
} catch (err) {
return await reply.send(err);
}
}
};
};
/**
* Request body:
* {
* scopes: [
* {
* action: 'execute',
* subject: 'workflow',
* workflowIds: ['workflowId1', 'workflowId2']
* },
* ],
* expiresIn: 3600
* }
*/
const createGenerateTokenHandler = function (integration: AuthIntegration, config: any) {
return {
schema: {
body: {
type: 'object',
required: ['scopes', 'expiresIn'],
properties: {
scopes: {
type: 'array',
items: {
type: 'object',
required: ['action', 'subject'],
properties: {
action: { type: 'string' },
subject: { type: 'string' },
orgId: { type: 'string' },
workflowIds: {
type: 'array',
items: { type: 'string' }
}
}
}
},
expiresIn: { type: 'number' }
}
},
response: {
200: {
type: 'object',
properties: {
token: { type: 'string' }
}
},
500: {
type: 'object',
properties: {
error: { type: 'string' }
}
}
}
},
handler: async function (request: FastifyRequest, reply: FastifyReply) {
// @ts-ignore
const { scopes, expiresIn } = request.body || {};
try {
if (integration.app.settings.get('omni:feature.permission')?.value) {
// @ts-ignore
const ability = new PermissionChecker(request.session.get('permission'));
if (!ability) {
throw new Error('Action not permitted');
}
// Scope will be either:
// 1. Execute a workflow
// 2. Adding user to an org
for (const scope of scopes) {
const { action, subject, orgId, workflowIds } = scope;
// Requested scope should match the user's permission
if (!ability.can(action, subject)) {
integration.debug('Action not permitted: ', action, subject);
throw new Error('Action not permitted');
}
}
}
// @ts-ignore
const user = request.user as User;
const token = await integration.generateJwtToken(scopes, user, expiresIn);
return await reply.code(200).send({ token });
} catch (err) {
integration.error('Error generating token: ', err);
return await reply.code(500).send('Internal error');
}
}
};
};
export {
createGetAuthenticatedUserHandler,
createLoginHandler,
createLogoutHandler,
createGenerateTokenHandler,
createAcceptTOSHandler
};
|