-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_auth.go
104 lines (98 loc) · 2.99 KB
/
client_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
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
package monta
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Token holds authentication tokens for the Monta Partner API.
type Token struct {
// AccessToken for accessing the Monta Partner API.
AccessToken string `json:"accessToken"`
// RefreshToken for refreshing an access token.
RefreshToken string `json:"refreshToken"`
// AccessTokenExpirationDate is the expiration time of the access token.
AccessTokenExpirationTime time.Time `json:"accessTokenExpirationDate"`
// RefreshTokenExpirationDate is the expiration date of the access token.
RefreshTokenExpirationTime time.Time `json:"refreshTokenExpirationDate"`
}
// CreateTokenRequest is the request input to the [Client.CreateToken] method.
type CreateTokenRequest struct {
// The ClientID to use.
ClientID string `json:"clientId"`
// The ClientSecret to use.
ClientSecret string `json:"clientSecret"`
}
// RefreshTokenRequest is the request input to the [Client.RefreshToken] method.
type RefreshTokenRequest struct {
// The refresh token.
RefreshToken string `json:"refreshToken"`
}
// CreateToken creates an authentication token.
func (c *clientImpl) CreateToken(ctx context.Context, request *CreateTokenRequest) (_ *Token, err error) {
const method, path = http.MethodPost, "/v1/auth/token"
defer func() {
if err != nil {
err = fmt.Errorf("%s %s: %w", method, path, err)
}
}()
var requestBody bytes.Buffer
if err := json.NewEncoder(&requestBody).Encode(request); err != nil {
return nil, err
}
httpRequest, err := http.NewRequestWithContext(ctx, method, apiHost+path, &requestBody)
if err != nil {
return nil, err
}
httpRequest.Header.Set("content-type", "application/json")
httpResponse, err := c.httpClient.Do(httpRequest)
if err != nil {
return nil, err
}
defer func() {
_ = httpResponse.Body.Close()
}()
if httpResponse.StatusCode != http.StatusOK {
return nil, newStatusError(httpResponse)
}
var token Token
if err := json.NewDecoder(httpResponse.Body).Decode(&token); err != nil {
return nil, err
}
return &token, nil
}
// RefreshToken creates an authentication token.
func (c *clientImpl) RefreshToken(ctx context.Context, request *RefreshTokenRequest) (_ *Token, err error) {
const method, path = http.MethodPost, "/v1/auth/refresh"
defer func() {
if err != nil {
err = fmt.Errorf("%s %s: %w", method, path, err)
}
}()
var requestBody bytes.Buffer
if err := json.NewEncoder(&requestBody).Encode(request); err != nil {
return nil, err
}
httpRequest, err := http.NewRequestWithContext(ctx, method, apiHost+path, &requestBody)
if err != nil {
return nil, err
}
httpRequest.Header.Set("content-type", "application/json")
httpResponse, err := c.httpClient.Do(httpRequest)
if err != nil {
return nil, err
}
defer func() {
_ = httpResponse.Body.Close()
}()
if httpResponse.StatusCode != http.StatusOK {
return nil, newStatusError(httpResponse)
}
var token Token
if err := json.NewDecoder(httpResponse.Body).Decode(&token); err != nil {
return nil, err
}
return &token, nil
}