-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoauth.go
75 lines (63 loc) · 1.82 KB
/
goauth.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package goauth
import (
"fmt"
"net/http"
)
var h []AuthHandler = []AuthHandler{}
// AuthHandler is the interface that wraps the AuthenticateFunc method
// and is used to authenticate the request
type AuthHandler interface {
Handle(h *http.Request) (request *http.Request, statusCode int, err error)
}
// AuthMiddlewareError is the error type returned by the middleware
type AuthMiddlewareError struct {
// Code is the HTTP status code
Code int
// Message is the error message
Message string
}
// Error implements the error interface
func (e *AuthMiddlewareError) Error() string {
return e.Message
}
// GetHandlers returns the authentication handlers
func GetHandlers() []AuthHandler {
return h
}
// SetHandlers sets the authentication handlers
func SetHandlers(handlers []AuthHandler) {
h = handlers
}
// Authenticate executes all the authentication handlers in the order they were added.
// If any of the handlers does not return an error, the request proceeds to the next handler.
// If the last handler returns an error, the request is aborted.
func Authenticate(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
var statusCode int
request := r
for _, handler := range h {
request, statusCode, err = handler.Handle(r)
if err == nil {
break
}
}
if err != nil {
respondWithError(w, &AuthMiddlewareError{
Code: statusCode,
Message: err.Error(),
})
return
}
if next != nil {
next.ServeHTTP(w, request)
}
})
}
// Helper function to abort the request with an error status code and message
func respondWithError(w http.ResponseWriter, err *AuthMiddlewareError) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(err.Code)
str := `{"error":"%s"}`
http.Error(w, fmt.Sprintf(str, err.Message), err.Code)
}