|
| 1 | +<?php |
| 2 | +namespace SimpleSAML\Module\elixir\Auth\Process; |
| 3 | + |
| 4 | +use SimpleSAML\Auth\ProcessingFilter; |
| 5 | +use SimpleSAML\Auth\State; |
| 6 | +use SimpleSAML\Configuration; |
| 7 | +use SimpleSAML\Error\Exception; |
| 8 | +use SimpleSAML\Logger; |
| 9 | +use SimpleSAML\Module; |
| 10 | + |
| 11 | +class CSCMfa extends ProcessingFilter |
| 12 | +{ |
| 13 | + const MFA_IDENTIFIER = 'https://refeds.org/profile/mfa'; |
| 14 | + const CONFIG_FILE_NAME = 'module_elixir.php'; |
| 15 | + |
| 16 | + const CLIENT_ID = 'clientId'; |
| 17 | + const CLIENT_SECRET = 'clientSecret'; |
| 18 | + const REQUESTED_SCOPES = 'requestedScopes'; |
| 19 | + const OPENID_CONFIGURATION_URL = 'openidConfigurationUrl'; |
| 20 | + const AUTHORIZATION_ENDPOINT = 'authorization_endpoint'; |
| 21 | + const TOKEN_ENDPOINT = 'token_endpoint'; |
| 22 | + const USERINFO_ENDPOINT = 'userinfo_endpoint'; |
| 23 | + |
| 24 | + private $clientId; |
| 25 | + private $requestedScopes; |
| 26 | + private $authorizationEndpoint; |
| 27 | + private $redirectUri; |
| 28 | + |
| 29 | + public function __construct($config, $reserved) |
| 30 | + { |
| 31 | + assert('is_array($config)'); |
| 32 | + parent::__construct($config, $reserved); |
| 33 | + |
| 34 | + $conf = Configuration::getConfig(self::CONFIG_FILE_NAME); |
| 35 | + |
| 36 | + $this->clientId = $conf->getString(self::CLIENT_ID, ''); |
| 37 | + $this->requestedScopes = $conf->getArray(self::REQUESTED_SCOPES, []); |
| 38 | + $openidConfigurationUrl =$conf->getString(self::OPENID_CONFIGURATION_URL, ''); |
| 39 | + |
| 40 | + if (empty($this->clientId)) { |
| 41 | + throw new Exception( |
| 42 | + 'elixir:CSCMfa: missing mandatory configuration option "' . self::CLIENT_ID . |
| 43 | + '" in configuration file "' . self::CONFIG_FILE_NAME . '".' |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + if (empty($this->requestedScopes)) { |
| 48 | + throw new Exception( |
| 49 | + 'elixir:CSCMfa: missing mandatory configuration option "' . self::REQUESTED_SCOPES . |
| 50 | + '" in configuration file "' . self::CONFIG_FILE_NAME . '".' |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + if (empty($openidConfigurationUrl)) { |
| 55 | + throw new Exception( |
| 56 | + 'elixir:CSCMfa: missing mandatory configuration option "' . self::AUTHORIZATION_ENDPOINT . |
| 57 | + '" in configuration file "' . self::CONFIG_FILE_NAME . '".' |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + $metadata = json_decode(file_get_contents($openidConfigurationUrl), true); |
| 62 | + $this->authorizationEndpoint = $metadata[self::AUTHORIZATION_ENDPOINT]; |
| 63 | + |
| 64 | + $this->redirectUri = Module::getModuleURL('elixir').'/CSCMfaContinue.php'; |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + public function process(&$request) |
| 70 | + { |
| 71 | + assert('is_array($request)'); |
| 72 | + |
| 73 | + $requestedAuthnContextClassRef = array(); |
| 74 | + |
| 75 | + if (isset($request['saml:RequestedAuthnContext']['AuthnContextClassRef'])) { |
| 76 | + $requestedAuthnContextClassRef = $request['saml:RequestedAuthnContext']['AuthnContextClassRef'][0]; |
| 77 | + if (! is_array($requestedAuthnContextClassRef)) { |
| 78 | + $requestedAuthnContextClassRef = array($requestedAuthnContextClassRef); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (!in_array(self::MFA_IDENTIFIER, $requestedAuthnContextClassRef)) { |
| 83 | + # Everything is OK, SP didn't requested MFA |
| 84 | + Logger::debug('Multi factor authentication is not required'); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + # Check if IdP did MFA |
| 89 | + $authContextClassRef = array(); |
| 90 | + if (isset($request['saml:sp:AuthnContext'])) { |
| 91 | + $authContextClassRef = $request['saml:sp:AuthnContext']; |
| 92 | + if (!is_array($authContextClassRef)) { |
| 93 | + $authContextClassRef = array($authContextClassRef); |
| 94 | + } |
| 95 | + } |
| 96 | + if (in_array(self::MFA_IDENTIFIER, $authContextClassRef)) { |
| 97 | + # MFA was performed on IdP |
| 98 | + Logger::debug('Multi factor authentication was performed on Identity provider side'); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + Logger::debug('Multi factor authentication wasn\'t performed and will be performed on CSC side.'); |
| 103 | + |
| 104 | + $stateId = State::saveState($request, 'elixir:CSCMfa', true); |
| 105 | + |
| 106 | + if (!isset($request['Attributes']['eduPersonUniqueId'][0])) { |
| 107 | + throw new Exception( |
| 108 | + 'elixir:CSCMfa: missing required attribute "eduPersonUniqueId" in request' |
| 109 | + ); |
| 110 | + } |
| 111 | + $elixirId = $request['Attributes']['eduPersonUniqueId'][0]; |
| 112 | + |
| 113 | + # Prepare claims |
| 114 | + $claims = [ |
| 115 | + 'id_token' => [ |
| 116 | + 'sub' => [ |
| 117 | + 'value' => $stateId |
| 118 | + ], |
| 119 | + 'otp_key' => [ |
| 120 | + 'value' => $elixirId |
| 121 | + ] |
| 122 | + ] |
| 123 | + ]; |
| 124 | + |
| 125 | + if (isset( $request['Attributes']['mobile'][0])) { |
| 126 | + $phoneNumber = $request['Attributes']['mobile'][0]; |
| 127 | + $claims['id_token']['mobile']['value'] = $phoneNumber; |
| 128 | + } |
| 129 | + |
| 130 | + # Prepare params |
| 131 | + $params = [ |
| 132 | + 'response_type' => 'code', |
| 133 | + 'scope' => implode(' ', $this->requestedScopes), |
| 134 | + 'client_id' => $this->clientId, |
| 135 | + 'redirect_uri' => $this->redirectUri, |
| 136 | + 'state' => $stateId, |
| 137 | + 'claims' => json_encode($claims), |
| 138 | + ]; |
| 139 | + |
| 140 | + $mfa_url = $this->authorizationEndpoint . '?' . http_build_query($params); |
| 141 | + |
| 142 | + Header('Location: ' . $mfa_url); |
| 143 | + exit(); |
| 144 | + |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +?> |
0 commit comments