Skip to content

Commit 582e29e

Browse files
lzhoucsozscheyge
authored andcommitted
Loosen isJWT check. (#93)
Previously added logic for checking if a token is JWT is too strict. According to spec: https://tools.ietf.org/html/rfc7519#section-5.1 `typ` header isn't even required, and its value may not be "JWT". `alg` on the other hand is more commonly used. So checking its existence is much more reliable.
1 parent 8f8e4c6 commit 582e29e

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

restdocs-api-spec/src/main/kotlin/com/epages/restdocs/apispec/JwtSecurityHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ internal class JwtSecurityHandler : SecurityRequirementsExtractor {
3939
val jwtHeader = jwtParts[0]
4040
val decodedJwtHeader = String(Base64.getDecoder().decode(jwtHeader))
4141
try {
42-
val jwtMap = ObjectMapper().readValue<Map<String, Any>>(decodedJwtHeader)
43-
return jwtMap["typ"] == "JWT"
42+
return ObjectMapper().readValue<Map<String, Any>>(decodedJwtHeader)
43+
.containsKey("alg")
4444
} catch (e: IOException) {
4545
// probably not JWT
4646
}

restdocs-api-spec/src/test/kotlin/com/epages/restdocs/apispec/JwtSecurityHandlerTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class JwtSecurityHandlerTest {
7676
operation = OperationBuilder().request("/some")
7777
.header(
7878
AUTHORIZATION,
79-
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
79+
// this jwt token doesn't contain typ header but is still valid format
80+
"Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.GuoUe6tw79bJlbU1HU0ADX0pr0u2kf3r_4OdrDufSfQ"
8081
)
8182
.build()
8283
}

0 commit comments

Comments
 (0)