Browse Source

fix authentication issue in identity.api

pull/2012/head
Tarun Jain 2 years ago
parent
commit
a464a6996e
11 changed files with 82 additions and 47 deletions
  1. +16
    -1
      src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs
  2. +6
    -8
      src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs
  3. +12
    -9
      src/Services/Basket/Basket.API/Startup.cs
  4. +3
    -3
      src/Services/Identity/Identity.API/Configuration/Config.cs
  5. +1
    -1
      src/Services/Identity/Identity.API/ProgramExtensions.cs
  6. +1
    -0
      src/Services/Identity/Identity.API/keys/is-signing-key-3139DF3FF07C8E3881CFA9743F89A787.json
  7. +9
    -13
      src/Services/Ordering/Ordering.API/Startup.cs
  8. +13
    -6
      src/Services/Ordering/Ordering.SignalrHub/Startup.cs
  9. +18
    -4
      src/Services/Webhooks/Webhooks.API/Startup.cs
  10. +2
    -1
      src/Web/WebMVC/Startup.cs
  11. +1
    -1
      src/Web/WebhookClient/Startup.cs

+ 16
- 1
src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs View File

@ -1,4 +1,7 @@
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator;
public class Startup
{
@ -151,6 +154,18 @@ public static class ServiceCollectionExtensions
return services;
}
public static IServiceCollection AddCustomAuthorization(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("scope", "mobileshoppingagg");
});
});
return services;
}
public static IServiceCollection AddHttpServices(this IServiceCollection services)
{


+ 6
- 8
src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs View File

@ -1,4 +1,7 @@
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator;
public class Startup
{
@ -22,6 +25,7 @@ public class Startup
services.AddCustomMvc(Configuration)
.AddCustomAuthentication(Configuration)
//.AddCustomAuthorization(Configuration)
.AddDevspaces()
.AddApplicationServices()
.AddGrpcServices();
@ -83,12 +87,7 @@ public static class ServiceCollectionExtensions
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");
var identityUrl = configuration.GetValue<string>("urls:identity");
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
services.AddAuthentication("Bearer")
.AddJwtBearer(options =>
{
options.Authority = identityUrl;
@ -102,7 +101,6 @@ public static class ServiceCollectionExtensions
return services;
}
public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions();


+ 12
- 9
src/Services/Basket/Basket.API/Startup.cs View File

@ -1,3 +1,6 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
namespace Microsoft.eShopOnContainers.Services.Basket.API;
public class Startup
{
@ -214,20 +217,20 @@ public class Startup
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
services.AddAuthentication("Bearer").AddJwtBearer(options =>
{
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
options.Audience = "basket";
options.TokenValidationParameters = new TokenValidationParameters
options.TokenValidationParameters.ValidateAudience = false;
});
services.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", policy =>
{
ValidateAudience = false
};
policy.RequireAuthenticatedUser();
policy.RequireClaim("scope", "basket");
});
});
}


+ 3
- 3
src/Services/Identity/Identity.API/Configuration/Config.cs View File

@ -110,7 +110,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration
new Secret("secret".Sha256())
},
ClientUri = $"{configuration["MvcClient"]}", // public uri of the client
AllowedGrantTypes = GrantTypes.Hybrid,
AllowedGrantTypes = GrantTypes.Code,
AllowAccessTokensViaBrowser = false,
RequireConsent = false,
AllowOfflineAccess = true,
@ -147,7 +147,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration
new Secret("secret".Sha256())
},
ClientUri = $"{configuration["WebhooksWebClient"]}", // public uri of the client
AllowedGrantTypes = GrantTypes.Hybrid,
AllowedGrantTypes = GrantTypes.Code,
AllowAccessTokensViaBrowser = false,
RequireConsent = false,
AllowOfflineAccess = true,
@ -179,7 +179,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration
new Secret("secret".Sha256())
},
ClientUri = $"{configuration["Mvc"]}", // public uri of the client
AllowedGrantTypes = GrantTypes.Hybrid,
AllowedGrantTypes = GrantTypes.Code,
AllowAccessTokensViaBrowser = true,
RequireConsent = false,
AllowOfflineAccess = true,


+ 1
- 1
src/Services/Identity/Identity.API/ProgramExtensions.cs View File

