@@ -19,6 +19,7 @@ public class AzureSphereAPI
1919 private static readonly Uri AzureSphereApiEndpoint = new Uri ( "https://prod.core.sphere.azure.net/" ) ;
2020
2121 private string AccessToken = string . Empty ;
22+ public string Username { get ; private set ; } = string . Empty ;
2223
2324 public async Task AuthenticationAsync ( CancellationToken cancellationToken )
2425 {
@@ -33,6 +34,7 @@ public async Task AuthenticationAsync(CancellationToken cancellationToken)
3334 . ExecuteAsync ( ) ;
3435
3536 AccessToken = authResult . AccessToken ;
37+ Username = authResult . Account . Username ;
3638 }
3739
3840 public async Task < List < AzureSphereTenant > > GetTenantsAsync ( CancellationToken cancellationToken )
@@ -51,6 +53,26 @@ public async Task<List<AzureSphereTenant>> GetTenantsAsync(CancellationToken can
5153 return tenants ;
5254 }
5355
56+ public async Task < List < string > > GetRolesAsync ( AzureSphereTenant tenant , string username , CancellationToken cancellationToken )
57+ {
58+ var jsonString = await GetAsync ( $ "v2/tenants/{ tenant . Id } /users/{ username } /role", cancellationToken ) ;
59+ Console . WriteLine ( "GetRolesAsync()" ) ;
60+ Console . WriteLine ( jsonString ) ;
61+ var json = JToken . Parse ( jsonString ) ;
62+ var jsonRoles = json . Value < JArray > ( "Roles" ) ;
63+ if ( jsonRoles . Count != 1 ) throw new ApplicationException ( ) ;
64+ if ( jsonRoles [ 0 ] . Value < string > ( "TenantId" ) != tenant . Id ) throw new ApplicationException ( ) ;
65+ var jsonRoleNames = jsonRoles [ 0 ] [ "RoleNames" ] ;
66+
67+ var roleNames = new List < string > ( ) ;
68+ foreach ( var jsonRoleName in jsonRoleNames )
69+ {
70+ roleNames . Add ( jsonRoleName . Value < string > ( ) ) ;
71+ }
72+
73+ return roleNames ;
74+ }
75+
5476 public async Task < List < AzureSphereProduct > > GetProductsAsync ( AzureSphereTenant tenant , CancellationToken cancellationToken )
5577 {
5678 var jsonString = await GetAsync ( $ "v2/tenants/{ tenant . Id } /products", cancellationToken ) ;
0 commit comments