Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
oauthservice.php
1
<?php
9
namespace
Bitrix\Rest
;
10
11
12
use
Bitrix\Main\ArgumentException
;
13
use
Bitrix\Main\Context
;
14
use
Bitrix\Main\Localization\Loc
;
15
use
Bitrix\Main\SystemException
;
16
use
Bitrix\Main\Web\HttpClient
;
17
use
Bitrix\Main\Web\Json
;
18
use
Bitrix\Rest\OAuth\Engine
;
19
20
Loc::loadMessages
(__FILE__);
21
22
if
(!defined(
"BITRIX_OAUTH_URL"
))
23
{
24
$defaultValue = \Bitrix\Main\Config\Option::get(
'rest'
,
'oauth_server'
,
'https://oauth.bitrix.info'
);
25
define(
"BITRIX_OAUTH_URL"
, $defaultValue);
26
}
27
28
if
(!defined(
'BITRIXREST_URL'
))
29
{
30
define(
'BITRIXREST_URL'
, BITRIX_OAUTH_URL);
31
}
32
33
34
class
OAuthService
35
{
36
const
SERVICE_URL
= BITRIXREST_URL;
37
const
CLIENT_TYPE
=
'B'
;
38
39
const
REGISTER
=
"/oauth/register/"
;
40
41
protected
static
$engine
=
null
;
42
46
public
static
function
getEngine
()
47
{
48
if
(!static::$engine)
49
{
50
static::$engine =
new
Engine
();
51
}
52
53
return
static::$engine;
54
}
55
56
public
static
function
register
()
57
{
58
$httpClient =
new
HttpClient
();
59
60
$queryParams = array(
61
"redirect_uri"
=> static::getRedirectUri(),
62
"type"
=> static::CLIENT_TYPE,
63
);
64
65
$memberId = \CRestUtil::getMemberId();
66
if
($memberId !==
null
)
67
{
68
$queryParams[
"member_id"
] = $memberId;
69
}
70
71
$queryParams = \CRestUtil::signLicenseRequest($queryParams, static::getEngine()->getLicense());
72
73
$httpResult = $httpClient->post(static::SERVICE_URL.static::REGISTER, $queryParams);
74
75
try
76
{
77
$result = Json::decode($httpResult);
78
}
79
catch
(
ArgumentException
$e)
80
{
81
$result = array(
82
"error"
=>
"Wrong answer from service: "
.$httpResult,
83
);
84
}
85
86
if
($result[
"error"
])
87
{
88
throw
new
SystemException
($result[
"error"
]);
89
}
90
else
91
{
92
static::getEngine()->setAccess($result);
93
}
94
}
95
96
public
static
function
unregister
()
97
{
98
if
(static::getEngine()->isRegistered())
99
{
100
static::getEngine()->clearAccess();
101
}
102
}
103
104
public
static
function
getMemberId
()
105
{
106
if
(static::getEngine()->isRegistered())
107
{
108
return
md5(static::getEngine()->getClientId());
109
}
110
else
111
{
112
return
null
;
113
}
114
}
115
116
public
static
function
getRedirectUri
()
117
{
118
$request =
Context::getCurrent
()->getRequest();
119
$server =
Context::getCurrent
()->getServer();
120
121
$host = defined(
'BX24_HOST_NAME'
) ? BX24_HOST_NAME : $server->getHttpHost();
122
123
return
($request->isHttps() ?
'https'
:
'http'
).
'://'
.preg_replace(
"/:(443|80)$/"
,
""
, $host);
124
}
125
}
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\Context\getCurrent
static getCurrent()
Definition
context.php:241
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Main\Web\HttpClient
Definition
httpclient.php:24
Bitrix\Main\Web\Json
Definition
json.php:11
Bitrix\Rest\OAuth\Engine
Definition
engine.php:16
Bitrix\Rest\OAuthService
Definition
oauthservice.php:35
Bitrix\Rest\OAuthService\unregister
static unregister()
Definition
oauthservice.php:96
Bitrix\Rest\OAuthService\getEngine
static getEngine()
Definition
oauthservice.php:46
Bitrix\Rest\OAuthService\getMemberId
static getMemberId()
Definition
oauthservice.php:104
Bitrix\Rest\OAuthService\$engine
static $engine
Definition
oauthservice.php:41
Bitrix\Rest\OAuthService\CLIENT_TYPE
const CLIENT_TYPE
Definition
oauthservice.php:37
Bitrix\Rest\OAuthService\REGISTER
const REGISTER
Definition
oauthservice.php:39
Bitrix\Rest\OAuthService\getRedirectUri
static getRedirectUri()
Definition
oauthservice.php:116
Bitrix\Rest\OAuthService\SERVICE_URL
const SERVICE_URL
Definition
oauthservice.php:36
Bitrix\Main\Context
Definition
culture.php:9
Bitrix\Rest
Definition
accessexception.php:2
modules
rest
lib
oauthservice.php
Создано системой
1.10.0