-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
109 lines (83 loc) · 2.14 KB
/
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
105
106
107
108
109
package pocketcasts
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
const endpointSignin = "https://play.pocketcasts.com/users/sign_in"
type Connection struct{}
// NewConnection creates a new connection to the PocketCasts API.
func NewConnection() *Connection {
return &Connection{}
}
type AuthedConnection struct {
Client *http.Client
*Connection
}
var ErrorInvalidUsernameOrPassword = errors.New("invalid username or password")
// Authenticate authenticates a user with the PocketCasts API
// and returns an AuthedConnection.
func (con *Connection) Authenticate(email, password string) (*AuthedConnection, error) {
type authRequest struct {
Email string `json:"email"`
Password string `json:"password"`
Scope string `json:"scope"`
}
type authSuccess struct {
Token string `json:"token"`
UUID string `json:"uuid"`
}
aReq := authRequest{
Email: email,
Password: password,
Scope: "webplayer",
}
reqJSON, err := json.Marshal(aReq)
if err != nil {
return nil, err
}
body := bytes.NewBuffer(reqJSON)
client := &http.Client{}
req, err := http.NewRequest("POST", "https://api.pocketcasts.com/user/login", body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusUnauthorized {
return nil, ErrorInvalidUsernameOrPassword
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request error: %s", resp.Status)
}
dec := json.NewDecoder(resp.Body)
respSuccess := authSuccess{}
err = dec.Decode(&respSuccess)
if err != nil {
return nil, err
}
client.Transport = tokenTransport{
Token: respSuccess.Token,
}
return &AuthedConnection{
Client: client,
Connection: con,
}, nil
}
type tokenTransport struct {
Token string
}
func (t tokenTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Add("Authorization", "Bearer "+t.Token)
if r.Header.Get("Content-Type") == "" {
r.Header.Add("Content-Type", "application/json")
}
resp, err := http.DefaultTransport.RoundTrip(r)
return resp, err
}