1 /**
2 * Copyright © DiamondMVC 2018
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.http.cookies;
7 
8 import diamond.core.apptype;
9 
10 static if (isWeb)
11 {
12   import vibe.d : Cookie;
13 
14   import diamond.errors.checks;
15   import diamond.core.senc;
16   import diamond.http.client;
17 
18   /// Wrapper around a http cookie collections.
19   final class HttpCookies
20   {
21     private:
22     /// The client.
23     HttpClient _client;
24 
25     public:
26     final:
27     /**
28     * Creates a new http cookie collection.
29     * Params:
30     *   client = The client.
31     */
32     package(diamond) this(HttpClient client)
33     {
34       _client = enforceInput(client, "Cannot create a cookie collection without a client.");
35     }
36 
37     /**
38     * Creates a cookie.
39     * Params:
40     *   name =     The name of the cookie.
41     *   value =    The value of the cookie.
42     *   maxAge =   The max-age of the cookie. (Seconds the cookie will be alive.)
43     *   path =     The path of the cookie. (Default: "/")
44     */
45     void create
46     (
47       string name, string value,
48       long maxAge,
49       string path = "/"
50     )
51     {
52       auto cookie = new Cookie;
53       cookie.path = path;
54       cookie.maxAge = maxAge;
55       cookie.setValue(value, Cookie.Encoding.none);
56 
57       _client.rawResponse.cookies[name] = cookie;
58     }
59 
60     /**
61     * Creates a cookie.
62     * Params:
63     *   name =      The name of the cookie.
64     *   buffer =    The buffer to encode into a SENC encoded cookie string.
65     *   maxAge =    The max-age of the cookie. (Seconds the cookie will be alive.)
66     *   path =      The path of the cookie. (Default: "/")
67     */
68     void createBuffered
69     (
70       string name, ubyte[] buffer,
71       long maxAge,
72       string path = "/"
73     )
74     {
75       return create(name, SENC.encode(buffer), maxAge, path);
76     }
77 
78     /**
79     * Gets a cookie.
80     * Params:
81     *   name =    The name of the cookie.
82     * Returns:
83     *   Returns the cookie if found, null otherwise.
84     */
85     string get(string name)
86     {
87       return _client.rawRequest.cookies.get(name);
88     }
89 
90     /**
91     * Gets a buffered cookie encoded as a SENC encoded string.
92     * Params:
93     *   name =    The name.
94     * Returns:
95     *   Returns the buffer.
96     */
97     ubyte[] getBuffered(string name)
98     {
99       return SENC.decode(get(name));
100     }
101 
102     /**
103     * Removes a cookie.
104     * Params:
105     *   name =     The name of the cookie to remove.
106     */
107     void remove(string name)
108     {
109       auto cookie = new Cookie;
110       cookie.path = "/";
111       cookie.maxAge = 1;
112       cookie.setValue(null, Cookie.Encoding.none);
113 
114       _client.rawResponse.cookies[name] = cookie;
115     }
116 
117     /**
118     * Checks whether a request has a cookie or not.
119     * Params:
120     *   name = The name of the cookie to check for existence.
121     * Returns:
122     *   True if the cookie exists, false otherwise.
123     */
124     bool has(string name)
125     {
126       return get(name) !is null;
127     }
128 
129     /**
130     * Gets the auth cookie.
131     * Returns:
132     *   Returns the auth cookie.
133     */
134     string getAuthCookie()
135     {
136       import diamondauth = diamond.authentication;
137 
138       return diamondauth.getAuthCookie(_client);
139     }
140 
141     /**
142     * Checks whether the auth cookie is present or not.
143     * Returns:
144     *   True if the auth cookie is present, false otherwise.
145     */
146     @property bool hasAuthCookie()
147     {
148       import diamondauth = diamond.authentication;
149 
150       return diamondauth.hasAuthCookie(_client);
151     }
152   }
153 }