Skip to content

Commit a5d1e9a

Browse files
committed
feat(authorization): Add OrtServerPrincipal class
This class holds information about the current user and his or her access rights. An instance is created during authentication. Signed-off-by: Oliver Heger <[email protected]>
1 parent 623e786 commit a5d1e9a

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 io.kotest.core.spec.style.StringSpec
25+
import io.kotest.matchers.shouldBe
26+
27+
import io.mockk.every
28+
import io.mockk.mockk
29+
30+
import org.eclipse.apoapsis.ortserver.components.authorization.rights.EffectiveRole
31+
32+
class OrtServerPrincipalTest : StringSpec({
33+
"An instance should be created correctly from a JWT payload" {
34+
val userId = "0x93847-973498-734987"
35+
val username = "jdoe"
36+
val fullName = "John Doe"
37+
val payload = mockk<Payload> {
38+
every { subject } returns userId
39+
every { getClaim("preferred_username").asString() } returns username
40+
every { getClaim("name").asString() } returns fullName
41+
}
42+
val effectiveRole = mockk<EffectiveRole>()
43+
44+
val principal = OrtServerPrincipal.create(payload, effectiveRole)
45+
46+
principal.userId shouldBe userId
47+
principal.username shouldBe username
48+
principal.fullName shouldBe fullName
49+
principal.effectiveRole shouldBe effectiveRole
50+
}
51+
})

0 commit comments

Comments
 (0)