generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
34 lines (29 loc) · 880 Bytes
/
middleware.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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package authmiddleware
import (
"context"
"log"
"clevergo.tech/auth"
"clevergo.tech/clevergo"
)
// GetIdentity retrieves a authenticated identity from context.
func GetIdentity(ctx context.Context) auth.Identity {
return auth.GetIdentity(ctx)
}
// New returns a middleware with the given authenticator.
func New(authenticator auth.Authenticator) clevergo.MiddlewareFunc {
return func(next clevergo.Handle) clevergo.Handle {
return func(c *clevergo.Context) error {
identity, err := authenticator.Authenticate(c.Request, c.Response)
if err != nil {
log.Println(err)
authenticator.Challenge(c.Request, c.Response)
} else {
c.WithValue(auth.IdentityKey, identity)
}
return next(c)
}
}
}