-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunblock.go
More file actions
94 lines (80 loc) · 2.57 KB
/
Copy pathunblock.go
File metadata and controls
94 lines (80 loc) · 2.57 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
// SPDX-FileCopyrightText: Copyright 2026 Securosys SA
// SPDX-License-Identifier: Apache-2.0
package client
import (
"bytes"
"context"
"encoding/json"
"net/http"
helpers "github.com/securosys-com/tsb-client-go/helpers"
)
// Function thats send unblock request to TSB
func (c *TSBClient) UnBlock(ctx context.Context, label string, password string) (int, error) {
if ctx == nil {
ctx = context.Background()
}
charsPasswordJson, _ := json.Marshal(helpers.StringToCharArray(password))
passwordString := ""
if len(charsPasswordJson) > 2 {
passwordString = `"keyPassword": ` + string(charsPasswordJson) + `,`
}
var jsonStr = []byte(`{
"unblockRequest": {
` + passwordString + `
"unblockKeyName": "` + label + `"
}
}`)
req, err := http.NewRequestWithContext(ctx, "POST", c.HostURL+"/v1/synchronousUnblock", bytes.NewBuffer(jsonStr))
if err != nil {
return 500, err
}
_, code, errRes := c.doRequest(req, KeyOperationTokenName)
if errRes != nil {
return code, errRes
}
return code, nil
}
// Function thats send asynchronous unblock request to TSB
func (c *TSBClient) AsyncUnBlock(ctx context.Context, label string, password string, customMetaData map[string]string) (string, int, error) {
if ctx == nil {
ctx = context.Background()
}
charsPasswordJson, _ := json.Marshal(helpers.StringToCharArray(password))
var additionalMetaDataInfo map[string]string = make(map[string]string)
metaDataB64, metaDataSignature, err := c.PrepareMetaData("UnBlock", additionalMetaDataInfo, customMetaData)
if err != nil {
return "", 500, err
}
passwordString := ""
if len(charsPasswordJson) > 2 {
passwordString = `"keyPassword": ` + string(charsPasswordJson) + `,`
}
metaDataSignatureString := "null"
if metaDataSignature != nil {
metaDataSignatureString = `"` + *metaDataSignature + `"`
}
requestJson := `{
"unblockKeyName": "` + label + `",
` + passwordString + `
"metaData": "` + metaDataB64 + `",
"metaDataSignature": ` + metaDataSignatureString + `
}`
var jsonStr = []byte(helpers.MinifyJson(`{
"unblockRequest": ` + requestJson + `,
"requestSignature":` + string(c.GenerateRequestSignature(requestJson)) + `
}`))
req, err := http.NewRequestWithContext(ctx, "POST", c.HostURL+"/v1/unblock", bytes.NewBuffer(jsonStr))
if err != nil {
return "", 500, err
}
body, code, errRes := c.doRequest(req, KeyOperationTokenName)
if errRes != nil {
return "", code, errRes
}
var result map[string]interface{}
errJSON := json.Unmarshal(body, &result)
if errJSON != nil {
return "", code, errJSON
}
return result["unblockKeyRequestId"].(string), code, nil
}