-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_auth.go
59 lines (47 loc) · 1.38 KB
/
api_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
package oblio
import (
"context"
"fmt"
"net/http"
"github.com/vcraescu/go-oblio-api/types"
"github.com/vcraescu/go-reqbuilder"
)
type generateTokenRequest struct {
ClientID string `json:"client_id,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
}
func (r *generateTokenRequest) Validate() error {
if r.ClientID == "" {
return fmt.Errorf("clientID is empty: %w", ErrInvalidArgument)
}
if r.ClientSecret == "" {
return fmt.Errorf("clientSecret is empty: %w", ErrInvalidArgument)
}
return nil
}
type GenerateTokenResponse struct {
AccessToken string `json:"access_token,omitempty"`
ExpiresIn types.Int `json:"expires_in,omitempty"`
TokenType string `json:"token_type,omitempty"`
Scope string `json:"scope,omitempty"`
RequestTime types.Timestamp `json:"request_time,omitempty"`
}
func (c *Client) GenerateToken(ctx context.Context) (*GenerateTokenResponse, error) {
req := generateTokenRequest{
ClientID: c.clientID,
ClientSecret: c.clientSecret,
}
if err := req.Validate(); err != nil {
return nil, err
}
builder := c.requestBuilder.
WithMethod(http.MethodPost).
WithPath("/authorize/token").
WithHeaders(reqbuilder.JSONContentHeader).
WithBody(req)
resp := &GenerateTokenResponse{}
if err := c.do(ctx, builder, resp); err != nil {
return resp, fmt.Errorf("do: %w", err)
}
return resp, nil
}