diff --git a/sources/MVCFramework.Middleware.Swagger.pas b/sources/MVCFramework.Middleware.Swagger.pas index d7976971..5c7f0bb9 100644 --- a/sources/MVCFramework.Middleware.Swagger.pas +++ b/sources/MVCFramework.Middleware.Swagger.pas @@ -48,6 +48,7 @@ TMVCSwaggerMiddleware = class(TInterfacedObject, IMVCMiddleware) fSwagDocURL: string; fJWTDescription: string; fEnableBasicAuthentication: Boolean; + fEnableBearerAuthentication: Boolean; fHost: string; fBasePath: string; fPathFilter: string; @@ -68,7 +69,8 @@ TMVCSwaggerMiddleware = class(TInterfacedObject, IMVCMiddleware) const AHost: string = ''; const ABasePath: string = ''; const APathFilter: String = ''; - const ATransferProtocolSchemes: TMVCTransferProtocolSchemes = [psHTTP, psHTTPS]); + const ATransferProtocolSchemes: TMVCTransferProtocolSchemes = [psHTTP, psHTTPS]; + const AEnableBearerAuthentication: Boolean = False); destructor Destroy; override; procedure OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean); procedure OnBeforeControllerAction(AContext: TWebContext; const AControllerQualifiedClassName: string; @@ -105,7 +107,7 @@ constructor TMVCSwaggerMiddleware.Create(const AEngine: TMVCEngine; const ASwagg const ASwaggerDocumentationURL, AJWTDescription: string; const AEnableBasicAuthentication: Boolean; const AHost, ABasePath: string; const APathFilter: String; - const ATransferProtocolSchemes: TMVCTransferProtocolSchemes); + const ATransferProtocolSchemes: TMVCTransferProtocolSchemes; const AEnableBearerAuthentication: Boolean); begin inherited Create; fSwagDocURL := ASwaggerDocumentationURL; @@ -113,6 +115,7 @@ constructor TMVCSwaggerMiddleware.Create(const AEngine: TMVCEngine; const ASwagg fSwaggerInfo := ASwaggerInfo; fJWTDescription := AJWTDescription; fEnableBasicAuthentication := AEnableBasicAuthentication; + fEnableBearerAuthentication := AEnableBearerAuthentication; fHost := AHost; fBasePath := ABasePath; fPathFilter := APathFilter; @@ -361,19 +364,23 @@ procedure TMVCSwaggerMiddleware.DocumentApiAuthentication(const ASwagDoc: TSwagD // Path operation Middleware JWT ASwagDoc.Paths.Add(TMVCSwagger.GetJWTAuthenticationPath(lJwtUrlSegment, lJWTMiddleware.UserNameHeaderName, lJWTMiddleware.PasswordHeaderName)); - - // Methods that have the MVCRequiresAuthentication attribute use bearer authentication. - lSecurityDefsBearer := TSwagSecurityDefinitionApiKey.Create; - lSecurityDefsBearer.SchemeName := SECURITY_BEARER_NAME; - lSecurityDefsBearer.InLocation := kilHeader; - lSecurityDefsBearer.Name := 'Authorization'; - lSecurityDefsBearer.Description := fJWTDescription; - ASwagDoc.SecurityDefinitions.Add(lSecurityDefsBearer); end; finally lRttiContext.Free; end; end; + + // Methods that have the MVCRequiresAuthentication attribute use bearer authentication. + if fEnableBearerAuthentication or + (Assigned(lJWTMiddleware) and Assigned(lJwtUrlField)) then + begin + lSecurityDefsBearer := TSwagSecurityDefinitionApiKey.Create; + lSecurityDefsBearer.SchemeName := SECURITY_BEARER_NAME; + lSecurityDefsBearer.InLocation := kilHeader; + lSecurityDefsBearer.Name := 'Authorization'; + lSecurityDefsBearer.Description := fJWTDescription; + ASwagDoc.SecurityDefinitions.Add(lSecurityDefsBearer); + end; end; procedure TMVCSwaggerMiddleware.DocumentApiSettings(AContext: TWebContext; ASwagDoc: TSwagDoc);