-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoodreads_auth.go
More file actions
79 lines (68 loc) · 2.41 KB
/
goodreads_auth.go
File metadata and controls
79 lines (68 loc) · 2.41 KB
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
package main
import (
"net/http"
"github.com/clockworkcoding/goodreads"
"github.com/mrjones/oauth"
_ "github.com/lib/pq"
)
// auth receives the callback from Goodreads, validates and displays the user information
func goodreadsAuthCallback(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("oauth_token")
authorize := r.FormValue("authorize")
if authorize != "1" || token == "" {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
auth, err := getGoodreadsAuth(goodreadsAuth{token: token})
if err != nil {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
c := goodreads.NewClient(config.Goodreads.Key, config.Goodreads.Secret)
accessToken, err := c.Consumer.AuthorizeToken(&oauth.RequestToken{Secret: auth.secret, Token: auth.token}, token)
if err != nil {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
auth.token = accessToken.Token
auth.secret = accessToken.Secret
c = goodreads.NewClientWithToken(config.Goodreads.Key, config.Goodreads.Secret, auth.token, auth.secret)
grUser, err := c.QueryUser()
if err != nil {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
auth.goodreadsUserID = grUser.Attr_id
if err = saveGoodreadsAuth(auth); err != nil {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
http.Redirect(w, r, config.RedirectURL+"/GoodreadsSuccess", http.StatusTemporaryRedirect)
}
// addToGoodreads initializes the oauth process and redirects to Goodreads
func addToGoodreads(w http.ResponseWriter, r *http.Request) {
teamID := r.FormValue("team")
userID := r.FormValue("user")
if teamID == "" || userID == "" {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
c := goodreads.NewClient(config.Goodreads.Key, config.Goodreads.Secret)
rtoken, url, err := c.Consumer.GetRequestTokenAndUrl(config.URL + "/grauth")
if err != nil {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
auth := goodreadsAuth{
secret: rtoken.Secret,
token: rtoken.Token,
slackUserID: userID,
teamID: teamID,
}
err = saveGoodreadsAuth(auth)
if err != nil {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
http.Redirect(w, r, url, http.StatusFound)
}