@@ -2,63 +2,43 @@ package controller
22
33import (
44 "context"
5- "crypto/md5"
6- "encoding/hex"
7- "errors"
8- "fmt"
5+ "math/rand"
96 "strconv"
10-
11- "github.com/Nerzal/gocloak/v13"
7+ "sync"
128)
139
14- var _ Keycloak = (* KeycloakDummy )(nil )
15-
16- type KeycloakDummy struct {
17- clients []gocloak.Client
18- }
19-
20- func (d * KeycloakDummy ) CreateClient (ctx context.Context , newClient gocloak.Client ) (string , error ) {
21- id := strconv .Itoa (len (d .clients ))
22- newClient .ID = & id
23- hash := md5 .Sum ([]byte (fmt .Sprintf ("%s-%s" , id , * (newClient .Name ))))
24- secret := hex .EncodeToString (hash [:])
25- newClient .Secret = & secret
26- d .clients = append (d .clients , newClient )
27- return id , nil
10+ type OidcDummy struct {
11+ mu sync.RWMutex
12+ clients map [string ]Client
2813}
2914
30- func (d * KeycloakDummy ) CreateClientProtocolMapper (ctx context.Context , idOfClient string , mapper gocloak.ProtocolMapperRepresentation ) (string , error ) {
31- return "" , nil
32- }
33-
34- func (d * KeycloakDummy ) GetClientByClientId (ctx context.Context , clientId string ) (* gocloak.Client , error ) {
35- for _ , client := range d .clients {
36- if * client .ClientID == clientId {
37- return & client , nil
38- }
15+ func (d * OidcDummy ) CreateClient (ctx context.Context , req CreateClientRequest ) (* Client , error ) {
16+ secret := strconv .Itoa (rand .Int ())
17+ client := Client {
18+ ClientID : req .Name ,
19+ ClientSecret : secret ,
3920 }
40- return nil , & ClientNotFoundError {ClientId : clientId }
21+ d .mu .Lock ()
22+ defer d .mu .Unlock ()
23+ d .clients [req .Name ] = client
24+ return & client , nil
4125}
4226
43- func (d * KeycloakDummy ) GetClient (ctx context.Context , idOfClient string ) (* gocloak.Client , error ) {
44- id , err := strconv .Atoi (idOfClient )
45- if err != nil {
46- return nil , err
27+ func (d * OidcDummy ) GetClient (ctx context.Context , req GetClientRequest ) (* Client , error ) {
28+ d .mu .RLock ()
29+ defer d .mu .RUnlock ()
30+ if client , ok := d .clients [req .Name ]; ok {
31+ return & client , nil
4732 }
48- if id < 0 || id >= len (d .clients ) {
49- return nil , fmt .Errorf ("invalid dummy client ID %d" , id )
50- }
51- return & d .clients [id ], nil
33+ return nil , ErrNotFound
5234}
5335
54- func (d * KeycloakDummy ) DeleteClient (ctx context.Context , idOfClient string ) error {
55- id , err := strconv .Atoi (idOfClient )
56- if err != nil {
57- return err
58- }
59- if id < 0 || id >= len (d .clients ) {
60- return errors .New ("invalid dummy client ID" )
36+ func (d * OidcDummy ) DeleteClient (ctx context.Context , req DeleteClientRequest ) error {
37+ d .mu .Lock ()
38+ defer d .mu .Unlock ()
39+ if _ , ok := d .clients [req .Name ]; ok {
40+ delete (d .clients , req .Name )
41+ return nil
6142 }
62- d .clients = append (d .clients [:id ], d .clients [id + 1 :]... )
63- return nil
43+ return ErrNotFound
6444}
0 commit comments