/
auth.go
55 lines (45 loc) · 1.29 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
package v1
import (
"encoding/json"
"github.com/shimmeringbee/controller/interface/http/auth"
"net/http"
)
type AuthenticationCheckPayload struct {
Authenticated bool `json:"authenticated"`
Identity string `json:"identity,omitempty"`
}
func authenticationCheck(w http.ResponseWriter, r *http.Request) {
var identity string
var ok bool
identityRaw := r.Context().Value(auth.UserIdentityContextKey)
if identityRaw != nil {
identity, ok = identityRaw.(string)
if !ok {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
authenticated := len(identity) > 0
payload := AuthenticationCheckPayload{
Authenticated: authenticated,
Identity: identity,
}
data, err := json.Marshal(payload)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func authenticationType(ap auth.AuthenticationProvider) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(ap.AuthenticationType())
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(data)
}
}