-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication_filter.go
49 lines (36 loc) · 1.16 KB
/
authentication_filter.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 keycloak
import (
"github.com/emicklei/go-restful"
"log"
//"encoding/json"
//"github.com/SermoDigital/jose/jwt"
"github.com/euforia/keycloak-client"
//"io/ioutil"
)
const (
UserVar = "_username_"
RoleVar = "_roles_"
)
// basic auth Filter
func BasicAuthFilter(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
log.Printf("[global-filter (logger)] %s,%s\n", req.Request.Method, req.Request.URL)
chain.ProcessFilter(req, resp)
}
// token auth Filter
func TokenAuthFilter(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
log.Printf("[token-auth-filter (logger)] %s,%s\n", req.Request.Method, req.Request.URL)
if Config != nil {
var kc, err = keycloak.NewHttpKeycloakAuthenticatorFromConfig(Config)
if err == nil {
keycloakJWT, roles, err := kc.AuthenticateRequest(req.Request)
if err == nil {
req.SetAttribute(UserVar, keycloakJWT.GetUsername())
req.SetAttribute(RoleVar, roles)
log.Printf("[token-auth-filter (logger)] %s,%s\n", keycloakJWT.GetUsername(), roles)
} else {
log.Printf("[token-auth-filter (logger)] %s\n", err)
}
}
}
chain.ProcessFilter(req, resp)
}