Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identity Server 4 Swagger Authentication

I'm currently having issues using swagger to authorize an api call to identity server 4.
My swagger dependency is using swashbuckle version -beta client object in the identity server 4 looks like

new Client
{
    ClientId="swagger",
    Enabled = true,
    ClientName="Swagger",
    AllowedGrantTypes = GrantTypes.Implicit,
    ClientSecrets = new List<Secret>
    {
        new Secret("secret".Sha256())
    },
    AllowedScopes = new List<string>
    {
        "apil"
    },
    RedirectUris = new List<string>
    {
        "http://localhost:15138/swagger/ui/popup.html"
    },
    AllowedCorsOrigins = new List<string>
    {
        "http://localhost:15138",
        "http://localhost:15138"
    },
    AllowAccessTokensViaBrowser = true,
    AllowAccessToAllScopes= true
}

The client object is the identity server 4 model in the configure method for authentication I have this

app.UseIdentityServer();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>();
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = "http://localhost:15138/",
    ScopeName = "apil",
    RequireHttpsMetadata = false,

});

Using fiddler my get request looks like this

GET /connect/authorize?state=9321480892748389&nonce=5279296493808222&client_id=swagger&redirect_uri=http%3A%2F%2Flocalhost%3A15138%2Fswagger%2Fui%2Fpopup.html&response_type=id_token%20token&scope=apil HTTP/1.1

All the necessary parameters are there, the client has the corresponding client id, but The response I get back is a redirect to an error page with the message of an invalid request. I was anticipating a login page to pass in credentials or something similar to get authorized I was wondering what I did wrong for that to happen.

like image 305
Hayden L Avatar asked Oct 02 '16 18:10

Hayden L


1 Answers

I ran into this same problem, and it was related to a few different things.

  1. Swagger needs a Security Definition configured.

  2. IdentityServerAuthentication AutomaticAuthenticate needs to be true.

  3. Swagger's client-id and client name need to be configured in Startup.cs.

See below:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerGen(c => {
        c.SwaggerDoc("v1", new Info
        {
            Version = "v1",
            Title = "my api title",
            Description = "my api desc",
            TermsOfService = "None",
            Contact = new Contact { Name = "contact" }
        });

        var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "api.xml");
        c.IncludeXmlComments(filePath);

        c.AddSecurityDefinition("oauth2", new OAuth2Scheme
        {
            Type = "oauth2",
            Flow = "implicit",
            AuthorizationUrl = "https://url",
            Scopes = new Dictionary<string, string>
            {
                { "api-name", "my api" }
            }
        });
    });
}

public void Configure(IApplicationBuilder app, ILoggerFactory    loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseIdentity();

    app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
    {
        Authority = "https://url",
        RequireHttpsMetadata = true,
        ApiName = "api-name",
        ApiSecret = "api-secret",
        AllowedScopes = { "api-name", "openid", "email", "profile" },
        ClaimsIssuer = "https://url",
        AutomaticAuthenticate = true,
    });

    app.UseStaticFiles();
    app.UseMvc();

    // Enable middleware to serve generated Swagger as a JSON endpoint
    app.UseSwagger();

    // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
    app.UseSwaggerUi(c =>
    {
        c.ConfigureOAuth2("swagger-name", "swagger-secret", "swagger-realm", "Swagger");
    });
}
like image 153
dpjas Avatar answered Sep 19 '22 02:09

dpjas