-
Notifications
You must be signed in to change notification settings - Fork 7
/
auth.go
59 lines (49 loc) · 1.26 KB
/
auth.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
package auth
import (
"crypto/sha1"
"fmt"
"strconv"
"time"
"github.com/GoPlusSecurity/goplus-sdk-go/pkg/gen/client"
"github.com/GoPlusSecurity/goplus-sdk-go/pkg/gen/client/token_controller"
"github.com/GoPlusSecurity/goplus-sdk-go/pkg/gen/models"
"k8s.io/utils/pointer"
)
type Config struct {
Timeout int
}
type Result = token_controller.GetAccessTokenUsingPOSTOK
type App struct {
Key string
Secret string
Config *Config
}
func NewApp(key, secret string, config *Config) *App {
return &App{
Key: key,
Secret: secret,
Config: config,
}
}
func (a *App) GetAccessToken() (*Result, error) {
now := time.Now().Unix()
nowStr := strconv.FormatInt(now, 10)
s := sign(a.Key, a.Secret, nowStr)
params := token_controller.NewGetAccessTokenUsingPOSTParams()
params.SetRequest(&models.GetAccessTokenRequest{
AppKey: pointer.String(a.Key),
Sign: pointer.String(s),
Time: pointer.Int64(now),
})
if a.Config != nil && a.Config.Timeout != 0 {
params.SetTimeout(time.Duration(a.Config.Timeout))
}
response, _, err := client.Default.TokenController.GetAccessTokenUsingPOST(params)
return response, err
}
func sign(key, secret, time string) string {
h := sha1.New()
s := key + time + secret
h.Write([]byte(s))
return fmt.Sprintf("%x", h.Sum(nil))
}