summaryrefslogtreecommitdiff
path: root/src/oauth/client.go
blob: fa5d04bcf57b794bf5be57d5e8a1cac0c0e89451 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
// The oauth package provides a Client that authenticates requests
// automatically with OAuth1.
//
// [Cardmarket OAuth] and [RFC 5849] were used as reference.
//
// [Cardmarket OAuth]: https://apiv2.cardmarket.com/ws/documentation/API:Auth_OAuthHeader
// [RFC 5849]: https://datatracker.ietf.org/doc/html/rfc5849#section-3.1
package oauth

import (
	"context"
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"strings"
)

const (
	headerAuthorization = "Authorization"
	headerContentType   = "Content-Type"
)

type ConsumerToken struct {
	Token  string
	Secret string
}

type AccessToken struct {
	Token  string
	Secret string
}

type Tokens struct {
	Consumer ConsumerToken
	Access   AccessToken
}

type Client struct {
	tokens Tokens
}

func NewClient(t Tokens) *Client {
	return &Client{tokens: t}
}

func (c *Client) GetWithContext(
	ctx context.Context,
	url string,
) (*http.Response, error) {
	return c.request(ctx, http.MethodGet, url, "", nil)
}

func (c *Client) PutWithContext(
	ctx context.Context,
	url, contentType string,
	body io.Reader,
) (*http.Response, error) {
	return c.request(ctx, http.MethodPut, url, contentType, body)
}

func (c *Client) PostWithContext(
	ctx context.Context,
	url, contentType string,
	body io.Reader,
) (*http.Response, error) {
	return c.request(ctx, http.MethodPost, url, contentType, body)
}

func (c *Client) DeleteWithContext(
	ctx context.Context,
	url string,
) (*http.Response, error) {
	return c.request(ctx, http.MethodDelete, url, "", nil)
}

// request hydrates and fulfils a http.Request using information stored in the client
func (c *Client) request(
	ctx context.Context,
	method string,
	url, contentType string,
	body io.Reader,
) (*http.Response, error) {
	req, err := http.NewRequestWithContext(ctx, method, url, body)
	if err != nil {
		return nil, err
	}

	if contentType != "" {
		req.Header.Set(headerContentType, contentType)
	}

	if err = c.authorize(req); err != nil {
		return nil, err
	}

	slog.DebugContext(
		ctx,
		"sending request",
		"method", req.Method,
		"url", req.URL.String(),
		"contentType", req.Header.Get(headerContentType),
		"authorization", strings.Join(req.Header.Values(headerAuthorization), ", "),
	)

	return http.DefaultClient.Do(req)
}

func (c *Client) authorize(req *http.Request) error {
	baseURL := fmt.Sprintf(
		"%s://%s%s",
		strings.ToLower(req.URL.Scheme),
		strings.ToLower(req.URL.Host),
		req.URL.Path,
	)

	ah := newAuthHeader(
		req.Method,
		baseURL,
		nil,
		c.tokens,
	)

	req.Header.Set(headerAuthorization, ah.String())

	return nil
}