-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable admins to query without restrictions (#142)
- Reads the `Authorization Bearer` token from the request (if present) and decodes it to a pre-known structure `RenkuToken` - a caller is an admin, if the role `renku-admin` is in `realmAccess.roles` - add another `AuthContext` for admins and convert to existing `SearchRole.Admin` which will remove any constraints to a given user query - Refactors auth-code in search routes for better testability
- Loading branch information
Showing
25 changed files
with
666 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
modules/jwt/src/main/scala/io/renku/search/jwt/RenkuToken.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.search.jwt | ||
|
||
import java.time.Instant | ||
|
||
import io.bullet.borer.NullOptions.given | ||
import io.bullet.borer.derivation.MapBasedCodecs | ||
import io.bullet.borer.derivation.key | ||
import io.bullet.borer.{Decoder, Encoder} | ||
import io.renku.search.jwt.RenkuToken.{Access, AccountRoles} | ||
|
||
final case class RenkuToken( | ||
@key("exp") expirationTime: Option[Instant] = None, | ||
@key("iat") issuedAt: Option[Instant] = None, | ||
@key("nbf") notBefore: Option[Instant] = None, | ||
@key("auth_time") authTime: Option[Instant] = None, | ||
@key("jti") jwtId: Option[String] = None, | ||
@key("iss") issuer: Option[String] = None, | ||
@key("sub") subject: Option[String] = None, | ||
@key("typ") tokenType: Option[String] = None, | ||
@key("realm_access") realmAccess: Option[Access] = None, | ||
@key("resource_access") resourceAccess: Option[AccountRoles] = None, | ||
@key("scope") scopeStr: Option[String] = None, | ||
name: Option[String] = None, | ||
email: Option[String] = None, | ||
@key("email_verified") emailVerified: Boolean = false, | ||
groups: Set[String] = Set.empty, | ||
@key("preferred_username") preferredUsername: Option[String] = None | ||
): | ||
|
||
lazy val isAdmin = | ||
realmAccess.exists(_.roles.contains("renku-admin")) | ||
|
||
object RenkuToken: | ||
final case class Access(roles: Set[String] = Set.empty) | ||
final case class AccountRoles(account: Access = Access()) | ||
|
||
private given Decoder[Instant] = Decoder.forLong.map(Instant.ofEpochSecond(_)) | ||
private given Encoder[Instant] = Encoder.forLong.contramap(_.getEpochSecond()) | ||
|
||
private given Decoder[Access] = MapBasedCodecs.deriveDecoder | ||
private given Encoder[Access] = MapBasedCodecs.deriveEncoder | ||
private given Decoder[AccountRoles] = MapBasedCodecs.deriveDecoder | ||
private given Encoder[AccountRoles] = MapBasedCodecs.deriveEncoder | ||
|
||
given Decoder[RenkuToken] = MapBasedCodecs.deriveDecoder | ||
given Encoder[RenkuToken] = MapBasedCodecs.deriveEncoder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"exp": 1716905204, | ||
"iat": 1716903404, | ||
"auth_time": 1716903404, | ||
"jti": "488ecd30-a7bb-473b-9eac-3ec43d5447a6", | ||
"iss": "https://ci-renku-3646.dev.renku.ch/auth/realms/Renku", | ||
"aud": [ | ||
"renku", | ||
"account" | ||
], | ||
"sub": "48c85c75-b407-4259-b06b-a611e71df5f0", | ||
"typ": "Bearer", | ||
"azp": "renku-ui", | ||
"session_state": "77bd8cc2-8230-4ec8-82d3-617bc3bf8013", | ||
"acr": "1", | ||
"allowed-origins": [ | ||
"https://ci-renku-3646.dev.renku.ch/*" | ||
], | ||
"realm_access": { | ||
"roles": [ | ||
"offline_access", | ||
"default-roles-renku", | ||
"uma_authorization" | ||
] | ||
}, | ||
"resource_access": { | ||
"account": { | ||
"roles": [ | ||
"manage-account", | ||
"manage-account-links", | ||
"view-profile" | ||
] | ||
} | ||
}, | ||
"scope": "openid microprofile-jwt email profile", | ||
"sid": "77bd8cc2-8230-4ec8-82d3-617bc3bf8013", | ||
"upn": "[email protected]", | ||
"email_verified": false, | ||
"name": "Eike Kettner", | ||
"groups": [ | ||
"offline_access", | ||
"default-roles-renku", | ||
"uma_authorization" | ||
], | ||
"preferred_username": "[email protected]", | ||
"given_name": "Eike", | ||
"family_name": "Kettner", | ||
"email": "[email protected]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"exp": 1716906437, | ||
"iat": 1716904637, | ||
"auth_time": 1716904637, | ||
"jti": "349d20d3-5ac2-4df6-b5e9-44bc8426b0ed", | ||
"iss": "https://ci-renku-3646.dev.renku.ch/auth/realms/Renku", | ||
"aud": [ | ||
"renku", | ||
"realm-management", | ||
"account" | ||
], | ||
"sub": "48c85c75-b407-4259-b06b-a611e71df5f0", | ||
"typ": "Bearer", | ||
"azp": "renku-ui", | ||
"session_state": "6f365bb5-2b3b-403c-ab97-e026292f5269", | ||
"acr": "1", | ||
"allowed-origins": [ | ||
"https://ci-renku-3646.dev.renku.ch/*" | ||
], | ||
"realm_access": { | ||
"roles": [ | ||
"offline_access", | ||
"renku-admin", | ||
"default-roles-renku", | ||
"uma_authorization" | ||
] | ||
}, | ||
"resource_access": { | ||
"realm-management": { | ||
"roles": [ | ||
"view-users", | ||
"query-groups", | ||
"query-users" | ||
] | ||
}, | ||
"account": { | ||
"roles": [ | ||
"manage-account", | ||
"manage-account-links", | ||
"view-profile" | ||
] | ||
} | ||
}, | ||
"scope": "openid microprofile-jwt email profile", | ||
"sid": "6f365bb5-2b3b-403c-ab97-e026292f5269", | ||
"upn": "[email protected]", | ||
"email_verified": false, | ||
"name": "Eike Kettner", | ||
"groups": [ | ||
"offline_access", | ||
"renku-admin", | ||
"default-roles-renku", | ||
"uma_authorization" | ||
], | ||
"preferred_username": "[email protected]", | ||
"given_name": "Eike", | ||
"family_name": "Kettner", | ||
"email": "[email protected]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
modules/jwt/src/test/scala/io/renku/search/jwt/RenkuTokenSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.renku.search.jwt | ||
|
||
import scala.io.Source | ||
|
||
import io.bullet.borer.Json | ||
import munit.FunSuite | ||
|
||
class RenkuTokenSpec extends FunSuite: | ||
|
||
test("decode jwt payload"): | ||
val jsonStr = Source.fromResource("jwt1.json").mkString | ||
val decoded = Json.decode(jsonStr.getBytes).to[RenkuToken].value | ||
assertEquals(decoded.subject, Some("48c85c75-b407-4259-b06b-a611e71df5f0")) | ||
assert(decoded.isAdmin == false) | ||
|
||
test("decode jwt payload (admin)"): | ||
val jsonStr = Source.fromResource("jwt2.json").mkString | ||
val decoded = Json.decode(jsonStr.getBytes).to[RenkuToken].value | ||
assertEquals(decoded.subject, Some("48c85c75-b407-4259-b06b-a611e71df5f0")) | ||
assert(decoded.isAdmin == true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.