forked from ibrahimab/flat_auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflat_auth.module
175 lines (128 loc) · 4.63 KB
/
flat_auth.module
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
<?php
define('FLAT_AUTH_THRESHOLD_EXPIRE', 60 * 5);
define('FLAT_AUTH_THRESHOLD_DELETE', 60 * 10);
define('FLAT_AUTH_MAX_ERROR_COUNT', 10);
define('FLAT_AUTH_MAX_USAGE_COUNT', 10);
function flat_auth_menu() {
$items = [];
$items['flat_auth'] = [
'type' => MENU_LOCAL_TASK,
'title' => 'Flat Auth - Check Token',
'description' => 'Flat Auth - Check Token',
'page callback' => 'flat_auth_check_token',
'access callback' => true,
];
$items['flat_auth/create'] = [
'type' => MENU_LOCAL_TASK,
'title' => 'Flat Auth - Create Token',
'description' => 'Flat Auth - Create Token',
'page callback' => 'flat_auth_create_token',
'access callback' => true,
];
$items['flat_auth/renew'] = [
'type' => MENU_LOCAL_TASK,
'title' => 'Flat Auth - Renew Token',
'description' => 'Flat Auth - Renew Token',
'page callback' => 'flat_auth_renew_token',
'access callback' => true,
];
$items['admin/config/flat_deposit/flat_auth'] = [
'title' => 'FLAT Auth Configuration',
'description' => 'FLAT Auth Configuration',
'access arguments' => array('admin deposit module'),
'page callback' => 'drupal_get_form',
'page arguments' => array('flat_auth_admin_form'),
'file' => 'includes/admin.inc',
'weight' => 999,
];
return $items;
}
function flat_auth_authtoken_type_info() {
return [
'flat_auth' => [
'entity type' => 'user',
'label' => 'FLAT Auth',
'settings' => [
'threshold expire' => variable_get('flat_auth_expiration_time', FLAT_AUTH_THRESHOLD_EXPIRE),
'threshold delete' => variable_get('flat_auth_deletion_time', FLAT_AUTH_THRESHOLD_DELETE),
'max error count' => variable_get('flat_auth_max_error_count', FLAT_AUTH_MAX_ERROR_COUNT),
'max usage count' => variable_get('flat_auth_max_usage_count', FLAT_AUTH_MAX_USAGE_COUNT),
],
'generate token callback' => 'flat_auth_generate_token',
],
];
}
/**
* Generate an unique token
*
* @return string
*/
function flat_auth_generate_token() {
return drupal_strtoupper(bin2hex(drupal_random_bytes(16)));
}
/**
* Get the bearer token
*/
function flat_auth_get_bearer_token() {
$bearer_token = NULL;
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
$bearer_token = str_replace('Bearer ', '', $_SERVER['HTTP_AUTHORIZATION']);
} elseif (isset($_SERVER['HTTP_X_AUTHORIZATION'])) {
$bearer_token = str_replace('Bearer ', '', $_SERVER['HTTP_X_AUTHORIZATION']);
} elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
$bearer_token = str_replace('Bearer ', '', $_SERVER['REDIRECT_HTTP_AUTHORIZATION']);
}
return $bearer_token;
}
function flat_auth_check_token() {
$token = flat_auth_get_bearer_token();
$authtoken = authtoken_load($token);
if ($authtoken) {
if (authtoken_authenticate($authtoken->token(), [$authtoken->type()])) {
return drupal_json_output([
'type' => 'success',
'expiration_time' => (int)variable_get('flat_auth_expiration_time', FLAT_AUTH_THRESHOLD_EXPIRE),
'deletion_time' => (int)variable_get('flat_auth_deletion_time', FLAT_AUTH_THRESHOLD_DELETE),
]);
}
}
return drupal_json_output([
'type' => 'error',
'message' => 'Invalid token',
]);
}
function flat_auth_get_token() {
global $user;
return flat_auth_get_token_for_user($user);
}
function flat_auth_get_token_for_user($user) {
$entity = $user;
$authtoken = authtoken_assign('flat_auth', $user, $entity);
return $authtoken->token();
}
function flat_auth_renew_token() {
$token = flat_auth_get_bearer_token();
$authtoken = authtoken_load($token);
if ($authtoken) {
if (authtoken_authenticate($authtoken->token(), [$authtoken->type()])) {
if ($user = user_load($authtoken->uid())) {
$token = flat_auth_get_token_for_user($user);
drupal_add_http_header('Authorization', 'Bearer ' . $token);
return drupal_json_output([
'type' => 'success',
'expiration_time' => (int)variable_get('flat_auth_expiration_time', FLAT_AUTH_THRESHOLD_EXPIRE),
'deletion_time' => (int)variable_get('flat_auth_deletion_time', FLAT_AUTH_THRESHOLD_DELETE),
]);
}
}
}
return drupal_json_output([
'type' => 'error',
'message' => 'Invalid token',
]);
}
function flat_auth_create_token() {
return drupal_json_output([
'token' => flat_auth_get_token(),
]);
}