Skip to content

Commit ac3034b

Browse files
committed
OAuth Bits
1 parent 9cbb20a commit ac3034b

File tree

8 files changed

+354
-8
lines changed

8 files changed

+354
-8
lines changed

src/Connections/AbstractConnection.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33

44
use Fortifi\Api\Core\IApiConnection;
55
use Fortifi\Api\Core\IApiRequestDetail;
6+
use Fortifi\Api\Core\OAuth\Tokens\IToken;
67

78
abstract class AbstractConnection implements IApiConnection
89
{
910
protected $_orgFid;
10-
protected $_accessToken;
11+
12+
/**
13+
* @var IToken
14+
*/
15+
protected $_token;
1116

1217
/**
1318
* @param string $fid Organisation FID
@@ -21,13 +26,13 @@ public function setOrganisationFid($fid)
2126
}
2227

2328
/**
24-
* @param string $token Access Token
29+
* @param IToken $token Access Token
2530
*
2631
* @return $this
2732
*/
28-
public function setAccessToken($token)
33+
public function setToken(IToken $token)
2934
{
30-
$this->_accessToken = $token;
35+
$this->_token = $token;
3136
return $this;
3237
}
3338

@@ -39,9 +44,10 @@ protected function _buildHeaders(IApiRequestDetail $request)
3944
$headers['X-Fortifi-Org'] = $this->_orgFid;
4045
}
4146

42-
if(!empty($this->_accessToken))
47+
if($this->_token)
4348
{
44-
$headers['Authorization'] = 'Bearer ' . $this->_accessToken;
49+
$headers['Authorization'] = $this->_token->getType()
50+
. ' ' . $this->_token->getToken();
4551
}
4652

4753
if($request->getRequestBody())

src/IApiConnection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Fortifi\Api\Core;
33

4+
use Fortifi\Api\Core\OAuth\Tokens\IToken;
5+
46
interface IApiConnection
57
{
68
/**
@@ -25,9 +27,9 @@ public function batchLoad($requests);
2527
public function setOrganisationFid($fid);
2628

2729
/**
28-
* @param string $token Access Token
30+
* @param IToken $token Access Token
2931
*
3032
* @return $this
3133
*/
32-
public function setAccessToken($token);
34+
public function setToken(IToken $token);
3335
}

