/
facecontrol.go
174 lines (144 loc) · 4.21 KB
/
facecontrol.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package facecontrol
import (
"encoding/json"
"fmt"
"net/http"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
// Claims is a custom JWT claims
type Claims struct {
Payload `json:"data,omitempty"`
jwt.StandardClaims
}
// Payload is a custom data to be append to issued token.
type Payload interface{}
// CredentialsValidator is an function that defines incoming credentials
// validation and payload construction logic. It accepts raw HTTP Request
// and returns payload data if credentials are valid.
// If given credentials are invalid it must return non-nil error.
type CredentialsValidator func(*http.Request) (Payload, error)
// Facecontrol is an SSO service.
type Facecontrol struct {
conf Config
}
// Config defines essential Faceontrol variables and parts.
type Config struct {
// Webserver run address
RunAt string
// EnableSSL forces facecontrol to run in HTTPS mode
EnableSSL bool
// SSLCert and SSLKey are a paths to corresponding SSL files
SSLCert string
SSLKey string
// JwtSecret will be used to sign auth tokens
JwtSecret string
// JwtTTL will be used to set token expiration
JwtTTL time.Duration
// Validator is a credentials validating function.
Validator CredentialsValidator
}
// New returns new instance of Facecontrol.
// Secret is a sign string for JWT token.
func New(conf Config) (*Facecontrol, error) {
if conf.JwtSecret == "" {
return nil, fmt.Errorf("JwtSecret cannot be empty")
}
if conf.Validator == nil {
return nil, fmt.Errorf("Cannot start without validator function")
}
return &Facecontrol{
conf: conf,
}, nil
}
func (f Facecontrol) getExpiration() int64 {
if f.conf.JwtTTL == 0 {
return 0
}
return time.Now().Add(f.conf.JwtTTL).Unix()
}
func (f Facecontrol) issueToken(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
credentials, err := f.conf.Validator(r)
if err != nil {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(err.Error()))
return
}
token := jwt.NewWithClaims(jwt.SigningMethodHS512, Claims{
Payload: credentials,
StandardClaims: jwt.StandardClaims{
Issuer: "facecontrol",
IssuedAt: time.Now().Unix(),
ExpiresAt: f.getExpiration(),
},
})
tokenString, err := token.SignedString([]byte(f.conf.JwtSecret))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Cannot sign token: %s", err)))
return
}
w.Header().Set("Content-Type", "application/jwt")
w.WriteHeader(http.StatusOK)
w.Write([]byte(tokenString))
}
func (f Facecontrol) validateToken(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
rawAuthHeader := r.Header.Get("Authorization")
if len(rawAuthHeader) < 8 && rawAuthHeader[:7] != "Bearer " {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`Malformed "Authorization" header`))
return
}
tokenString := rawAuthHeader[7:len(rawAuthHeader)]
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
// validate the alg
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(f.conf.JwtSecret), nil
})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
if !token.Valid {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`Cannot validate given token`))
return
}
if _, ok := token.Claims.(jwt.MapClaims); !ok {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`Cannot find token payload`))
return
}
payload, err := json.Marshal(token.Claims.(jwt.MapClaims))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`Cannot marshal token payload`))
return
}
w.Header().Set("Content-Type", "application/json+jwt")
w.WriteHeader(http.StatusOK)
w.Write(payload)
}
// Run starts Facecontrol instance.
func (f Facecontrol) Run() error {
http.HandleFunc("/issue", f.issueToken)
http.HandleFunc("/validate", f.validateToken)
var err error
if f.conf.EnableSSL {
err = http.ListenAndServeTLS(f.conf.RunAt, f.conf.SSLCert, f.conf.SSLKey, nil)
} else {
err = http.ListenAndServe(f.conf.RunAt, nil)
}
return err
}