Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAuth2 Resource Owner Password Credentials #326

Closed
ghost opened this issue Mar 5, 2017 · 12 comments
Closed

OAuth2 Resource Owner Password Credentials #326

ghost opened this issue Mar 5, 2017 · 12 comments

Comments

@ghost
Copy link

ghost commented Mar 5, 2017

Hey! I'm trying to use swagger with api, secured by IdentityServer4 with resource owner password credentials flow. When I try to login - I've got an 400 HTTP error. I look inside HTTP request and see, that swagger did not send clinet_id with acess token request, but it was configured in application middlewares pipeline, inside swaggerUI configureation options. Where am I wrong?

@xperiandri
Copy link

Do you use RC3?

@ghost
Copy link
Author

ghost commented Mar 8, 2017

@xperiandri Yes

@xperiandri
Copy link

Indeed, client_id is missing

@xperiandri
Copy link

When I pass client_id in configuration it is not used but if I switch Setup client authentication to Request body and enter client_id it is passed along with client_secret which is empty and I get an error about that.
image

@xperiandri
Copy link

@domaindrivendev, is client_id being taken from configuration in implicit flow? Contrary to what happens with password flow implementation in Swagger UI.

@domaindrivendev
Copy link
Owner

I think it would be better to submit this question to the swagger-ui folks. Swashbuckle just embeds that tool but doesn't actually do any of the development. It looks like there's already some related discussion in a recent PR to that repo that might shed some light on the subject ...

swagger-api/swagger-ui#2397

@xperiandri
Copy link

I just did't try implicit flow, so ask if it works the described way

@xperiandri
Copy link

Now it works well with password flow

@ghost
Copy link
Author

ghost commented Jul 31, 2017

I can't check this, but I trust you)
So, I close the issue.

@ghost ghost closed this as completed Jul 31, 2017
@theCuriousOne
Copy link

@xperiandri How did you manage to get it to work for password flow?

@xperiandri
Copy link

options.AddSecurityDefinition("OpenId Connect", new OAuth2Scheme {
    Type = "oauth2",
    Flow = "password",
    TokenUrl = tokenUrl.AbsoluteUri,
    Scopes = new Dictionary<string, string>
    {
        { Auth.Scopes.Scope1, "Scope 1" }
    }
});
.UseSwaggerUI(
    options => {
        var provider = app.ApplicationServices.GetService<IApiVersionDescriptionProvider>();
        foreach (var apiVersionDescription in provider
            .ApiVersionDescriptions
            .OrderByDescending(x => x.ApiVersion)) {
            options.SwaggerEndpoint(
                $"/swagger/{apiVersionDescription.GroupName}/swagger.json",
                $"Version {apiVersionDescription.ApiVersion}");
        }
        options.ConfigureOAuth2("swagger", null, null, "Swagger UI");
    });

Then either log in using Authorize button at the top
image
or add options.OperationFilter<SecurityRequirementsOperationFilter>(); that adds Scope1 to every secured API definition

@theCuriousOne
Copy link

@xperiandri Thank you for your help. For future reference for someone else:

`var provider = app.ApplicationServices.GetService<IApiVersionDescriptionProvider>();`

Is only needed for versioning (declare the api version). What I did wrong is that I didn't follow the OAuth2 Specs, i.e. I haven return a valid json with { "access_token": "34h3nf8nnf9a...", "grant_type": "Bearer"} -> this is important!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants