|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +package org.eclipse.apoapsis.ortserver.components.authorization.routes |
| 21 | + |
| 22 | +import io.ktor.server.application.ApplicationCall |
| 23 | + |
| 24 | +import org.eclipse.apoapsis.ortserver.components.authorization.rights.EffectiveRole |
| 25 | +import org.eclipse.apoapsis.ortserver.components.authorization.service.AuthorizationService |
| 26 | + |
| 27 | +/** |
| 28 | + * An interface defining a mechanism to check for required permissions using an [AuthorizationService] instance. |
| 29 | + * |
| 30 | + * The idea behind this interface is that a concrete implementation is responsible for doing a concrete authorization |
| 31 | + * check, such as testing for the presence of a specific permission on an element of the hierarchy. To do this, the |
| 32 | + * instance needs to load the permissions on this element from the service and then test whether the affected |
| 33 | + * permission is contained. |
| 34 | + * |
| 35 | + * Implementations of this interface can be passed into special routing functions that use them to perform |
| 36 | + * authorization checks automatically. There are convenience functions to create default instances easily. |
| 37 | + * |
| 38 | + * In addition to the functions defined here, concrete implementations should provide a meaningful `toString()` |
| 39 | + * implementation, since this is used to construct a routes selector internally. |
| 40 | + */ |
| 41 | +interface AuthorizationChecker { |
| 42 | + /** |
| 43 | + * Use the provided [service] to load the [EffectiveRole] of the user with the given [userId] for the current |
| 44 | + * [call]. A typical implementation will figure out the ID of an element in the hierarchy (organization, product, |
| 45 | + * or repository) based on current call parameters. Then it can invoke the [service] to query the permissions on |
| 46 | + * this element. |
| 47 | + */ |
| 48 | + suspend fun loadEffectiveRole(service: AuthorizationService, userId: String, call: ApplicationCall): EffectiveRole |
| 49 | + |
| 50 | + /** |
| 51 | + * Check whether the given [effectiveRole] contains the permission(s) required by this [AuthorizationChecker]. |
| 52 | + * This function is called with the [EffectiveRole] that was loaded via [loadEffectiveRole]. |
| 53 | + */ |
| 54 | + fun checkAuthorization(effectiveRole: EffectiveRole): Boolean |
| 55 | +} |
0 commit comments