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

How to secure swagger ui page ? #667

Open
imhdev opened this issue Sep 29, 2024 · 1 comment
Open

How to secure swagger ui page ? #667

imhdev opened this issue Sep 29, 2024 · 1 comment

Comments

@imhdev
Copy link

imhdev commented Sep 29, 2024

In out-of-proc azure function app, was able to secured the swagger endpoints from the sample examples. Is there a way to secure the swagger ui page (/swager/ui) itself using openapi extension ?

@morten-b
Copy link

You can implement IOpenApiHttpTriggerAuthorization, but you need to figure out what way you wan't it to be implemented yourself, depeding on the use case.

I've create functionality, so that it looks up its on function app, app key. Remember to provide the needed role for the function app to access its own key, if doing the same thing.

public class MyOpenApiHttpTriggerAuthorization : IOpenApiHttpTriggerAuthorization
{
    public async Task<OpenApiAuthorizationResult> AuthorizeAsync(IHttpRequestDataObject req)
    {
        var subscriptionId = $"{Environment.GetEnvironmentVariable("SubscriptionId")}";
        var resourceGroupName = $"{Environment.GetEnvironmentVariable("FunctionAppRg")}";
        var functionAppName = $"{Environment.GetEnvironmentVariable("FunctionAppName")}";

        var credential = new DefaultAzureCredential();
        var armClient = new ArmClient(credential);

        var resourceIdentifier = new ResourceIdentifier($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{functionAppName}");
        var functionApp = armClient.GetWebSiteResource(resourceIdentifier);

        var keys = await functionApp.GetHostKeysAsync();
        var code = req.Query["code"].FirstOrDefault();
                
        if (keys.Value.FunctionKeys["default"] != code)
        {
            return await Task.FromResult(new OpenApiAuthorizationResult
            {
                StatusCode = HttpStatusCode.Unauthorized,
                ContentType = "text/plain",
                Payload = "Unauthorized",
            });
        }

        return await Task.FromResult<OpenApiAuthorizationResult>(null);
    }
}

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

No branches or pull requests

2 participants