From 96b78a990111ccab2dd806d2aa38e6dba574231b Mon Sep 17 00:00:00 2001 From: Nate Barbettini Date: Tue, 11 Jul 2017 16:25:21 -0700 Subject: [PATCH] Add groups expansion back (closes #91) --- .../Model/MeGroupModel.cs | 19 ++++++++++++ .../Model/MeGroupsCollectionModel.cs | 9 ++++++ .../Model/MeResponseModel.cs | 2 +- .../Route/MeRoute.cs | 30 +++++++++++++++---- .../Stormpath.Owin.Middleware.csproj | 2 +- 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 src/Stormpath.Owin.Middleware/Model/MeGroupModel.cs create mode 100644 src/Stormpath.Owin.Middleware/Model/MeGroupsCollectionModel.cs diff --git a/src/Stormpath.Owin.Middleware/Model/MeGroupModel.cs b/src/Stormpath.Owin.Middleware/Model/MeGroupModel.cs new file mode 100644 index 0000000..c4c0b19 --- /dev/null +++ b/src/Stormpath.Owin.Middleware/Model/MeGroupModel.cs @@ -0,0 +1,19 @@ +using System; + +namespace Stormpath.Owin.Middleware.Model +{ + public sealed class MeGroupModel + { + public string Id { get; set; } + + public string Name { get; set; } + + public string Description { get; set; } + + public string Status { get; } = "ENABLED"; // Groups in Okta are always enabled + + public DateTimeOffset? CreatedAt { get; set; } + + public DateTimeOffset? ModifiedAt { get; set; } + } +} diff --git a/src/Stormpath.Owin.Middleware/Model/MeGroupsCollectionModel.cs b/src/Stormpath.Owin.Middleware/Model/MeGroupsCollectionModel.cs new file mode 100644 index 0000000..0f627b3 --- /dev/null +++ b/src/Stormpath.Owin.Middleware/Model/MeGroupsCollectionModel.cs @@ -0,0 +1,9 @@ +namespace Stormpath.Owin.Middleware.Model +{ + public sealed class MeGroupsCollectionModel + { + public int Size { get; set; } + + public MeGroupModel[] Items { get; set; } + } +} diff --git a/src/Stormpath.Owin.Middleware/Model/MeResponseModel.cs b/src/Stormpath.Owin.Middleware/Model/MeResponseModel.cs index 928c71f..3838c6a 100644 --- a/src/Stormpath.Owin.Middleware/Model/MeResponseModel.cs +++ b/src/Stormpath.Owin.Middleware/Model/MeResponseModel.cs @@ -40,7 +40,7 @@ public sealed class MeResponseModel public IDictionary CustomData { get; set; } = null; [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] - public object Groups { get; set; } = null; + public MeGroupsCollectionModel Groups { get; set; } = null; [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public object ProviderData { get; set; } = null; diff --git a/src/Stormpath.Owin.Middleware/Route/MeRoute.cs b/src/Stormpath.Owin.Middleware/Route/MeRoute.cs index e1c1d4c..ee6d48c 100644 --- a/src/Stormpath.Owin.Middleware/Route/MeRoute.cs +++ b/src/Stormpath.Owin.Middleware/Route/MeRoute.cs @@ -41,14 +41,15 @@ protected override async Task GetJsonAsync(IOwinEnvironment context, Cance var responseModel = new { - account = await ExpandAccount(stormpathAccount, _configuration.Web.Me.Expand, cancellationToken) + account = await ExpandAccount(stormpathAccount, _oktaClient, _configuration.Web.Me.Expand, cancellationToken) }; return await JsonResponse.Ok(context, responseModel); } - private static Task ExpandAccount( + private static async Task ExpandAccount( ICompatibleOktaAccount account, + IOktaClient oktaClient, IReadOnlyDictionary expansionOptions, CancellationToken cancellationToken) { @@ -70,7 +71,7 @@ private static Task ExpandAccount( if (!expansionOptions.Any(e => e.Value)) { - return Task.FromResult(sanitizedModel); + return sanitizedModel; } if (expansionOptions.Any(e => e.Key.Equals("customData", StringComparison.OrdinalIgnoreCase) && e.Value)) @@ -78,9 +79,28 @@ private static Task ExpandAccount( sanitizedModel.CustomData = account.CustomData; } - // TODO other expansion patches + if (expansionOptions.Any(e => e.Key.Equals("groups", StringComparison.OrdinalIgnoreCase) && e.Value)) + { + var groups = await oktaClient.GetGroupsForUserIdAsync(account.GetOktaUser().Id, cancellationToken); + + sanitizedModel.Groups = new MeGroupsCollectionModel + { + Size = groups.Length, + Items = groups.Select(g => new MeGroupModel + { + Id = g.Id, + Name = g.Profile.Name, + Description = g.Profile.Description, + CreatedAt = g.Created, + ModifiedAt = g.LastUpdated + }) + .ToArray() + }; + } + + // TODO other expansion patches? - return Task.FromResult(sanitizedModel); + return sanitizedModel; } } } diff --git a/src/Stormpath.Owin.Middleware/Stormpath.Owin.Middleware.csproj b/src/Stormpath.Owin.Middleware/Stormpath.Owin.Middleware.csproj index ebb52cc..685f6df 100644 --- a/src/Stormpath.Owin.Middleware/Stormpath.Owin.Middleware.csproj +++ b/src/Stormpath.Owin.Middleware/Stormpath.Owin.Middleware.csproj @@ -3,7 +3,7 @@ Stormpath OWIN middleware library (c) 2016 Stormpath, Inc. - 4.0.0-rc4 + 4.0.0-rc5 Nate Barbettini net451;netstandard1.4 $(NoWarn);CS1591;CS0618