-
Notifications
You must be signed in to change notification settings - Fork 11
/
configuration.go
109 lines (90 loc) · 3.39 KB
/
configuration.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
*
*
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: 1.0.0
*
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/
package logicmonitor
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
)
type Configuration struct {
Username string `json:"userName,omitempty"`
Password string `json:"password,omitempty"`
APIKeyPrefix map[string]string `json:"APIKeyPrefix,omitempty"`
APIKey map[string]map[string]string `json:"APIKey,omitempty"`
Debug bool `json:"debug,omitempty"`
DebugFile string `json:"debugFile,omitempty"`
OAuthToken string `json:"oAuthToken,omitempty"`
BasePath string `json:"basePath,omitempty"`
Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"`
AccessToken string `json:"accessToken,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
APIClient *APIClient
Transport *http.Transport
Timeout *time.Duration `json:"timeout,omitempty"`
}
func NewConfiguration() *Configuration {
cfg := &Configuration{
BasePath: "https://localhost/santaba/rest",
DefaultHeader: make(map[string]string),
APIKey: make(map[string]map[string]string),
APIKeyPrefix: make(map[string]string),
UserAgent: "Swagger-Codegen/1.0.0/go",
APIClient: &APIClient{},
}
cfg.APIClient.config = cfg
return cfg
}
func (c *Configuration) GetBasicAuthEncodedString() string {
return base64.StdEncoding.EncodeToString([]byte(c.Username + ":" + c.Password))
}
func (c *Configuration) AddDefaultHeader(key string, value string) {
c.DefaultHeader[key] = value
}
func (c *Configuration) GetAPIKeyWithPrefix(APIKeyIdentifier string, ResourcePath string, Method string, Body interface{}) string {
if c.APIKeyPrefix[APIKeyIdentifier] != "" {
return c.APIKeyPrefix[APIKeyIdentifier] + " " + c.GetAuthHash(APIKeyIdentifier, ResourcePath, Method, Body)
}
return c.GetAuthHash(APIKeyIdentifier, ResourcePath, Method, Body)
}
func (c *Configuration) GetAuthHash(APIKeyIdentifier string, ResourcePath string, Method string, Body interface{}) string {
data, err := json.Marshal(Body)
if err != nil {
return ""
}
if string(data) == "null" {
data = []byte{}
}
authHash := buildAuthHash(c.APIKey[APIKeyIdentifier], Method, string(data), ResourcePath)
return authHash
}
func buildAuthHash(A map[string]string, method, data, resource string) string {
now := time.Now()
nanos := now.UnixNano()
epoch := strconv.FormatInt(nanos/1000000, 10)
signature := buildSignature(A["AccessKey"], strings.ToUpper(method), epoch, data, resource)
auth := fmt.Sprintf("LMv1 %s:%s:%s", A["AccessID"], signature, epoch)
return auth
}
func buildSignature(accessKey, method, epoch, data, resource string) string {
h := hmac.New(sha256.New, []byte(accessKey))
h.Write([]byte(method + epoch + data + resource))
hexDigest := hex.EncodeToString(h.Sum(nil))
signature := base64.StdEncoding.EncodeToString([]byte(hexDigest))
return signature
}