2023-02-17 21:06:41 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2023-02-18 11:29:12 +08:00
|
|
|
type IdentityProviderType string
|
2023-02-17 21:06:41 +08:00
|
|
|
|
|
|
|
const (
|
2023-07-02 18:56:25 +08:00
|
|
|
IdentityProviderOAuth2Type IdentityProviderType = "OAUTH2"
|
2023-02-17 21:06:41 +08:00
|
|
|
)
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
func (t IdentityProviderType) String() string {
|
|
|
|
return string(t)
|
|
|
|
}
|
|
|
|
|
2023-02-18 18:31:03 +08:00
|
|
|
type IdentityProviderConfig struct {
|
|
|
|
OAuth2Config *IdentityProviderOAuth2Config
|
|
|
|
}
|
2023-02-17 21:06:41 +08:00
|
|
|
|
|
|
|
type IdentityProviderOAuth2Config struct {
|
|
|
|
ClientID string `json:"clientId"`
|
|
|
|
ClientSecret string `json:"clientSecret"`
|
|
|
|
AuthURL string `json:"authUrl"`
|
|
|
|
TokenURL string `json:"tokenUrl"`
|
|
|
|
UserInfoURL string `json:"userInfoUrl"`
|
|
|
|
Scopes []string `json:"scopes"`
|
|
|
|
FieldMapping *FieldMapping `json:"fieldMapping"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FieldMapping struct {
|
2023-02-18 11:29:12 +08:00
|
|
|
Identifier string `json:"identifier"`
|
|
|
|
DisplayName string `json:"displayName"`
|
|
|
|
Email string `json:"email"`
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
type IdentityProvider struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID int32
|
2023-02-17 21:06:41 +08:00
|
|
|
Name string
|
2023-02-18 11:29:12 +08:00
|
|
|
Type IdentityProviderType
|
2023-02-17 21:06:41 +08:00
|
|
|
IdentifierFilter string
|
|
|
|
Config *IdentityProviderConfig
|
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
type FindIdentityProvider struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID *int32
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
type UpdateIdentityProvider struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID int32
|
2023-02-18 11:29:12 +08:00
|
|
|
Type IdentityProviderType
|
2023-02-17 21:06:41 +08:00
|
|
|
Name *string
|
|
|
|
IdentifierFilter *string
|
|
|
|
Config *IdentityProviderConfig
|
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
type DeleteIdentityProvider struct {
|
2023-08-04 21:55:07 +08:00
|
|
|
ID int32
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
func (s *Store) CreateIdentityProvider(ctx context.Context, create *IdentityProvider) (*IdentityProvider, error) {
|
2023-09-26 19:17:17 +08:00
|
|
|
identityProvider, err := s.driver.CreateIdentityProvider(ctx, create)
|
|
|
|
if err != nil {
|
2023-06-26 23:46:01 +08:00
|
|
|
return nil, err
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
2023-06-26 23:46:01 +08:00
|
|
|
|
|
|
|
s.idpCache.Store(identityProvider.ID, identityProvider)
|
|
|
|
return identityProvider, nil
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
func (s *Store) ListIdentityProviders(ctx context.Context, find *FindIdentityProvider) ([]*IdentityProvider, error) {
|
2023-09-26 19:17:17 +08:00
|
|
|
identityProviders, err := s.driver.ListIdentityProviders(ctx, find)
|
2023-02-17 21:06:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-06 21:56:42 +08:00
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
for _, item := range identityProviders {
|
2023-02-18 18:41:52 +08:00
|
|
|
s.idpCache.Store(item.ID, item)
|
|
|
|
}
|
2023-07-20 23:15:56 +08:00
|
|
|
return identityProviders, nil
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
func (s *Store) GetIdentityProvider(ctx context.Context, find *FindIdentityProvider) (*IdentityProvider, error) {
|
2023-02-18 18:41:52 +08:00
|
|
|
if find.ID != nil {
|
|
|
|
if cache, ok := s.idpCache.Load(*find.ID); ok {
|
2023-06-26 23:46:01 +08:00
|
|
|
return cache.(*IdentityProvider), nil
|
2023-02-18 18:41:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:15:56 +08:00
|
|
|
list, err := s.ListIdentityProviders(ctx, find)
|
2023-02-17 21:06:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(list) == 0 {
|
2023-06-26 23:46:01 +08:00
|
|
|
return nil, nil
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
identityProvider := list[0]
|
|
|
|
return identityProvider, nil
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
func (s *Store) UpdateIdentityProvider(ctx context.Context, update *UpdateIdentityProvider) (*IdentityProvider, error) {
|
2023-09-26 19:17:17 +08:00
|
|
|
identityProvider, err := s.driver.UpdateIdentityProvider(ctx, update)
|
|
|
|
if err != nil {
|
2023-06-26 23:46:01 +08:00
|
|
|
return nil, err
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
2023-06-26 23:46:01 +08:00
|
|
|
|
|
|
|
s.idpCache.Store(identityProvider.ID, identityProvider)
|
2023-09-26 19:17:17 +08:00
|
|
|
return identityProvider, nil
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:46:01 +08:00
|
|
|
func (s *Store) DeleteIdentityProvider(ctx context.Context, delete *DeleteIdentityProvider) error {
|
2023-09-26 19:17:17 +08:00
|
|
|
err := s.driver.DeleteIdentityProvider(ctx, delete)
|
2023-02-17 21:06:41 +08:00
|
|
|
if err != nil {
|
2023-06-26 23:46:01 +08:00
|
|
|
return err
|
2023-02-17 21:06:41 +08:00
|
|
|
}
|
2023-09-26 19:17:17 +08:00
|
|
|
|
2023-02-18 18:41:52 +08:00
|
|
|
s.idpCache.Delete(delete.ID)
|
2023-02-17 21:06:41 +08:00
|
|
|
return nil
|
|
|
|
}
|