ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Auth_Yubico Class Reference

Class for verifying Yubico One-Time-Passcodes. More...

+ Collaboration diagram for Auth_Yubico:

Public Member Functions

 __construct ($id, $key='')
 Constructor. More...
 
 getLastResponse ()
 Return the last data received from the server, if any. More...
 
 verify ($token)
 Verify Yubico OTP. More...
 

Data Fields

 $_id
 
 $_key
 
 $_response
 

Detailed Description

Class for verifying Yubico One-Time-Passcodes.

Simple example: require_once 'Auth/Yubico.php'; $yubi = &new Auth_Yubico('42'); $auth = $yubi->verify("ccbbddeertkrctjkkcglfndnlihhnvekchkcctif"); if (PEAR::isError($auth)) { print "<p>Authentication failed: " . $auth->getMessage(); print "<p>Debug output from server: " . $yubi->getLastResponse(); } else { print "<p>You are authenticated!"; }

Definition at line 59 of file Yubico.php.

Constructor & Destructor Documentation

◆ __construct()

Auth_Yubico::__construct (   $id,
  $key = '' 
)

Constructor.

Sets up the object

Parameters
stringThe client identity
stringThe client MAC key (optional) public

Definition at line 91 of file Yubico.php.

References $id, and $key.

92  {
93  $this->_id = $id;
94  $this->_key = base64_decode($key);
95  }
if(!array_key_exists('StateId', $_REQUEST)) $id
$key
Definition: croninfo.php:18

Member Function Documentation

◆ getLastResponse()

Auth_Yubico::getLastResponse ( )

Return the last data received from the server, if any.

Returns
string Output from server. public

Definition at line 103 of file Yubico.php.

References $_response.

104  {
105  return $this->_response;
106  }

◆ verify()

Auth_Yubico::verify (   $token)

Verify Yubico OTP.

Parameters
string$tokenYubico OTP
Returns
mixed PEAR error on error, true otherwise public

Definition at line 117 of file Yubico.php.

References $key, $out, $response, $row, $rows, and $url.

118  {
119  $parameters = "id=" . $this->_id . "&otp=" . $token;
120  // Generate signature
121  if($this->_key <> "") {
122  $signature = base64_encode(hash_hmac('sha1', $parameters, $this->_key, true));
123  $parameters .= '&h=' . $signature;
124  }
125  /* Support https. */
126  $url = "https://api.yubico.com/wsapi/verify?" . $parameters;
127 
128  $responseMsg = file_get_contents($url);
129 
130  if(!preg_match("/status=([a-zA-Z0-9_]+)/", $responseMsg, $out)) {
131  throw new Exception('Could not parse response');
132  }
133 
134  $status = $out[1];
135 
136  /* Verify signature. */
137  if($this->_key <> "") {
138  $rows = explode("\r\n", $responseMsg);
139  while (list($key, $val) = each($rows)) {
140  // = is also used in BASE64 encoding so we only replace the first = by # which is not used in BASE64
141  $val = preg_replace('/=/', '#', $val, 1);
142  $row = explode("#", $val);
143  $response[$row[0]] = (isset($row[1])) ? $row[1] : "";
144  }
145 
146  $check = 'status=' . $response['status'] . '&t='. $response['t'];
147  $checksignature = base64_encode(hash_hmac('sha1', $check, $this->_key, true));
148 
149  if($response['h'] != $checksignature) {
150  throw new Exception('Checked Signature failed');
151  }
152  }
153 
154  if ($status != 'OK') {
155  throw new Exception('Status was not OK: ' . $status);
156  }
157 
158  return true;
159  }
$rows
Definition: xhr_table.php:10
$url
$response
$key
Definition: croninfo.php:18

Field Documentation

◆ $_id

Auth_Yubico::$_id

Definition at line 69 of file Yubico.php.

◆ $_key

Auth_Yubico::$_key

Definition at line 75 of file Yubico.php.

◆ $_response

Auth_Yubico::$_response

Definition at line 81 of file Yubico.php.

Referenced by getLastResponse().


The documentation for this class was generated from the following file: