ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
YubiKey.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * Copyright (C) 2009 Andreas Åkre Solberg <andreas.solberg@uninett.no>
5  * Copyright (C) 2009 Simon Josefsson <simon@yubico.com>.
6  *
7  * This file is part of SimpleSAMLphp
8  *
9  * SimpleSAMLphp is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 3 of
12  * the License, or (at your option) any later version.
13  *
14  * SimpleSAMLphp is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License License along with GNU SASL Library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25 
44 {
48  const STAGEID = 'sspmod_authYubiKey_Auth_Source_YubiKey.state';
49 
54  const TOKENSIZE = 32;
55 
59  const AUTHID = 'sspmod_authYubiKey_Auth_Source_YubiKey.AuthId';
60 
64  private $yubi_id;
65  private $yubi_key;
66 
73  public function __construct($info, $config)
74  {
75  assert(is_array($info));
76  assert(is_array($config));
77 
78  // Call the parent constructor first, as required by the interface
79  parent::__construct($info, $config);
80 
81  if (array_key_exists('id', $config)) {
82  $this->yubi_id = $config['id'];
83  }
84 
85  if (array_key_exists('key', $config)) {
86  $this->yubi_key = $config['key'];
87  }
88  }
89 
90 
99  public function authenticate(&$state)
100  {
101  assert(is_array($state));
102 
103  // We are going to need the authId in order to retrieve this authentication source later
104  $state[self::AUTHID] = $this->authId;
105 
106  $id = SimpleSAML_Auth_State::saveState($state, self::STAGEID);
107  $url = SimpleSAML\Module::getModuleURL('authYubiKey/yubikeylogin.php');
108  \SimpleSAML\Utils\HTTP::redirectTrustedURL($url, array('AuthState' => $id));
109  }
110 
111 
124  public static function handleLogin($authStateId, $otp)
125  {
126  assert(is_string($authStateId));
127  assert(is_string($otp));
128 
129  /* Retrieve the authentication state. */
131 
132  /* Find authentication source. */
133  assert(array_key_exists(self::AUTHID, $state));
135  if ($source === null) {
136  throw new Exception('Could not find authentication source with id '.$state[self::AUTHID]);
137  }
138 
139  try {
140  /* Attempt to log in. */
141  $attributes = $source->login($otp);
142  } catch (SimpleSAML_Error_Error $e) {
143  /* An error occurred during login. Check if it is because of the wrong
144  * username/password - if it is, we pass that error up to the login form,
145  * if not, we let the generic error handler deal with it.
146  */
147  if ($e->getErrorCode() === 'WRONGUSERPASS') {
148  return 'WRONGUSERPASS';
149  }
150 
151  /* Some other error occurred. Rethrow exception and let the generic error
152  * handler deal with it.
153  */
154  throw $e;
155  }
156 
157  $state['Attributes'] = $attributes;
159  }
160 
164  public static function getYubiKeyPrefix($otp)
165  {
166  $uid = substr($otp, 0, strlen ($otp) - self::TOKENSIZE);
167  return $uid;
168  }
169 
182  protected function login($otp)
183  {
184  assert(is_string($otp));
185 
186  require_once dirname(dirname(dirname(dirname(__FILE__)))).'/libextinc/Yubico.php';
187 
188  try {
189  $yubi = new Auth_Yubico($this->yubi_id, $this->yubi_key);
190  $yubi->verify($otp);
191  $uid = self::getYubiKeyPrefix($otp);
192  $attributes = array('uid' => array($uid));
193  } catch (Exception $e) {
194  SimpleSAML\Logger::info('YubiKey:'.$this->authId.': Validation error (otp '.$otp.'), debug output: '.$yubi->getLastResponse());
195  throw new SimpleSAML_Error_Error('WRONGUSERPASS', $e);
196  }
197 
198  SimpleSAML\Logger::info('YubiKey:'.$this->authId.': YubiKey otp '.$otp.' validated successfully: '.$yubi->getLastResponse());
199  return $attributes;
200  }
201 }
static getYubiKeyPrefix($otp)
Return the user id part of a one time passord.
Definition: YubiKey.php:164
$yubi_id
The client id/key for use with the Auth_Yubico PHP module.
Definition: YubiKey.php:64
$config
Definition: bootstrap.php:15
const AUTHID
The key of the AuthId field in the state.
Definition: YubiKey.php:59
login($otp)
Attempt to log in using the given username and password.
Definition: YubiKey.php:182
const TOKENSIZE
The number of characters of the OTP that is the secure token.
Definition: YubiKey.php:54
if(!array_key_exists('StateId', $_REQUEST)) $id
static redirectTrustedURL($url, $parameters=array())
This function redirects to the specified URL without performing any security checks.
Definition: HTTP.php:959
__construct($info, $config)
Constructor for this authentication source.
Definition: YubiKey.php:73
static getModuleURL($resource, array $parameters=array())
Get absolute URL to a specified module resource.
Definition: Module.php:220
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
static info($string)
Definition: Logger.php:199
authenticate(&$state)
Initialize login.
Definition: YubiKey.php:99
static loadState($id, $stage, $allowMissing=false)
Retrieve saved state.
Definition: State.php:259
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
const STAGEID
The string used to identify our states.
Definition: YubiKey.php:48
static handleLogin($authStateId, $otp)
Handle login request.
Definition: YubiKey.php:124
$url
static completeAuth(&$state)
Complete authentication.
Definition: Source.php:136
$source
Definition: linkback.php:22
static getById($authId, $type=null)
Retrieve authentication source.
Definition: Source.php:340
$info
Definition: index.php:5
if(!array_key_exists('AuthState', $_REQUEST)) $authStateId
getErrorCode()
Retrieve the error code given when throwing this error.
Definition: Error.php:120
static saveState(&$state, $stage, $rawId=false)
Save the state.
Definition: State.php:194