@ -55,7 +55,7 @@ public static class ProgramExtensions
{
var identityServerBuilder = builder.Services.AddIdentityServer(options =>
{
options.IssuerUri = builder.Configuration["IssuerUrl"];
options.IssuerUri = "null";
options.Authentication.CookieLifetime = TimeSpan.FromHours(2);
options.Events.RaiseErrorEvents = true;


+ 1
- 0
src/Services/Identity/Identity.API/keys/is-signing-key-3139DF3FF07C8E3881CFA9743F89A787.json View File

@ -0,0 +1 @@
{"Version":1,"Id":"3139DF3FF07C8E3881CFA9743F89A787","Created":"2022-12-13T11:30:03.9474661Z","Algorithm":"RS256","IsX509Certificate":false,"Data":"CfDJ8LWRXnsATcxOsVa-h-Gywwx7S1URlnPkv5vJtTle9xG_ytf2YXKlRsyey8qFhmrfLZ9uixxUmHTLywyrhUV6-gdvwyirWsUU8qn3SNe4rlPgNYHESkjYwWH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GRe5gNOaT0BZ71SIlKQY1iLq5NmzEGM1iIOEJI-JA3VHhXs2mw7WDKpsE_k5kbFDlgdachB778jMTSs8xWjESxyidPz-tjqvWaDg482VjoOVYBQOGyUuFRMKVJ9xZBE03nVLG29cKVCNJ_KylBUy0O4LkHskJRzrbZE_HlcoCafl1z1","DataProtected":true}

+ 9
- 13
src/Services/Ordering/Ordering.API/Startup.cs View File

@ -1,3 +1,6 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
namespace Microsoft.eShopOnContainers.Services.Ordering.API;
public class Startup
@ -22,10 +25,11 @@ public class Startup
.AddHealthChecks(Configuration)
.AddCustomDbContext(Configuration)
.AddCustomSwagger(Configuration)
.AddCustomAuthentication(Configuration)
.AddCustomAuthorization(Configuration)
.AddCustomIntegrations(Configuration)
.AddCustomConfiguration(Configuration)
.AddEventBus(Configuration)
.AddCustomAuthentication(Configuration);
.AddEventBus(Configuration);
//configure autofac
var container = new ContainerBuilder();
@ -372,20 +376,12 @@ static class CustomExtensionsMethods
var identityUrl = configuration.GetValue<string>("IdentityUrl");
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
services.AddAuthentication("Bearer").AddJwtBearer(options =>
{
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
options.Audience = "orders";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false
};
options.TokenValidationParameters.ValidateAudience = false;
});
return services;
@ -397,7 +393,7 @@ static class CustomExtensionsMethods
options.AddPolicy("ApiScope", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("scope", "ordering");
policy.RequireClaim("scope", "orders");
});
});
return services;


+ 13
- 6
src/Services/Ordering/Ordering.SignalrHub/Startup.cs View File

@ -1,3 +1,6 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
namespace Microsoft.eShopOnContainers.Services.Ordering.SignalrHub;
public class Startup
@ -151,16 +154,12 @@ public class Startup
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
services.AddAuthentication("Bearer").AddJwtBearer(options =>
{
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
options.Audience = "orders.signalrhub";
options.TokenValidationParameters.ValidateAudience = false;
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
@ -176,6 +175,14 @@ public class Startup
}
};
});
services.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("scope", "orders.signalrhub");
});
});
}
private void RegisterEventBus(IServiceCollection services)


+ 18
- 4
src/Services/Webhooks/Webhooks.API/Startup.cs View File

@ -1,3 +1,6 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
namespace Webhooks.API;
public class Startup
{
@ -303,12 +306,23 @@ internal static class CustomExtensionMethods
options.Authority = identityUrl;
options.RequireHttpsMetadata = false;
options.Audience = "webhooks";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false
};
options.TokenValidationParameters.ValidateAudience = false;
});
return services;
}
public static IServiceCollection AddCustomAuthorization(this IServiceCollection services, IConfiguration configuration)
{
services.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("scope", "webhooks");
});
});
return services;
}
}

+ 2
- 1
src/Web/WebMVC/Startup.cs View File

@ -164,7 +164,7 @@ static class ServiceCollectionExtensions
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(setup => setup.ExpireTimeSpan = TimeSpan.FromMinutes(sessionCookieLifetime))
.AddOpenIdConnect(options =>
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = identityUrl.ToString();
@ -181,6 +181,7 @@ static class ServiceCollectionExtensions
options.Scope.Add("basket");
options.Scope.Add("webshoppingagg");
options.Scope.Add("orders.signalrhub");
options.Scope.Add("webhooks");
});
return services;


+ 1
- 1
src/Web/WebhookClient/Startup.cs View File

@ -121,7 +121,7 @@ static class ServiceExtensions
options.SignedOutRedirectUri = callBackUrl.ToString();
options.ClientId = "webhooksclient";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.ResponseType = "code";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.RequireHttpsMetadata = false;


Loading…
Cancel
Save