forked from refiito/pipes-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorization.go
208 lines (195 loc) · 4.75 KB
/
authorization.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"code.google.com/p/goauth2/oauth"
"database/sql"
"encoding/json"
"errors"
"github.com/tambet/oauthplain"
)
type Authorization struct {
WorkspaceID int
ServiceID string
WorkspaceToken string
Data []byte
}
const (
selectAuthorizationSQL = `SELECT
workspace_id, service, workspace_token, data
FROM authorizations
WHERE workspace_id = $1
AND service = $2
LIMIT 1
`
insertAuthorizationSQL = `WITH existing_auth AS (
UPDATE authorizations SET data = $4, workspace_token = $3
WHERE workspace_id = $1 AND service = $2
RETURNING service
),
inserted_auth AS (
INSERT INTO
authorizations(workspace_id, service, workspace_token, data)
SELECT $1, $2, $3, $4
WHERE NOT EXISTS (SELECT 1 FROM existing_auth)
RETURNING service
)
SELECT * FROM inserted_auth
UNION
SELECT * FROM existing_auth
`
deleteAuthorizationSQL = `DELETE FROM authorizations
WHERE workspace_id = $1
AND service = $2
`
)
func NewAuthorization(workspaceID int, serviceID string) *Authorization {
return &Authorization{
WorkspaceID: workspaceID,
ServiceID: serviceID,
}
}
func loadAuth(s Service) (*Authorization, error) {
rows, err := db.Query(selectAuthorizationSQL, s.WorkspaceID(), s.Name())
if err != nil {
return nil, err
}
defer rows.Close()
if !rows.Next() {
return nil, rows.Err()
}
var authorization Authorization
if err := authorization.load(rows); err != nil {
return nil, err
}
if err := s.setAuthData(authorization.Data); err != nil {
return nil, err
}
return &authorization, nil
}
func (a *Authorization) refresh() error {
if availableAuthorizations[a.ServiceID] != "oauth2" {
return nil
}
var token oauth.Token
if err := json.Unmarshal(a.Data, &token); err != nil {
return err
}
if !token.Expired() {
return nil
}
config, res := oAuth2Configs[a.ServiceID+"_"+environment]
if !res {
return errors.New("service OAuth config not found")
}
transport := &oauth.Transport{Config: config, Token: &token}
if err := transport.Refresh(); err != nil {
return err
}
b, err := json.Marshal(token)
if err != nil {
return err
}
a.Data = b
if err := a.save(); err != nil {
return err
}
return nil
}
func (a *Authorization) save() error {
_, err := db.Exec(insertAuthorizationSQL,
a.WorkspaceID, a.ServiceID, a.WorkspaceToken, a.Data)
if err != nil {
return err
}
return nil
}
func (a *Authorization) load(rows *sql.Rows) error {
err := rows.Scan(&a.WorkspaceID, &a.ServiceID, &a.WorkspaceToken, &a.Data)
if err != nil {
return err
}
return nil
}
func (a *Authorization) destroy(s Service) error {
_, err := db.Exec(deleteAuthorizationSQL, s.WorkspaceID(), s.Name())
return err
}
func loadAuthorizations(workspaceID int) (map[string]bool, error) {
authorizations := make(map[string]bool)
rows, err := db.Query(`
SELECT service FROM authorizations
WHERE workspace_id = $1`, workspaceID)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var service string
if err := rows.Scan(&service); err != nil {
return nil, err
}
authorizations[service] = true
}
return authorizations, nil
}
func oAuth2URL(service string) string {
config, ok := oAuth2Configs[service+"_"+environment]
if !ok {
return ""
}
return config.AuthCodeURL("__STATE__") + "&type=web_server"
}
func oAuth1Exchange(serviceID string, payload map[string]interface{}) ([]byte, error) {
accountName := payload["account_name"].(string)
if accountName == "" {
return nil, errors.New("missing account_name")
}
oAuthToken := payload["oauth_token"].(string)
if oAuthToken == "" {
return nil, errors.New("missing oauth_token")
}
oAuthVerifier := payload["oauth_verifier"].(string)
if oAuthVerifier == "" {
return nil, errors.New("missing oauth_verifier")
}
config, res := oAuth1Configs[serviceID]
if !res {
return nil, errors.New("service OAuth config not found")
}
transport := &oauthplain.Transport{
Config: config.UpdateURLs(accountName),
}
token := &oauthplain.Token{
OAuthToken: oAuthToken,
OAuthVerifier: oAuthVerifier,
}
if err := transport.Exchange(token); err != nil {
return nil, err
}
token.Extra = make(map[string]string)
token.Extra["account_name"] = accountName
b, err := json.Marshal(token)
if err != nil {
return nil, err
}
return b, nil
}
func oAuth2Exchange(serviceID string, payload map[string]interface{}) ([]byte, error) {
code := payload["code"].(string)
if code == "" {
return nil, errors.New("missing code")
}
config, res := oAuth2Configs[serviceID+"_"+environment]
if !res {
return nil, errors.New("service OAuth config not found")
}
transport := &oauth.Transport{Config: config}
token, err := transport.Exchange(code)
if err != nil {
return nil, err
}
b, err := json.Marshal(token)
if err != nil {
return nil, err
}
return b, nil
}