-
Notifications
You must be signed in to change notification settings - Fork 803
/
token.go
121 lines (96 loc) · 3.5 KB
/
token.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
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright 2022 The KodeRover Authors.
Licensed 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 gitlab
import (
"fmt"
"sync"
"time"
"github.com/koderover/zadig/pkg/shared/client/systemconfig"
"github.com/koderover/zadig/pkg/tool/httpclient"
"github.com/koderover/zadig/pkg/tool/log"
)
type AccessToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
CreatedAt int `json:"created_at"`
}
const TokenExpirationThreshold int64 = 7000
var mu = &sync.RWMutex{}
func UpdateGitlabToken(id int, accessToken string) (string, error) {
// if accessToken is empty, then it is either of ssh token type or username/password, we return empty
if accessToken == "" {
return "", nil
}
mu.Lock()
defer mu.Unlock()
ch, err := systemconfig.New().GetCodeHost(id)
if err != nil {
return "", fmt.Errorf("get codehost info error: [%s]", err)
}
oldToken := ch.AccessToken
if time.Now().Unix()-ch.UpdatedAt <= TokenExpirationThreshold {
return ch.AccessToken, nil
}
log.Infof("Starting to refresh gitlab token, old token issued time: %d", ch.UpdatedAt)
token, err := refreshAccessToken(ch.Address, ch.AccessKey, ch.SecretKey, ch.RefreshToken)
if err != nil {
return "", err
}
newCodehost := &systemconfig.CodeHost{
ID: ch.ID,
Address: ch.Address,
Type: ch.Type,
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
Namespace: ch.Namespace,
Region: ch.Region,
AccessKey: ch.AccessKey,
SecretKey: ch.SecretKey,
Username: ch.Username,
Password: ch.Password,
EnableProxy: ch.EnableProxy,
UpdatedAt: time.Now().Unix(),
Alias: ch.Alias,
AuthType: ch.AuthType,
SSHKey: ch.SSHKey,
PrivateAccessToken: ch.PrivateAccessToken,
}
log.Infof("the update time of the codehost is: %d", newCodehost.UpdatedAt)
// Since the new token is valid, we simply log the error
// No error will be returned, only the new token is returned
if err = systemconfig.New().UpdateCodeHost(ch.ID, newCodehost); err != nil {
log.Errorf("failed to update codehost, err: %s", err)
}
log.Infof("gitlab token for client [%d] changed from [%s] to [%s]", id, oldToken, token.AccessToken)
return token.AccessToken, nil
}
func refreshAccessToken(address, clientID, clientSecret, refreshToken string) (*AccessToken, error) {
httpClient := httpclient.New(
httpclient.SetHostURL(address),
)
url := "/oauth/token"
queryParams := make(map[string]string)
queryParams["grant_type"] = "refresh_token"
queryParams["refresh_token"] = refreshToken
queryParams["client_id"] = clientID
queryParams["client_secret"] = clientSecret
var accessToken *AccessToken
_, err := httpClient.Post(url, httpclient.SetQueryParams(queryParams), httpclient.SetResult(&accessToken))
if err != nil {
return nil, err
}
return accessToken, nil
}