This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
71 lines (60 loc) · 2.16 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"context"
"gopkg.in/aabizri/goil.v0"
"net/http"
"strings"
)
const (
AuthErrorAuthorizationFailure string = "A0"
AuthErrorAuthorizationHeaderAbsent = "A10"
AuthErrorAuthorizationHeaderInvalid = "A11"
AuthErrorAuthorizationHeaderElementsLenNotTwo = "A110"
AuthErrorAuthorizationHeaderBearerTagAbsent = "A111"
AuthErrorInvalidToken = "A20"
)
// authenticationMiddleware wraps the normal handler around in order to satisfy http.Handler type
type authenticationMiddleware struct {
wrapped http.Handler
session *session
}
// Returns a function which wraps around the router to provide authentification
func (s *session) genAuthHandler(wrapped http.Handler) authenticationMiddleware {
return authenticationMiddleware{wrapped: wrapped, session: s}
}
// The actual logic behind it
// WARNING / WIP / TODO / TOKENTRANSITION: The token used by the client SHOULD NOT be the goil login cookie but should be independent.
func (h authenticationMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Check the header presence
field := r.Header.Get("Authorization")
if field == "" {
err := NewError(AuthErrorAuthorizationHeaderAbsent, "", "").JSONWrite(w)
if err != nil {
panic(err)
}
return
}
subFields := strings.Split(field, " ")
// Check the header validity
if len(subFields) != 2 {
err := NewError(AuthErrorAuthorizationHeaderElementsLenNotTwo, "", "").JSONWrite(w)
if err != nil {
panic(err)
}
return
} else if subFields[0] != "Bearer" { // Check if the authentification scheme is set to "Bearer"
err := NewError(AuthErrorAuthorizationHeaderBearerTagAbsent, "", "").JSONWrite(w)
if err != nil {
panic(err)
}
return
}
// TODO / TOKENTRANSITION: Retrieve the cookie
// Get a goil.Session using the cookie value given
// TODO: Allow customization to the client options
goilSession := goil.CreateSessionByCookieValue(subFields[1], &http.Client{})
// Add to context
ctx := context.WithValue(r.Context(), "session", goilSession)
// If OK, then send it to the wrapped handler
h.wrapped.ServeHTTP(w, r.WithContext(ctx))
}