-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_validate.go
50 lines (43 loc) · 1.14 KB
/
api_validate.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
package main
import (
"fmt"
"net/http"
log "github.com/Sirupsen/logrus"
"gopkg.in/mgo.v2"
)
// ValidateHandler determines whether or not an API key is valid for a specific account.
func ValidateHandler(c *Context, w http.ResponseWriter, r *http.Request) {
if !MethodOk(w, r, "GET") {
return
}
accountName, apiKey, ok := ExtractKeyCredentials(w, r, "Key validation")
if !ok {
return
}
ok, err := c.Storage.AccountHasKey(accountName, apiKey)
if err != nil {
if err == mgo.ErrNotFound {
APIError{
Message: "Unrecognized account or API key.",
}.Log(accountName).Report(w, http.StatusUnauthorized)
return
}
APIError{
UserMessage: "Internal storage error encountered. Please try again later.",
LogMessage: fmt.Sprintf("Storage error: %v", err),
}.Log(accountName).Report(w, http.StatusInternalServerError)
return
}
var message string
if ok {
w.WriteHeader(http.StatusNoContent)
message = "API key successfully validated."
} else {
w.WriteHeader(http.StatusNotFound)
message = "Invalid API key encountered."
}
log.WithFields(log.Fields{
"account": accountName,
"key": apiKey,
}).Info(message)
}