-
Notifications
You must be signed in to change notification settings - Fork 17
/
uOAuth2Tools.pas
280 lines (254 loc) · 6.08 KB
/
uOAuth2Tools.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{
Simple OAuth2 client
(C) 2016, Stefan Ascher
}
unit uOAuth2Tools;
{$IFDEF FPC}
{$mode objfpc}
{$H+}
{$ENDIF}
interface
uses
SysUtils, Hash;
type
TUrlParts = record
Protocol: string;
Host: string;
Path: string;
Query: string;
Port: Word;
User: string;
Pass: string;
end;
function ParseUrl(const AUrl: string): TUrlParts;
function BuildUrl(const AParts: TUrlParts): string;
function EncodeBase64(const S: string): AnsiString;
function DecodeBase64(const S: AnsiString): string;
function EncodeCredentials(const AUsername, APassword: string): string;
function GetBasicAuthHeader(const AUsername, APassword: string): string;
function RemoveLeadingSlash(const S: string): string;
function AddLeadingSlash(const S: string): string;
function RemoveTrailingSlash(const S: string): string;
function AddTrailingSlash(const S: string): string;
function IsJson(const AContentType: string): boolean;
function IsAccessDenied(const ACode: integer): boolean;
implementation
uses
uOAuth2Consts
{$IFDEF FPC}
, StrUtils
{$ENDIF}
;
const
BASE64_CODES: AnsiString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/';
function GetBasicAuthHeader(const AUsername, APassword: string): string;
begin
Result := Format('%s %s', [OATUH2_BASIC, EncodeCredentials(AUsername, APassword)]);
end;
function EncodeCredentials(const AUsername, APassword: string): string;
var
cred: string;
begin
cred := Format('%s:%s', [AUsername, APassword]);
Result := string(EncodeBase64(cred));
end;
function IsAccessDenied(const ACode: integer): boolean;
begin
Result := (ACode = HTTP_UNAUTHORIZED) or (ACode = HTTP_FORBIDDEN);
end;
function IsJson(const AContentType: string): boolean;
const
CT_JSON: array[0..4] of string = (
'application/json',
'application/x-javascript',
'text/javascript',
'text/x-javascript',
'text/x-json'
);
var
i, p: integer;
val: string;
begin
val := AContentType;
p := Pos(';', val);
if p <> 0 then begin
Delete(val, p, MaxInt);
end;
for i := Low(CT_JSON) to High(CT_JSON) do begin
if CompareText(CT_JSON[i], val) = 0 then begin
Result := true;
Exit;
end;
end;
Result := false;
end;
function RemoveLeadingSlash(const S: string): string;
begin
Result := S;
if Result <> '' then begin
if Result[1] = '/' then
Delete(Result, 1, 1);
end;
end;
function AddLeadingSlash(const S: string): string;
begin
Result := S;
if Result <> '' then begin
if Result[1] <> '/' then
Result := '/' + Result;
end;
end;
function RemoveTrailingSlash(const S: string): string;
begin
Result := S;
if Result <> '' then begin
if Result[Length(Result)] = '/' then
Delete(Result, Length(Result), 1);
end;
end;
function AddTrailingSlash(const S: string): string;
begin
Result := S;
if Result <> '' then begin
if Result[Length(Result)] <> '/' then
Result := Result + '/';
end;
end;
function BuildUrl(const AParts: TUrlParts): string;
begin
Result := AParts.Protocol + '://';
if AParts.User <> '' then begin
Result := Result + AParts.User;
if AParts.Pass <> '' then begin
Result := Result + ':' + AParts.Pass;
end;
Result := Result + '@';
end;
Result := Result + AParts.Host;
if AParts.Port <> 0 then begin
if ((AParts.Protocol = 'http') and (AParts.Port <> 80)) or
((AParts.Protocol = 'https') and (AParts.Port <> 443)) then
Result := Format('%s:%d', [Result, AParts.Port]);
end;
if AParts.Path <> '' then
Result := Result + AParts.Path
else
Result := Result + '/';
if AParts.Query <> '' then
Result := Result + '?' + AParts.Query;
end;
{
https://user:pass@server:1337/path/to/file?query=somevalue
}
function ParseUrl(const AUrl: string): TUrlParts;
var
p, p2, p3: integer;
userpass, h, port, fq: string;
begin
with Result do begin
Protocol := '';
Host := '';
Path := '';
Query:= '';
Port := 0;
User := '';
Pass := '';
end;
p := Pos('://', AUrl);
if p = 0 then
Exit;
Result.Protocol := LowerCase(Copy(AUrl, 1, p - 1));
Inc(p, 3);
p2 := Pos('@', AUrl);
if p2 <> 0 then begin
userpass := Copy(AUrl, p, p2 - p);
p3 := Pos(':', userpass);
if p3 <> 0 then begin
Result.User := Copy(userpass, 1, p3 - 1);
Result.Pass := Copy(userpass, p3 + 1, MaxInt);
end else begin
Result.User := userpass;
end;
p := p2 + 1;
end;
p2 := {$IFDEF FPC}PosEx{$ELSE}Pos{$ENDIF}('/', AUrl, p);
if p2 = 0 then
Exit;
h := Copy(AUrl, p, p2 - p);
p3 := Pos(':', h);
if (p3 <> 0) then begin
Result.Host := Copy(h, 1, p3 - 1);
port := Copy(h, p3 + 1, MaxInt);
Result.Port := StrToIntDef(port, 0);
end else begin
Result.Host := h;
end;
if (Result.Port = 0) and (Result.Protocol <> '') then begin
if Result.Protocol = 'http' then
Result.Port := 80
else if Result.Protocol = 'https' then
Result.Port := 443;
end;
p := p2;
fq := Copy(AUrl, p, MaxInt);
p2 := Pos('?', fq);
if p2 <> 0 then begin
Result.Path := Copy(fq, 1, p2 - 1);
Result.Query := Copy(fq, p2 + 1, MaxInt);
end else begin
Result.Path := fq;
end;
end;
function EncodeBase64(const S: string): AnsiString;
var
i: Integer;
a: Integer;
x: Integer;
b: Integer;
begin
Result := '';
a := 0;
b := 0;
for i := 1 to Length(s) do begin
x := Ord(S[i]);
b := b * 256 + x;
a := a + 8;
while a >= 6 do begin
a := a - 6;
x := b div (1 shl a);
b := b mod (1 shl a);
Result := Result + BASE64_CODES[x + 1];
end;
end;
if a > 0 then begin
x := b shl (6 - a);
Result := Result + BASE64_CODES[x + 1];
end;
end;
function DecodeBase64(const S: AnsiString): string;
var
i: Integer;
a: Integer;
x: Integer;
b: Integer;
begin
Result := '';
a := 0;
b := 0;
for i := 1 to Length(s) do begin
x := Pos(s[i], BASE64_CODES) - 1;
if x >= 0 then begin
b := b * 64 + x;
a := a + 6;
if a >= 8 then begin
a := a - 8;
x := b shr a;
b := b mod (1 shl a);
x := x mod 256;
Result := Result + Chr(x);
end;
end else
Exit;
end;
end;
end.