Issues#771 - Adds the security definition of the apiKey type (bearer authorization) to the swagger middleware in a customized way (#772)

* issue#771
This commit is contained in:
Marcelo Jaloto 2024-09-03 08:49:28 -03:00 committed by GitHub
parent febdf40863
commit 3ae1a2a766
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -48,6 +48,7 @@ type
fSwagDocURL: string; fSwagDocURL: string;
fJWTDescription: string; fJWTDescription: string;
fEnableBasicAuthentication: Boolean; fEnableBasicAuthentication: Boolean;
fEnableBearerAuthentication: Boolean;
fHost: string; fHost: string;
fBasePath: string; fBasePath: string;
fPathFilter: string; fPathFilter: string;
@ -68,7 +69,8 @@ type
const AHost: string = ''; const AHost: string = '';
const ABasePath: string = ''; const ABasePath: string = '';
const APathFilter: String = ''; const APathFilter: String = '';
const ATransferProtocolSchemes: TMVCTransferProtocolSchemes = [psHTTP, psHTTPS]); const ATransferProtocolSchemes: TMVCTransferProtocolSchemes = [psHTTP, psHTTPS];
const AEnableBearerAuthentication: Boolean = False);
destructor Destroy; override; destructor Destroy; override;
procedure OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean); procedure OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
procedure OnBeforeControllerAction(AContext: TWebContext; const AControllerQualifiedClassName: string; 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 ASwaggerDocumentationURL, AJWTDescription: string; const AEnableBasicAuthentication: Boolean;
const AHost, ABasePath: string; const AHost, ABasePath: string;
const APathFilter: String; const APathFilter: String;
const ATransferProtocolSchemes: TMVCTransferProtocolSchemes); const ATransferProtocolSchemes: TMVCTransferProtocolSchemes; const AEnableBearerAuthentication: Boolean);
begin begin
inherited Create; inherited Create;
fSwagDocURL := ASwaggerDocumentationURL; fSwagDocURL := ASwaggerDocumentationURL;
@ -113,6 +115,7 @@ begin
fSwaggerInfo := ASwaggerInfo; fSwaggerInfo := ASwaggerInfo;
fJWTDescription := AJWTDescription; fJWTDescription := AJWTDescription;
fEnableBasicAuthentication := AEnableBasicAuthentication; fEnableBasicAuthentication := AEnableBasicAuthentication;
fEnableBearerAuthentication := AEnableBearerAuthentication;
fHost := AHost; fHost := AHost;
fBasePath := ABasePath; fBasePath := ABasePath;
fPathFilter := APathFilter; fPathFilter := APathFilter;
@ -361,8 +364,16 @@ begin
// Path operation Middleware JWT // Path operation Middleware JWT
ASwagDoc.Paths.Add(TMVCSwagger.GetJWTAuthenticationPath(lJwtUrlSegment, ASwagDoc.Paths.Add(TMVCSwagger.GetJWTAuthenticationPath(lJwtUrlSegment,
lJWTMiddleware.UserNameHeaderName, lJWTMiddleware.PasswordHeaderName)); lJWTMiddleware.UserNameHeaderName, lJWTMiddleware.PasswordHeaderName));
end;
finally
lRttiContext.Free;
end;
end;
// Methods that have the MVCRequiresAuthentication attribute use bearer authentication. // Methods that have the MVCRequiresAuthentication attribute use bearer authentication.
if fEnableBearerAuthentication or
(Assigned(lJWTMiddleware) and Assigned(lJwtUrlField)) then
begin
lSecurityDefsBearer := TSwagSecurityDefinitionApiKey.Create; lSecurityDefsBearer := TSwagSecurityDefinitionApiKey.Create;
lSecurityDefsBearer.SchemeName := SECURITY_BEARER_NAME; lSecurityDefsBearer.SchemeName := SECURITY_BEARER_NAME;
lSecurityDefsBearer.InLocation := kilHeader; lSecurityDefsBearer.InLocation := kilHeader;
@ -370,10 +381,6 @@ begin
lSecurityDefsBearer.Description := fJWTDescription; lSecurityDefsBearer.Description := fJWTDescription;
ASwagDoc.SecurityDefinitions.Add(lSecurityDefsBearer); ASwagDoc.SecurityDefinitions.Add(lSecurityDefsBearer);
end; end;
finally
lRttiContext.Free;
end;
end;
end; end;
procedure TMVCSwaggerMiddleware.DocumentApiSettings(AContext: TWebContext; ASwagDoc: TSwagDoc); procedure TMVCSwaggerMiddleware.DocumentApiSettings(AContext: TWebContext; ASwagDoc: TSwagDoc);