forked from theopenlane/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
71 lines (55 loc) · 1.73 KB
/
Copy pathclient.go
File metadata and controls
71 lines (55 loc) · 1.73 KB
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
package keycloak
import (
"context"
"time"
gocloak "github.com/Nerzal/gocloak/v13"
"github.com/theopenlane/core/internal/integrations/types"
)
const (
// keycloakRequestTimeout is the per-request timeout for Keycloak API calls
keycloakRequestTimeout = 30 * time.Second
// keycloakDefaultPageSize is the number of records requested per Keycloak API page
keycloakDefaultPageSize = 100
// keycloakMaxLoginEvents is the maximum number of LOGIN events fetched when resolving last login times
keycloakMaxLoginEvents = 1000
)
// Client builds Keycloak API clients for one installation
type Client struct{}
// Build constructs the Keycloak API client for one installation
func (Client) Build(_ context.Context, req types.ClientBuildRequest) (any, error) {
cred, err := resolveCredential(req.Credentials)
if err != nil {
return nil, err
}
if cred.BaseURL == "" {
return nil, ErrBaseURLMissing
}
if cred.Realm == "" {
return nil, ErrRealmMissing
}
if cred.ClientID == "" {
return nil, ErrClientIDMissing
}
if cred.ClientSecret == "" {
return nil, ErrClientSecretMissing
}
gc := gocloak.NewClient(cred.BaseURL)
gc.RestyClient().SetTimeout(keycloakRequestTimeout)
return gc, nil
}
// resolveCredential extracts the CredentialSchema from the provided credential bindings
func resolveCredential(bindings types.CredentialBindings) (CredentialSchema, error) {
cred, ok, err := keycloakCredential.Resolve(bindings)
if err != nil {
return CredentialSchema{}, ErrCredentialDecode
}
if !ok {
return CredentialSchema{}, ErrCredentialDecode
}
return cred, nil
}
// enrichedUser wraps a Keycloak user with additional data not on the user object
type enrichedUser struct {
*gocloak.User
LastLogin *int64 `json:"lastLogin,omitempty"`
}