Skip to content

Commit aca2056

Browse files
authored
Pass in the current jwt payload in to getJWTCustomClaims (#34)
* Pass in the current jwt payload in to getJWTCustomClaims * Make our example user implement the IJwtSubject interface
1 parent 43ee787 commit aca2056

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

interfaces/jwt/IJwtSubject.cfc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface{
99
/**
1010
* A struct of custom claims to add to the JWT token
1111
*/
12-
struct function getJwtCustomClaims();
12+
struct function getJwtCustomClaims( required struct payload );
1313

1414
/**
1515
* This function returns an array of all the scopes that should be attached to the JWT token that will be used for authorization.

models/jwt/JwtService.cfc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ component accessors="true" singleton threadsafe {
791791
// Append user custom claims with override, they take prescedence
792792
structAppend(
793793
payload,
794-
arguments.user.getJwtCustomClaims(),
794+
arguments.user.getJwtCustomClaims( payload ),
795795
true
796796
);
797797

@@ -864,7 +864,7 @@ component accessors="true" singleton threadsafe {
864864
*
865865
* @return The discovered refresh token or an empty string
866866
*/
867-
private string function discoverRefreshToken(){
867+
public string function discoverRefreshToken(){
868868
var event = variables.requestService.getContext();
869869

870870
// Discover api token from headers using a custom header or the incoming RC

test-harness/models/User.cfc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
component accessors="true" {
1+
component accessors="true" implements="cbsecurity.interfaces.jwt.IJwtSubject" {
22

33
property name="auth" inject="authenticationService@cbauth";
44

@@ -27,8 +27,11 @@ component accessors="true" {
2727
/**
2828
* A struct of custom claims to add to the JWT token
2929
*/
30-
struct function getJWTCustomClaims(){
31-
return { "role" : "admin" };
30+
struct function getJWTCustomClaims( required struct payload ){
31+
return {
32+
"duplicatedJTI": arguments.payload.jti,
33+
"role" : "admin"
34+
};
3235
}
3336

3437
/**

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ component extends="coldbox.system.testing.BaseTestCase" appMapping="/root" {
189189
} );
190190
} );
191191

192+
given( "a getJwtCustomClaims method on user", function(){
193+
then( "it should pass the current payload in to the function", function(){
194+
var oUser = variables.userService.retrieveUserByUsername( "test" );
195+
var tokens = variables.jwtService.fromUser( oUser );
196+
expect( tokens ).toBeStruct().toHaveKey( "access_token" );
197+
198+
var decodedAccessToken = variables.jwtService.decode(
199+
tokens.access_token
200+
);
201+
expect( decodedAccessToken ).toHaveKey( "jti" );
202+
expect( decodedAccessToken ).toHaveKey( "duplicatedJTI" );
203+
expect( decodedAccessToken.duplicatedJTI ).toBe(
204+
decodedAccessToken.jti
205+
);
206+
} );
207+
} );
208+
192209
given( "an invalid refresh token", function(){
193210
then( "an exception should be thrown", function(){
194211
expect( function(){

0 commit comments

Comments
 (0)