Skip to content

Commit 9e3e47e

Browse files
committed
Merge branch 'release/v1.1.0'
2 parents 640d9a0 + 332f17f commit 9e3e47e

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

Wissance.Authorization/Wissance.Authorization.Tests/Checkers/UserInfoChecker.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ internal static class UserInfoChecker
1010
{
1111
public static void Check(UserInfo expected, UserInfo actual)
1212
{
13+
Assert.Equal(expected.UserId, actual.UserId);
1314
Assert.Equal(expected.UserName, actual.UserName);
1415
Assert.Equal(expected.FullName, actual.FullName);
1516
Assert.Equal(expected.Email, actual.Email);
1617
Assert.Equal(expected.IsEmailVerified, actual.IsEmailVerified);
18+
if (expected.Roles == null)
19+
{
20+
Assert.Null(actual.Roles);
21+
}
1722
}
1823
}
1924
}

Wissance.Authorization/Wissance.Authorization.Tests/OpenId/TestKeyCloakOpenIdAuthenticator.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,26 @@ public void TestPrivateClientAuthentication(string userName, string password, st
5252
}
5353

5454
[Theory]
55-
[InlineData(TestUser, TestPassword, TestScope)]
56-
public void TestGetUserInfo(string userName, string password, string scope)
55+
[InlineData(TestUser, TestPassword, TestScope, true, true)]
56+
[InlineData(TestUser, TestPassword, TestScope, false, false)]
57+
public void TestGetUserInfo(string userName, string password, string scope, bool isPrivateClient, bool userIdExists)
5758
{
58-
IOpenIdAuthenticator authenticator = new KeyCloakOpenIdAuthenticator(_testPrivateKeyCloakConfig, new LoggerFactory());
59+
KeyCloakServerConfig config = isPrivateClient ? _testPrivateKeyCloakConfig : _testPublicKeyCloakConfig;
60+
IOpenIdAuthenticator authenticator = new KeyCloakOpenIdAuthenticator(config, new LoggerFactory());
5961
TokenInfo token = GetToken(authenticator, userName, password, scope);
6062
Assert.NotNull(token);
6163
Task<UserInfo> getUserInfoTask = authenticator.GetUserInfoAsync(token.AccessToken, token.TokenType);
6264
getUserInfoTask.Wait();
6365
UserInfo actualUserInfo = getUserInfoTask.Result;
6466
Assert.NotNull(actualUserInfo);
65-
UserInfo expectedUserInfo = new UserInfo(actualUserInfo.Session, TestUser, "firstTestName lastTestName",
67+
UserInfo expectedUserInfo = new UserInfo(actualUserInfo.UserId, actualUserInfo.Session, TestUser, "firstTestName lastTestName",
6668
null, false, null);
6769
UserInfoChecker.Check(expectedUserInfo, actualUserInfo);
70+
71+
if (userIdExists)
72+
{
73+
Assert.NotNull(expectedUserInfo.UserId);
74+
}
6875
}
6976

7077
[Theory]

Wissance.Authorization/Wissance.Authorization/Authentication/OpenIdAuthenticationHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
6060
{
6161
new Claim(ClaimTypes.NameIdentifier, userInfo.UserName),
6262
new Claim(ClaimTypes.Name, userInfo.FullName)
63-
//new Claim(ClaimTypes.Email, null),
6463
};
6564

65+
if (userInfo.UserId != null)
66+
{
67+
claims.Add(new Claim(Wissance.Authorization.Claims.ClaimTypes.UserId, userInfo.UserId));
68+
}
69+
6670
if (userInfo.Email != null)
6771
claims.Add(new Claim(ClaimTypes.Email, userInfo.Email));
6872
// todo: umv: add Custom claims (EmailVerified & so on )
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Wissance.Authorization.Claims
2+
{
3+
public class ClaimTypes
4+
{
5+
public const string UserId = "id";
6+
}
7+
}

Wissance.Authorization/Wissance.Authorization/Data/UserInfo.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ public UserInfo()
1010
{
1111
}
1212

13-
public UserInfo(string session, string userName, string fullName, string[] roles, bool isEmailVerified, string email)
13+
public UserInfo(string userId, string session, string userName, string fullName, string[] roles, bool isEmailVerified, string email)
1414
{
15+
UserId = userId;
1516
Session = session;
1617
UserName = userName;
1718
FullName = fullName;
1819
Roles = roles;
1920
IsEmailVerified = isEmailVerified;
2021
Email = email;
2122
}
22-
23+
24+
public string UserId { get; set; }
2325
public string Session { get; set; }
2426
public string UserName { get; set; }
2527
public string FullName { get; set; }

Wissance.Authorization/Wissance.Authorization/Dto/KeyCloak/UserInfoDto.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ public UserInfoDto()
1212

1313
}
1414

15-
public UserInfoDto(string sub, bool isEmailVerified, string[] roles, string name, string userName,
15+
public UserInfoDto(string id, string sub, bool isEmailVerified, string[] roles, string name, string userName,
1616
string firstName, string lastName)
1717
{
18+
Id = id;
1819
Sub = sub;
1920
IsEmailVerified = isEmailVerified;
2021
Roles = roles;
@@ -23,6 +24,9 @@ public UserInfoDto(string sub, bool isEmailVerified, string[] roles, string name
2324
FirstName = firstName;
2425
LastName = lastName;
2526
}
27+
28+
[JsonProperty("id")]
29+
public string Id { get; set; }
2630

2731
[JsonProperty("sub")]
2832
public string Sub { get; set; }

Wissance.Authorization/Wissance.Authorization/OpenId/KeyCloakOpenIdAuthenticator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public async Task<UserInfo> GetUserInfoAsync(string accessToken, string tokenTyp
5252
}
5353

5454
// todo: umv: add pass and parse e-mail to userinfo
55-
return new UserInfo(kcUserInfo.Sub, kcUserInfo.UserName, kcUserInfo.Name, kcUserInfo.Roles, kcUserInfo.IsEmailVerified, null);
55+
return new UserInfo(kcUserInfo.Id, kcUserInfo.Sub, kcUserInfo.UserName, kcUserInfo.Name,
56+
kcUserInfo.Roles, kcUserInfo.IsEmailVerified, null);
5657
}
5758
}
5859
catch (Exception ex)

0 commit comments

Comments
 (0)