-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
49 lines (40 loc) · 1.13 KB
/
auth.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
package main
import (
"database/sql"
"log"
"net/http"
"github.com/gorilla/mux"
)
type AuthenticationMiddleware struct {
db *sql.DB
}
func NewAuthMiddleware(db *sql.DB) (*AuthenticationMiddleware, error) {
return &AuthenticationMiddleware{
db: db,
}, nil
}
func (amw *AuthenticationMiddleware) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, span := tracer.Start(r.Context(), "Service")
defer span.End()
route := mux.CurrentRoute(r)
path, _ := route.GetPathTemplate()
if path == "/metrics" {
next.ServeHTTP(w, r)
}
account, token, _ := r.BasicAuth()
var found bool
if err := amw.db.QueryRowContext(r.Context(), "SELECT (count(1)==1) FROM auth WHERE account = ? AND token = ?", account, token).Scan(&found); err != nil {
if err == sql.ErrNoRows {
http.Error(w, "Forbidden", http.StatusForbidden)
}
}
if found {
log.Printf("Authenticated account %s\n", account)
// Pass down the request to the next middleware (or final handler)
next.ServeHTTP(w, r)
} else {
http.Error(w, "Forbidden", http.StatusForbidden)
}
})
}