summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_auth_backend_http/examples/rabbitmq_auth_backend_webapi_dotnet/Controllers/AuthController.cs
blob: 8282d1407e1e6b1fe87717e64b3c1c0ebb93c2f0 (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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Text;
using System.Web.Http;

namespace WebApiHttpAuthService.Controllers
{
    [RoutePrefix("auth")]
    public class AuthController : ApiController
    {
        // Note: the following is necessary to ensure that no
        // BOM is part of the response
        private static readonly UTF8Encoding encoding = new UTF8Encoding(false);

        [Route("user")]
        [HttpPost]
        public HttpResponseMessage user(FormDataCollection form)
        {
            string content = "allow administrator management";
            try
            {
                if (form != null)
                {
                    string username = form.Get("username");
                    string password = form.Get("password");

                    if(username=="authuser") //Sample check you can put your custom logic over here
                        content = "deny";

                    string userlog = string.Format("user :{0}, password :{1}", username, password);
                }
            }
            catch(Exception ex)
            {
                //check or log error
            }

            var resp = new HttpResponseMessage(HttpStatusCode.OK);
            resp.Content = new StringContent(content, encoding, "text/plain");
            return resp;
        }

        [Route("vhost")]
        [HttpPost]
        public HttpResponseMessage vhost(FormDataCollection form)
        {
            string content = "allow";
            try
            {
                if (form != null)
                {
                    string username = form.Get("username");
                    string ip = form.Get("ip");

                    if (username == "authuser") //Sample checks you can put your custom logic over here
                        content = "deny";

                    string userlog = string.Format("user :{0}, ip :{1}", username, ip);
                }
            }
            catch (Exception ex)
            {
                //check or log error
            }

            var resp = new HttpResponseMessage(HttpStatusCode.OK);
            resp.Content = new StringContent(content, encoding, "text/plain");
            return resp;
        }

        [Route("resource")]
        [HttpPost]
        public HttpResponseMessage resource(FormDataCollection form)
        {
            string content = "allow";

            try
            {
                if (form != null)
                {
                    string username = form.Get("username");
                    string vhost = form.Get("vhost");
                    string resource = form.Get("resource");
                    string name = form.Get("name");
                    string permission = form.Get("permission");

                    if (username == "authuser") //Sample checks you can put your custom logic over here
                        content = "deny";

                    string userlog = string.Format("user :{0}, vhost :{1}, resource :{2}, name: {3}, permission: {4}", username, vhost, resource, name, permission);
                   
                }
            }
            catch (Exception ex)
            {
                //check or log error
            }


            var resp = new HttpResponseMessage(HttpStatusCode.OK);
            resp.Content = new StringContent(content, encoding, "text/plain");
            return resp;
        }

        [Route("topic")]
        [HttpPost]
        public HttpResponseMessage topic(FormDataCollection form)
        {
            string content = "allow";
            try
            {
                if (form != null)
                {
                    string username = form.Get("username");
                    string vhost = form.Get("vhost");
                    string resource = form.Get("resource");
                    string name = form.Get("name");
                    string permission = form.Get("permission");
                    string routing_key = form.Get("routing_key");

                    if (username == "authuser") //Sample checks you can put your custom logic over here
                        content = "deny";

                    string userlog = string.Format("user :{0}, vhost :{1}, resource :{2}, name: {3}, permission: {4}, routing_key :{5}", username, vhost, resource, name, permission, routing_key);

                }
            }
            catch (Exception ex)
            {
                //check or log error
            }

            var resp = new HttpResponseMessage(HttpStatusCode.OK);
            resp.Content = new StringContent(content, encoding, "text/plain");
            return resp;
        }
                       


    }
}