This repository has been archived on 2022-08-18. You can view files and clone it, but cannot push or open issues or pull requests.
rageshake/src/github.com/matrix-org/rageshake/main.go

86 lines
2.3 KiB
Go
Raw Normal View History

2017-04-04 21:29:18 +05:30
/*
Copyright 2017 Vector Creations Ltd
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"
"crypto/subtle"
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"log"
"net/http"
"os"
)
func basicAuth(handler http.Handler, username, password, realm string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth() // pull creds from the request
// check user and pass securely
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorised.\n"))
return
}
handler.ServeHTTP(w, r)
})
}
func main() {
ghToken := os.Getenv("GITHUB_TOKEN")
var ghClient *github.Client
if ghToken == "" {
fmt.Println("No GITHUB_TOKEN env var set. Reporting bugs to github is disabled.")
} else {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken},
)
tc := oauth2.NewClient(ctx, ts)
ghClient = github.NewClient(tc)
}
http.Handle("/api/submit", &submitServer{ghClient})
// Make sure bugs directory exists
_ = os.Mkdir("bugs", os.ModePerm)
// serve files under "bugs"
ls := &logServer{"bugs"}
fs := http.StripPrefix("/api/listing/", ls)
// set auth if env vars exist
usr := os.Getenv("BUGS_USER")
pass := os.Getenv("BUGS_PASS")
if usr == "" || pass == "" {
fmt.Println("BUGS_USER and BUGS_PASS env vars not found. No authentication is running for /api/listing")
} else {
fs = basicAuth(fs, usr, pass, "Riot bug reports")
}
http.Handle("/api/listing/", fs)
port := os.Args[1]
log.Println("Listening on port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}