|  | @@ -76,24 +76,51 @@ class auth_plugin_macaroons extends auth_plugin_base {
 | 
	
		
			
			| 76 | 76 |  		if(!empty($_COOKIE[$this->config->cookie_name])) {
 | 
	
		
			
			| 77 | 77 |  			try {
 | 
	
		
			
			| 78 | 78 |  				$m = Macaroon::deserialize($_COOKIE[$this->config->cookie_name]);
 | 
	
		
			
			|  | 79 | +
 | 
	
		
			
			|  | 80 | +				$callbacks = array();
 | 
	
		
			
			|  | 81 | +
 | 
	
		
			
			|  | 82 | +				if(!empty($this->config->caveat1_condition)) {
 | 
	
		
			
			|  | 83 | +					array_push($callbacks, function($a) {
 | 
	
		
			
			|  | 84 | +						return !strcmp($a, $this->config->caveat1_condition);
 | 
	
		
			
			|  | 85 | +					});
 | 
	
		
			
			|  | 86 | +				}
 | 
	
		
			
			|  | 87 | +				if(!empty($this->config->caveat2_condition)) {
 | 
	
		
			
			|  | 88 | +					array_push($callbacks, function($a) {
 | 
	
		
			
			|  | 89 | +						return !strcmp($a, $this->config->caveat2_condition);
 | 
	
		
			
			|  | 90 | +					});
 | 
	
		
			
			|  | 91 | +				}
 | 
	
		
			
			|  | 92 | +				if(!empty($this->config->caveat3_condition)) {
 | 
	
		
			
			|  | 93 | +					array_push($callbacks, function($a) {
 | 
	
		
			
			|  | 94 | +						return !strcmp($a, $this->config->caveat3_condition);
 | 
	
		
			
			|  | 95 | +					});
 | 
	
		
			
			|  | 96 | +				}
 | 
	
		
			
			|  | 97 | +
 | 
	
		
			
			| 79 | 98 |  				$v = new Verifier();
 | 
	
		
			
			| 80 |  | -				$v->setCallbacks([
 | 
	
		
			
			| 81 |  | -					function($a) {
 | 
	
		
			
			| 82 |  | -						return !strcmp($a, "status = student");
 | 
	
		
			
			| 83 |  | -					}
 | 
	
		
			
			| 84 |  | -				]);
 | 
	
		
			
			|  | 99 | +				$v->setCallbacks($callbacks);
 | 
	
		
			
			| 85 | 100 |  
 | 
	
		
			
			| 86 | 101 |  				if($v->verify($m, $this->config->secret)) {
 | 
	
		
			
			| 87 |  | -					$name = explode(";", $m->getIdentifier());
 | 
	
		
			
			| 88 |  | -					$login = join("", $name);
 | 
	
		
			
			|  | 102 | +					$identifier = explode(";", $m->getIdentifier());
 | 
	
		
			
			|  | 103 | +					$parsed_id = $this->parse_identifier($identifier);
 | 
	
		
			
			|  | 104 | +					if(empty($parsed_id["username"])) {
 | 
	
		
			
			|  | 105 | +						$login = $parsed_id["firstname"].$parsed_id["lastname"];
 | 
	
		
			
			|  | 106 | +					} else {
 | 
	
		
			
			|  | 107 | +						$login = $parsed_id["username"];
 | 
	
		
			
			|  | 108 | +					}
 | 
	
		
			
			| 89 | 109 |  					$user = authenticate_user_login($login, null);
 | 
	
		
			
			| 90 | 110 |  
 | 
	
		
			
			| 91 | 111 |  					if($user) {
 | 
	
		
			
			| 92 |  | -						$user->firstname = $name[0];
 | 
	
		
			
			| 93 |  | -						$user->lastname = $name[1];
 | 
	
		
			
			| 94 |  | -						$user->email = preg_replace($placeholders, $name, $this->config->email_config);
 | 
	
		
			
			|  | 112 | +						if(!empty($parsed_id["firstname"])) {
 | 
	
		
			
			|  | 113 | +							$user->firstname = $parsed_id["firstname"];
 | 
	
		
			
			|  | 114 | +						}
 | 
	
		
			
			|  | 115 | +						if(!empty($parsed_id["lastname"])) {
 | 
	
		
			
			|  | 116 | +							$user->lastname = $parsed_id["lastname"];
 | 
	
		
			
			|  | 117 | +						}
 | 
	
		
			
			|  | 118 | +						$user->email = preg_replace($placeholders, [
 | 
	
		
			
			|  | 119 | +							$parsed_id["firstname"],
 | 
	
		
			
			|  | 120 | +							$parsed_id["lastname"]
 | 
	
		
			
			|  | 121 | +						], $this->config->email_config);
 | 
	
		
			
			| 95 | 122 |  						$DB->update_record('user', $user);
 | 
	
		
			
			| 96 |  | -						
 | 
	
		
			
			|  | 123 | +						var_dump($user);
 | 
	
		
			
			| 97 | 124 |  						complete_user_login($user);
 | 
	
		
			
			| 98 | 125 |  						redirect($CFG->wwwroot);
 | 
	
		
			
			| 99 | 126 |  					}
 | 
	
	
		
			
			|  | @@ -104,6 +131,31 @@ class auth_plugin_macaroons extends auth_plugin_base {
 | 
	
		
			
			| 104 | 131 |  		}
 | 
	
		
			
			| 105 | 132 |  	}
 | 
	
		
			
			| 106 | 133 |  
 | 
	
		
			
			|  | 134 | +	function parse_identifier($identifier) {
 | 
	
		
			
			|  | 135 | +		$placeholders = explode(";", $this->config->identifier_format);
 | 
	
		
			
			|  | 136 | +
 | 
	
		
			
			|  | 137 | +		$parsed_id = array();
 | 
	
		
			
			|  | 138 | +
 | 
	
		
			
			|  | 139 | +		// Check if the identifier has the same number of fields as configured
 | 
	
		
			
			|  | 140 | +		if(sizeof($placeholders) != sizeof($identifier)) {
 | 
	
		
			
			|  | 141 | +			// Returning an empty array as the return value is expected to be
 | 
	
		
			
			|  | 142 | +			// an array
 | 
	
		
			
			|  | 143 | +			return $parsed_id;
 | 
	
		
			
			|  | 144 | +		}
 | 
	
		
			
			|  | 145 | +
 | 
	
		
			
			|  | 146 | +		if(is_numeric($index = array_search("{{username}}", $placeholders))) {
 | 
	
		
			
			|  | 147 | +			$parsed_id["username"] = $identifier[$index];
 | 
	
		
			
			|  | 148 | +		}
 | 
	
		
			
			|  | 149 | +		if(is_numeric($index = array_search("{{firstname}}", $placeholders))) {
 | 
	
		
			
			|  | 150 | +			$parsed_id["firstname"] = $identifier[$index];
 | 
	
		
			
			|  | 151 | +		}
 | 
	
		
			
			|  | 152 | +		if(is_numeric($index = array_search("{{lastname}}", $placeholders))) {
 | 
	
		
			
			|  | 153 | +			$parsed_id["lastname"] = $identifier[$index];
 | 
	
		
			
			|  | 154 | +		}
 | 
	
		
			
			|  | 155 | +
 | 
	
		
			
			|  | 156 | +		return $parsed_id;
 | 
	
		
			
			|  | 157 | +	}
 | 
	
		
			
			|  | 158 | +
 | 
	
		
			
			| 107 | 159 |  	/**
 | 
	
		
			
			| 108 | 160 |  	 * Returns true if the username and password work or don't exist and false
 | 
	
		
			
			| 109 | 161 |  	 * if the user exists and the password is wrong.
 | 
	
	
		
			
			|  | @@ -206,18 +258,38 @@ class auth_plugin_macaroons extends auth_plugin_base {
 | 
	
		
			
			| 206 | 258 |  	 */
 | 
	
		
			
			| 207 | 259 |  	function process_config($config) {
 | 
	
		
			
			| 208 | 260 |  		if(!isset($config->cookie_name)) {
 | 
	
		
			
			| 209 |  | -			$config->cookie_name = '';
 | 
	
		
			
			|  | 261 | +			$config->cookie_name = 'das-macaroon';
 | 
	
		
			
			| 210 | 262 |  		}
 | 
	
		
			
			| 211 | 263 |  		if(!isset($config->secret)) {
 | 
	
		
			
			| 212 |  | -			$config->secret = '';
 | 
	
		
			
			|  | 264 | +			$config->secret = 'pocsecret';
 | 
	
		
			
			|  | 265 | +		}
 | 
	
		
			
			|  | 266 | +		if(!isset($config->identifier_format)) {
 | 
	
		
			
			|  | 267 | +			$config->identifier_format = '{{firstname}};{{lastname}}';
 | 
	
		
			
			| 213 | 268 |  		}
 | 
	
		
			
			| 214 | 269 |  		if(!isset($config->email_config)) {
 | 
	
		
			
			| 215 |  | -			$config->email_config = '';
 | 
	
		
			
			|  | 270 | +			$config->email_config = '{{firstname}}.{{lastname}}@company.tld';
 | 
	
		
			
			|  | 271 | +		}
 | 
	
		
			
			|  | 272 | +		// Caveats
 | 
	
		
			
			|  | 273 | +		if(!isset($config->caveat1_condition)) {
 | 
	
		
			
			|  | 274 | +				$config->caveat1_condition = '';
 | 
	
		
			
			|  | 275 | +		}
 | 
	
		
			
			|  | 276 | +		if(!isset($config->caveat2_condition)) {
 | 
	
		
			
			|  | 277 | +				$config->caveat2_condition = '';
 | 
	
		
			
			| 216 | 278 |  		}
 | 
	
		
			
			|  | 279 | +		if(!isset($config->caveat3_condition)) {
 | 
	
		
			
			|  | 280 | +				$config->caveat3_condition = '';
 | 
	
		
			
			|  | 281 | +		}
 | 
	
		
			
			|  | 282 | +
 | 
	
		
			
			| 217 | 283 |  
 | 
	
		
			
			| 218 | 284 |  		set_config('cookie_name', $config->cookie_name, self::COMPONENT_NAME);
 | 
	
		
			
			| 219 | 285 |  		set_config('secret', $config->secret, self::COMPONENT_NAME);
 | 
	
		
			
			|  | 286 | +		set_config('identifier_format', $config->identifier_format, self::COMPONENT_NAME);
 | 
	
		
			
			| 220 | 287 |  		set_config('email_config', $config->email_config, self::COMPONENT_NAME);
 | 
	
		
			
			|  | 288 | +		// Caveats
 | 
	
		
			
			|  | 289 | +		set_config('caveat1_condition', $config->caveat1_condition, self::COMPONENT_NAME);
 | 
	
		
			
			|  | 290 | +		set_config('caveat2_condition', $config->caveat2_condition, self::COMPONENT_NAME);
 | 
	
		
			
			|  | 291 | +		set_config('caveat3_condition', $config->caveat3_condition, self::COMPONENT_NAME);
 | 
	
		
			
			|  | 292 | +
 | 
	
		
			
			| 221 | 293 |  		return true;
 | 
	
		
			
			| 222 | 294 |  	}
 | 
	
		
			
			| 223 | 295 |  
 |