private string Render(string endpoint, OpenApiAuthLevelType authLevel = OpenApiAuthLevelType.Anonymous, string authKey = null) { var swaggerUiTitle = $"{this._info.Title} - Swagger UI"; var swaggerUrl = $"{this._baseUrl.TrimEnd('/')}/{endpoint}"; var queries = this._req.Query.ToDictionary(p => p.Key, p => p.Value); if (string.IsNullOrEmpty(authKey)) { authKey = queries.TryGetValue("code", out var queryCodeValue) ? (string)queryCodeValue : null; } if (this.IsAuthKeyRequired(authLevel, authKey)) { queries["code"] = new StringValues(authKey); } if (queries.Any()) { swaggerUrl += "?" + string.Join("&", queries.SelectMany(p => p.Value.Select(q => $"{p.Key}={q}"))); } var html = this._indexHtml.Replace(SwaggerUITitlePlaceholder, swaggerUiTitle) .Replace(SwaggerUICssPlaceholder, this._swaggerUiCss) .Replace(SwaggerUICustomCssPlaceholder, this._swaggerUiCustomCss) .Replace(SwaggerUIBundleJsPlaceholder, this._swaggerUiBundleJs) .Replace(SwaggerUICustomJsPlaceholder, this._swaggerUiCustomJs) .Replace(SwaggerUIStandalonePresetJsPlaceholder, this._swaggerUiStandalonePresetJs) .Replace(SwaggerUrlPlaceholder, swaggerUrl); return(html); }
/// <inheritdoc /> public async Task <string> RenderOAuth2RedirectAsync(string endpoint, OpenApiAuthLevelType authLevel = OpenApiAuthLevelType.Anonymous, string authKey = null) { var html = await Task.Factory .StartNew(() => this.RenderOAuth2Redirect(endpoint, authLevel, authKey)) .ConfigureAwait(false); return(html); }
/// <inheritdoc /> public async Task <string> RenderAsync(string endpoint, OpenApiAuthLevelType authLevel = OpenApiAuthLevelType.Anonymous, string authKey = null) { endpoint.ThrowIfNullOrWhiteSpace(); var html = await Task.Factory .StartNew(() => this.Render(endpoint, authLevel, authKey)) .ConfigureAwait(false); return(html); }
public async Task Given_AuthKey_Options_When_RenderAsync_Invoked_Then_It_Should_Be_Used_As_Request_Key( string queryKey, OpenApiAuthLevelType configuredAuthLevel, string configKey, string expectedRequestKey, bool throwsException = false) { var endpoint = "swagger/ui"; var baseUrl = "https://localhost:7071"; var ui = new SwaggerUI(); ui.AddMetadata(new OpenApiInfo()); var uiType = ui.GetType(); //Generate Request Object with query key var queryDict = new Dictionary <string, StringValues>(); if (queryKey != null) { queryDict["code"] = queryKey; } var req = new Mock <IHttpRequestDataObject>(); req.SetupGet(p => p.Query).Returns(new QueryCollection(queryDict)); uiType.GetField("_req", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ui, req.Object); //Set BaseUrl uiType.GetField("_baseUrl", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(ui, baseUrl); //Set html string just to contain url placeholder var swaggerUrlPlaceholder = uiType.GetField("SwaggerUrlPlaceholder", BindingFlags.Static | BindingFlags.NonPublic) .GetRawConstantValue() as string; uiType.GetField("_indexHtml", BindingFlags.Instance | BindingFlags.NonPublic) .SetValue(ui, swaggerUrlPlaceholder); Func <Task <string> > action = async() => await ui.RenderAsync(endpoint, configuredAuthLevel, configKey); if (throwsException) { await action.Should().ThrowAsync <InvalidOperationException>(); } else { var result = await action(); if (expectedRequestKey != null) { result.Should().Contain($"code={expectedRequestKey}"); } else { result.Should().NotContain($"code="); } } }
/// <inheritdoc /> private string RenderOAuth2Redirect(string endpoint, OpenApiAuthLevelType authLevel = OpenApiAuthLevelType.Anonymous, string authKey = null) { var pageUrl = $"{this._baseUrl.TrimEnd('/')}/{endpoint}"; if (this.IsAuthKeyRequired(authLevel, authKey)) { pageUrl += $"?code={authKey}"; } var html = this._oauth2RedirectHtml; return(html); }
private bool IsAuthKeyRequired(OpenApiAuthLevelType authLevel = OpenApiAuthLevelType.Anonymous, string authKey = null) { if (authLevel == OpenApiAuthLevelType.Anonymous) { return(false); } if (authKey.IsNullOrWhiteSpace()) { throw new InvalidOperationException("API key is required to access OpenAPI document"); } return(true); }
public void Given_Options_When_IsAuthKeyRequired_Invoked_Then_It_Should_Return_Result( OpenApiAuthLevelType configuredAuthLevel, string configKey, bool throwsException, bool expected) { var ui = new SwaggerUI(); var method = ui.GetType() .GetMethod("IsAuthKeyRequired", BindingFlags.Instance | BindingFlags.NonPublic); Func <bool> action = () => (bool)method.Invoke(ui, new object[] { configuredAuthLevel, configKey }); if (throwsException) { action.Should().Throw <TargetInvocationException>().And.InnerException.Should() .BeOfType <InvalidOperationException>(); } else { action.Invoke().Should().Be(expected); } }
/// <summary> /// Renders the OAuth2 Redirect page in HTML. /// </summary> /// <param name="ui"><see cref="ISwaggerUI"/> instance.</param> /// <param name="endpoint">The endpoint of the OAuth2 Redirect page.</param> /// <param name="authLevel">The authorisation level of the Swagger document.</param> /// <param name="authKey">API key of the HTTP endpoint to render OpenAPI document.</param> /// <returns>The OAuth2 Redirect page in HTML.</returns> public static async Task <string> RenderOAuth2RedirectAsync(this Task <ISwaggerUI> ui, string endpoint, OpenApiAuthLevelType authLevel = OpenApiAuthLevelType.Anonymous, string authKey = null) { var instance = await ui.ThrowIfNullOrDefault().ConfigureAwait(false); endpoint.ThrowIfNullOrWhiteSpace(); return(await instance.RenderOAuth2RedirectAsync(endpoint, authLevel, authKey).ConfigureAwait(false)); }