Skip to content

Commit 6e57453

Browse files
[Fix] not IsActive or IsDeprecated returns unauthorized response (Fixes #212)
1 parent de61329 commit 6e57453

File tree

3 files changed

+93
-18
lines changed

3 files changed

+93
-18
lines changed

CDP4Authentication/AuthenticationPerson.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
// --------------------------------------------------------------------------------------------------------------------
22
// <copyright file="AuthenticationPerson.cs" company="RHEA System S.A.">
3-
// Copyright (c) 2016 RHEA System S.A.
3+
// Copyright (c) 2015-2021 RHEA System S.A.
4+
//
5+
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft.
6+
//
7+
// This file is part of CDP4 Web Services Community Edition.
8+
// The CDP4 Web Services Community Edition is the RHEA implementation of ECSS-E-TM-10-25 Annex A and Annex C.
9+
// This is an auto-generated class. Any manual changes to this file will be overwritten!
10+
//
11+
// The CDP4 Web Services Community Edition is free software; you can redistribute it and/or
12+
// modify it under the terms of the GNU Affero General Public
13+
// License as published by the Free Software Foundation; either
14+
// version 3 of the License, or (at your option) any later version.
15+
//
16+
// The CDP4 Web Services Community Edition is distributed in the hope that it will be useful,
17+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
// Lesser General Public License for more details.
20+
//
21+
// You should have received a copy of the GNU Affero General Public License
22+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
423
// </copyright>
524
// --------------------------------------------------------------------------------------------------------------------
625

@@ -53,6 +72,11 @@ public AuthenticationPerson(Guid iid, int revisionNumber)
5372
/// </summary>
5473
public bool IsActive { get; set; }
5574

75+
/// <summary>
76+
/// Gets or sets a value indicating whether is deprecated.
77+
/// </summary>
78+
public bool IsDeprecated { get; set; }
79+
5680
/// <summary>
5781
/// Gets or sets the salt.
5882
/// </summary>

CDP4Orm/Dao/Authentication/AuthenticationDao.cs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
// --------------------------------------------------------------------------------------------------------------------
22
// <copyright file="AuthenticationDao.cs" company="RHEA System S.A.">
3-
// Copyright (c) 2016 RHEA System S.A.
3+
// Copyright (c) 2015-2021 RHEA System S.A.
4+
//
5+
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft.
6+
//
7+
// This file is part of CDP4 Web Services Community Edition.
8+
// The CDP4 Web Services Community Edition is the RHEA implementation of ECSS-E-TM-10-25 Annex A and Annex C.
9+
// This is an auto-generated class. Any manual changes to this file will be overwritten!
10+
//
11+
// The CDP4 Web Services Community Edition is free software; you can redistribute it and/or
12+
// modify it under the terms of the GNU Affero General Public
13+
// License as published by the Free Software Foundation; either
14+
// version 3 of the License, or (at your option) any later version.
15+
//
16+
// The CDP4 Web Services Community Edition is distributed in the hope that it will be useful,
17+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
// Lesser General Public License for more details.
20+
//
21+
// You should have received a copy of the GNU Affero General Public License
22+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
423
// </copyright>
524
// --------------------------------------------------------------------------------------------------------------------
625

@@ -74,37 +93,38 @@ public IEnumerable<AuthenticationPerson> Read(NpgsqlTransaction transaction, str
7493
/// </returns>
7594
private AuthenticationPerson MapToDto(NpgsqlDataReader reader)
7695
{
77-
string tempIsActive;
78-
string tempPassword;
79-
string tempSalt;
80-
string tempShortName;
81-
8296
var valueDict = (Dictionary<string, string>)reader["ValueTypeSet"];
8397
var iid = Guid.Parse(reader["Iid"].ToString());
8498
var revisionNumber = int.Parse(valueDict["RevisionNumber"]);
85-
86-
var dto = new AuthenticationPerson(iid, revisionNumber);
8799

88-
dto.Role = reader["Role"] is DBNull ? (Guid?)null : Guid.Parse(reader["Role"].ToString());
89-
dto.DefaultDomain = reader["DefaultDomain"] is DBNull? (Guid?)null : Guid.Parse(reader["DefaultDomain"].ToString());
90-
dto.Organization = reader["Organization"] is DBNull ? (Guid?)null : Guid.Parse(reader["Organization"].ToString());
100+
var dto = new AuthenticationPerson(iid, revisionNumber)
101+
{
102+
Role = reader["Role"] is DBNull ? (Guid?) null : Guid.Parse(reader["Role"].ToString()),
103+
DefaultDomain = reader["DefaultDomain"] is DBNull ? (Guid?) null : Guid.Parse(reader["DefaultDomain"].ToString()),
104+
Organization = reader["Organization"] is DBNull ? (Guid?) null : Guid.Parse(reader["Organization"].ToString())
105+
};
91106

92-
if (valueDict.TryGetValue("IsActive", out tempIsActive))
107+
if (valueDict.TryGetValue("IsActive", out var tempIsActive))
93108
{
94109
dto.IsActive = bool.Parse(tempIsActive);
95110
}
96111

97-
if (valueDict.TryGetValue("Password", out tempPassword) && !string.IsNullOrEmpty(tempPassword))
112+
if (valueDict.TryGetValue("IsDeprecated", out var tempIsDeprecated))
113+
{
114+
dto.IsDeprecated = bool.Parse(tempIsDeprecated);
115+
}
116+
117+
if (valueDict.TryGetValue("Password", out var tempPassword) && !string.IsNullOrEmpty(tempPassword))
98118
{
99119
dto.Password = tempPassword.UnEscape();
100120
}
101121

102-
if (valueDict.TryGetValue("Salt", out tempSalt))
122+
if (valueDict.TryGetValue("Salt", out var tempSalt))
103123
{
104124
dto.Salt = tempSalt.UnEscape();
105125
}
106126

107-
if (valueDict.TryGetValue("ShortName", out tempShortName))
127+
if (valueDict.TryGetValue("ShortName", out var tempShortName))
108128
{
109129
// map shortname to UserName
110130
dto.UserName = tempShortName.UnEscape();

CDP4WebServices.API/Modules/10-25/ApiBase.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// --------------------------------------------------------------------------------------------------------------------
22
// <copyright file="ApiBase.cs" company="RHEA System S.A.">
3-
// Copyright (c) 2015-2019 RHEA System S.A.
3+
// Copyright (c) 2015-2021 RHEA System S.A.
44
//
55
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft.
66
//
@@ -33,7 +33,6 @@ namespace CDP4WebServices.API.Modules
3333
using System.Net.Http;
3434
using System.Security.Cryptography;
3535
using System.Text;
36-
using System.Threading.Tasks;
3736

3837
using CDP4Common.DTO;
3938

@@ -384,6 +383,12 @@ protected virtual Response GetResponse(dynamic routeParams)
384383
{
385384
// wireup cdp authorization support
386385
this.CdpAuthorization();
386+
387+
if (!this.IsAuthorized())
388+
{
389+
return this.GetUnauthorizedResponse();
390+
}
391+
387392
var response = this.GetResponseData(routeParams);
388393

389394
// Register the required CDP4 headers to every response send
@@ -415,6 +420,12 @@ protected virtual Response PostResponse(dynamic routeParams)
415420
{
416421
// wireup cdp authorization support
417422
this.CdpAuthorization();
423+
424+
if (!this.IsAuthorized())
425+
{
426+
return this.GetUnauthorizedResponse();
427+
}
428+
418429
var response = this.PostResponseData(routeParams);
419430

420431
this.HeaderInfoProvider.RegisterResponseHeaders(response);
@@ -495,6 +506,26 @@ protected Response GetJsonResponse(
495506
};
496507
}
497508

509+
/// <summary>
510+
/// Checks if the user is authorized to perform reads or writes to the data store
511+
/// </summary>
512+
/// <returns>True is the user is authorized, otherwise false.</returns>
513+
protected bool IsAuthorized()
514+
{
515+
var credentials = this.RequestUtils.Context.AuthenticatedCredentials;
516+
517+
return credentials.Person.IsActive && !credentials.Person.IsDeprecated;
518+
}
519+
520+
/// <summary>
521+
/// Gets the default Unauthorized <see cref="Response"/>
522+
/// </summary>
523+
/// <returns>The <see cref="Response"/></returns>
524+
protected Response GetUnauthorizedResponse()
525+
{
526+
return HttpStatusCode.Unauthorized;
527+
}
528+
498529
/// <summary>
499530
/// Create a multipart response for the included file revisions.
500531
/// </summary>

0 commit comments

Comments
 (0)