Skip to content

Commit

Permalink
Merge pull request #27 from gabrielbaltazar/master
Browse files Browse the repository at this point in the history
Delphi sample with Session
  • Loading branch information
viniciussanchez authored Mar 1, 2022
2 parents bf4bf8b + b4ac34a commit 3176449
Show file tree
Hide file tree
Showing 9 changed files with 1,705 additions and 136 deletions.
60 changes: 60 additions & 0 deletions samples/delphi/Samples.groupproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{2E261CC6-ED1F-4329-9A57-9EE8D35D96EB}</ProjectGuid>
</PropertyGroup>
<ItemGroup>
<Projects Include="auth\samples_auth.dproj">
<Dependencies/>
</Projects>
<Projects Include="client\samples_client.dproj">
<Dependencies/>
</Projects>
<Projects Include="session\samples_session.dproj">
<Dependencies/>
</Projects>
</ItemGroup>
<ProjectExtensions>
<Borland.Personality>Default.Personality.12</Borland.Personality>
<Borland.ProjectType/>
<BorlandProject>
<Default.Personality/>
</BorlandProject>
</ProjectExtensions>
<Target Name="samples_auth">
<MSBuild Projects="auth\samples_auth.dproj"/>
</Target>
<Target Name="samples_auth:Clean">
<MSBuild Projects="auth\samples_auth.dproj" Targets="Clean"/>
</Target>
<Target Name="samples_auth:Make">
<MSBuild Projects="auth\samples_auth.dproj" Targets="Make"/>
</Target>
<Target Name="samples_client">
<MSBuild Projects="client\samples_client.dproj"/>
</Target>
<Target Name="samples_client:Clean">
<MSBuild Projects="client\samples_client.dproj" Targets="Clean"/>
</Target>
<Target Name="samples_client:Make">
<MSBuild Projects="client\samples_client.dproj" Targets="Make"/>
</Target>
<Target Name="samples_session">
<MSBuild Projects="session\samples_session.dproj"/>
</Target>
<Target Name="samples_session:Clean">
<MSBuild Projects="session\samples_session.dproj" Targets="Clean"/>
</Target>
<Target Name="samples_session:Make">
<MSBuild Projects="session\samples_session.dproj" Targets="Make"/>
</Target>
<Target Name="Build">
<CallTarget Targets="samples_auth;samples_client;samples_session"/>
</Target>
<Target Name="Clean">
<CallTarget Targets="samples_auth:Clean;samples_client:Clean;samples_session:Clean"/>
</Target>
<Target Name="Make">
<CallTarget Targets="samples_auth:Make;samples_client:Make;samples_session:Make"/>
</Target>
<Import Project="$(BDS)\Bin\CodeGear.Group.Targets" Condition="Exists('$(BDS)\Bin\CodeGear.Group.Targets')"/>
</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
program samples;
program samples_auth;

{$APPTYPE CONSOLE}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
program samples;
program samples_client;

{$APPTYPE CONSOLE}

Expand Down

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions samples/delphi/session/boss-lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"hash": "50dc00b07cdce99d02ee2c83174db89c",
"updated": "2022-03-01T10:54:43.2242997-03:00",
"installedModules": {
"github.com/andre-djsystem/hashlib4pascal": {
"name": "hashlib4pascal",
"version": "1.0.0",
"hash": "8528036224699ab509c7d8f31d691ffb",
"artifacts": {},
"failed": false,
"changed": false
},
"github.com/hashload/horse": {
"name": "horse",
"version": "3.0.0",
"hash": "195e14c9c225f81a8c69d18b5118c149",
"artifacts": {},
"failed": false,
"changed": false
},
"github.com/hashload/horse-jwt": {
"name": "horse-jwt",
"version": "2.0.8",
"hash": "583190e995fecfc4e9a81406c77fe6b1",
"artifacts": {},
"failed": false,
"changed": false
},
"github.com/hashload/jhonson": {
"name": "jhonson",
"version": "1.1.4",
"hash": "7f48e0509b98505a12fa80ee39b53aea",
"artifacts": {},
"failed": false,
"changed": false
},
"github.com/paolo-rossi/delphi-jose-jwt": {
"name": "delphi-jose-jwt",
"version": "v3.0.3",
"hash": "d8b6ff80aff10e83d87219f45fa43ffb",
"artifacts": {},
"failed": false,
"changed": false
}
}
}
14 changes: 14 additions & 0 deletions samples/delphi/session/boss.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "samples_session",
"description": "",
"version": "1.0.0",
"homepage": "",
"mainsrc": "./",
"projects": [],
"dependencies": {
"github.com/hashload/horse": "^3.0.0",
"github.com/hashload/horse-jwt": "^2.0.8",
"github.com/hashload/jhonson": "^1.1.4",
"github.com/paolo-rossi/delphi-jose-jwt": "^v3.0.3"
}
}
90 changes: 90 additions & 0 deletions samples/delphi/session/samples_session.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
program samples_session;

{$APPTYPE CONSOLE}

{$R *.res}

uses
Horse,
Horse.Jhonson,
Horse.JWT,
JOSE.Core.JWT,
JOSE.Core.Builder,
JOSE.Types.JSON,
System.SysUtils,
System.JSON;

type
TMyClaims = class(TJWTClaims)
private
function GetuserId: string;
procedure SetuserId(const Value: string);

public
property userId: string read GetuserId write SetuserId;
end;

{ TMyClaims }

function TMyClaims.GetuserId: string;
begin
Result := TJSONUtils.GetJSONValue('userId', FJSON).AsString;
end;

procedure TMyClaims.SetuserId(const Value: string);
begin
TJSONUtils.SetJSONValueFrom<string>('userId', Value, FJSON);
end;

begin
{$IFDEF MSWINDOWS}
ReportMemoryLeaksOnShutdown := True;
IsConsole := False;
{$ENDIF}

THorse.Use(Jhonson);

THorse.Get('/auth',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
LJWT: TJWT;
LClaims: TMyClaims;
LToken: String;
begin
LJWT := TJWT.Create(TMyClaims);
try
LClaims := TMyClaims(LJWT.Claims);

LClaims.Issuer := 'Horse';
LClaims.Subject := 'Vinicius Sanchez';
LClaims.Expiration := Now + 1;
LClaims.userId := '1234';
LToken := TJOSE.SHA256CompactToken('my-private-key', LJWT);
Res.Send(TJSONObject.Create(TJSONPair.Create('token', LToken)));
finally
LJWT.Free;
end;
end);

THorse
.AddCallback(HorseJWT('my-private-key', THorseJWTConfig.New.SessionClass(TMyClaims)))
.Get('ping',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
var
LJSON: TJSONObject;
LSession: TMyClaims;
begin
LSession := Req.Session<TMyClaims>;
LJSON := TJSONObject.Create;
LJSON.AddPair('userId', LSession.userId);
Res.Send<TJSONObject>(LJSON);
end);

THorse.Listen(9000,
procedure(Horse: THorse)
begin
System.Writeln(Format('Server is running in %d. Press Enter to finish...', [Horse.Port]));
System.Readln;
end);

end.
Loading

0 comments on commit 3176449

Please sign in to comment.