/
api.go
35 lines (28 loc) · 816 Bytes
/
api.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
package api
import (
"fmt"
"net/http"
"os"
log "github.com/sirupsen/logrus"
)
func basicAuth(r *http.Request) error {
u, p, ok := r.BasicAuth()
log.Debugf("user: '%s', pass: '********', basicAuthUsed?: '%t'", u, ok)
if ok {
if !(u == os.Getenv("YAAM_USER") && p == os.Getenv("YAAM_PASS")) {
return fmt.Errorf("auth failed")
}
} else {
return fmt.Errorf("request is NOT using basic authentication")
}
return nil
}
func Validation(method string, r *http.Request, w http.ResponseWriter) error {
if !(method == "PUT" || method == "POST" || method == "GET" || method == "HEAD") {
return fmt.Errorf("only PUTs, POSTs, GETs and HEADs are supported. Used method: '%s'", method)
}
if err := basicAuth(r); err != nil {
return fmt.Errorf("basic auth failed. Error: '%v'", err)
}
return nil
}