Debian Codegraph @

doc/src/guide/cookies.asciidoc

raw
1 [[cookies]]
2 == Using cookies
3
4 Cookies are a mechanism allowing applications to maintain
5 state on top of the stateless HTTP protocol.
6
7 Cookies are a name/value store where the names and values are
8 stored in plain text. They expire either after a delay
9 or when the browser closes. They can be configured on a
10 specific domain name or path, and restricted to secure
11 resources (sent or downloaded over HTTPS), or restricted
12 to the server (disallowing access from client-side scripts).
13
14 Cookie names are de facto case sensitive.
15
16 Cookies are stored client-side and sent with every subsequent
17 request that matches the domain and path for which they were
18 stored, until they expire. This can create a non-negligible
19 cost.
20
21 Cookies should not be considered secure. They are stored on
22 the user's computer in plain text, and can be read by any
23 program. They can also be read by proxies when using clear
24 connections. Always validate the value before using it,
25 and never store any sensitive information inside it.
26
27 Cookies set by the server are only available in requests
28 following the client reception of the response containing
29 them.
30
31 Cookies may be sent repeatedly. This is often useful to
32 update the expiration time and avoid losing a cookie.
33
34 === Setting cookies
35
36 By default cookies are defined for the duration of the session:
37
38 [source,erlang]
39 ----
40 SessionID = generate_session_id(),
41 Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0).
42 ----
43
44 They can also be set for a duration in seconds:
45
46 [source,erlang]
47 ----
48 SessionID = generate_session_id(),
49 Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0,
50 #{max_age => 3600}).
51 ----
52
53 To delete cookies, set `max_age` to 0:
54
55 [source,erlang]
56 ----
57 SessionID = generate_session_id(),
58 Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0,
59 #{max_age => 0}).
60 ----
61
62 To restrict cookies to a specific domain and path, the options
63 of the same name can be used:
64
65 [source,erlang]
66 ----
67 Req = cowboy_req:set_resp_cookie(<<"inaccount">>, <<"1">>, Req0,
68 #{domain => "my.example.org", path => "/account"}).
69 ----
70
71 Cookies will be sent with requests to this domain and all
72 its subdomains, and to resources on this path or deeper
73 in the path hierarchy.
74
75 To restrict cookies to secure channels (typically resources
76 available over HTTPS):
77
78 [source,erlang]
79 ----
80 SessionID = generate_session_id(),
81 Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0,
82 #{secure => true}).
83 ----
84
85 To prevent client-side scripts from accessing a cookie:
86
87 [source,erlang]
88 ----
89 SessionID = generate_session_id(),
90 Req = cowboy_req:set_resp_cookie(<<"sessionid">>, SessionID, Req0,
91 #{http_only => true}).
92 ----
93
94 Cookies may also be set client-side, for example using
95 Javascript.
96
97 === Reading cookies
98
99 The client only ever sends back the cookie name and value.
100 All other options that can be set are never sent back.
101
102 Cowboy provides two functions for reading cookies. Both
103 involve parsing the cookie header(s) and so should not
104 be called repeatedly.
105
106 You can get all cookies as a key/value list:
107
108 [source,erlang]
109 Cookies = cowboy_req:parse_cookies(Req),
110 {_, Lang} = lists:keyfind(<<"lang">>, 1, Cookies).
111
112 Or you can perform a match against cookies and retrieve
113 only the ones you need, while at the same time doing
114 any required post processing using xref:constraints[constraints].
115 This function returns a map:
116
117 [source,erlang]
118 #{id := ID, lang := Lang} = cowboy_req:match_cookies([id, lang], Req).
119
120 You can use constraints to validate the values while matching
121 them. The following snippet will crash if the `id` cookie is
122 not an integer number or if the `lang` cookie is empty. Additionally
123 the `id` cookie value will be converted to an integer term:
124
125 [source,erlang]
126 CookiesMap = cowboy_req:match_cookies([{id, int}, {lang, nonempty}], Req).
127
128 Note that if two cookies share the same name, then the map value
129 will be a list of the two cookie values.
130
131 A default value can be provided. The default will be used
132 if the `lang` cookie is not found. It will not be used if
133 the cookie is found but has an empty value:
134
135 [source,erlang]
136 #{lang := Lang} = cowboy_req:match_cookies([{lang, [], <<"en-US">>}], Req).
137
138 If no default is provided and the value is missing, an
139 exception is thrown.