-
Notifications
You must be signed in to change notification settings - Fork 0
/
identify.go
77 lines (67 loc) · 2.25 KB
/
identify.go
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
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
func geneDeviceSign(macConfData MacConfDataRespStu) string {
sharekey := macConfData.Primarykey
policyName := Config.DeviceProvisioning.PolicyName
uri := GetRegDpsUri(macConfData)
expires := time.Now().Unix() + 3600
signstr := fmt.Sprintf("%s\n%d", url.QueryEscape(uri), expires)
signkey := Base64Decode(sharekey)
h := hmac.New(sha256.New, signkey)
h.Write([]byte(signstr))
token := base64.StdEncoding.EncodeToString(h.Sum(nil))
params := url.Values{}
params.Add("sr", uri)
params.Add("sig", token)
params.Add("se", strconv.FormatInt(expires, 10))
params.Add("skn", policyName)
Debug("Authorization:----", fmt.Sprintf("SharedAccessSignature %s", params.Encode()))
return fmt.Sprintf("SharedAccessSignature %s", params.Encode())
}
// GetIotHubConnectString HostName=fornanjing.azure-devices.cn;DeviceId=dca6321ac5fa;SharedAccessKey=6ZOVJ15hjrKFtZSNv1S6N9Vr7nx1Is0HwmTaKQO+GwM2F4oaL5sfgEWthkMvLFypPSAdaMU4ZkkLajUDVOxncA==
func GetIotHubConnectString(macConfData MacConfDataRespStu, assignedHub string) string {
return fmt.Sprintf("HostName=%s;DeviceId=%s;SharedAccessKey=%s", assignedHub, macConfData.Mac, macConfData.Primarykey)
}
//accessKey
func accessKey(auth *Authentication, secondary bool) (string, error) {
if secondary {
return auth.SymmetricKey.SecondaryKey, nil
}
return auth.SymmetricKey.PrimaryKey, nil
}
// DeviceConnectionString builds up a connection string for the given device.
func DeviceConnectionString(device *Device, secondary bool, assignedHub string) (string, error) {
key, err := accessKey(device.Authentication, secondary)
if err != nil {
return "", err
}
return fmt.Sprintf("HostName=%s;DeviceId=%s;SharedAccessKey=%s",
assignedHub, device.DeviceID, key,
), nil
}
//DeviceSAS 生成sas-token
func DeviceSAS(device *Device, assignedHub, resource string, duration time.Duration, secondary bool) (string, error) {
key, err := accessKey(device.Authentication, secondary)
if err != nil {
return "", err
}
sas, _ := NewSharedAccessSignature(
assignedHub+"/"+strings.TrimLeft(resource, "/"),
"iothubowner",
key,
time.Now().Add(duration),
)
if err != nil {
return "", err
}
return sas.String(), nil
}