1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<tt>
<div dir="ltr">I also found the answer to my own question: custom middleware<div><br></div><div>I just created:</div><div><br></div><div><div>� �1 -module(authentication_middleware).</div><div>� �2</div><div>� �3 -behaviour(cowboy_middleware).</div><br>
<div>� �4</div><div>� �5 -export([execute/2]).</div><div>� �6</div><div>� �7 execute(Req, Env) -></div><div>� �8</div><div>� �9 � � {Path, Req1} = cowboy_req:path(Req),</div><div>� 10</div><div>� 11 � � case Path of</div><br>
<div>� 12 � � � � <<"/login.html">> -></div><div>� 13 � � � � � � {ok, Req1, Env};</div><div>� 14 � � � � <<"/do_login">> -></div><div>� 15 � � � � � � {ok, Req1, Env};</div><br>
<div>� 16 � � � � _ -></div><div>� 17 � � � � � � case id3as_security:is_request_authenticated(Req1) of</div><div>� 18 � � � � � � � � {error, eauth, Req2} -></div><div>� 19 � � � � � � � � � � {ok, Req4} = cowboy_req:reply(303, [{<<"Location">>, <<"/login.html">>}], "", Req2),</div><br>
<div>� 20 � � � � � � � � � � {halt, Req4};</div><div>� 21 � � � � � � � � {authenticated, _Id, Req2} -></div><div>� 22 � � � � � � � � � �{ok, Req2, Env}</div><div>� 23 � � � � � � end</div><div>� 24 � � end.</div></div><br>
<div><br></div><div>And put this between the cowboy_router and cowboy_handler and life is all good.</div><div><br></div><div>-Mark</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 6, 2014 at 12:47 AM, Mark Nijhof <span dir="ltr"><<a href="mailto:[email protected]" target="_blank">[email protected]</a>></span> wrote:<br><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>I want to create a module that basically sits between the incoming request and the http handler for that request to ensure a request is authenticated (using a cookie), if the request is not authenticated then I like to redirect to a specific login page (which should not be filtered).</div><br>
<br>
<div><br></div><div>Is this possible with Cowboy? Should I use the onrequest hook (not sure if I can force redirects from there) for that or is there a better way?</div><div><br></div><div>Cheers,</div><div><br></div><div><br>
<br>
-Mark<span class="HOEnZb"><font color="#888888"><br clear="all"><div><br></div>-- <br><div dir="ltr">Mark Nijhof<br><div><div>t: � <a href="https://twitter.com/MarkNijhof" target="_blank">@MarkNijhof</a><br>s: �marknijhof</div><br>
</div><div><br></div></div><br>
</font></span></div></div><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">Mark Nijhof<br><div><div>t: � <a href="https://twitter.com/MarkNijhof" target="_blank">@MarkNijhof</a><br>s: �marknijhof</div></div><div><br></div><br>
</div><br>
</div><br>
</tt>
|