-
Notifications
You must be signed in to change notification settings - Fork 497
/
connection.go
78 lines (65 loc) · 2.23 KB
/
connection.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package models
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/url"
"sort"
"strings"
"github.com/apache/incubator-devlake/plugins/helper"
)
type AeConnection struct {
helper.RestConnection `mapstructure:",squash"`
helper.AppKey `mapstructure:",squash"`
}
type TestConnectionRequest struct {
Endpoint string `json:"endpoint"`
Proxy string `json:"proxy"`
helper.AppKey `mapstructure:",squash"`
}
// This object conforms to what the frontend currently expects.
type AeResponse struct {
AeConnection
Name string `json:"name"`
ID int `json:"id"`
}
func (AeConnection) TableName() string {
return "_tool_ae_connections"
}
func GetSign(query url.Values, appId, secretKey, nonceStr, timestamp string) string {
// clone query because we need to add items
kvs := make([]string, 0, len(query)+3)
kvs = append(kvs, fmt.Sprintf("app_id=%s", appId))
kvs = append(kvs, fmt.Sprintf("timestamp=%s", timestamp))
kvs = append(kvs, fmt.Sprintf("nonce_str=%s", nonceStr))
for key, values := range query {
for _, value := range values {
kvs = append(kvs, fmt.Sprintf("%s=%s", url.QueryEscape(key), url.QueryEscape(value)))
}
}
// sort by alphabetical order
sort.Strings(kvs)
// generate text for signature
querystring := fmt.Sprintf("%s&key=%s", strings.Join(kvs, "&"), url.QueryEscape(secretKey))
// sign it
hasher := md5.New()
_, err := hasher.Write([]byte(querystring))
if err != nil {
return ""
}
return strings.ToUpper(hex.EncodeToString(hasher.Sum(nil)))
}