From 8be482935a9b282fb3a9692e6cb1da0bd0f53e6a Mon Sep 17 00:00:00 2001 From: "d.breimaier" Date: Mon, 27 Feb 2023 12:08:54 +0100 Subject: [PATCH] Commit Description --- .../App_Start/BasicAuthenticationAttribute.vb | 2 +- VERAG_REST_SERVER/App_Start/SwaggerConfig.vb | 30 ++-- .../BasicAuthentificationHandler.vb | 68 ------- .../V1/AvisoController/AVISOController.vb | 33 ++-- VERAG_REST_SERVER/IService/IUserService.vb | 5 - VERAG_REST_SERVER/Service/Service.vb | 7 - .../SwaggerExtensions/my_index.html | 167 ++++++++++++++++++ VERAG_REST_SERVER/VERAG_REST_SERVER.vbproj | 4 +- 8 files changed, 202 insertions(+), 114 deletions(-) delete mode 100644 VERAG_REST_SERVER/BasicAuthentificationHandler.vb delete mode 100644 VERAG_REST_SERVER/IService/IUserService.vb delete mode 100644 VERAG_REST_SERVER/Service/Service.vb create mode 100644 VERAG_REST_SERVER/SwaggerExtensions/my_index.html diff --git a/VERAG_REST_SERVER/App_Start/BasicAuthenticationAttribute.vb b/VERAG_REST_SERVER/App_Start/BasicAuthenticationAttribute.vb index cdbf520..bd55eb2 100644 --- a/VERAG_REST_SERVER/App_Start/BasicAuthenticationAttribute.vb +++ b/VERAG_REST_SERVER/App_Start/BasicAuthenticationAttribute.vb @@ -25,7 +25,7 @@ Public Class BasicAuthenticationAttribute If isValid Then Dim principal = New GenericPrincipal(New GenericIdentity(userName), Nothing) Thread.CurrentPrincipal = principal - actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, "User " & userName & " successfully authenticated") + 'actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, "User " & userName & " successfully authenticated") Return End If End If diff --git a/VERAG_REST_SERVER/App_Start/SwaggerConfig.vb b/VERAG_REST_SERVER/App_Start/SwaggerConfig.vb index 2055c95..ccef701 100644 --- a/VERAG_REST_SERVER/App_Start/SwaggerConfig.vb +++ b/VERAG_REST_SERVER/App_Start/SwaggerConfig.vb @@ -1,4 +1,6 @@ -Imports System.Net.Http +Imports System.IO +Imports System.Net.Http +Imports System.Reflection Imports System.Web.Http Imports System.Web.Http.Description Imports Microsoft.Extensions.Options @@ -19,9 +21,13 @@ Public Class SwaggerConfig Public Shared Sub Register(ByVal config As HttpConfiguration) Dim thisAssembly = GetType(SwaggerConfig).Assembly + Dim baseDirectory = AppDomain.CurrentDomain.BaseDirectory + baseDirectory &= "\bin\" + Dim commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML" + Dim commentsFile = Path.Combine(baseDirectory, commentsFileName) GlobalConfiguration.Configuration.EnableSwagger(Function(c) - 'c.SingleApiVersion("v1", descr) + c.PrettyPrint() c.MultipleApiVersions(Function(apiDesc, targetApiVersion) ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion), Function(vc) @@ -41,33 +47,29 @@ Public Class SwaggerConfig End Function) - ' c.OAuth2("oauth2").Description("OAuth2 Implicit Grant").Flow("implicit").AuthorizationUrl("http://petstore.swagger.wordnik.com/api/oauth/dialog").Scopes(Function(scopes) - 'End Function) c.BasicAuth("basic").Description("Basic HTTP Authentication") + c.IncludeXmlComments(commentsFile) - 'c.ApiKey("apiKey").Description("API Key Authentication").Name("apiKey").In("header") + 'c.RootUrl(Function(req) req.GetRouteData) 'c.OperationFilter(Of AddAuthorizationHeaderParameterOperationFilter)() 'c.OperationFilter(Of BasicAuthenticationAttribute)() - 'c.IncludeXmlComments($"{AppDomain.CurrentDomain.BaseDirectory}\bin\MyApi.XML") - 'c.RootUrl(Function(req) req.GetRouteData) End Function).EnableSwaggerUi(Function(c) c.DocumentTitle(descr) c.EnableDiscoveryUrlSelector() - c.DocExpansion(DocExpansion.Full) - 'c.EnableApiKeySupport("apiKey", "header") - 'c.CustomAsset("index", yourAssembly, "YourWebApiProject.SwaggerExtensions.index.html") - 'c.EnableOAuth2Support(clientId:="test-client-id", clientSecret:=Nothing, realm:="test-realm", appName:="Swagger UI") - 'additionalQueryStringParams:=New Dictionary(Of String, String)() From { - ' {"foo", "bar"} - '}) + c.DocExpansion(DocExpansion.List) + + c.CustomAsset("index", thisAssembly, "VERAG_REST_SERVER.my_index.html") + + + End Function) diff --git a/VERAG_REST_SERVER/BasicAuthentificationHandler.vb b/VERAG_REST_SERVER/BasicAuthentificationHandler.vb deleted file mode 100644 index 56e5028..0000000 --- a/VERAG_REST_SERVER/BasicAuthentificationHandler.vb +++ /dev/null @@ -1,68 +0,0 @@ -Imports System.Net.Http.Headers -Imports System.Security.Claims -Imports System.Text.Encodings.Web -Imports System.Threading.Tasks -Imports Microsoft.AspNetCore.Authentication -Imports Microsoft.Extensions.Logging -Imports Microsoft.Extensions.Options -Imports Microsoft.AspNetCore.Http.Abstractions - - -Public Class BasicAuthentificationHandler - Inherits AuthenticationHandler(Of AuthenticationSchemeOptions) - - ReadOnly _userService As IUserService - - - Public Sub New(userService As IUserService, options As Microsoft.Extensions.Options.IOptionsMonitor(Of AuthenticationSchemeOptions), logger As ILoggerFactory, encoder As UrlEncoder, clock As ISystemClock) - - MyBase.New(options, logger, encoder, clock) - _userService = userService - - - End Sub - - Protected Overrides Function HandleChallengeAsync(properties As AuthenticationProperties) As Task - GetResponse().Headers("WWW-Authenticate") = "Basic" - Return MyBase.HandleChallengeAsync(properties) - End Function - - Private Function GetResponse() As Object - Return Response - End Function - - Protected Overrides Function HandleAuthenticateAsync() As Task(Of AuthenticateResult) - - Dim username As String = "" - - Try - - Dim authHeader = AuthenticationHeaderValue.Parse(GetResponse().Headers("Authorization")) - Dim credentials = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter)).Split(":") - username = credentials.FirstOrDefault - Dim password = credentials.LastOrDefault - - If Not _userService.CheckUser(username, password) Then - Throw New NotImplementedException("Invalid Username or password") - End If - - - - Catch ex As Exception - - Return Task.FromResult(AuthenticateResult.Fail(ex.Message)) - - End Try - - Dim claims = {New Claim(ClaimTypes.Name, username)} - Dim idendity = New ClaimsIdentity(claims, Scheme.Name) - Dim principal = New ClaimsPrincipal(idendity) - Dim ticket = New AuthenticationTicket(principal, Scheme.Name) - - Return Task.FromResult(AuthenticateResult.Success(ticket)) - - - - - End Function -End Class diff --git a/VERAG_REST_SERVER/Controllers/V1/AvisoController/AVISOController.vb b/VERAG_REST_SERVER/Controllers/V1/AvisoController/AVISOController.vb index 2959855..93076db 100644 --- a/VERAG_REST_SERVER/Controllers/V1/AvisoController/AVISOController.vb +++ b/VERAG_REST_SERVER/Controllers/V1/AvisoController/AVISOController.vb @@ -12,26 +12,24 @@ Namespace ApiController.Controllers - 'If myTokenAttribute Then - - 'If operation.parameters Is Nothing Then - ' operation.parameters = New List(Of Parameter)() - ' End If - - ' operation.parameters.Add(New Parameter() With { - ' .name = "Authorization Token", - ' .[in] = "header", - ' .description = "my token description", - ' .required = True, - ' .type = "string" - ' }) - 'End If - - + ''' + ''' Gets the Hello World Response + ''' + ''' A SharePriceResponse which contains the price of the share + ''' Returns 200 And Hallo World + ''' Returns 400 if the query Is invalid Public Function GetValue() As String Return "Hello world!" End Function + ''' + ''' Set the Test + ''' + ''' TRAviso + ''' A SharePriceResponse which contains the price of the share + ''' Returns 200 and the TRAviso-JSON-Object + ''' Returns 400 if the query Is invalid + ''' Returns 401 if your are not authorized Public Function PostValue(ByVal API_AVISO As VERAG_PROG_ALLGEMEIN.cVERAG_in_TRAviso) As String VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM = False VERAG_PROG_ALLGEMEIN.cAllgemein.ERR_OP_GLOBAL = VERAG_PROG_ALLGEMEIN.ERROR_OP.MAIL @@ -216,6 +214,9 @@ Namespace ApiController.Controllers Public Class AVISO1TESTController Inherits System.Web.Http.ApiController + ''' + ''' Gets the Hello World Response + ''' Public Function GetValue() As String Return "Hello world!" End Function diff --git a/VERAG_REST_SERVER/IService/IUserService.vb b/VERAG_REST_SERVER/IService/IUserService.vb deleted file mode 100644 index 5d4c789..0000000 --- a/VERAG_REST_SERVER/IService/IUserService.vb +++ /dev/null @@ -1,5 +0,0 @@ -Public Interface IUserService - - Function CheckUser(ByVal username As String, ByVal passowrd As String) - -End Interface diff --git a/VERAG_REST_SERVER/Service/Service.vb b/VERAG_REST_SERVER/Service/Service.vb deleted file mode 100644 index ef2cfb9..0000000 --- a/VERAG_REST_SERVER/Service/Service.vb +++ /dev/null @@ -1,7 +0,0 @@ -Public Class Service - Implements IUserService - - Public Function CheckUser(username As String, passowrd As String) As Object Implements IUserService.CheckUser - Return username.Equals("testuser") & passowrd.Equals("pwd") - End Function -End Class diff --git a/VERAG_REST_SERVER/SwaggerExtensions/my_index.html b/VERAG_REST_SERVER/SwaggerExtensions/my_index.html new file mode 100644 index 0000000..9ec3153 --- /dev/null +++ b/VERAG_REST_SERVER/SwaggerExtensions/my_index.html @@ -0,0 +1,167 @@ + + + + + Swagger UI + + + + + + + + %(StylesheetIncludes) + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 
+
+ + diff --git a/VERAG_REST_SERVER/VERAG_REST_SERVER.vbproj b/VERAG_REST_SERVER/VERAG_REST_SERVER.vbproj index 8e12b9e..7871382 100644 --- a/VERAG_REST_SERVER/VERAG_REST_SERVER.vbproj +++ b/VERAG_REST_SERVER/VERAG_REST_SERVER.vbproj @@ -298,6 +298,7 @@ + @@ -308,7 +309,6 @@ - @@ -321,7 +321,6 @@ Global.asax - @@ -343,7 +342,6 @@ Settings.settings True -