-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrezor_connect.manage.inc
303 lines (227 loc) · 6.62 KB
/
trezor_connect.manage.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* @file
* Provides manage functionality.
*/
/**
* Provides the manage mappings form.
*
* @param $form
* @param $form_state
* @param $account
*
* @return array
* @throws \Exception
*/
function trezor_connect_manage_form($form, &$form_state, $account) {
$form = array();
$mapping = trezor_connect_account_mapping($account);
if (!$mapping) {
$form_id = 'trezor_connect_manage_form';
trezor_connect_alter_form($form, $form_state, $form_id);
$mode = 'manage';
$url = '/' . str_replace('%user', $account->uid, TREZOR_CONNECT_URL_MANAGE_JS);
$form['#attached']['js'][] = array(
'data' => array(
'trezor_connect' => array(
'mode' => $mode,
'url' => $url,
'form_id' => $form_id,
),
),
'type' => 'setting',
);
}
else {
$header = array(
t('Created On'),
//t('Address'),
);
$rows = array();
$created = $mapping['created'];
$row[] = format_date($created);
//$address = $mapping['address'];
//$address = check_plain($address);
/*
$path = 'http://blockchain.info/address/' . $address;
$options = array(
'attributes' => array(
'target' => '_new',
),
);
$link = l($address, $path, $options);
*/
//$link = $address;
//$row[] = $link;
$rows[] = $row;
$variables = array(
'header' => $header,
'rows' => $rows,
);
$markup = theme('table', $variables);
$form['table'] = array(
'#markup' => $markup,
);
$form['forget'] = array(
'#type' => 'submit',
'#value' => t('Forget Authentication Device'),
);
$form['account'] = array(
'#type' => 'value',
'#value' => $account,
);
}
return $form;
}
/**
* Provides the manage mappings form submit handler.
*
* @param $form
* @param $form_state
* @param $account
*
* @return array
* @throws \Exception
*/
function trezor_connect_manage_form_submit($form, &$form_state) {
$values = $form_state['values'];
$account = $values['account'];
$uid = $account->uid;
$form_state['redirect'] = 'user/' . $uid . '/trezor-connect/forget';
}
/**
* Provides the forget authenticated device confirmation form.
*
* @param $form
* @param $form_state
* @param $account
*
* @return mixed
*/
function trezor_connect_forget_form($form, &$form_state, $account) {
$form['account'] = array(
'#type' => 'value',
'#value' => $account,
);
$question = t('Are you sure you want to forget your authentication device?');
$path = url('user/' . $account->uid . '/trezor-connect');
$description = t('You will have to re-associate your authentication device to your account if you continue.');
$yes = t('Confirm');
$no = t('Cancel');
$form = confirm_form($form, $question, $path, $description, $yes, $no);
return $form;
}
/**
* Provides the forget authenticated device confirmation form submit handler.
*
* @param $form
* @param $form_state
*/
function trezor_connect_forget_form_submit($form, &$form_state) {
$values = $form_state['values'];
$account = $values['account'];
trezor_connect_delete_mapping($account);
$message = t('Your authentication device has been forgotten.');
drupal_set_message($message);
$path = url('user/' . $account->uid . '/trezor-connect');
$form_state['redirect'] = $path;
}
/**
* Provides the page callback used to process a TREZOR connect manage response.
*/
function trezor_connect_page_callback_manage($account, $type = 'nojs') {
$output = NULL;
$response = $_POST['response'];
$challenge = $_POST['challenge'];
$result = trezor_connect_response_valid($response, $challenge);
if (!$result) {
if ($type == 'nojs') {
drupal_access_denied();
}
else {
$commands = array();
$selector = '';
if (isset($_POST['selector'])) {
$selector = $_POST['selector'];
$selector = check_plain($selector);
//$selector = '#' . $selector;
}
$arguments = array();
$arguments['redirect'] = FALSE;
$arguments['error'] = TRUE;
$message = t('An error has occurred validating your TREZOR credentials.');
$variables = array(
'type' => 'error',
'message' => $message,
);
$message = theme('trezor_connect_message', $variables);
$arguments['message'] = $message;
// IMPORTANT: misc/ajax.js line 605 $element[response.method].apply($element, response.arguments);
// requires a very specific format otherwise the $arguments will be passed as undefined
$arguments = array(
'callback',
$arguments,
);
$commands[] = ajax_command_invoke($selector, 'trezor_connect', $arguments);
$output = array(
'#type' => 'ajax',
'#commands' => $commands,
);
$output = ajax_deliver($output);
}
}
else {
$result = trezor_connect_mapping($response);
if (is_array($result) && isset($result['uid'])) {
$message = t('There is already an account associated with the TREZOR device.');
drupal_set_message($message, 'warning');
}
else {
$keys = array(
'challenge_hidden',
'challenge_visual',
);
foreach ($keys as $key) {
$response[$key] = $challenge[$key];
}
$response['uid'] = $account->uid;
trezor_connect_write_map($response);
$message = t('Your TREZOR device has been associated to your account. You should now be able to login with just your TREZOR device.');
drupal_set_message($message);
}
$path = str_replace('%user', $account->uid, TREZOR_CONNECT_URL_MANAGE);
if ($type != 'ajax') {
drupal_goto($path);
}
else {
$commands = array();
$selector = '';
if (isset($_POST['selector'])) {
$selector = $_POST['selector'];
$selector = check_plain($selector);
//$selector = '#' . $selector;
}
$arguments = array();
$arguments['redirect'] = TRUE;
$variables = array(
'message' => $message,
);
$message = theme('trezor_connect_message', $variables);
$arguments['message'] = $message;
$arguments['url'] = '/' . $path;
// IMPORTANT: misc/ajax.js line 605 $element[response.method].apply($element, response.arguments);
// requires a very specific format otherwise the $arguments will be passed as undefined
$arguments = array(
'callback',
$arguments,
);
$commands[] = ajax_command_invoke($selector, 'trezor_connect', $arguments);
$output = array(
'#type' => 'ajax',
'#commands' => $commands,
);
$output = ajax_deliver($output);
}
}
return $output;
}