Skip to content

Commit cabcc52

Browse files
Oussama ZghebOussama Zgheb
authored andcommitted
finish #93
1 parent 0d039d7 commit cabcc52

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/main/java/app/tokenposition/AuthorizationBearerHeader.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public boolean positionFound() {
2525
for (HttpHeader header : httpMessage.headers()) {
2626
containedJwt = containsJwt(header.value(), List.of("Bearer","bearer","BEARER"));
2727
if (containedJwt.isPresent()) {
28+
headerName = header.name();
2829
this.token = containedJwt.get();
2930
return true;
3031
}
@@ -40,13 +41,15 @@ private Optional<String> containsJwt(String headerValue, List<String> jwtKeyword
4041
if (headerValue.startsWith(keyword)) {
4142
String potentialJwt = headerValue.replace(keyword, "").trim();
4243
if (CustomJWToken.isValidJWT(potentialJwt)) {
44+
headerKeyword = keyword;
4345
return Optional.of(potentialJwt);
4446
}
4547
}
4648
}
4749
if(headerValue.toLowerCase().startsWith("ey") || containsExactlyTwoDots(headerValue)) {
4850
String potentialJwt = headerValue.trim();
4951
if (CustomJWToken.isValidJWT(potentialJwt)) {
52+
headerKeyword = "";
5053
return Optional.of(potentialJwt);
5154
}
5255
}
@@ -59,7 +62,7 @@ public HttpRequest getRequest() {
5962
if (containedJwt.isEmpty()) {
6063
return httpRequest;
6164
}
62-
return httpRequest.withUpdatedHeader(headerName, headerKeyword + " " + token);
65+
return httpRequest.withUpdatedHeader(headerName, headerKeyword + needsSpace(headerKeyword) + token);
6366
}
6467

6568
@Override
@@ -68,9 +71,15 @@ public HttpResponse getResponse() {
6871
if (containedJwt.isEmpty()) {
6972
return httpResponse;
7073
}
71-
return httpResponse.withUpdatedHeader(headerName, headerKeyword + " " + token);
74+
return httpResponse.withUpdatedHeader(headerName, headerKeyword + needsSpace(headerKeyword) + token);
7275
}
7376

77+
78+
private String needsSpace(String headerKeyword) {
79+
return headerKeyword.equals("")?"":" ";
80+
}
81+
82+
7483
private boolean containsExactlyTwoDots(String str) {
7584
int firstDotIndex = str.indexOf('.');
7685
if (firstDotIndex == -1) {

0 commit comments

Comments
 (0)