Browse Source

Configurable caveats

Brendan Abolivier 7 years ago
parent
commit
884d408bc9
2 changed files with 139 additions and 17 deletions
  1. 86
    14
      auth.php
  2. 53
    3
      config.html

+ 86
- 14
auth.php View File

@@ -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
 

+ 53
- 3
config.html View File

@@ -1,12 +1,25 @@
1 1
 <?php
2 2
 	if(!isset($config->cookie_name)) {
3
-		$config->cookie_name = '';
3
+		$config->cookie_name = 'das-macaroon';
4 4
 	}
5 5
 	if(!isset($config->secret)) {
6
-		$config->secret = '';
6
+		$config->secret = 'pocsecret';
7
+	}
8
+	if(!isset($config->identifier_format)) {
9
+		$config->identifier_format = '{{firstname}};{{lastname}}';
7 10
 	}
8 11
 	if(!isset($config->email_config)) {
9
-		$config->email_config = '';
12
+		$config->email_config = '{{firstname}}.{{lastname}}@company.tld';
13
+	}
14
+	// Caveats
15
+	if(!isset($config->caveat1_condition)) {
16
+		$config->caveat1_condition = '';
17
+	}
18
+	if(!isset($config->caveat2_condition)) {
19
+		$config->caveat2_condition = '';
20
+	}
21
+	if(!isset($config->caveat3_condition)) {
22
+		$config->caveat3_condition = '';
10 23
 	}
11 24
 ?>
12 25
 <table cellspacing="0" cellpadding="5" border="0">
@@ -35,6 +48,16 @@
35 48
 	</tr>
36 49
 	<tr>
37 50
 		<td align="right">
51
+			<label for="identifier_format">Identifier format</label>
52
+		</td>
53
+		<td>
54
+			<input name="identifier_format" id="identifier_format" type="text" size="50" value="<?php echo $config->identifier_format; ?>" />
55
+		</td>
56
+		<td>Your Macaroon's identifier format. Available placeholders are {{username}}, {{firstname}}, {{lastname}}. Elements must me delimited with semicolons (";").<br />
57
+eg: {{firstname}};{{lastname}}</td>
58
+	</tr>
59
+	<tr>
60
+		<td align="right">
38 61
 			<label for="email_config">E-mail template</label>
39 62
 		</td>
40 63
 		<td>
@@ -43,4 +66,31 @@
43 66
 		<td>Template for emails. Available placeholders are {{firstname}} and {{lastname}}.<br />
44 67
 eg: {{firstname}}.{{lastname}}@company.tld</td>
45 68
 	</tr>
69
+	<tr>
70
+		<td align="right">
71
+			<label for="caveat1_condition">First caveat condition</label>
72
+		</td>
73
+		<td>
74
+			<input name="caveat1_condition" id="caveat1_condition" type="text" size="50" value="<?php echo $config->caveat1_condition; ?>" />
75
+		</td>
76
+		<td>The condition in your macaroon's first caveat (optional)</td>
77
+	</tr>
78
+	<tr>
79
+		<td align="right">
80
+			<label for="caveat2_condition">Second caveat condition</label>
81
+		</td>
82
+		<td>
83
+			<input name="caveat2_condition" id="caveat2_condition" type="text" size="50" value="<?php echo $config->caveat2_condition; ?>" />
84
+		</td>
85
+		<td>The condition in your macaroon's second caveat (optional)</td>
86
+	</tr>
87
+	<tr>
88
+		<td align="right">
89
+			<label for="caveat3_condition">Third caveat condition</label>
90
+		</td>
91
+		<td>
92
+			<input name="caveat3_condition" id="caveat3_condition" type="text" size="50" value="<?php echo $config->caveat3_condition; ?>" />
93
+		</td>
94
+		<td>The condition in your macaroon's third caveat (optional)</td>
95
+	</tr>
46 96
 </table>