src/OAuth/Grants/IGrant.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Fortifi\Api\Core\OAuth\Grants;
3+
4+
use Fortifi\Api\Core\ApiDefinition\SecurityDefinition;
5+
use Fortifi\Api\Core\IApiConnection;
6+
use Fortifi\Api\Core\OAuth\Tokens\IToken;
7+
8+
interface IGrant
9+
{
10+
/**
11+
* @param IApiConnection $connection
12+
* @param SecurityDefinition $definition
13+
*
14+
* @return IToken
15+
*/
16+
public function getToken(
17+
IApiConnection $connection, SecurityDefinition $definition
18+
);
19+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
namespace Fortifi\Api\Core\OAuth\Grants;
3+
4+
use Fortifi\Api\Core\ApiDefinition\SecurityDefinition;
5+
use Fortifi\Api\Core\ApiRequest;
6+
use Fortifi\Api\Core\ApiRequestDetail;
7+
use Fortifi\Api\Core\IApiConnection;
8+
use Fortifi\Api\Core\OAuth\Tokens\AccessToken;
9+
use Fortifi\Api\Core\OAuth\Tokens\IToken;
10+
use Packaged\Helpers\Objects;
11+
12+
class ServiceAccountGrant implements IGrant
13+
{
14+
protected $_apiSecret;
15+
protected $_apiUser;
16+
17+
/**
18+
* @param mixed $apiSecret
19+
*
20+
* @return ServiceAccountGrant
21+
*/
22+
public function setApiSecret($apiSecret)
23+
{
24+
$this->_apiSecret = $apiSecret;
25+
return $this;
26+
}
27+
28+
/**
29+
* @param mixed $apiUser
30+
*
31+
* @return ServiceAccountGrant
32+
*/
33+
public function setApiUser($apiUser)
34+
{
35+
$this->_apiUser = $apiUser;
36+
return $this;
37+
}
38+
39+
/**
40+
* @return mixed
41+
*/
42+
public function getApiSecret()
43+
{
44+
return $this->_apiSecret;
45+
}
46+
47+
/**
48+
* @return mixed
49+
*/
50+
public function getApiUser()
51+
{
52+
return $this->_apiUser;
53+
}
54+
55+
/**
56+
* @param IApiConnection $connection
57+
* @param SecurityDefinition $definition
58+
*
59+
* @return IToken
60+
*/
61+
public function getToken(
62+
IApiConnection $connection, SecurityDefinition $definition
63+
)
64+
{
65+
if($definition->getType() !== 'oauth2')
66+
{
67+
throw new \InvalidArgumentException(
68+
'The security definition provided is not a valid oAuth2 definition'
69+
);
70+
}
71+
72+
$params = [];
73+
$params['grant_type'] = $this->getGrantType();
74+
$params['api_user'] = $this->getApiUser();
75+
$params['api_key'] = $this->getApiSecret();
76+
77+
$request = new ApiRequest();
78+
$request->setConnection($connection);
79+
$detail = new ApiRequestDetail();
80+
$detail->setUrl($definition->getTokenUrl());
81+
$detail->setPostFields($params);
82+
$request->setRequestDetail($detail);
83+
$connection->load($request);
84+
$tokenResponse = $request->getDecodedResponse();
85+
86+
$token = new AccessToken();
87+
$token->setToken(Objects::property($tokenResponse, 'access_token'));
88+
$token->setType(Objects::property($tokenResponse, 'token_type', 'Bearer'));
89+
$token->setExpirySeconds(Objects::property($tokenResponse, 'expires_in'));
90+
$token->setExpiryTime(Objects::property($tokenResponse, 'expiry_time'));
91+
$token->setUserId(Objects::property($tokenResponse, 'uid'));
92+
$token->setSessionSecret(
93+
Objects::property($tokenResponse, 'session_secret')
94+
);
95+
96+
return $token;
97+
}
98+
99+
public function getGrantType()
100+
{
101+
return 'service_account';
102+
}
103+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace Fortifi\Api\Core\OAuth\TokenStorage;
3+
4+
use Packaged\Helpers\Path;
5+
6+
class TmpFileTokenStorage implements TokenStorageInterface
7+
{
8+
/**
9+
* Store a token in storage
10+
*
11+
* @param string $key location key to store the token in
12+
*
13+
* @param string $token
14+
*
15+
* @return bool
16+
*/
17+
public function storeToken($key, $token)
18+
{
19+
return file_put_contents($this->_createFileName($key), $token) !== false;
20+
}
21+
22+
/**
23+
* Retrieve a token from storage
24+
*
25+
* @param string $key location key for token
26+
*
27+
* @return string|null
28+
*/
29+
public function retrieveToken($key)
30+
{
31+
$location = $this->_createFileName($key);
32+
if(file_exists($location))
33+
{
34+
return file_get_contents($location);
35+
}
36+
return null;
37+
}
38+
39+
/**
40+
* Create a temporary filename
41+
*
42+
* @param $key
43+
*
44+
* @return string
45+
*/
46+
private function _createFileName($key)
47+
{
48+
return Path::build(sys_get_temp_dir(), 'Fortifi-Token-' . $key);
49+
}
50+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Fortifi\Api\Core\OAuth\TokenStorage;
3+
4+
interface TokenStorageInterface
5+
{
6+
/**
7+
* Store a token in storage
8+
*
9+
* @param string $key location key to store the token in
10+
*
11+
* @param string $token
12+
*
13+
* @return bool
14+
*/
15+
public function storeToken($key, $token);
16+
17+
/**
18+
* Retrieve a token from storage
19+
*
20+
* @param string $key location key for token
21+
*
22+
* @return string|null
23+
*/
24+
public function retrieveToken($key);
25+
}

0 commit comments

Comments
 (0)