# Writing a module to check validity of user sessions

**URL:** <https://www.ruby-forum.com/t/writing-a-module-to-check-validity-of-user-sessions/182186>\
**Category:** NGINX\
**Created:** [January 24, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/writing-a-module-to-check-validity-of-user-sessions/182186 "2010-01-24T16:44:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [January 24, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/writing-a-module-to-check-validity-of-user-sessions/182186/1 "2010-01-24T16:44:36Z")

</div>

hi

i am currently writing a module which should send the session\_id  
cookie to an upstream, receive the upstreams answer, and based on the  
answer decide if it should deliver a page for authenticated users or a  
page for nonauthenticated ones. i do actually have a working version  
now, but i think the way how i solved it is not very optimal yet  
because i am kind of abusing the status\_n integer of  
upstream-\>headers\_in of the upstream for something that it isn’t made  
for.  
my current solution connects to the upstream, sends the session\_id  
cookie and then reads the first byte from the upstreams answers. if  
the first byte is 0, it sets the upstreams http status code to 404, if  
the first byte is a one it sets the status code to 403. then i just  
use the ‘error\_page’ directive to do an internal redirect to either  
the authenticated location or the not authenticated location.

this works, but i think its not very nice…

i am looking for a way how i can do an internal redirect in my module  
after i finished reading the reply from the upstream. i cannot do it  
in the handler function because it returns before i got the answer  
from the upstream. i tried using the internal\_redirect function in the  
callbacks which i can set on the upstream, but that doesn’t seem to  
work for several reasons like segfaults everywhere.

i would be really glad about a tip how to do internal redirects after  
reading an upstreams answer.

i attached my module, the module configuration and the nginx  
configuration which is currently working the way i want with nginx  
0.7.64.

additionally i attached tcpserver.py, which is just a simple test  
authentication server, it only returns a hardcoded 0 or 1 and i used  
it for testing the module.

i hope i expressed more or less clearly what i am trying to achieve.

i would be really glad about any idea,

mauro

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [January 24, 2010, 4:49pm UTC](https://www.ruby-forum.com/t/writing-a-module-to-check-validity-of-user-sessions/182186/2 "2010-01-24T16:49:52Z")

</div>

Use eval module.

Best regards,  
Piotr S. \< removed\_email\_address@domain.invalid \>

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [January 24, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/writing-a-module-to-check-validity-of-user-sessions/182186/3 "2010-01-24T17:00:24Z")

</div>

looks great, i will try that,

thanks

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [January 27, 2010, 11:20am UTC](https://www.ruby-forum.com/t/writing-a-module-to-check-validity-of-user-sessions/182186/4 "2010-01-27T11:20:57Z")

</div>

thanks for the tip, the combination with mod\_eval works great.

i modified my module a little and use it in combination with mod\_eval  
to create a variable in the nginx config file that contains the  
authentication status of a user.

my colleague alvaro videla wrote a little python server which can give  
me the authentication status of a user based on the php session cookie  
which i am passing to him using the check\_session module:

> **[Inspecting PHP sessions from Python](http://obvioushints.blogspot.com/2010/01/inspecting-php-sessions-from-python.html)**
>
> For one of our PHP projects we wanted to be able to inspect the PHP sessions from outside PHP. For example we want to know the users privile...

thats cool because we can now, using this variable in the nginx  
config, cache pages in memcache which are specific to an  
authentication level. thats going to save us a lot of cpu on our  
really have php framework.

i attached my modified module and a test nginx config to the mail.

mauro
