Skip to content

Commit 8039ffe

Browse files
committed
upgrade to Identity Server 4 rtm
1 parent 608c4c5 commit 8039ffe

7 files changed

Lines changed: 49 additions & 44 deletions

File tree

src/Services/Identity/Identity.API/Configuration/Config.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
using IdentityServer4.Models;
22
using Microsoft.Extensions.Options;
33
using System.Collections.Generic;
4+
using IdentityServer4;
45

56
namespace Identity.API.Configuration
67
{
78
public class Config
89
{
9-
// scopes define the resources in your system
10-
public static IEnumerable<Scope> GetScopes()
10+
// ApiResources define the apis in your system
11+
public static IEnumerable<ApiResource> GetApis()
1112
{
12-
return new List<Scope>
13+
return new List<ApiResource>
1314
{
14-
//Authentication OpenId uses this scopes;
15-
StandardScopes.OpenId,
16-
StandardScopes.Profile,
15+
new ApiResource("orders", "Orders Service"),
16+
new ApiResource("basket", "Basket Service")
17+
};
18+
}
1719

18-
//Each api we want to securice;
19-
new Scope
20-
{
21-
Name = "orders",
22-
Description = "Orders Service"
23-
},
24-
new Scope
25-
{
26-
Name = "basket",
27-
Description = "Basket Service"
28-
}
20+
// Identity resources are data like user ID, name, or email address of a user
21+
// see: http://docs.identityserver.io/en/release/configuration/resources.html
22+
public static IEnumerable<IdentityResource> GetResources()
23+
{
24+
return new List<IdentityResource>
25+
{
26+
new IdentityResources.OpenId(),
27+
new IdentityResources.Profile()
2928
};
3029
}
3130

@@ -47,8 +46,8 @@ public static IEnumerable<Client> GetClients(Dictionary<string,string> clientsUr
4746
AllowedCorsOrigins = { $"{clientsUrl["Spa"]}" },
4847
AllowedScopes =
4948
{
50-
StandardScopes.OpenId.Name,
51-
StandardScopes.Profile.Name,
49+
IdentityServerConstants.StandardScopes.OpenId,
50+
IdentityServerConstants.StandardScopes.Profile,
5251
"orders",
5352
"basket"
5453
}
@@ -65,8 +64,8 @@ public static IEnumerable<Client> GetClients(Dictionary<string,string> clientsUr
6564
AllowedCorsOrigins = { "http://eshopxamarin" },
6665
AllowedScopes =
6766
{
68-
StandardScopes.OpenId.Name,
69-
StandardScopes.Profile.Name,
67+
IdentityServerConstants.StandardScopes.OpenId,
68+
IdentityServerConstants.StandardScopes.Profile,
7069
"orders",
7170
"basket"
7271
}
@@ -82,6 +81,7 @@ public static IEnumerable<Client> GetClients(Dictionary<string,string> clientsUr
8281
ClientUri = $"{clientsUrl["Mvc"]}", // public uri of the client
8382
AllowedGrantTypes = GrantTypes.Hybrid,
8483
RequireConsent = false,
84+
AllowOfflineAccess = true,
8585
RedirectUris = new List<string>
8686
{
8787
$"{clientsUrl["Mvc"]}/signin-oidc",
@@ -96,9 +96,9 @@ public static IEnumerable<Client> GetClients(Dictionary<string,string> clientsUr
9696
},
9797
AllowedScopes = new List<string>
9898
{
99-
StandardScopes.OpenId.Name,
100-
StandardScopes.Profile.Name,
101-
StandardScopes.OfflineAccess.Name,
99+
IdentityServerConstants.StandardScopes.OpenId,
100+
IdentityServerConstants.StandardScopes.Profile,
101+
IdentityServerConstants.StandardScopes.OfflineAccess,
102102
"orders",
103103
"basket",
104104
},

src/Services/Identity/Identity.API/Controllers/AccountController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using IdentityModel;
66
using IdentityServer4.Quickstart.UI.Models;
77
using IdentityServer4.Services;
8-
using IdentityServer4.Services.InMemory;
98
using Microsoft.AspNetCore.Http.Authentication;
109
using Microsoft.AspNetCore.Mvc;
1110
using System;

src/Services/Identity/Identity.API/Controllers/ConsentController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ public class ConsentController : Controller
2222
{
2323
private readonly ILogger<ConsentController> _logger;
2424
private readonly IClientStore _clientStore;
25-
private readonly IScopeStore _scopeStore;
25+
private readonly IResourceStore _resourceStore;
2626
private readonly IIdentityServerInteractionService _interaction;
2727

2828

2929
public ConsentController(
3030
ILogger<ConsentController> logger,
3131
IIdentityServerInteractionService interaction,
3232
IClientStore clientStore,
33-
IScopeStore scopeStore)
33+
IResourceStore resourceStore)
3434
{
3535
_logger = logger;
3636
_interaction = interaction;
3737
_clientStore = clientStore;
38-
_scopeStore = scopeStore;
38+
_resourceStore = resourceStore;
3939
}
4040

4141
/// <summary>
@@ -120,10 +120,10 @@ async Task<ConsentViewModel> BuildViewModelAsync(string returnUrl, ConsentInputM
120120
var client = await _clientStore.FindEnabledClientByIdAsync(request.ClientId);
121121
if (client != null)
122122
{
123-
var scopes = await _scopeStore.FindEnabledScopesAsync(request.ScopesRequested);
124-
if (scopes != null && scopes.Any())
123+
var resources = await _resourceStore.FindEnabledResourcesByScopeAsync(request.ScopesRequested);
124+
if (resources != null && (resources.IdentityResources.Any() || resources.ApiResources.Any()))
125125
{
126-
return new ConsentViewModel(model, returnUrl, request, client, scopes);
126+
return new ConsentViewModel(model, returnUrl, request, client, resources);
127127
}
128128
else
129129
{

src/Services/Identity/Identity.API/Identity.API.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0-msbuild3-final">
4242
<PrivateAssets>All</PrivateAssets>
4343
</PackageReference>
44-
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.0-rc3" />
45-
<PackageReference Include="IdentityServer4.EntityFramework" Version="1.0.0-rc3" />
44+
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.0" />
45+
<PackageReference Include="IdentityServer4.EntityFramework" Version="1.0.0" />
4646
</ItemGroup>
4747

4848
<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">

src/Services/Identity/Identity.API/Models/AccountViewModels/ConsentViewModel.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Identity.API.Models.AccountViewModels
1010
{
1111
public class ConsentViewModel : ConsentInputModel
1212
{
13-
public ConsentViewModel(ConsentInputModel model, string returnUrl, AuthorizationRequest request, Client client, IEnumerable<Scope> scopes)
13+
public ConsentViewModel(ConsentInputModel model, string returnUrl, AuthorizationRequest request, Client client, Resources resources)
1414
{
1515
RememberConsent = model?.RememberConsent ?? true;
1616
ScopesConsented = model?.ScopesConsented ?? Enumerable.Empty<string>();
@@ -22,8 +22,8 @@ public ConsentViewModel(ConsentInputModel model, string returnUrl, Authorization
2222
ClientLogoUrl = client.LogoUri;
2323
AllowRememberConsent = client.AllowRememberConsent;
2424

25-
IdentityScopes = scopes.Where(x => x.Type == ScopeType.Identity).Select(x => new ScopeViewModel(x, ScopesConsented.Contains(x.Name) || model == null)).ToArray();
26-
ResourceScopes = scopes.Where(x => x.Type == ScopeType.Resource).Select(x => new ScopeViewModel(x, ScopesConsented.Contains(x.Name) || model == null)).ToArray();
25+
IdentityScopes = resources.IdentityResources.Select(x => new ScopeViewModel(x, ScopesConsented.Contains(x.Name) || model == null)).ToArray();
26+
ResourceScopes = resources.ApiResources.SelectMany(x => x.Scopes).Select(x => new ScopeViewModel(x, ScopesConsented.Contains(x.Name) || model == null)).ToArray();
2727
}
2828

2929
public string ClientName { get; set; }
@@ -47,6 +47,16 @@ public ScopeViewModel(Scope scope, bool check)
4747
Checked = check || scope.Required;
4848
}
4949

50+
public ScopeViewModel(IdentityResource identity, bool check)
51+
{
52+
Name = identity.Name;
53+
DisplayName = identity.DisplayName;
54+
Description = identity.Description;
55+
Emphasize = identity.Emphasize;
56+
Required = identity.Required;
57+
Checked = check || identity.Required;
58+
}
59+
5060
public string Name { get; set; }
5161
public string DisplayName { get; set; }
5262
public string Description { get; set; }

src/Services/Identity/Identity.API/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public void ConfigureServices(IServiceCollection services)
7777
// Adds IdentityServer
7878
services.AddIdentityServer(x => x.IssuerUri = "null")
7979
.AddSigningCredential(Certificate.Get())
80-
.AddInMemoryScopes(Config.GetScopes())
80+
.AddInMemoryApiResources(Config.GetApis())
81+
.AddInMemoryIdentityResources(Config.GetResources())
8182
.AddInMemoryClients(Config.GetClients(clientUrls))
8283
.AddAspNetIdentity<ApplicationUser>()
8384
.Services.AddTransient<IProfileService, ProfileService>();

src/Web/WebMVC/Startup.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
112112
ResponseType = "code id_token",
113113
SaveTokens = true,
114114
GetClaimsFromUserInfoEndpoint = true,
115-
RequireHttpsMetadata = false,
115+
RequireHttpsMetadata = false,
116+
Scope = { "openid", "profile", "orders", "basket" }
116117
};
117118

118-
oidcOptions.Scope.Clear();
119-
oidcOptions.Scope.Add("openid");
120-
oidcOptions.Scope.Add("profile");
121-
oidcOptions.Scope.Add("orders");
122-
oidcOptions.Scope.Add("basket");
123-
124119
//Wait untill identity service is ready on compose.
125120
app.UseOpenIdConnectAuthentication(oidcOptions);
126121

0 commit comments

Comments
 (0)