Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #92 from stormpath/91-expand-groups
Browse files Browse the repository at this point in the history
Add groups expansion back (closes #91)
  • Loading branch information
nbarbettini authored Jul 11, 2017
2 parents 76c7407 + 96b78a9 commit 65a1593
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/Stormpath.Owin.Middleware/Model/MeGroupModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Stormpath.Owin.Middleware.Model
{
public sealed class MeGroupsCollectionModel
{
public int Size { get; set; }

public MeGroupModel[] Items { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Stormpath.Owin.Middleware/Model/MeResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public sealed class MeResponseModel
public IDictionary<string, object> 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;
Expand Down
30 changes: 25 additions & 5 deletions src/Stormpath.Owin.Middleware/Route/MeRoute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ protected override async Task<bool> 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<MeResponseModel> ExpandAccount(
private static async Task<MeResponseModel> ExpandAccount(
ICompatibleOktaAccount account,
IOktaClient oktaClient,
IReadOnlyDictionary<string, bool> expansionOptions,
CancellationToken cancellationToken)
{
Expand All @@ -70,17 +71,36 @@ private static Task<MeResponseModel> 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))
{
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Stormpath OWIN middleware library</Description>
<Copyright>(c) 2016 Stormpath, Inc.</Copyright>
<VersionPrefix>4.0.0-rc4</VersionPrefix>
<VersionPrefix>4.0.0-rc5</VersionPrefix>
<Authors>Nate Barbettini</Authors>
<TargetFrameworks>net451;netstandard1.4</TargetFrameworks>
<NoWarn>$(NoWarn);CS1591;CS0618</NoWarn>
Expand Down

0 comments on commit 65a1593

Please sign in to comment.