How to show WebApi OAuth token endpoint in Swagger

对着背影说爱祢 提交于 2019-11-27 14:42:27

问题


I've created a new Web Api project, added Asp.Net Identity and configured OAuth like so:

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    Provider = new ApplicationOAuthProvider(PublicClientId),
    AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    AllowInsecureHttp = true
};

This all works fine, I can call the /Token endpoint and get a bearer token back.

The problem is that this is not discoverable in Swagger I assume because it's not on a controller and therefore has no xml documentation generated for it.

Does anyone know of a way to display this login endpoint in my Swagger docs?

Thanks.

Also, i should've said that the Swagger documentation is working with all my controllers, it's just that I'm missing this one obvious method - how to login.


回答1:


ApiExplorer won't be automatically generating any info for your endpoint so you'll need to add a custom DocumentFilter in order to manually describe the token endpoint.

There's an example of this at https://github.com/domaindrivendev/Swashbuckle/issues/332 :

class AuthTokenOperation : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.paths.Add("/auth/token", new PathItem
        {
            post = new Operation
            {
                tags = new List<string> { "Auth" },
                consumes = new List<string>
                {
                    "application/x-www-form-urlencoded"
                },
                parameters = new List<Parameter> {
                    new Parameter
                    {
                        type = "string",
                        name = "grant_type",
                        required = true,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "username",
                        required = false,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "password",
                        required = false,
                        @in = "formData"
                    }
                }
            }
        });
    }
}

httpConfig.EnableSwagger(c =>
{
    c.DocumentFilter<AuthTokenOperation>();
});



回答2:


If anyone wondered how to add a response body to this operation, here's updated Ruaidhri's code:

class AuthTokenOperation : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.paths.Add("/token", new PathItem
        {
            post = new Operation
            {
                tags = new List<string> { "Auth" },
                consumes = new List<string>
                    {
                        "application/x-www-form-urlencoded"
                    },
                parameters = new List<Parameter> {
                    new Parameter
                    {
                        type = "string",
                        name = "grant_type",
                        required = true,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "username",
                        required = false,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "password",
                        required = false,
                        @in = "formData"
                    }
                },
                responses = new Dictionary<string, Response>()
                {
                    {
                        "200",
                        new Response {schema = schemaRegistry.GetOrRegister(typeof(OAuthTokenResponse))}
                    }
                }
            }
        });
    }
}

class OAuthTokenResponse
{
    [JsonProperty("access_token")]
    public string AccessToken { get; set; }

    [JsonProperty("token_type")]
    public string TokenType { get; set; }

    [JsonProperty("expires_in")]
    public long ExpiresIn { get; set; }

    [JsonProperty("userName")]
    public string Username { get; set; }

    [JsonProperty(".issued")]
    public DateTime Issued { get; set; }

    [JsonProperty(".expires")]
    public DateTime Expires { get; set; }
}


来源:https://stackoverflow.com/questions/32164117/how-to-show-webapi-oauth-token-endpoint-in-swagger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!