GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#5)
by
unknown
01:19
created

credentials.*RsaKeyPairCredential.GetBearerToken   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
nop 0
1
package credentials
2
3
import (
4
	"encoding/json"
5
	"errors"
6
	"fmt"
7
	"strconv"
8
	"time"
9
10
	"github.com/aliyun/credentials-go/credentials/request"
11
	"github.com/aliyun/credentials-go/credentials/utils"
12
)
13
14
type RsaKeyPairCredential struct {
0 ignored issues
show
introduced by
exported type RsaKeyPairCredential should have comment or be unexported
Loading history...
15
	*credentialUpdater
16
	PrivateKey        string
17
	PublicKeyID       string
18
	SessionExpiration int
19
	sessionCredential *sessionCredential
20
	runtime           *utils.Runtime
21
}
22
23
type RsaKeyPairResponse struct {
0 ignored issues
show
introduced by
exported type RsaKeyPairResponse should have comment or be unexported
Loading history...
24
	SessionAccessKey *SessionAccessKey `json:"SessionAccessKey" xml:"SessionAccessKey"`
25
}
26
27
type SessionAccessKey struct {
0 ignored issues
show
introduced by
exported type SessionAccessKey should have comment or be unexported
Loading history...
28
	SessionAccessKeyID     string `json:"SessionAccessKeyID" xml:"SessionAccessKeyID"`
29
	SessionAccessKeySecret string `json:"SessionAccessKeySecret" xml:"SessionAccessKeySecret"`
30
	Expiration             string `json:"Expiration" xml:"Expiration"`
31
}
32
33
func newRsaKeyPairCredential(privateKey, publicKeyID string, sessionExpiration int, runtime *utils.Runtime) *RsaKeyPairCredential {
34
	return &RsaKeyPairCredential{
35
		PrivateKey:        privateKey,
36
		PublicKeyID:       publicKeyID,
37
		SessionExpiration: sessionExpiration,
38
		credentialUpdater: new(credentialUpdater),
39
		runtime:           runtime,
40
	}
41
}
42
43
func (r *RsaKeyPairCredential) GetAccessKeyID() (string, error) {
0 ignored issues
show
introduced by
exported method RsaKeyPairCredential.GetAccessKeyID should have comment or be unexported
Loading history...
44
	if r.sessionCredential == nil || r.needUpdateCredential() {
45
		err := r.updateCredential()
46
		if err != nil {
47
			return "", err
48
		}
49
	}
50
	return r.sessionCredential.AccessKeyID, nil
51
}
52
53
func (r *RsaKeyPairCredential) GetAccessSecret() (string, error) {
0 ignored issues
show
introduced by
exported method RsaKeyPairCredential.GetAccessSecret should have comment or be unexported
Loading history...
54
	if r.sessionCredential == nil || r.needUpdateCredential() {
55
		err := r.updateCredential()
56
		if err != nil {
57
			return "", err
58
		}
59
	}
60
	return r.sessionCredential.AccessKeySecret, nil
61
}
62
63
func (r *RsaKeyPairCredential) GetSecurityToken() (string, error) {
0 ignored issues
show
introduced by
exported method RsaKeyPairCredential.GetSecurityToken should have comment or be unexported
Loading history...
64
	return "", nil
65
}
66
67
func (r *RsaKeyPairCredential) GetBearerToken() string {
0 ignored issues
show
introduced by
exported method RsaKeyPairCredential.GetBearerToken should have comment or be unexported
Loading history...
68
	return ""
69
}
70
71
func (r *RsaKeyPairCredential) GetType() string {
0 ignored issues
show
introduced by
exported method RsaKeyPairCredential.GetType should have comment or be unexported
Loading history...
72
	return "rsa_key_pair"
73
}
74
75
func (r *RsaKeyPairCredential) updateCredential() (err error) {
76
	if r.runtime == nil {
77
		r.runtime = new(utils.Runtime)
78
	}
79
	request := request.NewCommonRequest()
80
	request.Domain = "sts.aliyuncs.com"
81
	if r.runtime.Host != "" {
82
		request.Domain = r.runtime.Host
83
	}
84
	request.Scheme = "HTTPS"
85
	request.Method = "GET"
86
	request.QueryParams["AccessKeyId"] = r.PublicKeyID
87
	request.QueryParams["Action"] = "GenerateSessionAccessKey"
88
	request.QueryParams["Format"] = "JSON"
89
	if r.SessionExpiration > 0 {
90
		if r.SessionExpiration >= 900 && r.SessionExpiration <= 3600 {
91
			request.QueryParams["DurationSeconds"] = strconv.Itoa(r.SessionExpiration)
92
		} else {
93
			err = errors.New("[InvalidParam]:Key Pair session duration should be in the range of 15min - 1Hr")
94
			return
95
		}
96
	} else {
97
		request.QueryParams["DurationSeconds"] = strconv.Itoa(defaultDurationSeconds)
98
	}
99
	request.QueryParams["SignatureMethod"] = "SHA256withRSA"
100
	request.QueryParams["SignatureType"] = "PRIVATEKEY"
101
	request.QueryParams["SignatureVersion"] = "1.0"
102
	request.QueryParams["Version"] = "2015-04-01"
103
	request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601()
104
	request.QueryParams["SignatureNonce"] = utils.GetUUID()
105
	signature := utils.Sha256WithRsa(request.BuildStringToSign(), r.PrivateKey)
106
	request.QueryParams["Signature"] = signature
107
	request.Headers["Host"] = request.Domain
108
	request.Headers["Accept-Encoding"] = "identity"
109
	request.Url = request.BuildUrl()
110
	content, err := doAction(request, r.runtime)
111
	if err != nil {
112
		return fmt.Errorf("refresh KeyPair err: %s", err.Error())
113
	}
114
	var resp *RsaKeyPairResponse
115
	err = json.Unmarshal(content, &resp)
116
	if err != nil {
117
		return fmt.Errorf("refresh KeyPair err: Json Unmarshal fail: %s", err.Error())
118
	}
119
	if resp == nil || resp.SessionAccessKey == nil {
120
		return fmt.Errorf("refresh KeyPair err: SessionAccessKey is empty")
121
	}
122
	sessionAccessKey := resp.SessionAccessKey
123
	if sessionAccessKey.SessionAccessKeyID == "" || sessionAccessKey.SessionAccessKeySecret == "" || sessionAccessKey.Expiration == "" {
124
		return fmt.Errorf("refresh KeyPair err: SessionAccessKeyID: %v, SessionAccessKeySecret: %v, Expiration: %v", sessionAccessKey.SessionAccessKeyID, sessionAccessKey.SessionAccessKeySecret, sessionAccessKey.Expiration)
125
	}
126
127
	expirationTime, err := time.Parse("2006-01-02T15:04:05Z", sessionAccessKey.Expiration)
128
	r.lastUpdateTimestamp = time.Now().Unix()
129
	r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
130
	r.sessionCredential = &sessionCredential{
131
		AccessKeyID:     sessionAccessKey.SessionAccessKeyID,
132
		AccessKeySecret: sessionAccessKey.SessionAccessKeySecret,
133
	}
134
135
	return
136
}
137