auth_digest.php
1.91 KB
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
<?php
require_once("utils.php");
require_once("conf.php");
// ----------------------------------------------------------
class Qiniu_Mac {
public $AccessKey;
public $SecretKey;
public function __construct($accessKey, $secretKey)
{
$this->AccessKey = $accessKey;
$this->SecretKey = $secretKey;
}
public function Sign($data) // => $token
{
$sign = hash_hmac('sha1', $data, $this->SecretKey, true);
return $this->AccessKey . ':' . Qiniu_Encode($sign);
}
public function SignWithData($data) // => $token
{
$data = Qiniu_Encode($data);
return $this->Sign($data) . ':' . $data;
}
public function SignRequest($req, $incbody) // => ($token, $error)
{
$url = $req->URL;
$url = parse_url($url['path']);
$data = '';
if (isset($url['path'])) {
$data = $url['path'];
}
if (isset($url['query'])) {
$data .= '?' . $url['query'];
}
$data .= "\n";
if ($incbody) {
$data .= $req->Body;
}
return $this->Sign($data);
}
public function VerifyCallback($auth, $url, $body) // ==> bool
{
$url = parse_url($url);
$data = '';
if (isset($url['path'])) {
$data = $url['path'];
}
if (isset($url['query'])) {
$data .= '?' . $url['query'];
}
$data .= "\n";
$data .= $body;
$token = 'QBox ' . $this->Sign($data);
return $auth === $token;
}
}
function Qiniu_SetKeys($accessKey, $secretKey)
{
global $QINIU_ACCESS_KEY;
global $QINIU_SECRET_KEY;
$QINIU_ACCESS_KEY = $accessKey;
$QINIU_SECRET_KEY = $secretKey;
}
function Qiniu_RequireMac($mac) // => $mac
{
if (isset($mac)) {
return $mac;
}
global $QINIU_ACCESS_KEY;
global $QINIU_SECRET_KEY;
return new Qiniu_Mac($QINIU_ACCESS_KEY, $QINIU_SECRET_KEY);
}
function Qiniu_Sign($mac, $data) // => $token
{
return Qiniu_RequireMac($mac)->Sign($data);
}
function Qiniu_SignWithData($mac, $data) // => $token
{
return Qiniu_RequireMac($mac)->SignWithData($data);
}
// ----------------------------------------------------------