Skip to content

Commit e44b4de

Browse files
authored
Merge pull request #1 from tectonic/feature/api-v21
Changes for API V2.1
2 parents 24d1239 + 8dfea68 commit e44b4de

File tree

5 files changed

+81
-78
lines changed

5 files changed

+81
-78
lines changed

includes/class_award_force_api.inc

+14-56
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use GuzzleHttp\Client;
44

5-
class AwardForceAPI
6-
{
5+
class AwardForceAPIV2 {
6+
77
private $apiUrl = 'https://api.cr4ce.com';
88

99
private $apiKey;
@@ -14,7 +14,7 @@ class AwardForceAPI
1414
}
1515

1616
/**
17-
* Sends a GET request to the Award Force API.
17+
* Sends a GET request to the Award Force API
1818
*
1919
* @param $uri
2020
* @param array $options
@@ -37,55 +37,15 @@ class AwardForceAPI
3737
* @param $uri
3838
* @param array $options
3939
*
40-
* @return \Psr\Http\Message\ResponseInterface
41-
*/
42-
public function post($uri, $options = [])
43-
{
44-
try {
45-
$response = $this->getClient()->post($uri, $options);
46-
return json_decode($response->getBody()->getContents());
47-
} catch (Exception $e) {
48-
$this->handleException($e);
49-
}
50-
}
51-
52-
/**
53-
* Retrieves the API access token from the database. If not available,
54-
* requests it to the API.
55-
*
5640
* @return string
5741
*/
58-
private function getAccessToken()
59-
{
60-
if ($token = variable_get('sso_awardforce_access_token', '')) {
61-
return $token;
62-
}
63-
64-
$token = $this->requestAccessToken();
65-
66-
variable_set('sso_awardforce_access_token', '');
67-
68-
return $token;
69-
}
70-
71-
/**
72-
* Requests an access token to the Award Force API.
73-
*
74-
* @return string
75-
*/
76-
private function requestAccessToken()
42+
public function post($uri, $options = [])
7743
{
7844
try {
79-
$client = new Client([
80-
'base_uri' => $this->apiUrl,
81-
'headers' => [
82-
'Accept' => 'application/vnd.Award Force.v1.0+json',
83-
'Authorization' => 'Basic ' . $this->apiKey,
84-
],
85-
]);
86-
87-
$response = $client->get('/access-token');
88-
return $response->getBody()->getContents();
45+
$response = $this->getClient()->post($uri, ['json' => $options]);
46+
if ($response->getStatusCode() == 201) {
47+
return (object)['slug' => substr((string)$response->getHeader('Location')[0], -8)];
48+
}
8949
} catch (Exception $e) {
9050
$this->handleException($e);
9151
}
@@ -101,25 +61,23 @@ class AwardForceAPI
10161
return new Client([
10262
'base_uri' => $this->apiUrl,
10363
'headers' => [
104-
'Accept' => 'application/vnd.Award Force.v1.0+json',
105-
'Authorization' => 'Basic ' . $this->getAccessToken(),
64+
'Accept' => 'application/vnd.Creative Force.v2.1+json',
65+
'x-api-key' => $this->apiKey,
10666
],
67+
'http_errors' => false,
10768
]);
10869
}
10970

11071
/**
111-
* Logs any potential errors, clears the API access token from the database
112-
* and displays an error message to the user.
72+
* Logs any potential errors, clears the API access token from the database and displays an error message to
73+
* the user.
11374
*
11475
* @param Exception $e
11576
*/
116-
private function handleException(Exception $e)
77+
public function handleException(Exception $e)
11778
{
118-
variable_del('sso_awardforce_access_token');
119-
12079
watchdog('Award Force API', $e->getMessage(), array(), WATCHDOG_ERROR);
12180

12281
exit(t('An error has occurred. Please try again, and if the problem persists, contact the system administrator.'));
123-
12482
}
12583
}

includes/class_award_force_sso.inc

+64-19
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
<?php
22

