-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathoauth.go
60 lines (56 loc) · 1.18 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
package main
import (
"go.uber.org/zap"
"golang.org/x/oauth2"
"log"
"net/http"
"os"
)
var (
clientID = os.Getenv("SLACK_ID")
clientSecret = os.Getenv("SLACK_SECRET")
conf = oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Scopes: []string{},
Endpoint: oauth2.Endpoint{
AuthURL: "https://slack.com/oauth/v2/authorize",
TokenURL: "https://slack.com/api/oauth.v2.access",
},
}
)
const (
Cookie = "c"
Okay = "Okay"
Error = "Error"
)
func Oauth(w http.ResponseWriter, r *http.Request) {
logger, err := zap.NewProduction()
if err != nil {
log.Printf("can't initialize zap logger: %v", err)
}
code := r.FormValue("code")
if len(code) == 0 {
http.Redirect(w, r, "/", 303)
return
}
c := r.Context()
tok, err := conf.Exchange(c, code)
if err != nil || !tok.Valid() {
logger.Error("oauth error", zap.Any("token", tok), zap.Error(err))
http.SetCookie(w, &http.Cookie{
Name: Cookie,
Path: "/",
Value: Error,
})
http.Redirect(w, r, "/", 303)
return
}
logger.Info("oauth token", zap.Any("token", tok))
http.SetCookie(w, &http.Cookie{
Name: Cookie,
Path: "/",
Value: Okay,
})
http.Redirect(w, r, "/", 303)
}