-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauthloginframeunit.pas
145 lines (119 loc) · 3.63 KB
/
oauthloginframeunit.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
unit OAuthLoginFrameUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fphttpclient, Forms, Controls, FramBrwz, HtmlView, HtmlGlobals, HTMLUn2;
type
{ TOAuthLoginFrame }
TOnLoginedCallback = procedure (Token: String; ExpiresIn, Id: Integer) of object;
TOAuthLoginFrame = class(TFrame)
FPHTTPClient1: TFPHTTPClient;
FrameBrowser1: TFrameBrowser;
procedure FPHTTPClient1Redirect(Sender: TObject; const ASrc: String;
var ADest: String);
procedure FrameBrowser1GetPostRequestEx(Sender: TObject; IsGet: Boolean;
const URL, Query, EncType, Referer: ThtString; Reload: Boolean;
var NewURL: ThtString; var DocType: ThtmlFileType; var Stream: TStream);
procedure FrameBrowser1ImageRequest(Sender: TObject; const SRC: ThtString;
var Stream: TStream);
private
Cookies: TStrings;
Redirected: Boolean;
FURL: String;
FToken: String;
public
procedure OpenLoginPage;
constructor Create(TheOwner: TComponent); override;
public
OnLogined: TOnLoginedCallback;
property Token: String read FToken;
end;
implementation
uses
URIParser, LazLoggerBase;
{$R *.lfm}
{ TOAuthLoginFrame }
procedure TOAuthLoginFrame.FPHTTPClient1Redirect(Sender: TObject; const ASrc: String;
var ADest: String);
begin
FURL := ADest;
Redirected := True;
end;
procedure TOAuthLoginFrame.FrameBrowser1GetPostRequestEx(Sender: TObject;
IsGet: Boolean; const URL, Query, EncType, Referer: ThtString;
Reload: Boolean; var NewURL: ThtString; var DocType: ThtmlFileType;
var Stream: TStream);
var
URLParts: TURI;
ParamList: TStringList;
begin
ParamList := TStringList.Create;
ParamList.Delimiter := '&';
ParamList.StrictDelimiter := True;
Redirected := False;
URLParts := ParseURI(url);
//if String(URL).IndexOf('css') > 0 then
// exit;
//FPHTTPClient1.Cookies := Cookies;
DebugLn(Query);
if IsGet then
begin
if URLParts.Protocol = 'file' then
begin
Stream := TFileStream.Create(URLParts.Host, fmOpenRead);
DocType := ThtmlFileType.HTMLType;
exit;
end;
Stream := TBytesStream.Create;
FPHTTPClient1.Get(URL, Stream);
DocType := THtmlFileType.HTMLType;
end
else
begin
Stream := TBytesStream.Create;
ParamList.DelimitedText := Query;
FPHTTPClient1.AddHeader('Referer', Referer);
DebugLn(ParamList.Text);
FPHTTPClient1.FormPost(URL, ParamList, Stream);
DocType := THtmlFileType.HTMLType;
end;
//Cookies := FPHTTPClient1.Cookies;
DebugLn(FPHTTPClient1.Cookies.Text);
if not Redirected then
FURL := URL
else
NewURL := FURL;
URLParts := ParseURI(FURL);
if URLParts.Document = 'blank.html' then
begin
ParamList.DelimitedText := URLParts.Bookmark;
FToken := ParamList.Values['access_token'];
OnLogined(ParamList.Values['access_token'],
ParamList.Values['expires_in'].ToInteger,
ParamList.Values['access_token'].ToInteger);
end;
ParamList.Free;
end;
procedure TOAuthLoginFrame.FrameBrowser1ImageRequest(Sender: TObject;
const SRC: ThtString; var Stream: TStream);
begin
Stream := TBytesStream.Create;
//Filename := FrameBrowser1.HTMLExpandFilename(SRC);
FPHTTPClient1.Get(SRC, Stream);
end;
procedure TOAuthLoginFrame.OpenLoginPage;
begin
FrameBrowser1.LoadURL('https://oauth.vk.com/authorize?client_id=7950449&scope=69662&redirect_uri=https://oauth.vk.com/blank.html&display=mobile&response_type=token&revoke=1');
Show;
end;
constructor TOAuthLoginFrame.Create(TheOwner: TComponent);
var
Proxy: TProxyData;
begin
inherited Create(TheOwner);
//Proxy := TProxyData.Create;
//Proxy.Host:='127.0.0.1';
//Proxy.Port:=8888;
//FPHTTPClient1.Proxy := Proxy;
end;
end.