memos/store/idp.go

121 lines
2.9 KiB
Go
Raw Normal View History

package store
import (
"context"
)
type IdentityProviderType string
const (
IdentityProviderOAuth2Type IdentityProviderType = "OAUTH2"
)
func (t IdentityProviderType) String() string {
return string(t)
}
type IdentityProviderConfig struct {
OAuth2Config *IdentityProviderOAuth2Config
}
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 {
Identifier string `json:"identifier"`
DisplayName string `json:"displayName"`
Email string `json:"email"`
}
2023-06-26 23:46:01 +08:00
type IdentityProvider struct {
2023-08-04 21:55:07 +08:00
ID int32
Name string
Type IdentityProviderType
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-06-26 23:46:01 +08:00
type UpdateIdentityProvider struct {
2023-08-04 21:55:07 +08:00
ID int32
Type IdentityProviderType
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-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-06-26 23:46:01 +08:00
s.idpCache.Store(identityProvider.ID, identityProvider)
return identityProvider, nil
}
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)
if err != nil {
return nil, err
}
for _, item := range identityProviders {
s.idpCache.Store(item.ID, item)
}
return identityProviders, nil
}
2023-06-26 23:46:01 +08:00
func (s *Store) GetIdentityProvider(ctx context.Context, find *FindIdentityProvider) (*IdentityProvider, error) {
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
}
}
list, err := s.ListIdentityProviders(ctx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
2023-06-26 23:46:01 +08:00
return nil, nil
}
2023-06-26 23:46:01 +08:00
identityProvider := list[0]
return identityProvider, nil
}
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-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-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)
if err != nil {
2023-06-26 23:46:01 +08:00
return err
}
2023-09-26 19:17:17 +08:00
s.idpCache.Delete(delete.ID)
return nil
}