Skip to content

Commit 8f4be01

Browse files
committed
added constructor
1 parent 777c1ab commit 8f4be01

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

lib/Bitbucket/API/Http/Listener/OAuthListener.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,70 @@
1313

1414
use Buzz\Message\MessageInterface;
1515
use Buzz\Message\RequestInterface;
16+
use JacobKiers\OAuth\SignatureMethod\SignatureMethodInterface;
17+
use JacobKiers\OAuth\Consumer\ConsumerInterface;
18+
use JacobKiers\OAuth\Token\TokenInterface;
19+
use JacobKiers\OAuth as OAuth1;
1620

1721
/**
1822
* @author Alexandru G. <alex@gentle.ro>
1923
*/
2024
class OAuthListener implements ListenerInterface
2125
{
26+
/**
27+
* @var array
28+
*/
29+
protected $config = array(
30+
'oauth_consumer_key' => 'anon',
31+
'oauth_consumer_secret' => 'anon',
32+
'oauth_token' => '',
33+
'oauth_token_secret' => '',
34+
'oauth_signature_method' => 'HMAC-SHA1',
35+
'oauth_callback' => '',
36+
'oauth_verifier' => '',
37+
'oauth_version' => ''
38+
);
39+
40+
/**
41+
* @var SignatureMethodInterface
42+
*/
43+
protected $signature;
44+
45+
/**
46+
* @var TokenInterface
47+
*/
48+
protected $token;
49+
50+
/**
51+
* @var ConsumerInterface
52+
*/
53+
protected $consumer;
54+
55+
public function __construct(
56+
array $config,
57+
SignatureMethodInterface $signature = null,
58+
TokenInterface $token = null,
59+
ConsumerInterface $consumer = null
60+
)
61+
{
62+
$this->config = array_merge($this->config, $config);
63+
$this->signature = (!is_null($signature)) ? $signature : $this->getSigner();
64+
65+
$this->token = (!is_null($token)) ?
66+
$token :
67+
!isset($this->config['oauth_token']) ?
68+
new OAuth1\Token\NullToken :
69+
new OAuth1\Token\Token($this->config['oauth_token'], $this->config['oauth_token_secret'])
70+
;
71+
72+
$this->consumer = (!is_null($consumer)) ?
73+
$consumer :
74+
new OAuth1\Consumer\Consumer(
75+
$this->config['oauth_consumer_key'], $this->config['oauth_consumer_secret']
76+
)
77+
;
78+
}
79+
2280
/**
2381
* {@inheritDoc}
2482
*/
@@ -38,4 +96,24 @@ public function preSend(RequestInterface $request)
3896
*/
3997
public function postSend(RequestInterface $request, MessageInterface $response)
4098
{}
99+
100+
/**
101+
* Bitbucket supports only HMAC-SHA1 and PlainText signatures.
102+
*
103+
* For better security, HMAC-SHA1 is the default one.
104+
*
105+
* @return \JacobKiers\OAuth\SignatureMethod\SignatureMethodInterface
106+
*/
107+
protected function getSigner()
108+
{
109+
$signature = 'HmacSha1';
110+
111+
if ($this->config['oauth_signature_method'] == 'PLAINTEXT') {
112+
$signature = 'PlainText';
113+
}
114+
115+
$class = '\JacobKiers\OAuth\SignatureMethod\\'.$signature;
116+
117+
return new $class;
118+
}
41119
}

0 commit comments

Comments
 (0)