mirror of https://github.com/gophish/gophish
Fixed invalid/unset API Key header to be 400 instead of 500
Successfully handle OPTIONS header for APIpull/24/head
parent
eb8491c144
commit
73db7fbdf9
|
@ -42,12 +42,19 @@ func RequireAPIKey(handler http.Handler) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
ak := r.Form.Get("api_key")
|
ak := r.Form.Get("api_key")
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Max-Age", "1000")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
|
||||||
|
return
|
||||||
|
}
|
||||||
if ak == "" {
|
if ak == "" {
|
||||||
JSONError(w, 500, "API Key not set")
|
JSONError(w, 400, "API Key not set")
|
||||||
} else {
|
} else {
|
||||||
id, err := db.Conn.SelectInt("SELECT id FROM users WHERE api_key=?", ak)
|
id, err := db.Conn.SelectInt("SELECT id FROM users WHERE api_key=?", ak)
|
||||||
if id == 0 || err != nil {
|
if id == 0 || err != nil {
|
||||||
JSONError(w, 500, "Invalid API Key")
|
JSONError(w, 400, "Invalid API Key")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Set(r, "user_id", id)
|
ctx.Set(r, "user_id", id)
|
||||||
|
|
Loading…
Reference in New Issue