-
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.
feat: Use authenticated information to amend query (#59)
- Uses information from the request to amend the search query such that visibility of projects is honored - public projects (and users, that have a public visibility by default) are always included - private projects are only included, if the calling subject is owner or member - Rename field `projectId` to `id` for the user defined query. The id can select any entity type
- Loading branch information
Showing
42 changed files
with
771 additions
and
160 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
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
95 changes: 95 additions & 0 deletions
95
modules/jwt/src/main/scala/io/renku/search/jwt/BorerCodec.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,95 @@ | ||
/* | ||
* 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 cats.syntax.all.* | ||
|
||
import io.bullet.borer.{Decoder, Reader} | ||
import pdi.jwt.{JwtAlgorithm, JwtClaim, JwtHeader} | ||
|
||
trait BorerCodec: | ||
given Decoder[JwtAlgorithm] = | ||
Decoder.forString.map(JwtAlgorithm.fromString) | ||
|
||
given Decoder[JwtHeader] = new Decoder[JwtHeader]: | ||
def read(r: Reader): JwtHeader = | ||
r.readMapStart() | ||
r.readUntilBreak(JwtHeader(None, None, None, None).withType) { h => | ||
r.readString() match | ||
case "alg" => | ||
val alg = r.read[JwtAlgorithm]() | ||
JwtHeader(alg.some, h.typ, h.contentType, h.keyId) | ||
case "typ" => h.withType(r.readString()) | ||
case "cty" => | ||
JwtHeader(h.algorithm, h.typ, r.readString().some, h.keyId) | ||
case "kid" => h.withKeyId(r.readString()) | ||
case _ => | ||
r.skipElement() | ||
h | ||
} | ||
|
||
given Decoder[JwtClaim] = new Decoder[JwtClaim]: | ||
def read(r: Reader): JwtClaim = | ||
r.readMapStart() | ||
r.readUntilBreak(JwtClaim()) { c => | ||
r.readString() match | ||
case "iss" => c.copy(issuer = r.readStringOpt()) | ||
case "sub" => c.copy(subject = r.readStringOpt()) | ||
case "aud" => c.copy(audience = r.readSetStr()) | ||
case "exp" => c.copy(expiration = r.readLongOpt()) | ||
case "nbf" => c.copy(notBefore = r.readLongOpt()) | ||
case "iat" => c.copy(issuedAt = r.readLongOpt()) | ||
case "jti" => c.copy(jwtId = r.readStringOpt()) | ||
case _ => | ||
r.skipElement() | ||
c | ||
} | ||
|
||
extension (self: Reader) | ||
def readStringOpt(): Option[String] = | ||
if (self.tryReadNull()) None else self.readString().some | ||
|
||
def readLongOpt(): Option[Long] = | ||
if (self.tryReadNull()) None else self.readLong().some | ||
|
||
def readSetStr(): Option[Set[String]] = | ||
if (self.tryReadNull()) None | ||
else if (self.hasArrayStart) self.read[Set[String]]().some | ||
else Set(self.readString()).some | ||
|
||
extension (self: JwtClaim) | ||
def copy( | ||
issuer: Option[String] = self.issuer, | ||
subject: Option[String] = self.subject, | ||
audience: Option[Set[String]] = self.audience, | ||
expiration: Option[Long] = self.expiration, | ||
notBefore: Option[Long] = self.notBefore, | ||
issuedAt: Option[Long] = self.issuedAt, | ||
jwtId: Option[String] = self.jwtId | ||
): JwtClaim = | ||
JwtClaim( | ||
self.content, | ||
issuer, | ||
subject, | ||
audience, | ||
expiration, | ||
notBefore, | ||
issuedAt, | ||
jwtId | ||
) |
51 changes: 51 additions & 0 deletions
51
modules/jwt/src/main/scala/io/renku/search/jwt/JwtBorer.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,51 @@ | ||
/* | ||
* 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.Clock | ||
|
||
import scala.util.Try | ||
|
||
import io.bullet.borer.Json | ||
import pdi.jwt.* | ||
|
||
class JwtBorer(override val clock: Clock) | ||
extends JwtCore[JwtHeader, JwtClaim] | ||
with BorerCodec: | ||
private val noSigOptions = JwtOptions.DEFAULT.copy(signature = false) | ||
|
||
protected def parseHeader(header: String): JwtHeader = | ||
Json.decode(header.getBytes).to[JwtHeader].value | ||
|
||
protected def parseClaim(claim: String): JwtClaim = | ||
Json.decode(claim.getBytes).to[JwtClaim].value | ||
|
||
protected def extractAlgorithm(header: JwtHeader): Option[JwtAlgorithm] = | ||
header.algorithm | ||
protected def extractExpiration(claim: JwtClaim): Option[Long] = claim.expiration | ||
protected def extractNotBefore(claim: JwtClaim): Option[Long] = claim.notBefore | ||
|
||
def decodeAllNoSignatureCheck(token: String): Try[(JwtHeader, JwtClaim, String)] = | ||
decodeAll(token, noSigOptions) | ||
|
||
def decodeNoSignatureCheck(token: String): Try[JwtClaim] = | ||
decode(token, noSigOptions) | ||
|
||
object JwtBorer extends JwtBorer(Clock.systemUTC()): | ||
def apply(clock: Clock): JwtBorer = new JwtBorer(clock) |
50 changes: 50 additions & 0 deletions
50
modules/jwt/src/test/scala/io/renku/search/jwt/JwtBorerSpec.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,50 @@ | ||
/* | ||
* 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 munit.FunSuite | ||
import pdi.jwt.Jwt | ||
import pdi.jwt.JwtAlgorithm | ||
|
||
class JwtBorerSpec extends FunSuite: | ||
|
||
val secret = new javax.crypto.spec.SecretKeySpec("abcdefg".getBytes, "HS256") | ||
val exampleToken = | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJteS11c2VyLWlkIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.d7F1v9sfcQzVrEGXXhJoGukbfXhm3zKn0fUyvFAMzm0" | ||
|
||
val regexDecode = Jwt.decodeAll(exampleToken, secret).get | ||
|
||
test("decode"): | ||
val (header, claim, _) = JwtBorer.decodeAll(exampleToken, secret).get | ||
assertEquals(header.algorithm, Some(JwtAlgorithm.HS256)) | ||
assertEquals(header.typ, Some("JWT")) | ||
assertEquals(header.keyId, None) | ||
assertEquals(header.contentType, None) | ||
|
||
assertEquals(claim.subject, Some("my-user-id")) | ||
assertEquals(claim.issuedAt, Some(1516239022L)) | ||
assertEquals(header, regexDecode._1) | ||
assertEquals(claim, regexDecode._2.withContent("{}")) | ||
|
||
test("decode without secret"): | ||
val (header, claim, _) = JwtBorer.decodeAllNoSignatureCheck(exampleToken).get | ||
val claim2 = JwtBorer.decodeNoSignatureCheck(exampleToken).get | ||
assertEquals(claim, claim2) | ||
assertEquals(header, regexDecode._1) | ||
assertEquals(claim, regexDecode._2.withContent("{}")) |
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.