Signature::getHeaderSign()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 1
1
<?php
2
namespace Upyun;
3
4
/**
5
 * Class Signature
6
 * @package Upyun
7
 */
8
class Signature
9
{
10
    /**
11
     * 获取分块上传接口的签名
12
     */
13
    const SIGN_MULTIPART     = 1;
14
    /**
15
     * 生成视频处理接口的签名
16
     */
17
    const SIGN_VIDEO         = 2;
18
    /**
19
     * 生成视频处理接口的签名(不需要操作员时使用)
20
     */
21
    const SIGN_VIDEO_NO_OPERATOR   = 3;
22
23
    /**
24
     * 获取 Header 签名需要的请求头
25
     *
26
     * @param Config $serviceConfig
27
     * @param $method 请求方法
28
     * @param $path  请求路径
29
     * @param $contentMd5 文件内容 md5
30
     *
31
     * @return array
32
     */
33 21
    public static function getHeaderSign($serviceConfig, $method, $path, $contentMd5 = null)
34
    {
35 21
        $gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
36
37 21
        $policy = null;
38 21
        $sign = self::getBodySignature($serviceConfig, $method, $path, $gmtDate, $policy, $contentMd5);
39
40
        $headers = array(
41 21
            'Authorization' => $sign,
42 21
            'Date' => $gmtDate,
43 21
            'User-agent' => 'Php-Sdk/' . $serviceConfig->getVersion()
44 21
        );
45 21
        return $headers;
46
    }
47
48
    /**
49
     * 获取请求缓存刷新接口需要的签名头
50
     *
51
     * @param Config $serviceConfig
52
     * @param $urlString
53
     *
54
     * @return array
55
     */
56 1
    public static function getPurgeSignHeader(Config $serviceConfig, $urlString)
57
    {
58 1
        $gmtDate = gmdate('D, d M Y H:i:s \G\M\T');
59 1
        $sign = md5("$urlString&{$serviceConfig->serviceName}&$gmtDate&{$serviceConfig->operatorPassword}");
60
        return array(
61 1
            'Authorization' => "UpYun {$serviceConfig->serviceName}:{$serviceConfig->operatorName}:$sign",
62 1
            'Date' => $gmtDate,
63 1
            'User-agent' => 'Php-Sdk/' . $serviceConfig->getVersion() . ' (purge api)'
64 1
        );
65
    }
66
67
    /**
68
     * 获取表单 API 需要的签名,依据 body 签名规则计算
69
     * @param Config $serviceConfig
70
     * @param $method 请求方法
71
     * @param $uri 请求路径
72
     * @param $date 请求时间
73
     * @param $policy
74
     * @param $contentMd5 请求 body 的 md5
75
     *
76
     * @return array
77
     */
78 23
    public static function getBodySignature(Config $serviceConfig, $method, $uri, $date = null, $policy = null, $contentMd5 = null)
79
    {
80
        $data = array(
81 23
            $method,
82
            $uri
83 23
        );
84 23
        if ($date) {
85 21
            $data[] = $date;
86 21
        }
87
88 23
        if ($policy) {
89 1
            $data[] = $policy;
90 1
        }
91
92 23
        if ($contentMd5) {
93
            $data[] = $contentMd5;
94
        }
95 23
        $signature = base64_encode(hash_hmac('sha1', implode('&', $data), $serviceConfig->operatorPassword, true));
96 23
        return 'UPYUN ' . $serviceConfig->operatorName . ':' . $signature;
97
    }
98
}
99