Browse Source

Cleaning up and commenting

Brendan Abolivier 8 years ago
parent
commit
786b6dcde5
2 changed files with 21 additions and 3 deletions
  1. 20
    3
      auth.php
  2. 1
    0
      config.html

+ 20
- 3
auth.php View File

76
 	*/
76
 	*/
77
 	function loginpage_hook() {
77
 	function loginpage_hook() {
78
 		global $DB, $login, $CFG;
78
 		global $DB, $login, $CFG;
79
-		$placeholders[0] = "/{{firstname}}/";
80
-		$placeholders[1] = "/{{lastname}}/";
79
+
81
 		if(!empty($_COOKIE[$this->config->cookie_name])) {
80
 		if(!empty($_COOKIE[$this->config->cookie_name])) {
82
 			try {
81
 			try {
82
+				// Getting the macaroon from the cookie it's stored in
83
 				$m = Macaroon::deserialize($_COOKIE[$this->config->cookie_name]);
83
 				$m = Macaroon::deserialize($_COOKIE[$this->config->cookie_name]);
84
 
84
 
85
 				$callbacks = array();
85
 				$callbacks = array();
86
 
86
 
87
+				// Defining the callbacks according to the plugin's configuration
88
+				// in order to check all caveats
87
 				if(!empty($this->config->caveat1_condition)) {
89
 				if(!empty($this->config->caveat1_condition)) {
88
 					array_push($callbacks, function($a) {
90
 					array_push($callbacks, function($a) {
89
 						return !strcmp($a, $this->config->caveat1_condition);
91
 						return !strcmp($a, $this->config->caveat1_condition);
103
 				$v = new Verifier();
105
 				$v = new Verifier();
104
 				$v->setCallbacks($callbacks);
106
 				$v->setCallbacks($callbacks);
105
 
107
 
108
+				// This will check both the signature and the caveats. Both must be OK
109
+				// in order to continue
106
 				if($v->verify($m, $this->config->secret)) {
110
 				if($v->verify($m, $this->config->secret)) {
107
 					$identifier = explode(";", $m->getIdentifier());
111
 					$identifier = explode(";", $m->getIdentifier());
108
 					$parsed_id = $this->parse_identifier($identifier);
112
 					$parsed_id = $this->parse_identifier($identifier);
111
 					} else {
115
 					} else {
112
 						$login = $parsed_id["username"];
116
 						$login = $parsed_id["username"];
113
 					}
117
 					}
118
+
119
+					// Checking if the user is accepted by at least one authentication
120
+					// method (ours should accept it), and retrieving the user's class
121
+					// This will create the user if it doesn't exist
114
 					$user = authenticate_user_login($login, null);
122
 					$user = authenticate_user_login($login, null);
115
 
123
 
116
 					if($user) {
124
 					if($user) {
120
 						if(!empty($parsed_id["lastname"])) {
128
 						if(!empty($parsed_id["lastname"])) {
121
 							$user->lastname = $parsed_id["lastname"];
129
 							$user->lastname = $parsed_id["lastname"];
122
 						}
130
 						}
131
+
132
+						// Generating the user's e-mail address according
133
+						// to its name and the config's template
134
+						$placeholders[0] = "/{{firstname}}/";
135
+						$placeholders[1] = "/{{lastname}}/";
123
 						$user->email = preg_replace($placeholders, [
136
 						$user->email = preg_replace($placeholders, [
124
 							$parsed_id["firstname"],
137
 							$parsed_id["firstname"],
125
 							$parsed_id["lastname"]
138
 							$parsed_id["lastname"]
126
 						], $this->config->email_config);
139
 						], $this->config->email_config);
140
+						// Register modifications in DB, and logging the user in
127
 						$DB->update_record('user', $user);
141
 						$DB->update_record('user', $user);
128
-						var_dump($user);
129
 						complete_user_login($user);
142
 						complete_user_login($user);
143
+						// Authentication is OK, let's redirect the user out of
144
+						// the login page
130
 						redirect($CFG->wwwroot);
145
 						redirect($CFG->wwwroot);
131
 					}
146
 					}
132
 				}
147
 				}
133
 			} catch(Exception $e) {
148
 			} catch(Exception $e) {
149
+				// We currently do nothing with exceptions
134
 				$message = $e->getMessage();
150
 				$message = $e->getMessage();
135
 			}
151
 			}
136
 		}
152
 		}
155
 			return $parsed_id;
171
 			return $parsed_id;
156
 		}
172
 		}
157
 
173
 
174
+		// Filling the fields
158
 		if(is_numeric($index = array_search("{{username}}", $placeholders))) {
175
 		if(is_numeric($index = array_search("{{username}}", $placeholders))) {
159
 			$parsed_id["username"] = $identifier[$index];
176
 			$parsed_id["username"] = $identifier[$index];
160
 		}
177
 		}

+ 1
- 0
config.html View File

1
 <?php
1
 <?php
2
+	// Set to defaults if empty
2
 	if(!isset($config->cookie_name)) {
3
 	if(!isset($config->cookie_name)) {
3
 		$config->cookie_name = 'das-macaroon';
4
 		$config->cookie_name = 'das-macaroon';
4
 	}
5
 	}