-
Notifications
You must be signed in to change notification settings - Fork 446
/
apitoken.go
91 lines (81 loc) · 2.69 KB
/
apitoken.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
package gojenkins
import (
"context"
"fmt"
"net/http"
"strings"
)
const (
apiTokenBaseContext = "/me/descriptorByName/jenkins.security.ApiTokenProperty"
generateAPITokenURL = apiTokenBaseContext + "/generateNewToken"
revokeAPITokenURL = apiTokenBaseContext + "/revoke"
revokeAllAPITokensURL = apiTokenBaseContext + "/revokeAll"
)
// APIToken is a Jenkins API token to be created for the user instantiated with the Jenkins client
type APIToken struct {
Jenkins *Jenkins
Name string `json:"tokenName"`
UUID string `json:"tokenUuid"`
Value string `json:"tokenValue"`
}
// APITokenGenerateResponse is the response given by Jenkins when an API token is created
type APITokenGenerateResponse struct {
Status string `json:"status"`
Data APIToken `json:"data"`
}
// ErrAPIToken occurs when there is error creating or revoking API tokens
type ErrAPIToken struct {
Message string
}
func (e *ErrAPIToken) Error() string {
return e.Message
}
// GenerateAPIToken creates a new API token for the Jenkins client user
func (j *Jenkins) GenerateAPIToken(ctx context.Context, tokenName string) (APIToken, error) {
payload := "newTokenName=" + tokenName
apiTokenResponse := &APITokenGenerateResponse{}
response, err := j.Requester.Post(ctx, generateAPITokenURL, strings.NewReader(payload), apiTokenResponse, nil)
if err != nil {
return apiTokenResponse.Data, err
}
if response.StatusCode != http.StatusOK {
return apiTokenResponse.Data, &ErrAPIToken{
Message: fmt.Sprintf("error creating API token. Status is %d", response.StatusCode),
}
}
apiToken := apiTokenResponse.Data
// Set Jenkins client pointer to be able to revoke token later
apiToken.Jenkins = j
return apiToken, nil
}
// RevokeAPIToken revokes an API token
func (j *Jenkins) RevokeAPIToken(ctx context.Context, tokenUuid string) error {
payload := "tokenUuid=" + tokenUuid
response, err := j.Requester.Post(ctx, revokeAPITokenURL, strings.NewReader(payload), nil, nil)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return &ErrAPIToken{
Message: fmt.Sprintf("error revoking API token. Status is %d", response.StatusCode),
}
}
return nil
}
// RevokeAllAPITokens revokes all API tokens for the Jenkins client user
func (j *Jenkins) RevokeAllAPITokens(ctx context.Context) error {
response, err := j.Requester.Post(ctx, revokeAllAPITokensURL, nil, nil, nil)
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return &ErrAPIToken{
Message: fmt.Sprintf("error revoking all API tokens. Status is %d", response.StatusCode),
}
}
return nil
}
// Revoke revokes an API token
func (a *APIToken) Revoke() error {
return a.Jenkins.RevokeAPIToken(context.Background(), a.UUID)
}