Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Commit 36a03ca

Browse files
committed
Merge pull request #6401 from DFurnes/northstar-verify-touchups
Fix bugs with Northstar auth verification.
2 parents 696a915 + b27921d commit 36a03ca

File tree

3 files changed

+118
-148
lines changed

3 files changed

+118
-148
lines changed

lib/modules/dosomething/dosomething_northstar/dosomething_northstar.module

Lines changed: 71 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -71,45 +71,54 @@ function dosomething_northstar_verify_user($credentials) {
7171
return json_decode($response->data);
7272
}
7373

74+
/**
75+
* Save the user's Northstar ID to their local profile.
76+
*
77+
* @param $user - Drupal user
78+
* @param $northstar_response - Northstar user JSON response
79+
*/
80+
function dosomething_northstar_save_id_field($user, $northstar_response) {
81+
$northstar_id = !empty($northstar_response->data->id) ? $northstar_response->data->id : 'NONE';
82+
83+
$edit = [];
84+
dosomething_user_set_fields($edit, ['northstar_id' => $northstar_id]);
85+
user_save($user, $edit);
86+
}
87+
7488
/**
7589
* Send user registration events to northstar.
7690
*/
7791
function dosomething_northstar_register_user($form_state) {
7892
global $user;
7993
// Build a user that Northstar expects.
8094
$ns_user = dosomething_northstar_build_ns_user($user, $form_state);
81-
// Send that to NS.
82-
// $northstar = new Northstar();
83-
// try {
84-
// $response = $northstar->updateUser($ns_user);
85-
// if ($response->getStatusCode() === '200' && module_exists('stathat')) {
86-
// stathat_send_ez_count('drupal - Northstar - user migrated - count', 1);
87-
// }
88-
// }
89-
// catch (Exception $e) {
90-
91-
// watchdog('dosomething_northstar', 'User not migrated : ' . $e, NULL, WATCHDOG_ERROR);
92-
// }
95+
9396
$client = _dosomething_northstar_build_http_client();
9497
$response = drupal_http_request($client['base_url'] . '/users', array(
9598
'headers' => $client['headers'],
9699
'method' => 'POST',
97100
'data' => json_encode($ns_user),
98-
));
99-
if ($response->code === '200' && module_exists('stathat')) {
100-
stathat_send_ez_count('drupal - Northstar - user migrated - count', 1);
101-
}
102-
elseif ($response->code !== '200') {
103-
watchdog('dosomething_northstar', 'User not migrated : ' . $response->code, NULL, WATCHDOG_ERROR);
104-
}
101+
));
102+
103+
// Save the newly registered user's ID to their local profile field.
104+
dosomething_northstar_save_id_field($user, json_decode($response->data));
105105

106106
// Add to request log if enabled.
107107
dosomething_northstar_log_request('register_user', $user, $ns_user, $response);
108+
109+
if ($response->code !== '200') {
110+
watchdog('dosomething_northstar', 'User not migrated : ' . $response->code, NULL, WATCHDOG_ERROR);
111+
return;
112+
}
113+
114+
if (module_exists('stathat')) {
115+
stathat_send_ez_count('drupal - Northstar - user migrated - count', 1);
116+
}
108117
}
109118

