-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tokens.go
116 lines (93 loc) · 2.75 KB
/
tokens.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
package desec
import (
"context"
"fmt"
"net/http"
"time"
)
// Token a token representation.
type Token struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Value string `json:"token,omitempty"`
Created *time.Time `json:"created,omitempty"`
}
// TokensService handles communication with the tokens related methods of the deSEC API.
//
// https://desec.readthedocs.io/en/latest/auth/tokens.html
type TokensService struct {
client *Client
}
// GetAll retrieving all current tokens.
// https://desec.readthedocs.io/en/latest/auth/tokens.html#retrieving-all-current-tokens
func (s *TokensService) GetAll(ctx context.Context) ([]Token, error) {
endpoint, err := s.client.createEndpoint("auth", "tokens")
if err != nil {
return nil, fmt.Errorf("failed to create endpoint: %w", err)
}
req, err := s.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
resp, err := s.client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call API: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, handleError(resp)
}
var tokens []Token
err = handleResponse(resp, &tokens)
if err != nil {
return nil, err
}
return tokens, nil
}
// Create creates additional tokens.
// https://desec.readthedocs.io/en/latest/auth/tokens.html#create-additional-tokens
func (s *TokensService) Create(ctx context.Context, name string) (*Token, error) {
endpoint, err := s.client.createEndpoint("auth", "tokens")
if err != nil {
return nil, fmt.Errorf("failed to create endpoint: %w", err)
}
req, err := s.client.newRequest(ctx, http.MethodPost, endpoint, Token{Name: name})
if err != nil {
return nil, err
}
resp, err := s.client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call API: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusCreated {
return nil, handleError(resp)
}
var token Token
err = handleResponse(resp, &token)
if err != nil {
return nil, err
}
return &token, nil
}
// Delete deletes tokens.
// https://desec.readthedocs.io/en/latest/auth/tokens.html#delete-tokens
func (s *TokensService) Delete(ctx context.Context, tokenID string) error {
endpoint, err := s.client.createEndpoint("auth", "tokens", tokenID)
if err != nil {
return fmt.Errorf("failed to create endpoint: %w", err)
}
req, err := s.client.newRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return err
}
resp, err := s.client.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to call API: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusNoContent {
return handleError(resp)
}
return nil
}