Storing session cookies

Sharing and discussing custom actions for LaunchBar
Post Reply
Kevin Yank
Posts: 3
Joined: Tue Dec 22, 2015 12:00 pm

Storing session cookies

Post by Kevin Yank » Tue Dec 22, 2015 12:13 pm

Hi there,

I’m trying to build a JavaScript action that accesses a website that does not offer a full-fledged API. To do so, my action will need to sign into the site using the HTML login form that is presented when a request is sent without a valid session cookie.

This much I have been able to achieve, and I’ve discovered that LaunchBar automatically stores cookies received in response to one request and sends them in subsequent requests during a single action script. As a result, my action is able to log in with one request and then make any subsequent requests it needs to in that logged-in session. But as soon as the action script finishes running, LaunchBar seems to discard any cookies. Subsequent invocations of the same action begin with an “empty cookie jar” again.

I had hoped to improve my action’s performance by holding onto the session cookie (say, by storing it in a preference) and reusing it for subsequent action script runs, only submitting the login form (and obtaining a new session cookie) when the stored cookie expires. Unfortunately, although I’m able to retrieve and store the session cookie as expected (using result.response.headerFields['Set-Cookie']), sending the stored cookie with HTTP.get doesn’t seem to work. Given this code…

Code: Select all

result = HTTP.get(url, {headerFields: {'Cookie': Action.preferences['session']}});

…LaunchBar does not seem to send the Cookie header I have supplied.

Is this a known limitation in LaunchBar’s HTTP object, and if so, is there any other way to persist and reuse a session cookie?

marco
Objective Development
Objective Development
Posts: 64
Joined: Mon Jul 28, 2014 3:00 pm
Location: Vienna, Austria

Re: Storing session cookies

Post by marco » Mon Dec 28, 2015 3:28 pm

I’m absolutely not an expert on web stuff and cookies in particular, but the LaunchBar Action part of what you’re doing looks right to me.
Can you verify that the cookie gets sent correctly? You can easily check this by using netcat on the Terminal using the following command:

Code: Select all

nc -l 1234

Then, adapt the code you posted:

Code: Select all

var url = 'http://127.0.0.1:1234/';
result = HTTP.get(url, {headerFields: {'Cookie': Action.preferences['session']}});

Running this should print the whole HTTP request including its headers in the Terminal, which could help in debugging.

If the “Cookie” HTTP header field looks correct, but it still doesn’t work against the website, you’d probably have to intercept and dump the HTTP request (e.g. using Little Snitch Network Monitor :wink:) and then see what actually gets sent out for the case when it works. Note that you have to dump the request that gets sent to the website because the system will not include the cookie HTTP header field when it sends the request to localhost.
(This sounds awfully complicated, maybe we should add an option to dump the actual HTTP request sent by LaunchBar’s JavaScript to the console for debugging…)

The reason why sending the cookie during a single run of the action works is very likely related to how the HTTP object in LaunchBar’s JavaScript is implemented because it uses the NSURLSession API provided by Apple’s Foundation framework and that keeps its cookie jar around as long as the script runs.

Hope this helps! Let me know if you have any questions.

Cheers,

Marco

nbjahan
Rank 1
Rank 1
Posts: 34
Joined: Sat Jun 28, 2014 1:06 pm

Re: Storing session cookies

Post by nbjahan » Tue Jan 05, 2016 4:43 pm

You can also debug it with httpbin.org

Code: Select all

result = HTTP.get("https://httpbin.org/cookies", {headerFields: {'Cookie': Action.preferences['session']}});


I've tested it with LB6.5, working as expected.

Post Reply