-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(amphora-service): protect public endpoints
Signed-off-by: Sebastian Becker <[email protected]>
- Loading branch information
Showing
27 changed files
with
864 additions
and
397 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
22 changes: 22 additions & 0 deletions
22
amphora-service/src/main/java/io/carbynestack/amphora/service/config/AuthProperties.java
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,22 @@ | ||
/* | ||
* Copyright (c) 2024 - for information on the respective copyright owner | ||
* see the NOTICE file and/or the repository https://github.com/carbynestack/amphora. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.carbynestack.amphora.service.config; | ||
|
||
|
||
import lombok.Data; | ||
import lombok.experimental.Accessors; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@ConfigurationProperties(prefix = "carbynestack.auth") | ||
@Component | ||
@Data | ||
@Accessors(chain = true) | ||
public class AuthProperties { | ||
private String userIdFieldName; | ||
} |
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
14 changes: 14 additions & 0 deletions
14
...rvice/src/main/java/io/carbynestack/amphora/service/exceptions/UnauthorizedException.java
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,14 @@ | ||
/* | ||
* Copyright (c) 2024 - for information on the respective copyright owner | ||
* see the NOTICE file and/or the repository https://github.com/carbynestack/amphora. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.carbynestack.amphora.service.exceptions; | ||
|
||
public class UnauthorizedException extends Exception { | ||
public UnauthorizedException(String message) { | ||
super(message); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
amphora-service/src/main/java/io/carbynestack/amphora/service/opa/JwtReader.java
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,68 @@ | ||
/* | ||
* Copyright (c) 2024 - for information on the respective copyright owner | ||
* see the NOTICE file and/or the repository https://github.com/carbynestack/amphora. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.carbynestack.amphora.service.opa; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.carbynestack.amphora.service.exceptions.UnauthorizedException; | ||
import io.vavr.control.Option; | ||
|
||
import java.util.Base64; | ||
|
||
public class JwtReader { | ||
static ObjectMapper mapper = new ObjectMapper(); | ||
private final String userIdField; | ||
|
||
public JwtReader(String userIdField) { | ||
this.userIdField = userIdField; | ||
} | ||
|
||
public String extractUserIdFromAuthHeader(String header) throws UnauthorizedException { | ||
return extractFieldFromAuthHeader(header, userIdField); | ||
} | ||
|
||
private static String extractFieldFromAuthHeader(String header, String field) throws UnauthorizedException { | ||
return tokenFromHeader(header) | ||
.flatMap(JwtReader::dataNodeFromToken) | ||
.flatMap(node -> fieldFromNode(node, field)) | ||
.getOrElseThrow(() -> new UnauthorizedException("No token provided")); | ||
} | ||
|
||
private static Option<JsonNode> dataNodeFromToken(String token) { | ||
String[] parts = token.split("\\."); | ||
if (parts.length != 3) { | ||
return Option.none(); | ||
} | ||
try { | ||
String jwt = new String(Base64.getDecoder().decode(parts[1])); | ||
return Option.of(mapper.reader().readTree(jwt)); | ||
} catch (JsonProcessingException e) { | ||
return Option.none(); | ||
Check warning on line 46 in amphora-service/src/main/java/io/carbynestack/amphora/service/opa/JwtReader.java Codecov / codecov/patchamphora-service/src/main/java/io/carbynestack/amphora/service/opa/JwtReader.java#L45-L46
|
||
} | ||
} | ||
|
||
private static Option<String> tokenFromHeader(String header) { | ||
if (header != null && header.startsWith("Bearer ")) { | ||
return Option.of(header.substring(7)); | ||
} | ||
return Option.none(); | ||
} | ||
|
||
private static Option<String> fieldFromNode(JsonNode node, String field) { | ||
try { | ||
for(String f : field.split("\\.")) { | ||
node = node.get(f); | ||
Check warning on line 60 in amphora-service/src/main/java/io/carbynestack/amphora/service/opa/JwtReader.java Codacy Production / Codacy Static Code Analysisamphora-service/src/main/java/io/carbynestack/amphora/service/opa/JwtReader.java#L60
|
||
} | ||
return Option.of(node.asText()); | ||
} catch (NullPointerException e) { | ||
return Option.none(); | ||
} | ||
} | ||
} | ||
|
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.