Skip to content

Commit 26fdd6b

Browse files
David DesbergDavid Desberg
authored andcommitted
Merge pull request #322 from Fruitware/bitrix24-service
Add bitrix24 service
2 parents b617831 + da95262 commit 26fdd6b

File tree

5 files changed

+357
-0
lines changed

5 files changed

+357
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Included service implementations
6161
- OAuth2
6262
- Amazon
6363
- BitLy
64+
- Bitrix24
6465
- Box
6566
- Dailymotion
6667
- DeviantArt

examples/bitrix24.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* Example of retrieving an authentication token of the Bitrix24 service
5+
*
6+
* PHP version 5.4
7+
*
8+
* @author David Desberg <[email protected]>
9+
* @author Pieter Hordijk <[email protected]>
10+
* @copyright Copyright (c) 2012 The authors
11+
* @license http://www.opensource.org/licenses/mit-license.html MIT License
12+
*/
13+
14+
use OAuth\OAuth2\Service\GitHub;
15+
use OAuth\Common\Storage\Session;
16+
use OAuth\Common\Consumer\Credentials;
17+
18+
/**
19+
* Bootstrap the example
20+
*/
21+
require_once __DIR__ . '/bootstrap.php';
22+
23+
// Session storage
24+
$storage = new Session();
25+
26+
// Setup the credentials for the requests
27+
$credentials = new Credentials(
28+
$servicesCredentials['bitrix24']['key'],
29+
$servicesCredentials['bitrix24']['secret'],
30+
$currentUri->getAbsoluteUri()
31+
);
32+
33+
// Instantiate the GitHub service using the credentials, http client and storage mechanism for the token
34+
35+
$yourDomain = new \OAuth\Common\Http\Uri\Uri('https://'.$servicesCredentials['bitrix24']['domain']);
36+
/** @var $bitrix24 \OAuth\OAuth2\Service\Bitrix24 */
37+
$bitrix24 = $serviceFactory->createService('Bitrix24', $credentials, $storage, array('user'), $yourDomain);
38+
39+
if (!empty($_GET['code'])) {
40+
// This was a callback request from bitrix24, get the token
41+
$bitrix24->requestAccessToken($_GET['code']);
42+
43+
$response = json_decode($bitrix24->request('user.current'), true);
44+
$userInfo = $response['result'];
45+
46+
// Show some of the resultant data
47+
echo 'Your email on your bitrix24 account is ' . $userInfo['EMAIL'];
48+
49+
} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
50+
$url = $bitrix24->getAuthorizationUri();
51+
header('Location: ' . $url);
52+
53+
} else {
54+
$url = $currentUri->getRelativeUri() . '?go=go';
55+
echo "<a href='$url'>Login with Bitrix24!</a>";
56+
}

