I'm building a modern API using Lucee and want to ...
# lucee
t
I'm building a modern API using Lucee and want to maintain session state without relying on cookies or URL parameters. Specifically, I’d like to pass
CFID
and
CFTOKEN
as custom HTTP headers (e.g.,
Authorization: CFID xxxxx
) with each request, and have Lucee bind to the appropriate session. However: • Passing session cookies cross-site triggers CORS restrictions. • Including session IDs in URLs is insecure and messy. Is there any supported way in Lucee to restore or bind a session using
CFID
and
CFTOKEN
from headers rather than cookies or query params?
If not natively supported, is there a way to hook into restore a session in onRequestStart using the CFID/CFTOKEN? I did figure out how to retrieve the session data from Redis but if I loop over the values and add it to the current session, I'm basically duplicating the data to a new session on every load. Any help is appreciated. Thanks
b
Hey Tim, Unless there is a specific reason to use a stateful API, just know most RESTful APIs are stateless... as state is typically handled by the client. Stateless APIs are also more performant and easier to build. But if you need state, maybe think more in terms of a MVC monolith and consider using a framework with tools to handle sessions. I won't suggest which framework, there are a few choices, and a lot of opinions. Hope that helps!
👍 1
👆🏼 1
👆 1
r
Tim, API should be stateless, however you should be passing some identifier token to know who your user is and what kind of information user allowed to see. As a bonus, stateless api is scalable without need to jump hoops with additional infrastructure
t
Thanks — totally get the stateless argument, but this is a legacy app that depends on sessions. I’m not looking to re-architect right now — just want to know: Can Lucee bind a session from CFID/CFTOKEN passed in headers instead of cookies or URL params? Trying to avoid reinventing session handling when Lucee already does it.
e
interesting question - i don't know the answer, but i would do a quick check to see if this would work - in OnRequestStart grab CFID/TK from your headers and add them to the URL scope. If lucee doesn't grab CFID/TK internally until after OnRequestStart then that might work, although i could see it setting up session stuff before ...
t
Unfortunately all the session setup happens before any CF code has a chance to run so there is no way to intercept or modify Lucee's default behaviour, my hope is that there is some undocumented hook or way to intercept and change the current cfid/tk for the request.
For now I came up with a workaround. I'm using Caddy already so I just updated my config to convert the cfid and cftoken headers to query params before they reach the Lucee server:
Copy code
# Handle CFID and CFTOKEN headers
@hasHeaders {
    header cfid *
    header cftoken *
}

handle @hasHeaders {
    rewrite * {path}?{query}&cfid={header.cfid}&cftoken={header.cftoken}
}

reverse_proxy <http://cfml:8080>
Works great for now, but would be great if Lucee had a better way of doing this or allowed us more control over the current session.
👍 1
e
@Tim Badolato never heard of caddy before, how do you like it? any issues you've run into?
t
Using it as a reverse proxy in front of Lucee — handles SSL, routing, and rewrites. It's been great overall.
b
Hey Tim, Caddy looks interesting, apparently easier than Ngnix along with auto SSL. Written in GO is a plus. Thanks for sharing.
r
interesting, if nginx or other reverse proxy can create Cookie header out of some other header, Lucee will not see the difference
j
https://cfml.slack.com/archives/C06TA0A9W/p1750172396371069?thread_ts=1750126791.387849&amp;cid=C06TA0A9W @Tim Badolato did you test this? It's a bit hacky, but I believe you can set
cookie.cfid
in the psuedo-constructor of Application.cfc and it should take effect - e.g.:
Copy code
component {
    try {
        cookie.cfid = getHTTPRequestData().headers.cfid;
        cookie.cftoken = 0;
    } catch(any e) {
        // do something?
    }

    this.name = '.....';
    this.sessionmanagement = true;
}
Since cookies are passed in the
Cookie
HTTP header, I am guessing your issue is that you don't want that specific header to be used? You could also use Caddy to move your custom headers into the
Cookie
header when proxying to Lucee, I would think.
t
@jcberquist - That worked! That's exactly what I was looking for, thanks!
r
is this pseudo-construtor going to run at every request? I wonder
j
@Tim Badolato Glad to hear it! @rodyon Application.cfc is instantiated on every request, so the code in the psuedo-constructor (outside of any method) will execute on every request.
b
@Tim Badolato, I am surprised you say the above code works. (1) The variables in the cookie code in the pseudo-constructor are implicitly in the
variables
scope. That is a weird scope for session variables. (2) Session-scoped locks are missing. (3) I doubt whether
cfid
is actually a key of the struct
getHTTPRequestData().headers
.
On re-reading, I now see that you are probably passing the
cfid
,
cftoken
and
sessionid
as headers. In that case, is it then an idea to do the following (with any necessary changes) in
onRequestStart
?
<!--- Copy the session-key values from the headers --->
<cfset var tempSession = structnew()>
<cfset tempSession.cfid = cfid_from_header>
<cfset tempSession.cftoken = cftoken_from_header>
<cfset tempSession.sessionid = sessionid_from_header>
<!--- Clear any existing session --->
<cflock scope="session" type="exclusive" timeout=60>
<cfset structclear(session)>
</cflock>
<!--- Restore the session-key values. --->
<cflock scope="session" type="exclusive" timeout=60>
<cfset session.cfid = tempSession.cfid>
<cfset session.cftoken = tempSession.cftoken>
<cfset session.sessionid = tempSession.sessionid>
</cflock>