Skip to content

Commit

Permalink
feat: remove bearer prefix from token (#17)
Browse files Browse the repository at this point in the history
* feat: remove bearer prefix

* tests: update bearer token test
  • Loading branch information
andrejpetras authored Feb 13, 2024
1 parent 5e00bed commit 786e6d3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.tkit.quarkus.rs.context.token.TokenParserRequest;
import org.tkit.quarkus.rs.context.token.TokenParserService;

import io.quarkus.oidc.common.runtime.OidcConstants;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand All @@ -25,11 +26,17 @@ public class TokenService {
@Inject
TokenParserService tokenParserService;

private static final String BEARER_PREFIX = OidcConstants.BEARER_SCHEME + " ";

public List<String> getTokenRoles(String tokenData) {

try {
var token = tokenData;
if (token.startsWith(BEARER_PREFIX)) {
token = token.substring(BEARER_PREFIX.length());
}

var request = new TokenParserRequest(tokenData)
var request = new TokenParserRequest(token)
.verify(config.verified())
.issuerEnabled(config.publicKeyEnabled())
.issuerSuffix(config.publicKeyLocationSuffix());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class PermissionRestControllerTest extends AbstractTest {
@Test
void getApplicationPermissionsTest() {

var accessToken = createToken(List.of("n3"));
// bearer prefix
var accessToken = createTokenBearer(List.of("n3"));

var dto = given()
.contentType(APPLICATION_JSON)
Expand All @@ -44,6 +45,22 @@ void getApplicationPermissionsTest() {
assertThat(dto.getPermissions()).isNotNull().hasSize(1);
assertThat(dto.getPermissions().get("o1")).isNotNull().hasSize(1).containsExactly("a3");

// without bearer prefix
accessToken = createToken(null, List.of("n3"));

dto = given()
.contentType(APPLICATION_JSON)
.body(new PermissionRequestDTOV1().token(accessToken))
.post("app1")
.then()
.statusCode(OK.getStatusCode())
.extract()
.body().as(ApplicationPermissionsDTOV1.class);

assertThat(dto).isNotNull();
assertThat(dto.getPermissions()).isNotNull().hasSize(1);
assertThat(dto.getPermissions().get("o1")).isNotNull().hasSize(1).containsExactly("a3");

}

private static Stream<Arguments> badRequestData() {
Expand Down Expand Up @@ -83,7 +100,7 @@ void getApplicationPermissionsWrongTongTest() {
@Test
void getApplicationsPermissionsTest() {

var accessToken = createToken(List.of("n3"));
var accessToken = createTokenBearer(List.of("n3"));

var dto = given()
.contentType(APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ protected static String createToken(String organizationId) {
return createToken(organizationId, null);
}

protected static String createToken(List<String> roles) {
return createToken(null, roles);
protected static String createTokenBearer(List<String> roles) {
return "Bearer " + createToken(null, roles);
}

protected static String createToken(String organizationId, List<String> roles) {
Expand Down

0 comments on commit 786e6d3

Please sign in to comment.