-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
49 lines (39 loc) · 949 Bytes
/
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 http
import (
"net/http"
"github.com/boreq/eggplant/application"
"github.com/boreq/eggplant/application/auth"
"github.com/boreq/errors"
)
type HttpAuthProvider struct {
app *application.Application
}
func NewHttpAuthProvider(app *application.Application) *HttpAuthProvider {
return &HttpAuthProvider{
app: app,
}
}
func (h *HttpAuthProvider) Get(r *http.Request) (*AuthenticatedUser, error) {
token := h.getToken(r)
if token == "" {
return nil, nil
}
cmd := auth.CheckAccessToken{
Token: token,
}
user, err := h.app.Auth.CheckAccessToken.Execute(cmd)
if err != nil {
if errors.Is(err, auth.ErrUnauthorized) {
return nil, nil
}
return nil, errors.Wrap(err, "could not check the access token")
}
u := AuthenticatedUser{
User: *user,
Token: token,
}
return &u, nil
}
func (h *HttpAuthProvider) getToken(r *http.Request) auth.AccessToken {
return auth.AccessToken(r.Header.Get("Access-Token"))
}