|
| 1 | +<?php |
| 2 | + |
| 3 | +class sspmod_smartattributes_Auth_Process_SmartID extends SimpleSAML_Auth_ProcessingFilter { |
| 4 | + |
| 5 | + /** |
| 6 | + * Which attributes to use as identifiers? |
| 7 | + * |
| 8 | + * IMPORTANT: If you use the (default) attributemaps (twitter2name, facebook2name, |
| 9 | + * etc., be sure to comment out the entries that map xxx_targetedID to |
| 10 | + * eduPersonTargetedID, or there will be no way to see its origin any more. |
| 11 | + */ |
| 12 | + private $_candidates = array( |
| 13 | + 'eduPersonTargetedID', |
| 14 | + 'eduPersonPrincipalName', |
| 15 | + 'openid', |
| 16 | + 'facebook_targetedID', |
| 17 | + 'twitter_targetedID', |
| 18 | + 'windowslive_targetedID', |
| 19 | + 'myspace_targetedID', |
| 20 | + 'linkedin_targetedID', |
| 21 | + ); |
| 22 | + |
| 23 | + /** |
| 24 | + * The name of the generated ID attribute. |
| 25 | + */ |
| 26 | + private $_id_attribute = 'smart_id'; |
| 27 | + |
| 28 | + /** |
| 29 | + * Whether to append the AuthenticatingAuthority, separated by '!' |
| 30 | + * This only works when SSP is used as a gateway. |
| 31 | + */ |
| 32 | + private $_add_authority = true; |
| 33 | + |
| 34 | + /** |
| 35 | + * Attributes which should be added/appended. |
| 36 | + * |
| 37 | + * Associative array of arrays. |
| 38 | + */ |
| 39 | + private $attributes = array(); |
| 40 | + |
| 41 | + |
| 42 | + public function __construct($config, $reserved) { |
| 43 | + parent::__construct($config, $reserved); |
| 44 | + |
| 45 | + assert('is_array($config)'); |
| 46 | + |
| 47 | + if (array_key_exists('candidates', $config)) { |
| 48 | + $this->_candidates = $config['candidates']; |
| 49 | + if (!is_array($this->_candidates)) { |
| 50 | + throw new Exception('SmartID authproc configuration error: \'candidates\' should be an array.'); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (array_key_exists('id_attribute', $config)) { |
| 55 | + $this->_id_attribute = $config['id_attribute']; |
| 56 | + if (!is_string($this->_id_attribute)) { |
| 57 | + throw new Exception('SmartID authproc configuration error: \'id_attribute\' should be a string.'); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (array_key_exists('add_authority', $config)) { |
| 62 | + $this->_add_authority = $config['add_authority']; |
| 63 | + if (!is_bool($this->_add_authority)) { |
| 64 | + throw new Exception('SmartID authproc configuration error: \'add_authority\' should be a boolean.'); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + private function addID($attributes, $request) { |
| 71 | + foreach ($this->_candidates as $idCandidate) { |
| 72 | + if (isset($attributes[$idCandidate][0])) { |
| 73 | + if(($this->_add_authority) && (isset($request['saml:AuthenticatingAuthority'][0]))) { |
| 74 | + return $idCandidate.':'.$attributes[$idCandidate][0] . '!' . $request['saml:AuthenticatingAuthority'][0]; |
| 75 | + } else { |
| 76 | + return $idCandidate.':'.$attributes[$idCandidate][0]; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + /* |
| 81 | + * At this stage no usable id_candidate has been detected. |
| 82 | + */ |
| 83 | + throw new SimpleSAML_Error_Exception('This service needs at least one of the following |
| 84 | + attributes to identity users: '.implode(', ', $this->_candidates).'. Unfortunately not |
| 85 | + one of them was detected. Please ask your institution administrator to release one of |
| 86 | + them, or try using another identity provider.'); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + /** |
| 91 | + * Apply filter to add or replace attributes. |
| 92 | + * |
| 93 | + * Add or replace existing attributes with the configured values. |
| 94 | + * |
| 95 | + * @param array &$request The current request |
| 96 | + */ |
| 97 | + public function process(&$request) { |
| 98 | + assert('is_array($request)'); |
| 99 | + assert('array_key_exists("Attributes", $request)'); |
| 100 | + |
| 101 | + $ID = $this->addID($request['Attributes'], $request); |
| 102 | + |
| 103 | + if(isset($ID)) $request['Attributes'][$this->_id_attribute] = array($ID); |
| 104 | + } |
| 105 | +} |
0 commit comments