function login_history_get_device_id_from_cookie in Login History 7
Gets a device id from a login history cookie while validating authenticity.
Parameters
array $cookie: The cookie array i.e. $_COOKIE.
string $salt: The salt, e.g. from drupal_get_hash_salt().
Return value
string The device id, if it exists and is authenticated by the hmac.
Throws
Exception If there's any problem parsing the cookie.
6 calls to login_history_get_device_id_from_cookie()
- LoginHistoryCookieTest::testCookieDataContents in tests/
LoginHistoryTest.php - Invalid hmac.
- LoginHistoryCookieTest::testCookieDataStructure in tests/
LoginHistoryTest.php - All 3 elements are present, but fail basic sanity check for length.
- LoginHistoryCookieTest::testInvalidCookie in tests/
LoginHistoryTest.php - Cookie present, but invalid structure.
- LoginHistoryCookieTest::testMissingCookieException in tests/
LoginHistoryTest.php - No login history cookie present.
- LoginHistoryCookieTest::testParsingGoodCookies in tests/
LoginHistoryTest.php - Test parsing some good cookies. Yum.
File
- ./
login_history.inc, line 25 - Helper functions for login_history that have no dependencies.
Code
function login_history_get_device_id_from_cookie($cookie, $salt) {
// Is the cookie value even set?
if (array_key_exists('Drupal_visitor_login_history', $cookie)) {
// Are the elements set? Does it have some data right elements?
$potential_device_message = explode('-', $cookie['Drupal_visitor_login_history']);
if (3 == count($potential_device_message)) {
list($message_hmac, $device_id, $login_id) = $potential_device_message;
// Test all the required data is present and minimally valid.
if (strlen($message_hmac) == 64 && strlen($device_id) == 64 && !empty($login_id)) {
// If the hmac is valid, return the device id.
if (hash_equals(login_history_assemble_cookie($device_id, $login_id, $salt), $cookie['Drupal_visitor_login_history'])) {
return $device_id;
}
throw new Exception('Invalid login history hmac');
}
throw new Exception('Login history cookie data not structured properly.');
}
throw new Exception('Invalid login history cookie data.');
}
throw new Exception('Login history device id not present.');
}