3-
class AwardForceSSO
4-
{
5-
private $api;
3+
class AwardForceSSO {
64

5+
private $api;
76
private $installationDomain;
87

9-
public function __construct(AwardForceAPI $api)
8+
public function __construct(AwardForceAPIV2 $api)
109
{
1110
$this->api = $api;
1211
$this->installationDomain = variable_get('sso_awardforce_installation_domain', '');
1312
}
1413

1514
/**
16-
* Requests an Award Force authentication token and redirects the user to the
17-
* awards homepage.
15+
* Requests an Award Force authentication token and redirects the user to the awards homepage.
1816
*
1917
*/
2018
public function sso()
2119
{
2220
global $user;
2321

2422
$slug = $this->getSlug($user);
25-
$token = $this->requestAuthToken($slug);
23+
$token = $this->requestAuthToken($slug, $user);
2624

2725
drupal_goto('https://'.$this->installationDomain.'/login', array('external' => true, 'query' => array('token' => $token)));
2826

@@ -32,11 +30,13 @@ class AwardForceSSO
3230
/**
3331
* Returns the Award Force slug of a user.
3432
*
33+
* @param $user
34+
* @param bool $forceRequest
3535
* @return mixed
3636
*/
37-
private function getSlug($user)
37+
private function getSlug($user, $forceRequest = false)
3838
{
39-
if (!empty($user->data['award_force_slug'])) {
39+
if (!$forceRequest && !empty($user->data['award_force_slug'])) {
4040
return $user->data['award_force_slug'];
4141
}
4242

@@ -51,24 +51,44 @@ class AwardForceSSO
5151
/**
5252
* Sends a POST request to the Award Force API to obtain the user's slug.
5353
*
54+
* @param $user
5455
* @return mixed
5556
*/
5657
private function requestSlug($user)
58+
{
59+
$response = $this->requestSlugByEmail($user->mail);
60+
61+
if (isset($response->slug)) {
62+
return $response->slug;
63+
}
64+
65+
$response = $this->createUser($user);
66+
67+
if (!isset($response->slug)) {
68+
$this->api->handleException(new Exception($response->message ?: 'There was an error creating the user.'));
69+
}
70+
71+
return $response->slug;
72+
}
73+
74+
private function createUser($user)
5775
{
5876
$words = explode(' ', $user->name);
5977

6078
$firstName = array_shift($words);
6179
$lastName = implode(' ', $words);
6280

63-
$response = $this->api->post('/user', [
64-
'form_params' => [
65-
'email' => $user->mail,
66-
'firstName' => $firstName ?: 'First',
67-
'lastName' => $lastName ?: 'Last'
68-
],
81+
return $this->api->post('/user', [
82+
'email' => $user->mail,
83+
'first_name' => $firstName ?: 'First',
84+
'last_name' => $lastName ?: 'Last',
85+
'password' => uniqid(),
6986
]);
87+
}
7088

71-
return $response->slug;
89+
private function requestSlugByEmail($email)
90+
{
91+
return $this->api->get("user/" . $email);
7292
}
7393

7494
/**
@@ -77,10 +97,35 @@ class AwardForceSSO
7797
* @param $slug
7898
* @return mixed
7999
*/
80-
private function requestAuthToken($slug)
100+
private function requestAuthToken($slug, $user)
81101
{
82-
$response = $this->api->get('/user/'.$slug.'/auth-token');
102+
if ($token = $this->sendAuthTokenRequest($slug)->auth_token) {
103+
return $token;
104+
}
105+
106+
$slug = $this->getSlug($user, true);
107+
$retries = 5;
108+
109+
while ($retries > 0) {
110+
if ($response = $this->sendAuthTokenRequest($slug)) {
111+
if ($token = $response->auth_token) {
112+
return $token;
113+
}
114+
$this->api->handleException(new Exception($response->message));
115+
}
116+
sleep(1);
117+
$retries--;
118+
}
83119

84-
return $response->auth_token;
120+
if (!$token) {
121+
$this->api->handleException(new Exception('There was an error requesting a token from Award Force'));
122+
}
123+
124+
return $token;
125+
}
126+
127+
private function sendAuthTokenRequest($slug)
128+
{
129+
return $this->api->get('/user/' . $slug . '/auth-token');
85130
}
86131
}

sso_awardforce.admin.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function sso_awardforce_settings_form($form, $form_state)
1818
'#default_value' => variable_get('sso_awardforce_api_key', ''),
1919
'#required' => TRUE,
2020
'#size' => 58,
21-
'#description' => t('An Awards Manager can generate this key in Award Force (Settings > General > Integration)'),
21+
'#description' => t('An Awards Manager can generate this key in Award Force (Settings > Developers > API keys)'),
2222
);
2323

2424
$form = system_settings_form($form);

sso_awardforce.module

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ function sso_awardforce_login()
3232
module_load_include('inc', 'sso_awardforce', 'includes/class_award_force_api');
3333
module_load_include('inc', 'sso_awardforce', 'includes/class_award_force_sso');
3434

35-
$awardForce = new AwardForceSSO(new AwardForceAPI);
35+
$awardForce = new AwardForceSSO(new AwardForceAPIV2);
3636
$awardForce->sso();
3737
}

vendor/composer/autoload_classmap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$baseDir = dirname($vendorDir);
77

88
return array(
9-
'AwardForceAPI' => $baseDir . '/public/class-award-force-api.php',
9+
'AwardForceAPIV2' => $baseDir . '/public/class-award-force-api.php',
1010
'AwardForceSSO' => $baseDir . '/public/class-award-force-sso.php',
1111
'AwardForceSSO_Admin' => $baseDir . '/admin/class-award-force-sso-admin.php',
1212
);

0 commit comments

Comments
 (0)