1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE> [99s-extend] Cowboy pre request filter
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20Cowboy%20pre%20request%20filter&In-Reply-To=%3CCACE%3DTJqaoxrqsCSnN44s_5x4MabgWBVfaN_pecVrShnUuU-9aQ%40mail.gmail.com%3E">
<META NAME="robots" CONTENT="index,nofollow">
<style type="text/css">
pre {
white-space: pre-wrap; /* css-2.1, curent FF, Opera, Safari */
}
</style>
<META http-equiv="Content-Type" content="text/html; charset=us-ascii">
<LINK REL="Previous" HREF="000341.html">
<LINK REL="Next" HREF="000346.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[99s-extend] Cowboy pre request filter</H1>
<B>Mark Nijhof</B>
<A HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20Cowboy%20pre%20request%20filter&In-Reply-To=%3CCACE%3DTJqaoxrqsCSnN44s_5x4MabgWBVfaN_pecVrShnUuU-9aQ%40mail.gmail.com%3E"
TITLE="[99s-extend] Cowboy pre request filter">mark.nijhof at cre8ivethought.com
</A><BR>
<I>Thu Mar 6 10:52:01 CET 2014</I>
<P><UL>
<LI>Previous message: <A HREF="000341.html">[99s-extend] Cowboy pre request filter
</A></li>
<LI>Next message: <A HREF="000346.html">[99s-extend] Trying to grok erlang.mk
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#342">[ date ]</a>
<a href="thread.html#342">[ thread ]</a>
<a href="subject.html#342">[ subject ]</a>
<a href="author.html#342">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>I also found the answer to my own question: custom middleware
I just created:
1 -module(authentication_middleware).
2
3 -behaviour(cowboy_middleware).
4
5 -export([execute/2]).
6
7 execute(Req, Env) ->
8
9 {Path, Req1} = cowboy_req:path(Req),
10
11 case Path of
12 <<"/login.html">> ->
13 {ok, Req1, Env};
14 <<"/do_login">> ->
15 {ok, Req1, Env};
16 _ ->
17 case id3as_security:is_request_authenticated(Req1) of
18 {error, eauth, Req2} ->
19 {ok, Req4} = cowboy_req:reply(303,
[{<<"Location">>, <<"/login.html">>}], "", Req2),
20 {halt, Req4};
21 {authenticated, _Id, Req2} ->
22 {ok, Req2, Env}
23 end
24 end.
And put this between the cowboy_router and cowboy_handler and life is all
good.
-Mark
On Thu, Mar 6, 2014 at 12:47 AM, Mark Nijhof <<A HREF="https://lists.ninenines.eu/listinfo/extend">mark.nijhof at cre8ivethought.com</A>
><i> wrote:
</I>
><i> Hi,
</I>><i>
</I>><i> I want to create a module that basically sits between the incoming request
</I>><i> and the http handler for that request to ensure a request is authenticated
</I>><i> (using a cookie), if the request is not authenticated then I like to
</I>><i> redirect to a specific login page (which should not be filtered).
</I>><i>
</I>><i> Is this possible with Cowboy? Should I use the onrequest hook (not sure if
</I>><i> I can force redirects from there) for that or is there a better way?
</I>><i>
</I>><i> Cheers,
</I>><i>
</I>><i> -Mark
</I>><i>
</I>><i> --
</I>><i> Mark Nijhof
</I>><i> t: @MarkNijhof <<A HREF="https://twitter.com/MarkNijhof">https://twitter.com/MarkNijhof</A>>
</I>><i> s: marknijhof
</I>><i>
</I>><i>
</I>
--
Mark Nijhof
t: @MarkNijhof <<A HREF="https://twitter.com/MarkNijhof">https://twitter.com/MarkNijhof</A>>
s: marknijhof
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <<A HREF="http://lists.ninenines.eu/archives/extend/attachments/20140306/24422ef2/attachment.html">http://lists.ninenines.eu/archives/extend/attachments/20140306/24422ef2/attachment.html</A>>
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI>Previous message: <A HREF="000341.html">[99s-extend] Cowboy pre request filter
</A></li>
<LI>Next message: <A HREF="000346.html">[99s-extend] Trying to grok erlang.mk
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#342">[ date ]</a>
<a href="thread.html#342">[ thread ]</a>
<a href="subject.html#342">[ subject ]</a>
<a href="author.html#342">[ author ]</a>
</LI>
</UL>
<hr>
<a href="https://lists.ninenines.eu/listinfo/extend">More information about the Extend
mailing list</a><br>
</body></html>
|