98 lines
2.4 KiB
Go
98 lines
2.4 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (s *Service) CreateClient(ctx context.Context, input models.ClientCreateInput) (*models.OAuthClient, string, error) {
|
|
clientID, err := newOpaqueToken(12)
|
|
if err != nil {
|
|
return nil, "", models.ErrInternal
|
|
}
|
|
rawSecret, err := newOpaqueToken(32)
|
|
if err != nil {
|
|
return nil, "", models.ErrInternal
|
|
}
|
|
hashedSecret, err := HashClientSecret(rawSecret)
|
|
if err != nil {
|
|
return nil, "", models.ErrInternal
|
|
}
|
|
|
|
redirectURIsJSON, _ := json.Marshal(input.RedirectURIs)
|
|
grantTypesJSON, _ := json.Marshal(input.GrantTypes)
|
|
scopesJSON, _ := json.Marshal(input.Scopes)
|
|
|
|
client, err := s.oauth.CreateOAuthClient(ctx, models.OAuthClient{
|
|
ClientID: clientID,
|
|
ClientSecret: hashedSecret,
|
|
Name: input.Name,
|
|
RedirectURIs: redirectURIsJSON,
|
|
GrantTypes: grantTypesJSON,
|
|
Scopes: scopesJSON,
|
|
Trusted: false,
|
|
})
|
|
if err != nil {
|
|
return nil, "", models.ErrInternal
|
|
}
|
|
|
|
return client, rawSecret, nil
|
|
}
|
|
|
|
func (s *Service) ListClients(ctx context.Context) ([]models.OAuthClient, error) {
|
|
return s.oauth.ListOAuthClients(ctx)
|
|
}
|
|
|
|
func (s *Service) GetClient(ctx context.Context, id uuid.UUID) (*models.OAuthClient, error) {
|
|
client, err := s.oauth.GetOAuthClientByIDAdmin(ctx, id)
|
|
if err != nil {
|
|
return nil, models.ErrNotFound
|
|
}
|
|
return client, nil
|
|
}
|
|
|
|
func (s *Service) UpdateClient(ctx context.Context, id uuid.UUID, input models.ClientUpdateInput) (*models.OAuthClient, error) {
|
|
client, err := s.oauth.GetOAuthClientByIDAdmin(ctx, id)
|
|
if err != nil {
|
|
return nil, models.ErrNotFound
|
|
}
|
|
|
|
if input.Name != nil {
|
|
client.Name = *input.Name
|
|
}
|
|
if input.RedirectURIs != nil {
|
|
b, _ := json.Marshal(*input.RedirectURIs)
|
|
client.RedirectURIs = b
|
|
}
|
|
if input.GrantTypes != nil {
|
|
b, _ := json.Marshal(*input.GrantTypes)
|
|
client.GrantTypes = b
|
|
}
|
|
if input.Scopes != nil {
|
|
b, _ := json.Marshal(*input.Scopes)
|
|
client.Scopes = b
|
|
}
|
|
if input.Trusted != nil {
|
|
client.Trusted = *input.Trusted
|
|
}
|
|
if input.IsActive != nil {
|
|
client.IsActive = *input.IsActive
|
|
}
|
|
|
|
updated, err := s.oauth.UpdateOAuthClient(ctx, *client)
|
|
if err != nil {
|
|
return nil, models.ErrInternal
|
|
}
|
|
return updated, nil
|
|
}
|
|
|
|
func (s *Service) DeactivateClient(ctx context.Context, id uuid.UUID) error {
|
|
if err := s.oauth.DeactivateOAuthClient(ctx, id); err != nil {
|
|
return models.ErrInternal
|
|
}
|
|
return nil
|
|
}
|