# POAuth2
Simple OAuth2 client for Pascal.
## License
MIT
## Compatibility
OS
: Windows, Linux
Compiler
: Delphi, Free Pascal
## Limitations/Features
* Supported GrantTypes: user credentials, authorization code, client credentials
* Supported token types: Bearer, MAC (untested)
* Supports HTTP GET and POST.
* Server must return JSON.
* Refresh token must be returned with the access token.
## Usage
~~~pascal
var
client: TOAuth2Client;
res: TOAuth2Response;
cli: TIndyHttpClient;
begin
cli := TIndyHttpClient.Create;
client := TOAuth2Client.Create(cli);
try
client.Site := 'http://localhost';
client.GrantType := gtPassword;
client.UserName := 'testuser';
client.PassWord := 'testpass';
client.ClientId := 'testclient';
client.ClientSecret := 'testsecret';
resp := client.Get('/path/to/resource?name=value');
finally
client.Free;
cli.Free;
end;
end;
~~~
### If you have an AccessToken
~~~pascal
var
client: TOAuth2Client;
res: TOAuth2Response;
cli: TIndyHttpClient;
begin
cli := TIndyHttpClient.Create;
client := TOAuth2Client.Create(cli);
try
client.AccessToken := TOAuth2Token.Create; // Freed by client
with client.AccessToken do begin
TokenType := 'Bearer';
AccessToken := '486b9c03a389e6e747f19cf202afbb026036cf9a';
RefreshToken := '13664cc7c4b89e03fea2c4ab140ca546461f71cf';
ExpiresIn := 3600;
// Set after ExpiresIn overwrites calculated value
ExpiresAt := 42442.406256875
TokenType := 'Bearer';
end;
client.Site := 'http://localhost';
client.GrantType := gtPassword;
client.UserName := 'testuser';
client.PassWord := 'testpass';
client.ClientId := 'testclient';
client.ClientSecret := 'testsecret';
resp := client.Get('/path/to/resource?name=value');
finally
client.Free;
cli.Free;
end;
end;
~~~
### Lazarus
To compile the Lazarus demo (`laz` subdirectory) you need Indy 10 for Lazarus,
see [here](http://wiki.freepascal.org/Indy_with_Lazarus), download from [here](http://www.indyproject.org/Sockets/fpc/index.de.aspx).
If you can't use Indy for some reason, you must implement a `TOAuth2HttpClient` descendant which
implements the methods `Get` and `Post`.
### HTTPS
The Indy client (`TIndyHttpClient`) supports it, but you must copy the file `libeay32.dll`
and `ssleay32.dll` from the [OpenSLL](https://www.openssl.org/) distribution to the application directory.
If you compile a 64 Bit executeable you need the 64 Bit libraries. You can download OpenSSL binaries for
Windows [here](https://indy.fulgan.com/SSL/).
Depending on the version of OpenSSL you may get errors. Version 1.0.2a seems to work
fine, later versions may have some functions removed.
A quick solution for this problem would be to replace:
~~~pascal
@IdSslMethodV2 := LoadFunction(fn_SSLv2_method);
@IdSslMethodServerV2 := LoadFunction(fn_SSLv2_server_method);
@IdSslMethodClientV2 := LoadFunction(fn_SSLv2_client_method);
~~~
with:
~~~pascal
@IdSslMethodV2 := LoadFunction(fn_SSLv2_method, false);
@IdSslMethodServerV2 := LoadFunction(fn_SSLv2_server_method, false);
@IdSslMethodClientV2 := LoadFunction(fn_SSLv2_client_method, false);
~~~
in `IdSSLOpenSSLHeaders.pas`.
If it still failes, try also making these non-critical:
~~~pascal
@IdSslMethodV3 := LoadFunction(fn_SSLv3_method, false);
@IdSslMethodServerV3 := LoadFunction(fn_SSLv3_server_method, false);
@IdSslMethodClientV3 := LoadFunction(fn_SSLv3_client_method, false);
~~~
## Server
I used [oauth2-server-php](https://github.com/bshaffer/oauth2-server-php) as Server.
To extend the Resource controller to accept HTTP POST add a new route:
~~~php
class Resource
{
// Connects the routes in Silex
public static function addRoutes($routing)
{
$routing->get('/resource', array(new self(), 'resource'))->bind('access');
// Add a POST route
$routing->post('/resource', array(new self(), 'resourceFormSubmit'))->bind('resource_post');
}
// The new POST handler
public function resourceFormSubmit(Application $app)
{
// get the oauth server (configured in src/OAuth2Demo/Server/Server.php)
$server = $app['oauth_server'];
// get the oauth response (configured in src/OAuth2Demo/Server/Server.php)
$response = $app['oauth_response'];
if (!$server->verifyResourceRequest($app['request'], $response)) {
return $server->getResponse();
} else {
// return a fake API response - not that exciting
// @TODO return something more valuable, like the name of the logged in user
$api_response = array(
'friends' => array(
'john',
'matt',
'jane'
),
'get' => $app['request']->query->all(),
'post' => $app['request']->request->all()
);
return new Response(json_encode($api_response));
}
}
// The existing GET handler
public function resource(Application $app)
{
// get the oauth server (configured in src/OAuth2Demo/Server/Server.php)
$server = $app['oauth_server'];
// get the oauth response (configured in src/OAuth2Demo/Server/Server.php)
$response = $app['oauth_response'];
if (!$server->verifyResourceRequest($app['request'], $response)) {
return $server->getResponse();
} else {
// return a fake API response - not that exciting
// @TODO return something more valuable, like the name of the logged in user
$api_response = array(
'friends' => array(
'john',
'matt',
'jane'
),
'get' => $app['request']->query->all()
);
return new Response(json_encode($api_response));
}
}
}
~~~
### JSON required
To make the resource controller correctly return JSON:
~~~php
public function resource(Application $app)
{
// get the oauth server (configured in src/OAuth2Demo/Server/Server.php)
$server = $app['oauth_server'];
// get the oauth response (configured in src/OAuth2Demo/Server/Server.php)
$response = $app['oauth_response'];
if (!$server->verifyResourceRequest($app['request'], $response)) {
return $server->getResponse();
} else {
// return a fake API response - not that exciting
// @TODO return something more valuable, like the name of the logged in user
$api_response = array(
'friends' => array(
'john',
'matt',
'jane'
),
'get' => $app['request']->query->all()
);
// Add application/json as Content-Type header
return new Response(json_encode($api_response), 200, array('Content-Type' => 'application/json'));
}
}
~~~
## Credits
* JSON parser: [https://github.com/koldev/JsonParser](https://github.com/koldev/JsonParser)
## Get it
* [GitHub](https://github.com/stievie/POAuth2)
* [Win32 and Win64 Demo Binaries](https://github.com/stievie/POAuth2/releases)
没有合适的资源?快使用搜索试试~ 我知道了~
POAuth2-master_drinkopt_pascal_zip_源码
共49个文件
pas:28个
lfm:6个
res:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 73 浏览量
2021-10-02
15:02:25
上传
评论
收藏 158KB ZIP 举报
温馨提示
Auth2 for pascal library
资源详情
资源评论
资源推荐
收起资源包目录
POAuth2-master.zip (49个子文件)
POAuth2-master
uIndyClient.pas 6KB
HashRipeMD128.pas 14KB
HashSHA512Base.pas 28KB
test
json.json 187B
HashHaval.pas 33KB
uJson.pas 12KB
HashSHA1.pas 12KB
HashSHA512.pas 3KB
HashMD5.pas 8KB
LICENSE 1KB
uOAuth2HttpClient.pas 3KB
uOAuth2Tools.pas 6KB
delphi
POAuth2Test.dproj 11KB
frmMain.dfm 4KB
frmMain.pas 5KB
frmJson.pas 1006B
POAuth2Test.res 292KB
frmJson.dfm 2KB
POAuth2Test.dpr 318B
HashSHA384.pas 3KB
uOAuth2Token.pas 3KB
uOAuth2Client.pas 13KB
uOAuth2Config.pas 356B
.gitignore 187B
HashTiger.pas 33KB
uOAuth2Consts.pas 1KB
uOAuth2Hmac.pas 4KB
README.md 7KB
HashRipeMD160.pas 27KB
laz
dlgabout.pas 999B
poat.res 16KB
poat.ico 15KB
frmhistory.lfm 892B
frmlog.pas 3KB
dlgabout.lfm 23KB
poat.lpr 556B
frmhistory.pas 6KB
dlgoptions.pas 955B
frmmain.pas 13KB
frmhash.lfm 2KB
frmhash.pas 2KB
frmmain.lfm 9KB
dlgoptions.lfm 2KB
frmlog.lfm 1KB
poat.lpi 8KB
build.sh 53B
poat-48.ico 9KB
Hash.pas 6KB
HashSHA256.pas 24KB
共 49 条
- 1
浊池
- 粉丝: 48
- 资源: 4782
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0