1313
1414use Buzz \Message \MessageInterface ;
1515use 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 */
2024class 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