aboutsummaryrefslogtreecommitdiffhomepage
path: root/integration/pocket/connector.go
blob: 4cb0931f816b75d69e15bee5f3c41e364b6ab36f (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
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package pocket // import "miniflux.app/integration/pocket"

import (
	"errors"
	"fmt"
	"io/ioutil"
	"net/url"

	"miniflux.app/http/client"
)

// Connector manages the authorization flow with Pocket to get a personal access token.
type Connector struct {
	consumerKey string
}

// RequestToken fetches a new request token from Pocket API.
func (c *Connector) RequestToken(redirectURL string) (string, error) {
	type req struct {
		ConsumerKey string `json:"consumer_key"`
		RedirectURI string `json:"redirect_uri"`
	}

	clt := client.New("https://getpocket.com/v3/oauth/request")
	response, err := clt.PostJSON(&req{ConsumerKey: c.consumerKey, RedirectURI: redirectURL})
	if err != nil {
		return "", fmt.Errorf("pocket: unable to fetch request token: %v", err)
	}

	if response.HasServerFailure() {
		return "", fmt.Errorf("pocket: unable to fetch request token, status=%d", response.StatusCode)
	}

	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return "", fmt.Errorf("pocket: unable to read response body: %v", err)
	}

	values, err := url.ParseQuery(string(body))
	if err != nil {
		return "", fmt.Errorf("pocket: unable to parse response: %v", err)
	}

	code := values.Get("code")
	if code == "" {
		return "", errors.New("pocket: code is empty")
	}

	return code, nil
}

// AccessToken fetches a new access token once the end-user authorized the application.
func (c *Connector) AccessToken(requestToken string) (string, error) {
	type req struct {
		ConsumerKey string `json:"consumer_key"`
		Code        string `json:"code"`
	}

	clt := client.New("https://getpocket.com/v3/oauth/authorize")
	response, err := clt.PostJSON(&req{ConsumerKey: c.consumerKey, Code: requestToken})
	if err != nil {
		return "", fmt.Errorf("pocket: unable to fetch access token: %v", err)
	}

	if response.HasServerFailure() {
		return "", fmt.Errorf("pocket: unable to fetch access token, status=%d", response.StatusCode)
	}

	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return "", fmt.Errorf("pocket: unable to read response body: %v", err)
	}

	values, err := url.ParseQuery(string(body))
	if err != nil {
		return "", fmt.Errorf("pocket: unable to parse response: %v", err)
	}

	token := values.Get("access_token")
	if token == "" {
		return "", errors.New("pocket: access_token is empty")
	}

	return token, nil
}

// AuthorizationURL returns the authorization URL for the end-user.
func (c *Connector) AuthorizationURL(requestToken, redirectURL string) string {
	return fmt.Sprintf(
		"https://getpocket.com/auth/authorize?request_token=%s&redirect_uri=%s",
		requestToken,
		redirectURL,
	)
}

// NewConnector returns a new Pocket Connector.
func NewConnector(consumerKey string) *Connector {
	return &Connector{consumerKey}
}