-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathChronogears.Form.Authorize.pas
95 lines (76 loc) · 2.03 KB
/
Chronogears.Form.Authorize.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
unit Chronogears.Form.Authorize;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.WebBrowser,
Winapi.Windows,
Chronogears.Azure.Model;
type
TAuthorizeForm = class(TForm)
WebBrowser: TWebBrowser;
procedure WebBrowserDidFinishLoad(ASender: TObject);
private
FConnection: TAzureConnection;
{ Private declarations }
public
{ Public declarations }
procedure GetAuthToken(AConnection: TAzureConnection);
end;
var
AuthorizeForm: TAuthorizeForm;
implementation
{$R *.fmx}
uses
System.NetEncoding, System.Net.URLClient;
{ TAuthorizeForm }
procedure TAuthorizeForm.GetAuthToken(AConnection: TAzureConnection);
var
LURL: string;
begin
FConnection := AConnection;
LURL := FConnection.AuthorizeEndPoint +
'?client_id=' + FConnection.ClientId +
'&response_type=code' +
'&redirect_uri=' + TNetEncoding.URL.Encode(FConnection.RedirectURL) +
'&response_mode=query' +
'&state=1' +
'&scope=openid';
//'&scope=' + TNetEncoding.URL.Encode(FConnection.Resource);
WebBrowser.URL := LURL;
ShowModal;
end;
procedure TAuthorizeForm.WebBrowserDidFinishLoad(ASender: TObject);
var
LURI: TURI;
LParam: TNameValuePair;
LError: string;
LErrorDescription: string;
LMessage: string;
begin
if WebBrowser.URL.StartsWith(FConnection.RedirectURL) then
begin
LURI := TURI.Create(WebBrowser.URL);
for LParam in LURI.Params do
begin
if LParam.Name = 'code' then
begin
WebBrowser.Stop;
FConnection.AuthCode := LParam.Value;
ModalResult := mrOk;
Hide;
Exit;
end
else if LParam.Name = 'error' then
begin
LError := LParam.Value;
LErrorDescription := LURI.ParameterByName['error_description'];
LMessage := Format('Error: %s (%s)', [LErrorDescription, LError]);
ShowMessage(LMessage);
ModalResult := mrCancel;
Hide;
Exit;
end;
end;
end;
end;
end.