Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandolamp committed Feb 5, 2020
0 parents commit c37b7d1
Show file tree
Hide file tree
Showing 8 changed files with 1,220 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__history
__recovery
Win32
14 changes: 14 additions & 0 deletions DelphiOpenId.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
program DelphiOpenId;

uses
Vcl.Forms,
DelphiOpenIdKeyCloak in 'DelphiOpenIdKeyCloak.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
961 changes: 961 additions & 0 deletions DelphiOpenId.dproj

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions DelphiOpenId.dproj.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<BorlandProject>
<Transactions>
<Transaction>2016/08/26 20:00:57.000.846,=C:\Users\lampa\Documents\Embarcadero\Studio\Projects\Unit1.pas</Transaction>
<Transaction>2020/02/04 20:56:15.944,=E:\Documentos\Embarcadero\Studio\Projects\Unit1.pas</Transaction>
<Transaction>2020/02/04 22:02:12.971,E:\Documentos\Embarcadero\Studio\Projects\DelphiOpenIdKeycloak\DelphiOpenIdKeyCloak.pas=E:\Documentos\Embarcadero\Studio\Projects\Unit1.pas</Transaction>
<Transaction>2020/02/04 22:02:12.973,E:\Documentos\Embarcadero\Studio\Projects\DelphiOpenIdKeycloak\DelphiOpenIdKeyCloak.dfm=E:\Documentos\Embarcadero\Studio\Projects\Unit1.dfm</Transaction>
<Transaction>2020/02/04 22:02:43.948,E:\Documentos\Embarcadero\Studio\Projects\DelphiOpenIdKeycloak\DelphiOpenId.dproj=E:\Documentos\Embarcadero\Studio\Projects\Project1.dproj</Transaction>
</Transactions>
</BorlandProject>
Binary file added DelphiOpenId.res
Binary file not shown.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Delphi + Keycloak Simple Example

Example how to get a authentication token from keycloak using openid with grant type password.

## What i need to run the project?

1 - Configure a keycloack, you can use a docker image https://hub.docker.com/r/jboss/keycloak/

2 - Use the well known url of openId and paste on the field of formulary

3 - Fill the field username and passworkd

4 - Click on getToken

You can validate the token on jwt.io








118 changes: 118 additions & 0 deletions TPrincipal.dfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Token from keycloak'
ClientHeight = 353
ClientWidth = 640
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 16
Top = 15
Width = 22
Height = 13
Caption = 'User'
end
object Label2: TLabel
Left = 16
Top = 66
Width = 46
Height = 13
Caption = 'Password'
end
object Label3: TLabel
Left = 192
Top = 16
Width = 58
Height = 13
Caption = 'Well Known '
end
object Label4: TLabel
Left = 16
Top = 120
Width = 41
Height = 13
Caption = 'Client ID'
end
object Button1: TButton
Left = 16
Top = 164
Width = 75
Height = 25
Caption = 'Get Token'
TabOrder = 0
OnClick = Button1Click
end
object edtUsername: TEdit
Left = 16
Top = 34
Width = 121
Height = 21
TabOrder = 1
end
object edtPassword: TEdit
Left = 16
Top = 85
Width = 121
Height = 21
PasswordChar = '*'
TabOrder = 2
end
object edtWellKnown: TEdit
Left = 184
Top = 34
Width = 425
Height = 21
TabOrder = 3
Text =
'http://localhost:8080/auth/realms/master/.well-known/openid-conf' +
'iguration'
end
object edtClientId: TEdit
Left = 16
Top = 139
Width = 121
Height = 21
TabOrder = 4
end
object memoToken: TMemo
Left = 184
Top = 61
Width = 425
Height = 252
TabOrder = 5
end
object CopyToken: TButton
Left = 184
Top = 320
Width = 75
Height = 25
Caption = 'Copy'
TabOrder = 6
OnClick = CopyTokenClick
end
object RESTClient1: TRESTClient
Params = <>
Left = 24
Top = 200
end
object RESTRequest1: TRESTRequest
Client = RESTClient1
Params = <>
Response = RESTResponse1
SynchronizedEvents = False
Left = 64
Top = 200
end
object RESTResponse1: TRESTResponse
Left = 104
Top = 200
end
end
91 changes: 91 additions & 0 deletions TPrincipal.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
unit TPrincipal;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, REST.Types, Data.Bind.Components,
Data.Bind.ObjectScope, REST.Client, Vcl.StdCtrls,System.JSON,Vcl.Clipbrd;

type
TForm1 = class(TForm)
Button1: TButton;
edtUsername: TEdit;
edtPassword: TEdit;
Label1: TLabel;
Label2: TLabel;
edtWellKnown: TEdit;
Label3: TLabel;
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
RESTResponse1: TRESTResponse;
edtClientId: TEdit;
Label4: TLabel;
memoToken: TMemo;
CopyToken: TButton;
procedure Button1Click(Sender: TObject);
procedure CopyTokenClick(Sender: TObject);
private
function GetJsonValue(psValue, psJson: String):String;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
sTokenEndpoint:String;
sJwksUri:String;
begin
RESTClient1.BaseURL := edtWellKnown.Text;
RESTRequest1.Execute;

if not RESTResponse1.Status.Success then
raise Exception.Create('Sorry, was unable to get token url');

//first, get token url
RESTResponse1.GetSimpleValue('token_endpoint',sTokenEndpoint);
//get certificate to validate token on jwt.io
RestResponse1.GetSimpleValue('jwks_uri',sJwksUri);

//Set openid params to authenticate
RESTClient1.ResetToDefaults;
RESTClient1.BaseURL := sTokenEndpoint;
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.AddParameter('client_id',edtClientId.Text,TRESTRequestParameterKind.pkGETorPOST);
RESTRequest1.AddParameter('grant_type','password',TRESTRequestParameterKind.pkGETorPOST);
Restrequest1.AddParameter('username',edtusername.Text,TRESTRequestParameterKind.pkGETorPOST);
Restrequest1.AddParameter('password',edtPassword.Text,TRESTRequestParameterKind.pkGETorPOST);

RESTRequest1.Execute;
memoToken.Text := RESTRequest1.Response.Content;
end;

procedure TForm1.CopyTokenClick(Sender: TObject);
begin
ClipBoard.AsText := GetJsonValue('access_token',memoToken.Text);
end;

function TForm1.GetJsonValue(psValue:String;psJson:String):string;
var
json:TJSONObject;
jsonValue:TJsonValue;
begin
json := TJSONObject.Create;
try
jsonValue := json.ParseJSONValue(psJson);
result := jsonValue.FindValue(psvalue).Value;
finally
FreeAndNil(json);
end;

end;

end.

0 comments on commit c37b7d1

Please sign in to comment.