-
Notifications
You must be signed in to change notification settings - Fork 4
/
fmLogin.pas
173 lines (146 loc) · 4.65 KB
/
fmLogin.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
unit fmLogin;
interface
uses
Winapi.Windows
, Winapi.Messages
, Winapi.ActiveX
, System.SysUtils
, System.Variants
, System.Classes
, System.IniFiles
, System.Net.URLClient
, Vcl.Controls
, Vcl.Forms
, Vcl.Dialogs
, Vcl.OleCtrls
, Vcl.Edge
, WebView2
;
type
TfrmLogin = class(TForm)
EdgeBrowser: TEdgeBrowser;
procedure EdgeBrowserNavigationCompleted(Sender: TCustomEdgeBrowser; IsSuccess: Boolean; WebErrorStatus: TOleEnum);
procedure EdgeBrowserNavigationStarting(Sender: TCustomEdgeBrowser; Args: TNavigationStartingEventArgs);
procedure EdgeBrowserWebResourceRequested(Sender: TCustomEdgeBrowser; Args: TWebResourceRequestedEventArgs);
procedure FormCreate(Sender: TObject);
procedure EdgeBrowserCreateWebViewCompleted(Sender: TCustomEdgeBrowser; AResult: HRESULT);
private
{ Private declarations }
RedirectUrl : string;
public
{ Public declarations }
procedure Login(url:string; inRedirecturl: string);
end;
TURIParamHelper = record Helper for TURI
function ParamExists(paramName: string): Boolean;
end;
implementation
{$R *.dfm}
uses
fmIntuitDemo
, dmIntuit
;
procedure TfrmLogin.EdgeBrowserCreateWebViewCompleted(Sender: TCustomEdgeBrowser; AResult: HRESULT);
begin
if Succeeded(AResult) then
begin
// Set the OnWebResourceRequested event handler
EdgeBrowser.AddWebResourceRequestedFilter('*', COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);
EdgeBrowser.OnWebResourceRequested := EdgeBrowserWebResourceRequested;
end
else
ShowMessage('Failed to initialize WebView2');
end;
procedure TfrmLogin.EdgeBrowserNavigationCompleted(Sender: TCustomEdgeBrowser;
IsSuccess: Boolean; WebErrorStatus: TOleEnum);
var
uri : TURI;
url : string;
begin
url := Sender.LocationURL;
if URL.StartsWith('about:') then Exit;
uri := TURI.Create(URL);
OutputDebugString(PChar('Browser: ' + uri.ToString));
if not uri.ParamExists('code') then
Exit;
dmIntuitAPI.HandleOAuthCodeRedirect(uri);
{ code := uri.ParameterByName['code'];
state := uri.ParameterByName['state'];
dmIntuitAPI.RealmId := uri.ParameterByName['realmId'];
dmIntuitAPI.OAuth2Authenticator1.AuthCode := code;
Form1.Memo1.Lines.Add('url:'+URL);
dmIntuitAPI.OAuth2Authenticator1.ChangeAuthCodeToAccesToken;
Form1.Memo1.Lines.Add('Access Granted');
Form1.Memo1.Lines.Add('RefreshToken=' + dmIntuitAPI.OAuth2Authenticator1.RefreshToken);
Form1.Memo1.Lines.Add('Access Token');
Form1.Memo1.Lines.Add(dmIntuitAPI.OAuth2Authenticator1.AccessToken);
dmIntuitAPI.TokenManager.StoreEncryptedToken(dmIntuitAPI.OAuth2Authenticator1.RefreshToken);
dmIntuitAPI.TokenManager.StoreExtraData('RealmId', dmIntuitAPI.RealmId);}
Close;
end;
procedure TfrmLogin.EdgeBrowserNavigationStarting(Sender: TCustomEdgeBrowser; Args: TNavigationStartingEventArgs);
var
uri : TURI;
url : string;
begin
url := Sender.LocationURL;
if URL.StartsWith('about:') then Exit;
if url.IsEmpty then Exit;
uri := TURI.Create(URL);
OutputDebugString(PChar('Browser: ' + uri.ToString));
if not uri.ParamExists('code') then
Exit;
dmIntuitAPI.HandleOAuthCodeRedirect(uri);
Close;
end;
procedure TfrmLogin.EdgeBrowserWebResourceRequested(Sender: TCustomEdgeBrowser;
Args: TWebResourceRequestedEventArgs);
var
Request: ICoreWebView2WebResourceRequest;
Response: ICoreWebView2WebResourceResponse;
URL: PChar;
urlString : string;
EmptyStream: IStream;
begin
Args.ArgsInterface.Get_Request(Request);
Request.Get_uri(URL);
urlString := URL;
// Check if the request URL matches the domain we want to block
// need to update this to your domain so that the request doesn't actually get sent to server
if urlString.StartsWith(Redirecturl) then
begin
// Create a custom response to block the request
EmptyStream := TStreamAdapter.Create(TMemoryStream.Create, soOwned);
EdgeBrowser.EnvironmentInterface.CreateWebResourceResponse(
EmptyStream, 200, 'OK', 'Content-Type: text/html', Response);
Args.ArgsInterface.Set_Response(Response);
end;
end;
procedure TfrmLogin.FormCreate(Sender: TObject);
begin
// Start creating the WebView2 environment
EdgeBrowser.OnCreateWebViewCompleted := EdgeBrowserCreateWebViewCompleted;
EdgeBrowser.CreateWebView;
end;
procedure TfrmLogin.Login(url: string; inRedirecturl: string);
begin
Redirecturl := inRedirecturl;
EdgeBrowser.Navigate(url);
ShowModal;
end;
{ TURIParamHelper }
function TURIParamHelper.ParamExists(paramName: string): Boolean;
var
i : Integer;
begin
Result := False;
for i := 0 to Length(Params)-1 do
begin
if Params[i].Name='code' then
begin
Result := True;
Exit;
end;
end;
end;
end.