summaryrefslogtreecommitdiffstats
path: root/ComicRackWebViewer/Modules/AuthModule.cs
blob: 90d9d346430878bb4d86092d321c296c7cdc6128 (plain)
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
using Nancy;

namespace BCR
{
    public class AuthModule : NancyModule
    {
        public AuthModule() 
          : base(Database.Instance.GlobalSettings.url_base + "/auth")
        {
            ///////////////////////////////////////////////////////////////////////////////////////////
            // Login
            // The Post["/"] method returns the api key for subsequent REST calls.
            Post["/"] = x =>
                {
                    string apiKey = UserDatabase.LoginUser((string) this.Request.Form.Username,
                                                           (string) this.Request.Form.Password);

                    return string.IsNullOrEmpty(apiKey)
                               ? new Response {StatusCode = HttpStatusCode.Unauthorized}
                               : this.Response.AsJson(new {ApiKey = apiKey});
                };

            ///////////////////////////////////////////////////////////////////////////////////////////
            // Logout
            // Destroy the api key.
            Delete["/"] = x =>
                {
                    var apiKey = (string) this.Request.Form.ApiKey;
                    UserDatabase.RemoveApiKey(apiKey);
                    return new Response {StatusCode = HttpStatusCode.OK};
                };
        }
    }
}