Skip to content

Commit 10a26fc

Browse files
committed
Return response instead of exception if the refresh token is invalid during security checks
1 parent c11669e commit 10a26fc

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

models/jwt/JwtService.cfc

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -895,41 +895,60 @@ component accessors="true" singleton threadsafe {
895895
};
896896

897897
try {
898-
// Try to get the payload from the jwt token, if we have exceptions, we have failed :(
899-
// This takes care of authenticating the jwt tokens for us.
900-
// getPayload() => parseToken() => authenticateToken()
901-
var payload = getPayload();
902-
}
903-
// Access Token Has Expired
904-
catch ( TokenExpiredException e ) {
905-
// Do we have autoRefreshValidator turned on and we have an incoming refresh token?
906-
if ( variables.settings.jwt.enableAutoRefreshValidator && len( discoverRefreshToken() ) ) {
907-
autoRefreshTokens();
908-
} else {
909-
// Error out as normal
910-
results.messages = e.type & ":" & e.message;
911-
return results;
898+
try {
899+
// Try to get the payload from the jwt token, if we have exceptions, we have failed :(
900+
// This takes care of authenticating the jwt tokens for us.
901+
// getPayload() => parseToken() => authenticateToken()
902+
var payload = getPayload();
912903
}
913-
} catch ( TokenInvalidException e ) {
914-
// Do we have autoRefreshValidator turned on and we have an incoming refresh token?
915-
if ( variables.settings.jwt.enableAutoRefreshValidator && len( discoverRefreshToken() ) ) {
916-
autoRefreshTokens();
917-
} else {
918-
// Error out as normal
919-
results.messages = e.type & ":" & e.message;
920-
return results;
904+
// Access Token Has Expired
905+
catch ( TokenExpiredException e ) {
906+
// Do we have autoRefreshValidator turned on and we have an incoming refresh token?
907+
if (
908+
variables.settings.jwt.enableAutoRefreshValidator && len(
909+
discoverRefreshToken()
910+
)
911+
) {
912+
autoRefreshTokens();
913+
} else {
914+
// Error out as normal
915+
results.messages = e.type & ":" & e.message;
916+
return results;
917+
}
918+
} catch ( TokenInvalidException e ) {
919+
// Do we have autoRefreshValidator turned on and we have an incoming refresh token?
920+
if (
921+
variables.settings.jwt.enableAutoRefreshValidator && len(
922+
discoverRefreshToken()
923+
)
924+
) {
925+
autoRefreshTokens();
926+
} else {
927+
// Error out as normal
928+
results.messages = e.type & ":" & e.message;
929+
return results;
930+
}
931+
} catch ( TokenNotFoundException e ) {
932+
// Do we have autoRefreshValidator turned on and we have an incoming refresh token?
933+
if (
934+
variables.settings.jwt.enableAutoRefreshValidator && len(
935+
discoverRefreshToken()
936+
)
937+
) {
938+
autoRefreshTokens();
939+
} else {
940+
// Error out as normal
941+
results.messages = e.type & ":" & e.message;
942+
return results;
943+
}
921944
}
922-
} catch ( TokenNotFoundException e ) {
923-
// Do we have autoRefreshValidator turned on and we have an incoming refresh token?
924-
if ( variables.settings.jwt.enableAutoRefreshValidator && len( discoverRefreshToken() ) ) {
925-
autoRefreshTokens();
926-
} else {
927-
// Error out as normal
945+
// All other exceptions
946+
catch ( Any e ) {
928947
results.messages = e.type & ":" & e.message;
929948
return results;
930949
}
931950
}
932-
// All other exceptions
951+
// All exceptions for refreshTokens
933952
catch ( Any e ) {
934953
results.messages = e.type & ":" & e.message;
935954
return results;

test-harness/tests/specs/integration/JWTSpec.cfc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ component extends="coldbox.system.testing.BaseTestCase" appMapping="/root" {
9595
expect( results.allow ).toBeTrue();
9696
} );
9797
} );
98+
given( "Auto refresh is on and an expired access token is sent with an expired refresh token", function(){
99+
then( "the validation should fail", function(){
100+
getRequestContext().setValue( "x-auth-token", variables.expired_token );
101+
getRequestContext().setValue(
102+
"x-refresh-token",
103+
variables.expired_token
104+
);
105+
106+
var results = variables.jwtService.validateSecurity( "" );
107+
expect( results.allow ).toBeFalse();
108+
} );
109+
} );
98110
} );
99111

100112
story( "I can refresh tokens via the /refreshtoken endpoint", function(){

0 commit comments

Comments
 (0)