Skip to content

Commit 4c6337f

Browse files
committed
finalized correct storage for db token storage, exists was also wrong
1 parent eb8230d commit 4c6337f

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

models/jwt/JwtService.cfc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ component accessors="true" singleton{
171171
* @token The token to invalidate
172172
*/
173173
boolean function invalidate( required token ){
174+
if( variables.log.canInfo() ){
175+
variables.log.info( "Token invalidation request issued for :#arguments.token#" );
176+
}
174177
return getTokenStorage().clear( arguments.token );
175178
}
176179

@@ -314,6 +317,22 @@ component accessors="true" singleton{
314317
return event.getPrivateValue( "jwt_payload" );
315318
}
316319

320+
/**
321+
* Get the authenticated user stored on `prc` via the variables.settings.prcUserVariable setting.
322+
* if it doesn't exist, then call parseToken() and try to load it and authenticate it.
323+
*
324+
* @return The user that implements IAuth and IJwtSubject
325+
*/
326+
function getUser(){
327+
var event = variables.requestService.getContext();
328+
329+
if( !event.privateValueExists( variables.settings.prcUserVariable ) ){
330+
parseToken();
331+
}
332+
333+
return event.getPrivateValue( variables.settings.prcUserVariable );
334+
}
335+
317336
/************************************************************************************/
318337
/****************************** RAW JWT Methods *************************************/
319338
/************************************************************************************/

models/jwt/storages/CacheTokenStorage.cfc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ component accessors="true" singleton{
1010
property name="wirebox" inject="wirebox";
1111
property name="cachebox" inject="cachebox";
1212
property name="settings" inject="coldbox:moduleSettings:cbSecurity";
13+
property name="jwtService" inject="JwtService@cbSecurity";
1314

1415
/**
1516
* Storage properties
@@ -69,8 +70,8 @@ component accessors="true" singleton{
6970
buildKey( arguments.key ),
7071
{
7172
token : arguments.token,
72-
expiration : arguments.expiration,
73-
created : now(),
73+
expiration : jwtService.fromEpoch( arguments.payload.exp ),
74+
issued : jwtService.fromEpoch( arguments.payload.iat ),
7475
payload : arguments.payload
7576
},
7677
arguments.expiration

models/jwt/storages/DBTokenStorage.cfc

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* - id : db identifier
1616
* - cacheKey : varchar 255
1717
* - token : text
18-
* - expiration : varchar 255 (unix timestamp)
18+
* - expiration : (unix timestamp)
19+
* - issued : (unix timestamp)
1920
* - subject : varchar 255
2021
*
2122
*/
@@ -25,6 +26,7 @@ component accessors="true" singleton{
2526
property name="wirebox" inject="wirebox";
2627
property name="cachebox" inject="cachebox";
2728
property name="settings" inject="coldbox:moduleSettings:cbSecurity";
29+
property name="jwtService" inject="JwtService@cbSecurity";
2830

2931
/**
3032
* Storage properties
@@ -41,7 +43,7 @@ component accessors="true" singleton{
4143
*/
4244
property name="keyPrefix";
4345

44-
variables.COLUMNS = "id,cacheKey,token,expiration,created,subject";
46+
variables.COLUMNS = "id,cacheKey,token,expiration,issued,subject";
4547

4648
/**
4749
* Constructor
@@ -111,16 +113,16 @@ component accessors="true" singleton{
111113
:cacheKey,
112114
:token,
113115
:expiration,
114-
:created,
116+
:issued,
115117
:subject
116118
)
117119
",
118120
{
119121
uuid = { cfsqltype="varchar", value="#variables.uuid.randomUUID().toString()#" },
120122
cacheKey = { cfsqltype="varchar", value=arguments.key },
121123
token = { cfsqltype="longvarchar",value=arguments.token },
122-
expiration = { cfsqltype="varchar", value=arguments.expiration },
123-
created = { cfsqltype="timestamp", value=now() },
124+
expiration = { cfsqltype="timestamp", value=jwtService.fromEpoch( arguments.payload.exp ) },
125+
issued = { cfsqltype="timestamp", value=jwtService.fromEpoch( arguments.payload.iat ) },
124126
subject = { cfsqltype="varchar", value=arguments.payload.sub },
125127
},
126128
{
@@ -136,27 +138,16 @@ component accessors="true" singleton{
136138
* @key The cache key
137139
*/
138140
boolean function exists( required key ){
139-
queryExecute(
140-
"INSERT INTO #getTable()# (#variables.COLUMNS#)
141-
VALUES (
142-
:uuid,
143-
:cacheKey,
144-
:token,
145-
:expiration,
146-
:created
147-
)
148-
",
141+
var qResults = queryExecute(
142+
"SELECT cacheKey FROM #getTable()# WHERE cacheKey = :cacheKey",
149143
{
150-
uuid = { cfsqltype="varchar", value="#variables.uuid.randomUUID().toString()#" },
151-
cacheKey = { cfsqltype="varchar", value=arguments.key },
152-
token = { cfsqltype="varchar", value=arguments.token },
153-
expiration = { cfsqltype="timestamp", value=arguments.expiration },
154-
created = { cfsqltype="timestamp", value=now() }
144+
cacheKey : arguments.key
155145
},
156146
{
157-
datasource = variables.properties.dsn
147+
datsource = variables.properties.dsn
158148
}
159149
);
150+
return qResults.recordcount > 0;
160151
}
161152

162153
/**
@@ -170,7 +161,7 @@ component accessors="true" singleton{
170161
struct function get( required key, struct defaultValue ){
171162
// select entry
172163
var q = queryExecute(
173-
"SELECT cacheKey, token, expiration, created
164+
"SELECT cacheKey, token, expiration, issued
174165
FROM #getTable()#
175166
WHERE cacheKey = ?
176167
",
@@ -183,10 +174,10 @@ component accessors="true" singleton{
183174
// Just return if records found, else null
184175
if( q.recordCount ){
185176
return {
186-
"token" : q.token,
187-
"cacheKey" : q.cacheKey,
188-
"expiration" : q.expiration,
189-
"created" : q.created
177+
"token" : q.token,
178+
"cacheKey" : q.cacheKey,
179+
"expiration" : q.expiration,
180+
"issued" : q.issued
190181
};
191182
}
192183

@@ -333,8 +324,8 @@ component accessors="true" singleton{
333324
"CREATE TABLE #getTable()# (
334325
id VARCHAR(36) NOT NULL,
335326
cacheKey VARCHAR(255) NOT NULL,
336-
expiration VARCHAR(255) NOT NULL,
337-
created #getDateTimeColumnType()# NOT NULL,
327+
expiration #getDateTimeColumnType()# NOT NULL,
328+
issued #getDateTimeColumnType()# NOT NULL,
338329
token #getTextColumnType()# NOT NULL,
339330
subject VARCHAR(255) NOT NULL,
340331
PRIMARY KEY (id)

test-harness/config/Coldbox.cfc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@
124124
"tokenStorage" : {
125125
"enabled" : true,
126126
"keyPrefix" : "cbjwt_",
127-
"driver" : "cachebox",
128-
"properties" : { "cacheName" : "default" }
127+
//"driver" : "cachebox",
128+
//"properties" : { "cacheName" : "default" }
129+
130+
"driver" : "db",
131+
"properties" : { "table" : "jwtTokens" }
129132
}
130133
}
131134
}

0 commit comments

Comments
 (0)