|
1 | 1 | import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb"; |
2 | 2 | import { unmarshall } from "@aws-sdk/util-dynamodb"; |
3 | 3 | import { genericConfig } from "../../common/config.js"; |
4 | | -import { DatabaseFetchError } from "../../common/errors/index.js"; |
5 | | -import { allAppRoles, AppRoles } from "../../common/roles.js"; |
| 4 | +import { |
| 5 | + BaseError, |
| 6 | + DatabaseFetchError, |
| 7 | + InternalServerError, |
| 8 | +} from "../../common/errors/index.js"; |
| 9 | +import { |
| 10 | + allAppRoles, |
| 11 | + AppRoles, |
| 12 | + OrgRoleDefinition, |
| 13 | +} from "../../common/roles.js"; |
6 | 14 | import type Redis from "ioredis"; |
7 | 15 | import { AUTH_CACHE_PREFIX } from "api/plugins/auth.js"; |
8 | 16 | import type pino from "pino"; |
9 | | -import { type FastifyBaseLogger } from "fastify"; |
| 17 | +import { |
| 18 | + FastifyInstance, |
| 19 | + FastifyReply, |
| 20 | + FastifyRequest, |
| 21 | + type FastifyBaseLogger, |
| 22 | +} from "fastify"; |
| 23 | +import { getUserOrgRoles } from "./organizations.js"; |
10 | 24 |
|
11 | 25 | export async function getUserRoles( |
12 | 26 | dynamoClient: DynamoDBClient, |
@@ -91,3 +105,59 @@ export async function clearAuthCache({ |
91 | 105 | logger.debug(`Cleared ${result} auth cache keys.`); |
92 | 106 | return result; |
93 | 107 | } |
| 108 | + |
| 109 | +type AuthConfig = { |
| 110 | + validRoles: OrgRoleDefinition[]; |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
| 114 | + * Authorizes a request by checking if the user has at least one of the specified organization roles. |
| 115 | + * This function can be used as a preHandler in Fastify routes. |
| 116 | + * |
| 117 | + * @param fastify The Fastify instance. |
| 118 | + * @param request The Fastify request object. |
| 119 | + * @param reply The Fastify reply object. |
| 120 | + * @param config An object containing an array of valid OrgRoleDefinition instances. |
| 121 | + */ |
| 122 | +export async function authorizeByOrgRoleOrSchema( |
| 123 | + fastify: FastifyInstance, |
| 124 | + request: FastifyRequest, |
| 125 | + reply: FastifyReply, |
| 126 | + config: AuthConfig, |
| 127 | +) { |
| 128 | + let originalError = new InternalServerError({ |
| 129 | + message: "You do not have permission to perform this action.", |
| 130 | + }); |
| 131 | + |
| 132 | + try { |
| 133 | + await fastify.authorizeFromSchema(request, reply); |
| 134 | + return; |
| 135 | + } catch (e) { |
| 136 | + if (e instanceof BaseError) { |
| 137 | + originalError = e; |
| 138 | + } else { |
| 139 | + throw e; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (!request.username) { |
| 144 | + throw originalError; |
| 145 | + } |
| 146 | + |
| 147 | + const userRoles = await getUserOrgRoles({ |
| 148 | + username: request.username, |
| 149 | + dynamoClient: fastify.dynamoClient, |
| 150 | + logger: request.log, |
| 151 | + }); |
| 152 | + |
| 153 | + const isAuthorized = userRoles.some((userRole) => |
| 154 | + config.validRoles.some( |
| 155 | + (validRole) => |
| 156 | + userRole.org === validRole.org && userRole.role === validRole.role, |
| 157 | + ), |
| 158 | + ); |
| 159 | + |
| 160 | + if (!isAuthorized) { |
| 161 | + throw originalError; |
| 162 | + } |
| 163 | +} |
0 commit comments