-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
139 lines (129 loc) · 3.73 KB
/
main.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
package main
/* Slack Oauth app built according to
* https://api.slack.com/authentication/oauth-v2
*/
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
log "github.com/sirupsen/logrus"
)
var (
// irc-slack app client ID, see https://api.slack.com/apps/
clientID = os.Getenv("SLACK_APP_CLIENT_ID")
clientSecret = os.Getenv("SLACK_APP_CLIENT_SECRET")
)
func httpStatus(w http.ResponseWriter, r *http.Request, statusCode int, fmtstr string, args ...interface{}) {
w.WriteHeader(statusCode)
msg := fmt.Sprintf(fmtstr, args...)
fullmsg := fmt.Sprintf("%d - %s\n%s", statusCode, http.StatusText(statusCode), msg)
if _, err := w.Write([]byte(fullmsg)); err != nil {
log.Warningf("Cannot write response: %v", err)
}
}
type slackChallenge struct {
Token, Challenge, Type string
}
func handleSlackChallenge(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Infof("Cannot read body: %v", err)
httpStatus(w, r, 500, "")
return
}
var sc slackChallenge
if err := json.Unmarshal(data, &sc); err != nil {
log.Infof("Cannot unmarshal JSON: %v", err)
httpStatus(w, r, 400, "")
return
}
if _, err := w.Write([]byte(sc.Challenge)); err != nil {
log.Warningf("Failed to write response: %v", err)
}
}
func handleSlackAuth(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query()["code"]
if len(code) < 1 {
log.Info("Missing \"code\" parameter in request")
httpStatus(w, r, 400, "")
return
}
// get token from Slack
form := url.Values{}
form.Add("code", code[0])
form.Add("client_id", clientID)
form.Add("client_secret", clientSecret)
accessURL := "https://slack.com/api/oauth.access"
client := http.Client{}
req, err := http.NewRequest("POST", accessURL, strings.NewReader(form.Encode()))
if err != nil {
log.Infof("Failed to build request for Slack auth API: %v", err)
httpStatus(w, r, 500, "")
return
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
log.Infof("Failed to request token to Slack auth API: %v", err)
httpStatus(w, r, 500, "")
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Infof("Failed to read body from Slack auth API response: %v", err)
httpStatus(w, r, 500, "")
return
}
// parse the response message
// Documented at https://api.slack.com/methods/oauth.access .
// There are other fields, but we don't care about them here.
type authmsg struct {
Ok bool `json:"ok"`
Error string `json:"error"`
AccessToken string `json:"access_token"`
TeamName string `json:"team_name"`
TeamID string `json:"team_id"`
Scope string `json:"scope"`
EnterpriseID *string `json:"enterprise_id,omitempty"`
}
var msg authmsg
if err := json.Unmarshal(data, &msg); err != nil {
log.Infof("Failed to unmarshal API response response: %v", err)
httpStatus(w, r, 500, "")
}
if !msg.Ok {
log.Infof("Got error response from auth API: %s", msg.Error)
httpStatus(w, r, 500, "")
return
}
indented, err := json.MarshalIndent(msg, "", " ")
if err != nil {
log.Infof("Cannot re-marshal API response with indentation: %v", err)
httpStatus(w, r, 500, "")
return
}
httpStatus(w, r, 200, string(indented))
}
func main() {
flag.Parse()
addr := ":2020"
if flag.Arg(0) != "" {
addr = flag.Arg(0)
}
if clientID == "" {
log.Fatalf("SLACK_APP_CLIENT_ID is empty or not set")
}
if clientSecret == "" {
log.Fatalf("SLACK_APP_CLIENT_SECRET is empty or not set")
}
http.HandleFunc("/irc-slack/challenge/", handleSlackChallenge)
http.HandleFunc("/irc-slack/auth/", handleSlackAuth)
log.Printf("Listening on %s", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}