Skip to content

Commit df1ab9e

Browse files
committed
Merge branch 'development' of github.com:coldbox-modules/cbsecurity into development
2 parents b54f5ec + 8eb17fd commit df1ab9e

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

models/jwt/JwtService.cfc

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -894,17 +894,29 @@ component accessors="true" singleton threadsafe {
894894
"messages" : ""
895895
};
896896

897+
var payload = {};
898+
897899
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-
var refreshToken = discoverRefreshToken();
907-
if ( variables.settings.jwt.enableAutoRefreshValidator && len( refreshToken ) ) {
900+
try {
901+
// Try to get the payload from the jwt token, if we have exceptions, we have failed :(
902+
// This takes care of authenticating the jwt tokens for us.
903+
// getPayload() => parseToken() => authenticateToken()
904+
payload = getPayload();
905+
} catch ( any e ) {
906+
// if we aren't trying to refresh, return the false response now.
907+
var refreshToken = discoverRefreshToken();
908+
if (
909+
!variables.settings.jwt.enableAutoRefreshValidator ||
910+
!len( refreshToken ) ||
911+
!listFindNoCase(
912+
"TokenExpiredException,TokenInvalidException,TokenNotFoundException",
913+
e.type
914+
)
915+
) {
916+
results.messages = e.type & ":" & e.message;
917+
return results;
918+
}
919+
908920
// Try to Refresh the tokens
909921
var newTokens = this.refreshToken( refreshToken );
910922
// Setup payload + authenticate for current request
@@ -920,13 +932,9 @@ component accessors="true" singleton threadsafe {
920932
name : variables.settings.jwt.customRefreshHeader,
921933
value: newTokens.refresh_token
922934
);
923-
} else {
924-
// Error out as normal
925-
results.messages = e.type & ":" & e.message;
926-
return results;
927935
}
928936
}
929-
// All other exceptions
937+
// All exceptions for refreshTokens
930938
catch ( Any e ) {
931939
results.messages = e.type & ":" & e.message;
932940
return results;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ component extends="coldbox.system.testing.BaseTestCase" appMapping="/root" {
6464
expect( results.messages ).toInclude( "TokenNotFoundException" );
6565
} );
6666
} );
67+
given( "Auto refresh is on and no access token is sent but a refresh token is sent", function(){
68+
then( "the validation should pass and we should return our two new tokens as headers", function(){
69+
var oUser = variables.userService.retrieveUserByUsername( "test" );
70+
var tokens = variables.jwtService.fromUser( oUser );
71+
72+
getRequestContext().setValue( "x-refresh-token", tokens.refresh_token );
73+
74+
var results = variables.jwtService.validateSecurity( "" );
75+
expect( results.allow ).toBeTrue();
76+
} );
77+
} );
6778
given( "Auto refresh is on and an expired access token is sent but no refresh token is sent", function(){
6879
then( "the validation should fail", function(){
6980
getRequestContext().setValue( "x-auth-token", variables.expired_token );
@@ -84,6 +95,18 @@ component extends="coldbox.system.testing.BaseTestCase" appMapping="/root" {
8495
expect( results.allow ).toBeTrue();
8596
} );
8697
} );
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+
} );
87110
} );
88111

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

0 commit comments

Comments
 (0)