-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
executable file
·301 lines (255 loc) · 8.98 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* Copyright 2019 Amazon.com, Inc. or its affiliates
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"git-aws.internal.justin.tv/vpolouch/extension-automod/ebs/config"
"git-aws.internal.justin.tv/vpolouch/extension-automod/ebs/dynamodb"
"git-aws.internal.justin.tv/vpolouch/extension-automod/ebs/helix"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"golang.org/x/oauth2"
"golang.org/x/oauth2/twitch"
)
var (
clientID = os.Getenv("CLIENT_ID")
apiSecret = os.Getenv("API_SECRET")
extensionSecret = os.Getenv("EXTENSION_SECRET")
sessionSecret = os.Getenv("SESSION_SECRET")
authHeaderName = "Authorization"
authHeaderPrefix = "Bearer "
authSessionName = "automodder-session"
authHeaderPrefixLen = len(authHeaderPrefix)
minLegalTokenLength = authHeaderPrefixLen + 5
parser = jwt.Parser{ValidMethods: []string{"HS256"}}
)
type contextKeyType string
type loginHandler struct {
config *config.Config
sessionStore *sessions.CookieStore
}
type authCallbackHandler struct {
config *config.Config
dynamoDBService *dynamodb.DynamoDBService
helixService *helix.HelixService
sessionStore *sessions.CookieStore
}
type autoModCheckHandler struct {
dynamoDBService *dynamodb.DynamoDBService
helixService *helix.HelixService
}
type userHandler struct{}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
config := &config.Config{
ExtensionSecret: extensionSecret,
OAuth2: &oauth2.Config{
ClientID: clientID,
ClientSecret: apiSecret,
Scopes: []string{"moderation:read"},
Endpoint: twitch.Endpoint,
RedirectURL: "http://localhost:8080/auth/redirect",
},
}
dynamoDBService := dynamodb.NewDynamoDBService()
helixService := helix.NewHelixService(config, dynamoDBService)
sessionStore := sessions.NewCookieStore([]byte(sessionSecret))
r := mux.NewRouter()
s := r.PathPrefix("/auth").Subrouter()
s.Handle("/login", &loginHandler{config: config, sessionStore: sessionStore}).Methods("GET")
s.Handle("/redirect", &authCallbackHandler{config: config, dynamoDBService: dynamoDBService, helixService: helixService, sessionStore: sessionStore}).Methods("GET")
s = r.PathPrefix("/api").Subrouter()
s.Handle("/automod", &autoModCheckHandler{dynamoDBService: dynamoDBService, helixService: helixService}).Methods("POST")
s.Handle("/user", &userHandler{}).Methods("GET")
s.Use(verifyJWT)
// Serve frontend assets
r.PathPrefix("/").Handler(http.FileServer(http.Dir("../client/")))
fmt.Println("Started running on http://localhost:8080/")
fmt.Println(http.ListenAndServe(":8080", handlers.CORS(handlers.AllowedHeaders([]string{authHeaderName}))(r)))
}
func (h *loginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session, err := h.sessionStore.Get(r, authSessionName)
if err != nil {
log.Println("Could not read the session, generated a new one")
err = nil
}
var b [255]byte
state := hex.EncodeToString(b[:])
session.Values["state"] = state
if err = session.Save(r, w); err != nil {
log.Println("Could not save the session")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// For example purposes, we're passing force_verify to always prompt the broadcaster for authorization during extension configuration
http.Redirect(w, r, h.config.OAuth2.AuthCodeURL(state, oauth2.SetAuthURLParam("force_verify", "true")), http.StatusTemporaryRedirect)
}
func (h *authCallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session, err := h.sessionStore.Get(r, authSessionName)
if err != nil {
log.Println("Could not read session, generated a new one")
err = nil
}
switch stateChallenge, state := session.Values["state"].(string), r.FormValue("state"); {
case state == "", len(stateChallenge) < 1:
err = errors.New("Missing state challenge")
case state != stateChallenge:
err = fmt.Errorf("Invalid oauth state, expected '%s', got '%s'", state, stateChallenge)
}
if err != nil {
log.Println(err.Error())
http.Error(w, "Couldn't verify your confirmation, please try again.", http.StatusBadRequest)
return
}
token, err := h.config.OAuth2.Exchange(context.Background(), r.FormValue("code"))
if err != nil {
log.Println(err.Error())
http.Error(w, "Couldn't verify your confirmation, please try again.", http.StatusInternalServerError)
return
}
// Once the broadcaster has authenticated, we retrieve their TUID to we use it as our key in the database
userID, err := h.helixService.GetUserID(token.AccessToken)
if err != nil {
log.Println(err.Error())
http.Error(w, "Couldn't retrieve your confirmation, please try again.", http.StatusInternalServerError)
return
}
err = h.dynamoDBService.PutUser(&dynamodb.User{
UserID: userID,
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
Expiry: token.Expiry,
TokenType: token.TokenType,
})
if err != nil {
log.Println(err.Error())
http.Error(w, "Couldn't store your information, please try again.", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/auth_popup.html", http.StatusTemporaryRedirect)
return
}
func (h *autoModCheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Println(err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var body struct {
Message string `json:"message"`
}
err = json.Unmarshal(b, &body)
if err != nil {
log.Println(err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
claims := getClaims(r)
// A missing user ID indicates that they viewer has not shared their identity in the extension
if claims.UserID == "" {
log.Println("UserID is missing in the request context")
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
result, err := h.helixService.GetEnforcementStatus(claims.ChannelID, claims.UserID, body.Message)
if err != nil {
log.Println(err.Error())
http.Error(w, "Couldn't validate message against AutoMod, please try again.", http.StatusInternalServerError)
return
}
resp := struct {
IsPermitted bool `json:"is_permitted"`
}{
IsPermitted: result,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
func (h *userHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
claims := getClaims(r)
resp := struct {
OpaqueUserID string `json:"opaque_user_id"`
UserID string `json:"user_id,omitempty"`
Role string `json:"role"`
}{
OpaqueUserID: claims.OpaqueUserID,
UserID: claims.UserID,
Role: claims.Role,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
func verifyJWT(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var token string
tokens, ok := r.Header[authHeaderName]
if !ok {
log.Println("Missing authorization header")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
if len(tokens) != 1 {
log.Println("Multiple authorization headers found")
http.Error(w, "Multiple authorization headers found; only one header should be sent", http.StatusUnauthorized)
return
}
token = tokens[0]
if !strings.HasPrefix(token, authHeaderPrefix) || len(token) < minLegalTokenLength {
log.Println("Malformed authorization header")
http.Error(w, "Malformed authorization header", http.StatusUnauthorized)
return
}
token = strings.TrimPrefix(token, authHeaderPrefix)
parsedToken, err := parser.ParseWithClaims(token, &jwtClaims{}, getKey)
if err != nil {
log.Println(err)
http.Error(w, "Could not parse authorization header", http.StatusInternalServerError)
return
}
if claims, ok := parsedToken.Claims.(*jwtClaims); ok && parsedToken.Valid {
next.ServeHTTP(w, setClaims(r, claims))
} else {
log.Println("Could not parse JWT claims")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
})
}
func getKey(*jwt.Token) (interface{}, error) {
secret, err := base64.StdEncoding.DecodeString(extensionSecret)
if err != nil {
log.Fatalf("Could not parse extension secret: %v", err)
}
return secret, nil
}