|
| 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 com.auth0.jwt.interfaces.Payload |
| 23 | + |
| 24 | +import org.eclipse.apoapsis.ortserver.components.authorization.rights.EffectiveRole |
| 25 | + |
| 26 | +/** |
| 27 | + * A class storing information about the authenticated principal in the ORT Server. |
| 28 | + */ |
| 29 | +class OrtServerPrincipal( |
| 30 | + /** The internal ID of the user. */ |
| 31 | + val userId: String, |
| 32 | + |
| 33 | + /** The username of the principal.*/ |
| 34 | + val username: String, |
| 35 | + |
| 36 | + /** The full name of the principal. */ |
| 37 | + val fullName: String, |
| 38 | + |
| 39 | + /** The effective role computed for the principal. */ |
| 40 | + val effectiveRole: EffectiveRole |
| 41 | +) { |
| 42 | + companion object { |
| 43 | + /** Constant for the name of the claim containing the username. */ |
| 44 | + private const val CLAIM_USERNAME = "preferred_username" |
| 45 | + |
| 46 | + /** Constant for the name of the claim containing the full name. */ |
| 47 | + private const val CLAIM_FULL_NAME = "name" |
| 48 | + |
| 49 | + /** |
| 50 | + * Create an [OrtServerPrincipal] from the given JWT [payload] and [effectiveRole]. |
| 51 | + */ |
| 52 | + fun create(payload: Payload, effectiveRole: EffectiveRole): OrtServerPrincipal = |
| 53 | + OrtServerPrincipal( |
| 54 | + userId = payload.subject, |
| 55 | + username = payload.getClaim(CLAIM_USERNAME).asString(), |
| 56 | + fullName = payload.getClaim(CLAIM_FULL_NAME).asString(), |
| 57 | + effectiveRole = effectiveRole |
| 58 | + ) |
| 59 | + } |
| 60 | +} |
0 commit comments