-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeycloak.go
60 lines (44 loc) · 1.56 KB
/
keycloak.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
package saramakeycloak
import (
"time"
"github.com/Nerzal/gocloak/v5"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
keycloackRequestDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "keycloak_request_duration_seconds",
Buckets: prometheus.DefBuckets,
},
[]string{"method", "result"},
)
)
// Keycloak wraps method to interact with keycloak server.
type Keycloak interface {
// LoginClient sends a request to the token endpoint using client credentials.
LoginClient(clientID, clientSecret, realm string) (*gocloak.JWT, error)
// RefreshToken used to refresh the token.
RefreshToken(refreshToken string, clientID, clientSecret, realm string) (*gocloak.JWT, error)
}
type keycloakWithMetrics struct {
k Keycloak
}
func (k *keycloakWithMetrics) LoginClient(clientID, clientSecret, realm string) (*gocloak.JWT, error) {
start := time.Now()
jwt, err := k.k.LoginClient(clientID, clientSecret, realm)
keycloackRequestDuration.WithLabelValues("LoginClient", resultLabel(err)).Observe(time.Since(start).Seconds())
return jwt, err
}
func (k *keycloakWithMetrics) RefreshToken(refreshToken string, clientID, clientSecret, realm string) (*gocloak.JWT, error) {
start := time.Now()
jwt, err := k.k.RefreshToken(refreshToken, clientID, clientSecret, realm)
keycloackRequestDuration.WithLabelValues("RefreshToken", resultLabel(err)).Observe(time.Since(start).Seconds())
return jwt, err
}
func resultLabel(err error) string {
if err != nil {
return "error"
}
return "ok"
}