-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from gabrielbaltazar/master
Delphi sample with Session
- Loading branch information
Showing
9 changed files
with
1,705 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
2 changes: 1 addition & 1 deletion
2
samples/delphi/auth/samples.dpr → samples/delphi/auth/samples_auth.dpr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
program samples; | ||
program samples_auth; | ||
|
||
{$APPTYPE CONSOLE} | ||
|
||
|
339 changes: 264 additions & 75 deletions
339
samples/delphi/auth/samples.dproj → samples/delphi/auth/samples_auth.dproj
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
samples/delphi/client/samples.dpr → samples/delphi/client/samples_client.dpr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
program samples; | ||
program samples_client; | ||
|
||
{$APPTYPE CONSOLE} | ||
|
||
|
322 changes: 263 additions & 59 deletions
322
samples/delphi/client/samples.dproj → samples/delphi/client/samples_client.dproj
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.