Skip to content

Commit

Permalink
Merge pull request #40 from tuliovitor09/hotfix/created-exception-mes…
Browse files Browse the repository at this point in the history
…sage

Created a constant message for the exceptions of type EHorseCallbackInterrupted
  • Loading branch information
viniciussanchez authored Aug 3, 2023
2 parents 3695cd3 + a81d147 commit 3ed092c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Horse.JWT.pas
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ implementation
Config: IHorseJWTConfig;
{$ENDIF}

const
TOKEN_NOT_FOUND = 'Token not found';
INVALID_AUTHORIZATION_TYPE = 'Invalid authorization type';
UNAUTHORIZED = 'Unauthorized';

procedure Middleware(AHorseRequest: THorseRequest; AHorseResponse: THorseResponse; ANext: {$IF DEFINED(FPC)}TNextProc{$ELSE}TProc{$ENDIF}; const ASecretJWT: string; const AConfig: IHorseJWTConfig);
var
{$IF DEFINED(FPC)}
Expand Down Expand Up @@ -190,14 +195,14 @@ procedure Middleware(AHorseRequest: THorseRequest; AHorseResponse: THorseRespons
LConfig.Header, LToken) and not AHorseRequest.Query.TryGetValue(
LHeaderNormalize, LToken) then
begin
AHorseResponse.Send('Token not found').Status(THTTPStatus.Unauthorized);
raise EHorseCallbackInterrupted.Create;
AHorseResponse.Send(TOKEN_NOT_FOUND).Status(THTTPStatus.Unauthorized);
raise EHorseCallbackInterrupted.Create(TOKEN_NOT_FOUND);
end;

if Pos('bearer', LowerCase(LToken)) = 0 then
begin
AHorseResponse.Send('Invalid authorization type').Status(THTTPStatus.Unauthorized);
raise EHorseCallbackInterrupted.Create;
AHorseResponse.Send(INVALID_AUTHORIZATION_TYPE).Status(THTTPStatus.Unauthorized);
raise EHorseCallbackInterrupted.Create(INVALID_AUTHORIZATION_TYPE);
end;

LToken := Trim(LToken.Replace('bearer', '', [rfIgnoreCase]));
Expand Down Expand Up @@ -284,8 +289,8 @@ procedure Middleware(AHorseRequest: THorseRequest; AHorseResponse: THorseRespons
except
on E: Exception do
begin
AHorseResponse.Send('Unauthorized').Status(THTTPStatus.Unauthorized);
raise EHorseCallbackInterrupted.Create;
AHorseResponse.Send(UNAUTHORIZED).Status(THTTPStatus.Unauthorized);
raise EHorseCallbackInterrupted.Create(UNAUTHORIZED);
end;
end;
finally
Expand Down

0 comments on commit 3ed092c

Please sign in to comment.