Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
oauthservice.php
1<?php
9namespace Bitrix\Rest;
10
11
19
20Loc::loadMessages(__FILE__);
21
22if(!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
28if(!defined('BITRIXREST_URL'))
29{
30 define('BITRIXREST_URL', BITRIX_OAUTH_URL);
31}
32
33
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}
static getCurrent()
Definition context.php:241
static loadMessages($file)
Definition loc.php:64