-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttps_django_auth.php
56 lines (44 loc) · 1.54 KB
/
https_django_auth.php
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
<?php
function login_django_https($username, $password, $url) {
/*
Log in to Tournesol Django using an https request.
Args:
$username: username of the user trying to log in
$password: password of the user trying to log in in plain text
$url: the url to send requests to
Returns:
[authorized==true if the login was successful, false on authentication failure
id is a unique identified]
throws exceptions in case if connection failed/database does not exist/user does not exist
*/
// will do a PATCH request
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// sending json and receiving json
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// username and password as JSON
$data = json_encode(['username' => $username, 'password' => $password]);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
// executing the request
$resp = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// by-default, unauthorized
$authorized = false;
// resulting ID is invalid
$id_db = -1;
// on success, return the ID
if($code === 200) {
$authorized = true;
$resp_decoded = json_decode($resp, true);
$id_db = $resp_decoded['user__id'];
}
return ['authorized' => $authorized, 'id' => $id_db];
}
?>