-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement JWT management API * PR fixes
- Loading branch information
1 parent
0e51344
commit 9d81a36
Showing
7 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using Xunit; | ||
|
||
namespace Descope.Test.Integration | ||
{ | ||
public class JwtTests | ||
{ | ||
private readonly DescopeClient _descopeClient = IntegrationTestSetup.InitDescopeClient(); | ||
|
||
[Fact] | ||
public async Task Jwt_CustomClaims() | ||
{ | ||
string? loginId = null; | ||
try | ||
{ | ||
// Create a logged in test user | ||
var testUser = await IntegrationTestSetup.InitTestUser(_descopeClient); | ||
loginId = testUser.User.LoginIds.First(); | ||
|
||
var updateJwt = await _descopeClient.Management.Jwt.UpdateJwtWithCustomClaims(testUser.AuthInfo.SessionJwt, new Dictionary<string, object> { { "a", "b" } }); | ||
|
||
// Make sure the session is valid | ||
var token = await _descopeClient.Auth.ValidateSession(updateJwt); | ||
Assert.Contains("a", token.Claims.Keys); | ||
Assert.Equal("b", token.Claims["a"]); | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(loginId)) | ||
{ | ||
try { await _descopeClient.Management.User.Delete(loginId); } | ||
catch { } | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Jwt_Impersonate() | ||
{ | ||
string? loginId = null; | ||
string? loginId2 = null; | ||
string? roleName = null; | ||
try | ||
{ | ||
// Create a role that can impersonate | ||
roleName = Guid.NewGuid().ToString()[..20]; | ||
await _descopeClient.Management.Role.Create(roleName, permissionNames: new List<string> { "Impersonate" }); | ||
|
||
// Create impersonating user | ||
loginId = Guid.NewGuid().ToString(); | ||
var response = await _descopeClient.Management.User.Create(loginId: loginId, new UserRequest() | ||
{ | ||
Phone = "+972555555555", | ||
RoleNames = new List<string> { roleName }, | ||
}); | ||
var userId1 = response.UserId; | ||
|
||
// Create the target user | ||
loginId2 = Guid.NewGuid().ToString(); | ||
response = await _descopeClient.Management.User.Create(loginId: loginId2, new UserRequest() | ||
{ | ||
Phone = "+972666666666", | ||
}); | ||
var userId2 = response.UserId; | ||
|
||
// Have user1 impersonate user2 | ||
var jwt = await _descopeClient.Management.Jwt.Impersonate(userId1, loginId2); | ||
|
||
// Validate the impersonation data | ||
var token = await _descopeClient.Auth.ValidateSession(jwt); | ||
Assert.Equal(userId2, token.Id); | ||
Assert.Contains(userId1, token.Claims["act"].ToString()); | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(roleName)) | ||
{ | ||
try { await _descopeClient.Management.Role.Delete(roleName); } | ||
catch { } | ||
} | ||
if (!string.IsNullOrEmpty(loginId)) | ||
{ | ||
try { await _descopeClient.Management.User.Delete(loginId); } | ||
catch { } | ||
} | ||
if (!string.IsNullOrEmpty(loginId2)) | ||
{ | ||
try { await _descopeClient.Management.User.Delete(loginId2); } | ||
catch { } | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Descope.Internal.Management | ||
{ | ||
internal class Jwt : IJwt | ||
{ | ||
private readonly IHttpClient _httpClient; | ||
private readonly string _managementKey; | ||
|
||
internal Jwt(IHttpClient httpClient, string managementKey) | ||
{ | ||
_httpClient = httpClient; | ||
_managementKey = managementKey; | ||
} | ||
|
||
public async Task<string> UpdateJwtWithCustomClaims(string jwt, Dictionary<string, object> customClaims) | ||
{ | ||
if (string.IsNullOrEmpty(jwt)) throw new DescopeException("JWT is required to update custom claims"); | ||
var body = new { jwt, customClaims }; | ||
var response = await _httpClient.Post<SimpleJwtResponse>(Routes.JwtUpdate, _managementKey, body); | ||
return response.Jwt; | ||
} | ||
|
||
public async Task<string> Impersonate(string impersonatorId, string loginId, bool validateConcent) | ||
{ | ||
if (string.IsNullOrEmpty(impersonatorId)) throw new DescopeException("impersonatorId is required to impersonate"); | ||
if (string.IsNullOrEmpty(loginId)) throw new DescopeException("impersonatorId is required to impersonate"); | ||
var body = new { impersonatorId, loginId, validateConcent }; | ||
var response = await _httpClient.Post<SimpleJwtResponse>(Routes.Impersonate, _managementKey, body); | ||
return response.Jwt; | ||
} | ||
|
||
} | ||
|
||
internal class SimpleJwtResponse | ||
{ | ||
[JsonPropertyName("jwt")] | ||
public string Jwt { get; set; } | ||
|
||
public SimpleJwtResponse(string jwt) | ||
{ | ||
Jwt = jwt; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters