Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 5deb0a1

Browse files
[ENHANCEMENT]Logging out by user card (#486)
* Create draft PR for #468 * user can now deauthenitcate reading same token when currently authenticated Co-authored-by: PTKu <PTKu@users.noreply.github.com> Co-authored-by: Peter <61538034+PTKu@users.noreply.github.com>
1 parent f48c03a commit 5deb0a1

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/TcOpen.Inxton/src/Security/AuthenticationService.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,22 @@ public void ChangeToken(string userName, string token)
7878
}
7979
}
8080

81-
private User ExternalAuthorization_AuthorizationRequest(string token)
81+
private IUser ExternalAuthorization_AuthorizationRequest(string token)
8282
{
83-
return this.AuthenticateUser(token);
83+
var userName = TcOpen.Inxton.Local.Security.SecurityManager.Manager.Principal.Identity.Name;
84+
var currentUser = _users.FirstOrDefault(u => u.Username.Equals(userName));
85+
86+
// De authenticate when the token matches the token of currently authenticated user.
87+
if (currentUser != null && this.CalculateHash(token, string.Empty) == currentUser.AuthenticationToken)
88+
{
89+
this.DeAuthenticateCurrentUser();
90+
return null;
91+
}
92+
else
93+
{
94+
var authenticatedUser = this.AuthenticateUser(token);
95+
return authenticatedUser;
96+
}
8497
}
8598

8699
public IRepository<UserData> UserRepository { get; private set; }
@@ -163,7 +176,7 @@ public User AuthenticateUser(string token)
163176
}
164177

165178
VerifyRolesHash(userData);
166-
179+
167180
return AuthenticateUser(userData);
168181
}
169182

src/TcOpen.Inxton/tests/TcOpen.Inxton.Local.Security/TcOpen.Inxton.Security.Tests/AuthenticationServiceTests.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,37 @@ public void AuthenticateWithToken()
351351
Assert.AreEqual(roles[0], actual.Roles[0]);
352352
}
353353

354+
[Test()]
355+
public void DeAuthenticateWithToken()
356+
{
357+
//-- Arrange
358+
var userName = "UserTokenAuthenticationDeauthWithToken";
359+
var password = "token";
360+
var roles = new string[] { "Tester" };
361+
var token = "usersToken-UserTokenAuthenticationDeauthWithToken";
362+
363+
authService.ExternalAuthorization = new ExternalAuthenticator() { Token = token };
364+
365+
authService.UserRepository.Create(userName, new UserData(userName, password, roles.ToList()));
366+
367+
authService.AuthenticateUser(userName, password);
368+
369+
authService.ExternalAuthorization.RequestTokenChange(token);
370+
371+
authService.DeAuthenticateCurrentUser();
372+
373+
var actual = authService.ExternalAuthorization.RequestAuthorization(token);
374+
375+
Assert.AreEqual(userName, actual.UserName);
376+
Assert.AreEqual(1, roles.Length);
377+
Assert.AreEqual(roles[0], actual.Roles[0]);
378+
379+
actual = authService.ExternalAuthorization.RequestAuthorization(token);
380+
381+
Assert.IsNull(actual);
382+
Assert.AreEqual(typeof(AnonymousIdentity), TcOpen.Inxton.Local.Security.SecurityManager.Manager.Principal.Identity.GetType());
383+
}
384+
354385
[Test()]
355386
public void AuthenticateWithInexistingToken()
356387
{
@@ -372,10 +403,13 @@ public void AuthenticateWithInexistingToken()
372403

373404
authService.DeAuthenticateCurrentUser();
374405

375-
externalAuthorization.Token = "fjalsdjl";
406+
407+
var inexistingToken = "fjalsdjl";
408+
409+
externalAuthorization.Token = inexistingToken;
376410

377411

378-
authService.ExternalAuthorization.RequestAuthorization(token);
412+
authService.ExternalAuthorization.RequestAuthorization(inexistingToken);
379413

380414
AppPrincipal customPrincipal = Thread.CurrentPrincipal as AppPrincipal;
381415

@@ -440,6 +474,7 @@ public void AddExistingToken()
440474

441475
}
442476

477+
443478
public class ExternalAuthenticator : ExternalAuthorization
444479
{
445480

0 commit comments

Comments
 (0)