1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.controllers.authentication;
7 
8 import diamond.core.apptype;
9 
10 static if (isWeb)
11 {
12   import diamond.http;
13 
14   /// Wrapper for an authentication status.
15   final class AuthStatus
16   {
17     private:
18     /// The client.
19     HttpClient _client;
20 
21     /// Boolean determining whether the authentication was successful or not.
22     bool _authenticated;
23 
24     /// The message of the authentication.
25     string _message;
26 
27     public:
28     /**
29     * Creates a new authentcation status.
30     * Params:
31     *   client =       The client that was authenticated.
32     *   authenticated = Boolean determining whehter the authentication was successful or not.
33     *   message =       (optional) The message of the authentication status.
34     */
35     this(HttpClient client, bool authenticated, string message = null)
36     {
37       _client = client;
38       _authenticated = authenticated;
39       _message = message;
40     }
41 
42     @property
43     {
44       /// Gets the client that was authenticated.
45       HttpClient client() { return _client; }
46 
47       /// Gets a boolean determining whether the authentication was successful or not.
48       bool authenticated() { return _authenticated; }
49 
50       /// Gets the message of the authentication status.
51       string message() { return _message; }
52     }
53   }
54 
55   /// Interface to implement authentication.
56   interface IControllerAuth
57   {
58     /**
59     * Function called to validate authentication for a client.
60     * Params:
61     *   client =   The client to validate for authentication.
62     * Returns:
63     *   True if the client is authenticated.
64     */
65     AuthStatus isAuthenticated(HttpClient client);
66 
67     /**
68     * Function called when authentication fails.
69     * Params:
70     *   status = The status of the failed authentication.
71     */
72     void authenticationFailed(AuthStatus status);
73   }
74 
75   // TODO: Implement basic auth + digest auth wrappers.
76 }