1
1
<?php
2
2
3
- class AwardForceSSO
4
- {
5
- private $ api ;
3
+ class AwardForceSSO {
6
4
5
+ private $ api ;
7
6
private $ installationDomain ;
8
7
9
- public function __construct (AwardForceAPI $ api )
8
+ public function __construct (AwardForceAPIV2 $ api )
10
9
{
11
10
$ this ->api = $ api ;
12
11
$ this ->installationDomain = variable_get ('sso_awardforce_installation_domain ' , '' );
13
12
}
14
13
15
14
/**
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.
18
16
*
19
17
*/
20
18
public function sso ()
21
19
{
22
20
global $ user ;
23
21
24
22
$ slug = $ this ->getSlug ($ user );
25
- $ token = $ this ->requestAuthToken ($ slug );
23
+ $ token = $ this ->requestAuthToken ($ slug, $ user );
26
24
27
25
drupal_goto ('https:// ' .$ this ->installationDomain .'/login ' , array ('external ' => true , 'query ' => array ('token ' => $ token )));
28
26
@@ -32,11 +30,13 @@ class AwardForceSSO
32
30
/**
33
31
* Returns the Award Force slug of a user.
34
32
*
33
+ * @param $user
34
+ * @param bool $forceRequest
35
35
* @return mixed
36
36
*/
37
- private function getSlug ($ user )
37
+ private function getSlug ($ user, $ forceRequest = false )
38
38
{
39
- if (!empty ($ user ->data ['award_force_slug ' ])) {
39
+ if (!$ forceRequest && ! empty ($ user ->data ['award_force_slug ' ])) {
40
40
return $ user ->data ['award_force_slug ' ];
41
41
}
42
42
@@ -51,24 +51,44 @@ class AwardForceSSO
51
51
/**
52
52
* Sends a POST request to the Award Force API to obtain the user's slug.
53
53
*
54
+ * @param $user
54
55
* @return mixed
55
56
*/
56
57
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 )
57
75
{
58
76
$ words = explode (' ' , $ user ->name );
59
77
60
78
$ firstName = array_shift ($ words );
61
79
$ lastName = implode (' ' , $ words );
62
80
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 (),
69
86
]);
87
+ }
70
88
71
- return $ response ->slug ;
89
+ private function requestSlugByEmail ($ email )
90
+ {
91
+ return $ this ->api ->get ("user/ " . $ email );
72
92
}
73
93
74
94
/**
@@ -77,10 +97,35 @@ class AwardForceSSO
77
97
* @param $slug
78
98
* @return mixed
79
99
*/
80
- private function requestAuthToken ($ slug )
100
+ private function requestAuthToken ($ slug, $ user )
81
101
{
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
+ }
83
119
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 ' );
85
130
}
86
131
}
0 commit comments