110119
/**
111-
* Build the drupal_http_request object for nothstar calls.
112-
* This will be depricated once guzzle is fixed...
120+
* Build the drupal_http_request object for Northstar calls.
121+
* @returns array
113122
*/
114123
function _dosomething_northstar_build_http_client() {
115124
$base_url = NORTHSTAR_URL;
@@ -131,7 +140,10 @@ function _dosomething_northstar_build_http_client() {
131140
}
132141

133142
/**
134-
* Send user profile updates to northstar.
143+
* Update a user's existing Northstar profile based on
144+
* their Drupal ID.
145+
*
146+
* @return void
135147
*/
136148
function dosomething_northstar_update_user($form_state) {
137149
$user = $form_state['user'];
@@ -158,74 +170,43 @@ function dosomething_northstar_update_user($form_state) {
158170
}
159171

160172
/**
161-
* If the log is enabled, log this request to the database.
162-
*
163-
* @param string $op - label for the operation being performed
164-
* @param object $user - the Drupal user
165-
* @param array $request_body - the body of the request
166-
* @param string $response - response JSON
167-
*/
168-
function dosomething_northstar_log_request($op, $user, $request_body, $response) {
169-
if (!variable_get('dosomething_northstar_log')) return;
170-
171-
// Don't log plaintext passwords.
172-
if (isset($request_body['password'])) {
173-
$request_body['password'] = '*****';
174-
}
175-
176-
db_insert('dosomething_northstar_request_log')
177-
->fields([
178-
'op' => $op,
179-
'uid' => $user->uid,
180-
'user_lang' => $user->language,
181-
'user_country' => $user->field_address[LANGUAGE_NONE][0]['country'],
182-
'fastly_country' => dosomething_settings_get_geo_country_code(),
183-
'request_values' => json_encode($request_body),
184-
'response_code' => $response->code,
185-
'response_values' => $response->data,
186-
])
187-
->execute();
188-
}
189-
190-
/**
191-
* Get user data from northstar.
173+
* Get user profile data from Northstar for the given Drupal ID.
192174
*
193-
* @param int $id Drupal user id.
175+
* @param int $drupal_id Drupal user id.
194176
* @return object
195177
*/
196-
function dosomething_northstar_get_northstar_user($id) {
197-
$northstar_user_info = cache_get('northstar_user_info_' . $id, 'cache_dosomething_northstar');
178+
function dosomething_northstar_get_northstar_user($drupal_id) {
179+
$northstar_user_info = cache_get('northstar_user_info_' . $drupal_id, 'cache_dosomething_northstar');
198180

199181
if ($northstar_user_info) {
200182
$northstar_user_info = $northstar_user_info->data;
201183
}
202184
else {
203185
$client = _dosomething_northstar_build_http_client();
204186

205-
$northstar_user_info = drupal_http_request($client['base_url'] . '/users/drupal_id/' . $id, array(
187+
$northstar_user_info = drupal_http_request($client['base_url'] . '/users/drupal_id/' . $drupal_id, array(
206188
'headers' => $client['headers'],
207189
'method' => 'GET',
208190
));
209191

210192
$northstar_user_info = $northstar_user_info->data;
211193

212194
if (!empty($northstar_user_info->error)) {
213-
$error = sprintf("Error fetching Northstar user data, uid=%d: '%s'", $id, $northstar_user_info->error);
195+
$error = sprintf("Error fetching Northstar user data, uid=%d: '%s'", $drupal_id, $northstar_user_info->error);
214196
watchdog_exception('northstar', new Exception($error));
215197
} else {
216-
cache_set('northstar_user_info_' . $id, $northstar_user_info, 'cache_dosomething_northstar', REQUEST_TIME + 60*60*24);
198+
cache_set('northstar_user_info_' . $drupal_id, $northstar_user_info, 'cache_dosomething_northstar', REQUEST_TIME + 60*60*24);
217199
}
218200
}
219201

220202
return $northstar_user_info;
221203
}
222204

223205
/**
224-
* Build a user json object, that's accepted by Northstar.
225-
*
226-
* @param obj $user
227-
* Drupal user object
206+
* Build a user JSON object in the format that Northstar expects.
228207
*
208+
* @param $user - Drupal user object
209+
* @return array
229210
*/
230211
function dosomething_northstar_build_ns_user($user, $form_state) {
231212
// Optional fields
@@ -277,44 +258,33 @@ function dosomething_northstar_build_ns_user($user, $form_state) {
277258
return $ns_user;
278259
}
279260

261+
/**
262+
* If the log is enabled, log this request to the database.
263+
*
264+
* @param string $op - label for the operation being performed
265+
* @param object $user - the Drupal user
266+
* @param array $request_body - the body of the request
267+
* @param string $response - response JSON
268+
*/
269+
function dosomething_northstar_log_request($op, $user, $request_body, $response) {
270+
if (!variable_get('dosomething_northstar_log')) return;
280271

281-
class Northstar {
282-
283-
// protected $client;
284-
285-
// public function __construct() {
286-
287-
// $base_url = NORTHSTAR_URL;
288-
// if (getenv('DS_ENVIRONMENT') === 'local') {
289-
// $base_url .= ":" . NORTHSTAR_PORT;
290-
// }
291-
// $version = NORTHSTAR_VERSION;
292-
293-
// if (libraries_load('guzzle') == TRUE) {
294-
// $client = new GuzzleHttp\Client(array(
295-
// 'base_url' => array($base_url . '/{version}/', array('version' => $version)),
296-
// 'defaults' => array(
297-
// 'headers' => array(
298-
// 'X-DS-Application-Id' => NORTHSTAR_APP_ID,
299-
// 'X-DS-REST-API-Key' => NORTHSTAR_APP_KEY,
300-
// 'Content-Type' => 'application/json',
301-
// 'Accept' => 'application/json'
302-
// ),
303-
// ),
304-
// ));
305-
// $this->client = $client;
306-
307-
// }
308-
// }
309-
310-
// public function updateUser($user) {
311-
// // return $this->client->getUrl();
312-
// $response = $this->client->post('users', array(
313-
// 'body' => json_encode($user),
314-
// ));
315-
316-
// // watchdog('dosomething_northstar', 'full url' . $response->getUrl(), NULL, WATCHDOG_ERROR);
317-
// return $response;
318-
// }
272+
// Don't log plaintext passwords.
273+
if (isset($request_body['password'])) {
274+
$request_body['password'] = '*****';
275+
}
319276

277+
db_insert('dosomething_northstar_request_log')
278+
->fields([
279+
'op' => $op,
280+
'uid' => $user->uid,
281+
'user_lang' => $user->language,
282+
'user_country' => $user->field_address[LANGUAGE_NONE][0]['country'],
283+
'fastly_country' => dosomething_settings_get_geo_country_code(),
284+
'request_values' => json_encode($request_body),
285+
'response_code' => $response->code,
286+
'response_values' => $response->data,
287+
])
288+
->execute();
320289
}
290+

lib/modules/dosomething/dosomething_signup/includes/Signup.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ private function build($data) {
9696
$northstar_response = json_decode($northstar_response);
9797

9898
if (!empty($northstar_response->data) && !isset($northstar_response->error)) {
99-
$northstar_user = $northstar_response->data;
100-
user_save($drupal_user, ['field_northstar_id' => [LANGUAGE_NONE => [0 => ['value' => $northstar_user->id]]]]);
99+
dosomething_northstar_save_id_field($drupal_user, $northstar_response);
101100
}
102101
}
103102

0 commit comments

Comments
 (0)