examples/init.example.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
'key' => '',
2828
'secret' => '',
2929
),
30+
'bitrix24' => array(
31+
'key' => '',
32+
'secret' => '',
33+
),
3034
'box' => array(
3135
'key' => '',
3236
'secret' => '',

src/OAuth/OAuth2/Service/Bitrix24.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace OAuth\OAuth2\Service;
4+
5+
use OAuth\OAuth2\Token\StdOAuth2Token;
6+
use OAuth\Common\Http\Exception\TokenResponseException;
7+
use OAuth\Common\Http\Uri\Uri;
8+
use OAuth\Common\Consumer\CredentialsInterface;
9+
use OAuth\Common\Http\Client\ClientInterface;
10+
use OAuth\Common\Storage\TokenStorageInterface;
11+
use OAuth\Common\Http\Uri\UriInterface;
12+
13+
class Bitrix24 extends AbstractService
14+
{
15+
const SCOPE_DEPARTMENT = 'department';
16+
const SCOPE_CRM = 'crm';
17+
const SCOPE_CALENDAR = 'calendar';
18+
const SCOPE_USER = 'user';
19+
const SCOPE_ENTITY = 'entity';
20+
const SCOPE_TASK = 'task';
21+
const SCOPE_TASKS_EXTENDED = 'tasks_extended';
22+
const SCOPE_IM = 'im';
23+
const SCOPE_LOG = 'log';
24+
const SCOPE_SONET_GROUP = 'sonet_group';
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function getAuthorizationEndpoint()
30+
{
31+
return new Uri(sprintf('%s/oauth/authorize/', $this->baseApiUri));
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function getAccessTokenEndpoint()
38+
{
39+
return new Uri(sprintf('%s/oauth/token/', $this->baseApiUri));
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function requestAccessToken($code, $state = null)
46+
{
47+
if (null !== $state) {
48+
$this->validateAuthorizationState($state);
49+
}
50+
51+
$responseBody = $this->httpClient->retrieveResponse(
52+
$this->getAccessTokenUri($code),
53+
array(),
54+
$this->getExtraOAuthHeaders(),
55+
'GET'
56+
);
57+
58+
$token = $this->parseAccessTokenResponse($responseBody);
59+
$this->storage->storeAccessToken($this->service(), $token);
60+
61+
return $token;
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function getAccessTokenUri($code)
68+
{
69+
$parameters = array(
70+
'code' => $code,
71+
'client_id' => $this->credentials->getConsumerId(),
72+
'client_secret' => $this->credentials->getConsumerSecret(),
73+
'redirect_uri' => $this->credentials->getCallbackUrl(),
74+
'grant_type' => 'authorization_code',
75+
'scope' => $this->scopes
76+
);
77+
78+
$parameters['scope'] = implode(' ', $this->scopes);
79+
80+
// Build the url
81+
$url = $this->getAccessTokenEndpoint();
82+
foreach ($parameters as $key => $val) {
83+
$url->addToQuery($key, $val);
84+
}
85+
86+
return $url;
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
protected function parseAccessTokenResponse($responseBody)
93+
{
94+
$data = json_decode($responseBody, true);
95+
96+
if (null === $data || !is_array($data)) {
97+
throw new TokenResponseException('Unable to parse response.');
98+
} elseif (isset($data['error'])) {
99+
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
100+
}
101+
102+
$token = new StdOAuth2Token();
103+
$token->setAccessToken($data['access_token']);
104+
$token->setLifetime($data['expires_in']);
105+
106+
if (isset($data['refresh_token'])) {
107+
$token->setRefreshToken($data['refresh_token']);
108+
unset($data['refresh_token']);
109+
}
110+
111+
unset($data['access_token']);
112+
unset($data['expires_in']);
113+
114+
$token->setExtraParams($data);
115+
116+
return $token;
117+
}
118+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
namespace OAuthTest\Unit\OAuth2\Service;
4+
5+
use OAuth\OAuth2\Service\Bitrix24;
6+
use OAuth\Common\Token\TokenInterface;
7+
8+
class Bitrix24Test extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @covers OAuth\OAuth2\Service\Bitrix24::__construct
12+
*/
13+
public function testConstructCorrectInstanceWithCustomUri()
14+
{
15+
$service = new Bitrix24(
16+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
17+
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
18+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
19+
array(),
20+
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
21+
);
22+
23+
$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
24+
}
25+
26+
/**
27+
* @covers OAuth\OAuth2\Service\Bitrix24::__construct
28+
* @covers OAuth\OAuth2\Service\Bitrix24::getAuthorizationEndpoint
29+
*/
30+
public function testGetAuthorizationEndpoint()
31+
{
32+
$service = new Bitrix24(
33+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
34+
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
35+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
36+
array(),
37+
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
38+
);
39+
40+
$this->assertSame('https://bitrix24.com/oauth/authorize/', $service->getAuthorizationEndpoint()->getAbsoluteUri());
41+
}
42+
43+
/**
44+
* @covers OAuth\OAuth2\Service\Bitrix24::__construct
45+
* @covers OAuth\OAuth2\Service\Bitrix24::getAccessTokenEndpoint
46+
*/
47+
public function testGetAccessTokenEndpoint()
48+
{
49+
$service = new Bitrix24(
50+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
51+
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
52+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
53+
array(),
54+
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
55+
);
56+
57+
$this->assertSame('https://bitrix24.com/oauth/token/', $service->getAccessTokenEndpoint()->getAbsoluteUri());
58+
}
59+
60+
/**
61+
* @covers OAuth\OAuth2\Service\Bitrix24::__construct
62+
* @covers OAuth\OAuth2\Service\Bitrix24::getAuthorizationMethod
63+
*/
64+
public function testGetAuthorizationMethod()
65+
{
66+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
67+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0));
68+
69+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
70+
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
71+
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
72+
73+
$storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
74+
$storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
75+
76+
$service = new Bitrix24(
77+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
78+
$client,
79+
$storage,
80+
array(),
81+
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
82+
);
83+
84+
$uri = $service->request('https://pieterhordijk.com/my/awesome/path');
85+
$absoluteUri = parse_url($uri->getAbsoluteUri());
86+
87+
$this->assertSame('access_token=foo', $absoluteUri['query']);
88+
}
89+
90+
/**
91+
* @covers OAuth\OAuth2\Service\Bitrix24::__construct
92+
* @covers OAuth\OAuth2\Service\Bitrix24::parseAccessTokenResponse
93+
*/
94+
public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
95+
{
96+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
97+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
98+
99+
$service = new Bitrix24(
100+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
101+
$client,
102+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
103+
array(),
104+
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
105+
);
106+
107+
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
108+
109+
$service->requestAccessToken('foo');
110+
}
111+
112+
/**
113+
* @covers OAuth\OAuth2\Service\Bitrix24::__construct
114+
* @covers OAuth\OAuth2\Service\Bitrix24::parseAccessTokenResponse
115+
*/
116+
public function testParseAccessTokenResponseThrowsExceptionOnError()
117+
{
118+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
119+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"error":"some_error"}'));
120+
121+
$service = new Bitrix24(
122+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
123+
$client,
124+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
125+
array(),
126+
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
127+
);
128+
129+
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
130+
131+
$service->requestAccessToken('foo');
132+
}
133+
134+
/**
135+
* @covers OAuth\OAuth2\Service\Bitrix24::__construct
136+
* @covers OAuth\OAuth2\Service\Bitrix24::parseAccessTokenResponse
137+
*/
138+
public function testParseAccessTokenResponseValidWithoutRefreshToken()
139+
{
140+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
141+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
142+
143+
$service = new Bitrix24(
144+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
145+
$client,
146+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
147+
array(),
148+
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
149+
);
150+
151+
$this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
152+
}
153+
154+
/**
155+
* @covers OAuth\OAuth2\Service\Bitrix24::__construct
156+
* @covers OAuth\OAuth2\Service\Bitrix24::getExtraOAuthHeaders
157+
*/
158+
public function testGetExtraOAuthHeaders()
159+
{
160+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
161+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($uri, $params, $extraHeaders) {
162+
\PHPUnit_Framework_Assert::assertTrue(array_key_exists('Accept', $extraHeaders));
163+
\PHPUnit_Framework_Assert::assertTrue(in_array('application/json', $extraHeaders, true));
164+
165+
return '{"access_token":"foo","expires_in":"bar"}';
166+
}));
167+
168+
$service = new Bitrix24(
169+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
170+
$client,
171+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
172+
array(),
173+
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
174+
);
175+
176+
$this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
177+
}
178+
}

0 commit comments

Comments
 (0)