-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoauth.go
113 lines (103 loc) · 2.6 KB
/
oauth.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
110
111
112
113
package oauthio
import (
"encoding/json"
"errors"
"github.com/nu7hatch/gouuid"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"
)
const (
OAuthdURL = "https://oauth.io"
Version = "0.0.1"
)
type OAuth struct {
OAuthdURL string
appKey string
appSecret string
Version string
Client *http.Client
}
func timeoutHandler(network, address string) (net.Conn, error) {
return net.DialTimeout(network, address, time.Duration(5*time.Second))
}
func New(appkey, appsecret string) *OAuth {
transport := http.Transport{
Dial: timeoutHandler,
}
return &OAuth{
OAuthdURL: OAuthdURL,
appKey: appkey,
appSecret: appsecret,
Version: Version,
Client: &http.Client{
Transport: &transport,
},
}
}
func (o *OAuth) GetVersion() string {
return o.Version
}
func (o *OAuth) SetOAuthdURL(url string) {
o.OAuthdURL = url
}
func (o *OAuth) GenerateStateToken() (string, error) {
id, err := uuid.NewV4()
if err != nil {
return "", err
}
return id.String(), nil
}
func (o *OAuth) Auth(code string) (*OAuthRequestObject, error) {
data := url.Values{}
data.Set("code", code)
data.Set("key", o.appKey)
data.Set("secret", o.appSecret)
response, err := http.PostForm(o.OAuthdURL+"/auth/access_token", data)
if err != nil {
return nil, errors.New("oauth.go: Couldn't communicate with Oauthd")
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, errors.New("oauth.go: Couldn't read Oauthd response")
}
oauthResp := &OAuthRequestObject{}
err = json.Unmarshal(body, oauthResp)
if err != nil {
return nil, errors.New("oauth.go: Couldn't parse response")
}
if oauthResp.State == "" {
return nil, errors.New("oauth.go: State is missing in response")
}
oauthResp.ExpireDate = time.Now().Unix() + oauthResp.ExpiresIn
oauthResp.OAuthdURL = o.OAuthdURL
oauthResp.Client = o.Client
oauthResp.appKey = o.appKey
return oauthResp, nil
}
func (o *OAuth) RefreshCredentials(creds *OAuthRequestObject, force bool) error {
if force || time.Now().Unix() > creds.ExpireDate {
data := url.Values{}
data.Set("token", creds.RefreshToken)
data.Set("key", o.appKey)
data.Set("secret", o.appSecret)
response, err := http.PostForm(o.OAuthdURL+"/auth/refresh_token/", data)
if err != nil {
return errors.New("oauth.go: Couldn't communicate with Oauthd")
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return errors.New("oauth.go: Couldn't read Oauthd response")
}
err = json.Unmarshal(body, &creds)
if err != nil {
return errors.New("oauth.go: Couldn't parse response")
}
creds.Refreshed = true
}
return nil
}