Skip to content

Commit

Permalink
finish #93
Browse files Browse the repository at this point in the history
  • Loading branch information
Oussama Zgheb authored and Oussama Zgheb committed Aug 12, 2024
1 parent 0d039d7 commit cabcc52
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/app/tokenposition/AuthorizationBearerHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public boolean positionFound() {
for (HttpHeader header : httpMessage.headers()) {
containedJwt = containsJwt(header.value(), List.of("Bearer","bearer","BEARER"));
if (containedJwt.isPresent()) {
headerName = header.name();
this.token = containedJwt.get();
return true;
}
Expand All @@ -40,13 +41,15 @@ private Optional<String> containsJwt(String headerValue, List<String> jwtKeyword
if (headerValue.startsWith(keyword)) {
String potentialJwt = headerValue.replace(keyword, "").trim();
if (CustomJWToken.isValidJWT(potentialJwt)) {
headerKeyword = keyword;
return Optional.of(potentialJwt);
}
}
}
if(headerValue.toLowerCase().startsWith("ey") || containsExactlyTwoDots(headerValue)) {
String potentialJwt = headerValue.trim();
if (CustomJWToken.isValidJWT(potentialJwt)) {
headerKeyword = "";
return Optional.of(potentialJwt);
}
}
Expand All @@ -59,7 +62,7 @@ public HttpRequest getRequest() {
if (containedJwt.isEmpty()) {
return httpRequest;
}
return httpRequest.withUpdatedHeader(headerName, headerKeyword + " " + token);
return httpRequest.withUpdatedHeader(headerName, headerKeyword + needsSpace(headerKeyword) + token);
}

@Override
Expand All @@ -68,9 +71,15 @@ public HttpResponse getResponse() {
if (containedJwt.isEmpty()) {
return httpResponse;
}
return httpResponse.withUpdatedHeader(headerName, headerKeyword + " " + token);
return httpResponse.withUpdatedHeader(headerName, headerKeyword + needsSpace(headerKeyword) + token);
}


private String needsSpace(String headerKeyword) {
return headerKeyword.equals("")?"":" ";
}


private boolean containsExactlyTwoDots(String str) {
int firstDotIndex = str.indexOf('.');
if (firstDotIndex == -1) {
Expand Down

0 comments on commit cabcc52

Please sign in to comment.