Skip to content

Commit

Permalink
Improve jwt message with added reason
Browse files Browse the repository at this point in the history
  • Loading branch information
vordimous committed Jun 3, 2024
1 parent 656b19a commit 187bd0e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ public JwtEventContext(
public void authorizationFailed(
long traceId,
long bindingId,
String identity)
String identity,
String reason)
{
JwtEventExFW extension = jwtEventExRW
.wrap(extensionBuffer, 0, extensionBuffer.capacity())
.authorizationFailed(e -> e
.typeId(AUTHORIZATION_FAILED.value())
.identity(identity)
.reason(reason)
)
.build();
EventFW event = eventRW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,20 @@ public String format(
case AUTHORIZATION_FAILED:
{
JwtAuthorizationFailedExFW ex = extension.authorizationFailed();
result = String.format("No active session found for token identity (%s).", identity(ex.identity()));
result = String.format("JWT token authorization failed for identity (%s). %s",
asString(ex.identity()),
asString(ex.reason())
);
break;
}
}
return result;
}

private static String identity(
StringFW identity)
private static String asString(
StringFW stringFW)
{
int length = identity.length();
return length <= 0 ? "-" : identity.asString();
String s = stringFW.asString();
return s == null ? "" : s;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public long reauthorize(
{
JwtSession session = null;
String subject = null;
String reason = "";

authorize:
try
Expand All @@ -142,13 +143,15 @@ public long reauthorize(
key == null ||
!Objects.equals(alg, key.getAlgorithm()))
{
reason = "Invalid alg or key.";
break authorize;
}

signature.setKey(null);
signature.setKey(key.getKey());
if (!signature.verifySignature())
{
reason = "Unable to verify key signature.";
break authorize;
}

Expand All @@ -162,10 +165,15 @@ public long reauthorize(

long now = Instant.now().toEpochMilli();
if (notBefore != null && now < notBefore.getValueInMillis() ||
notAfter != null && now > notAfter.getValueInMillis() ||
issuer == null || !issuer.equals(this.issuer) ||
notAfter != null && now > notAfter.getValueInMillis())
{
reason = "Token is expired.";
break authorize;
}
if (issuer == null || !issuer.equals(this.issuer) ||
audience == null || !audience.contains(this.audience))
{
reason = "Invalid issuer or audience.";
break authorize;
}

Expand All @@ -191,11 +199,11 @@ public long reauthorize(
}
catch (JoseException | InvalidJwtException | MalformedClaimException ex)
{
// not authorized
reason = ex.getMessage();
}
if (session == null)
{
event.authorizationFailed(traceId, bindingId, subject);
event.authorizationFailed(traceId, bindingId, subject, reason);
}
return session != null ? session.authorized : NOT_AUTHORIZED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ scope jwt
struct JwtAuthorizationFailedEx extends core::stream::Extension
{
string8 identity;
string16 reason;
}

union JwtEventEx switch (JwtEventType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ telemetry:
- qname: test.net0
id: guard.jwt.authorization.failed
name: GUARD_JWT_AUTHORIZATION_FAILED
message: No active session found for token identity (user).
message: JWT token authorization failed for identity (user). Token is expired.
guards:
jwt0:
type: jwt
Expand Down

0 comments on commit 187bd0e

Please sign in to comment.