-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverdrive_auth.go
More file actions
71 lines (58 loc) · 1.99 KB
/
overdrive_auth.go
File metadata and controls
71 lines (58 loc) · 1.99 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
package main
import (
"log"
"net/http"
"github.com/clockworkcoding/goverdrive"
_ "github.com/lib/pq"
)
// auth receives the callback from Overdrive, validates and displays the user information
func overdriveAuthCallback(w http.ResponseWriter, r *http.Request) {
code := r.FormValue("code")
userid := r.FormValue("state")
if userid == "" || code == "" {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
auth, err := getOverdriveAuth(overdriveAuth{slackUserID: userid})
if err != nil {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
accessToken, err := goverdrive.GetToken(config.Overdrive.Key, config.Overdrive.Secret, auth.overdriveAccountID, code, config.URL+"/odauth")
if err != nil {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
auth.token = accessToken.AccessToken
auth.refreshToken = accessToken.RefreshToken
auth.tokenType = accessToken.TokenType
auth.expiry = accessToken.Expiry
err = saveOverdriveAuth(auth)
if err != nil {
log.Println(err.Error())
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
http.Redirect(w, r, config.RedirectURL+"/OverdriveSuccess", http.StatusTemporaryRedirect)
}
// addToOverdrive initializes the oauth process and redirects to Overdrive
func addToOverdrive(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
}
url := goverdrive.BuildAuthURL(config.Overdrive.Key, "4425", userID, config.URL+"/odauth")
auth := overdriveAuth{
slackUserID: userID,
teamID: teamID,
overdriveAccountID: "4425",
}
err := saveOverdriveAuth(auth)
if err != nil {
http.Redirect(w, r, config.RedirectURL+"/Error", http.StatusTemporaryRedirect)
return
}
http.Redirect(w, r, url, http.StatusFound)
}