This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathOAuthClientController.cs
More file actions
253 lines (225 loc) · 8.02 KB
/
OAuthClientController.cs
File metadata and controls
253 lines (225 loc) · 8.02 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using Commons;
using Commons.Collections;
using Commons.Enums;
using Commons.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace WebAPI.Controllers
{
/// <summary>
/// CRUD Controller for OAuthClient resource
/// </summary>
[ApiVersion("1")]
[Route("oauth_client")]
[ApiController]
public class OAuthClientController : Controller
{
private readonly ILogger<OAuthClientController> _logger;
private OAuthClientCollection _oAuthClientCollection = new OAuthClientCollection();
public OAuthClientController(ILogger<OAuthClientController> logger)
{
_logger = logger;
}
/// <summary>
/// Retrieves a specific OAuthClient by id
/// </summary>
/// <param name="id">The OAuthClient id</param>
[AllowAnonymous]
[EnableCors("CorsEveryone")]
[HttpGet("{id}"), MapToApiVersion("1")]
public APIResponse GetOne(long id)
{
try
{
OAuthClient client = this._oAuthClientCollection.Get(id);
if (client == null)
{
throw new APIException(HttpStatusCode.NotFound,
"Client not found",
$"Client with id {id} does not exists");
}
client.ClientSecret = Guid.Empty;
return APIManager.SuccessResponse("Client found", client);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
[AllowAnonymous]
[EnableCors("CorsEveryone")]
[HttpGet, MapToApiVersion("1")]
public APIResponse GetMore([FromQuery] OAuthClientFilter filter)
{
try
{
Paging<OAuthClient> result = this._oAuthClientCollection.GetList<OAuthClientFilter>(filter);
if (result.LastPage == 0)
{
throw new APIException(HttpStatusCode.NotFound,
"Zero clients found",
"");
}
if (filter.page > result.LastPage)
{
throw new APIException(HttpStatusCode.NotFound,
"Page out of range",
$"Last page number available is {result.LastPage}");
}
foreach (OAuthClient c in result.Documents)
{
c.ClientSecret = Guid.Empty;
}
return APIManager.SuccessResponse($"Page {result.CurrentPage} contains {result.Documents.Count} clients. Last page number is {result.LastPage} for a total of {result.Count} clients", result);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
/// <summary>
/// Create a new OAuthClient
/// </summary>
/// <param name="model">The OAuthClient model</param>
[Authorize]
#if DEBUG
[EnableCors("CorsEveryone")]
#else
[EnableCors("CorsInternal")]
#endif
[HttpPut, MapToApiVersion("1")]
[ApiExplorerSettings(IgnoreApi = true)]
public APIResponse Create([FromBody] OAuthClient model)
{
try
{
User authenticatedUser = (User)HttpContext.Items["user"];
if (string.IsNullOrEmpty(model.Name))
{
throw new APIException(HttpStatusCode.BadRequest,
"Missing name",
"Please provide a valid name");
}
model.UserID = authenticatedUser.Id;
model.ClientSecret = Guid.NewGuid();
this._oAuthClientCollection.Add(ref model);
return APIManager.SuccessResponse("OAuth client created", model);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
/// <summary>
/// Update an existing OAuthClient
/// </summary>
/// <param name="model">The OAuthClient model</param>
[Authorize]
#if DEBUG
[EnableCors("CorsEveryone")]
#else
[EnableCors("CorsInternal")]
#endif
[HttpPost, MapToApiVersion("1")]
[ApiExplorerSettings(IgnoreApi = true)]
public APIResponse Update([FromBody] OAuthClient model)
{
try
{
User authenticatedUser = (User)HttpContext.Items["user"];
if (authenticatedUser.Role == UserRoleEnum.BASIC && authenticatedUser.Id != model.UserID)
{
throw new APIException(HttpStatusCode.Forbidden,
"Forbidden",
"You have no access rights to edit this client");
}
if (!this._oAuthClientCollection.Exists(ref model, false))
{
throw new APIException(HttpStatusCode.NotFound,
"Client not found",
$"Client with id {model.Id} does not exists");
}
OAuthClient client = this._oAuthClientCollection.Get(model.Id);
client.Name = model.Name;
client.RedirectURI = model.RedirectURI;
this._oAuthClientCollection.Edit(ref client);
return APIManager.SuccessResponse("Client updated", client);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
/// <summary>
/// Delete an existing OAuthClient by id
/// </summary>
/// <param name="id">The OAuthClient id</param>
[Authorize]
#if DEBUG
[EnableCors("CorsEveryone")]
#else
[EnableCors("CorsInternal")]
#endif
[HttpDelete("{id}"), MapToApiVersion("1")]
[ApiExplorerSettings(IgnoreApi = true)]
public APIResponse Delete(long id)
{
try
{
User authenticatedUser = (User)HttpContext.Items["user"];
OAuthClient client = this._oAuthClientCollection.Get(id);
if(client == null)
{
throw new APIException(HttpStatusCode.NotFound,
"Client not found",
$"Client with id {id} does not exists");
}
if (authenticatedUser.Role == UserRoleEnum.BASIC && authenticatedUser.Id != client.UserID)
{
throw new APIException(HttpStatusCode.Forbidden,
"Forbidden",
"You have no access rights to delete this client");
}
this._oAuthClientCollection.Delete(id);
return APIManager.SuccessResponse("Client deleted");
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
}
}