Skip to content

Commit 35b3f5d

Browse files
authored
Merge pull request #251 from coldbox-modules/copilot/sub-pr-238-again
Use getApplicationMetadata() for cross-platform session detection in TokenService
2 parents b9d0d44 + d297b39 commit 35b3f5d

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

models/services/TokenService.cfc

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ component accessors="true" singleton {
1616
*/
1717
variables.csrfService = "";
1818

19+
/**
20+
* Cache for application metadata to avoid repeated lookups
21+
*/
22+
variables.appMetadata = {};
23+
1924
/**
2025
* Generates a CBWIRE-specific token that doesn't expire.
2126
* Stored using the configured storage implementation and lasts for session lifetime.
@@ -71,7 +76,31 @@ component accessors="true" singleton {
7176
private function generateNewToken() {
7277
// Generate a cryptographically secure random token
7378
var tokenBase = "#createUUID()##getRealIP()##randRange( 0, 65535, "SHA1PRNG" )##getTickCount()#";
74-
return uCase( left( hash( tokenBase & session.sessionid, "SHA-256" ), 40 ) );
79+
80+
// Include session ID if sessions are enabled (cross-platform check)
81+
var sessionId = "";
82+
if ( isSessionManagementEnabled() ) {
83+
try {
84+
sessionId = session.sessionid;
85+
} catch ( any e ) {
86+
// Handle cases where session scope exists but sessionid property is not yet available,
87+
// or when session operations fail during application startup
88+
}
89+
}
90+
91+
return uCase( left( hash( tokenBase & sessionId, "SHA-256" ), 40 ) );
92+
}
93+
94+
/**
95+
* Checks if session management is enabled in the application (cross-platform)
96+
*
97+
* @return True if session management is enabled, false otherwise
98+
*/
99+
private function isSessionManagementEnabled() {
100+
if ( structIsEmpty( variables.appMetadata ) ) {
101+
variables.appMetadata = getApplicationMetadata();
102+
}
103+
return structKeyExists( variables.appMetadata, "sessionManagement" ) && variables.appMetadata.sessionManagement;
75104
}
76105

77106
private function getRealIP() {

0 commit comments

Comments
 (0)