1+ <?php
2+
3+ namespace CareerBuilder \OAuth2 \Flows ;
4+
5+ use CareerBuilder \OAuth2 \AccessToken ;
6+ use Guzzle \Http \ClientInterface ;
7+ use Guzzle \Http \Client ;
8+ use Guzzle \Plugin \Log \LogPlugin ;
9+ use Guzzle \Log \PsrLogAdapter ;
10+ use JWT ;
11+ use Psr \Log \LoggerInterface ;
12+ use Psr \Log \NullLogger ;
13+
14+ /**
15+ * Base class for all oAuth 2 flows.
16+ *
17+ * @package CareerBuilder\OAuth2\Flows
18+ */
19+ abstract class Flow
20+ {
21+ /** @var ClientInterface */
22+ protected $ client ;
23+ /** @var LoggerInterface */
24+ protected $ logger ;
25+ /** @var string */
26+ protected $ clientId ;
27+ /** @var string */
28+ protected $ clientSecret ;
29+ /** @var string */
30+ protected $ sharedSecret ;
31+ /** @var array */
32+ protected $ headers ;
33+ /** @var array */
34+ protected $ body ;
35+
36+ /**
37+ * @param array $configs
38+ * @param ClientInterface $client
39+ * @param LoggerInterface $logger
40+ */
41+ protected function __construct (array $ configs , ClientInterface $ client = null , LoggerInterface $ logger = null )
42+ {
43+ $ this ->setCredentials ($ configs );
44+ $ this ->setDefaults ();
45+
46+
47+ if (isset ($ configs ['auth_in_header ' ]) && $ configs ['auth_in_header ' ]) {
48+ $ this ->headers ['Authorization ' ] = $ this ->getAuthHeader ();
49+ }
50+
51+ $ this ->logger = $ logger ?: new NullLogger ();
52+ $ this ->client = $ client ?: new Client ();
53+ $ this ->client ->setBaseUrl ($ configs ['base_url ' ]);
54+ $ this ->client ->addSubscriber (new LogPlugin (new PsrLogAdapter ($ this ->logger )));
55+ }
56+
57+ /**
58+ * @param array $configs
59+ */
60+ private function setCredentials (array $ configs )
61+ {
62+ $ this ->clientId = $ configs ['client_id ' ];
63+ $ this ->clientSecret = $ configs ['client_secret ' ];
64+ $ this ->sharedSecret = $ configs ['shared_secret ' ];
65+ }
66+
67+ /**
68+ * Set default headers and body
69+ */
70+ private function setDefaults ()
71+ {
72+ $ this ->headers = array ('Content-Type ' => 'application/x-www-form-urlencoded ' );
73+ $ this ->body = array (
74+ 'client_id ' => $ this ->clientId ,
75+ 'client_secret ' => $ this ->clientSecret
76+ );
77+ }
78+
79+ /**
80+ * Build the authorization header for client information in header
81+ */
82+ private function getAuthHeader ()
83+ {
84+ $ unencodedParams = "{$ this ->clientId }: {$ this ->clientSecret }" ;
85+ $ encodedParams = base64_encode ($ unencodedParams );
86+
87+ return "Basic {$ encodedParams }" ;
88+ }
89+
90+ /**
91+ * @param AccessToken $token
92+ * @return AcccessToken
93+ */
94+ public function getToken (AccessToken $ token = null )
95+ {
96+ if ($ token && $ token ->getRefreshToken ()) {
97+ $ this ->body ['grant_type ' ] = 'refresh_token ' ;
98+ $ this ->body ['refresh_token ' ] = $ token ->getRefreshToken ();
99+ } else {
100+ $ this ->buildBody ();
101+ }
102+
103+ $ request = $ this ->client ->post ('/share/oauth2/token.aspx ' , $ this ->headers , $ this ->body );
104+ $ response = $ request ->send ();
105+ $ data = $ response ->json ();
106+
107+ $ refreshToken = isset ($ data ['refresh_token ' ]) ?: '' ;
108+
109+ return new AccessToken ($ data ['access_token ' ], $ refreshToken , $ data ['expires_in ' ]);
110+ }
111+
112+ protected abstract function buildBody ();
113+
114+ /**
115+ * Encode the claims into a JWT and sign using the HS512 algorithm
116+ */
117+ protected function getJWT ($ claims )
118+ {
119+ return JWT ::encode ($ claims , $ this ->sharedSecret , 'HS512 ' );
120+ }
121+ }
0 commit comments