55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (s *Service) GetUserConsents(ctx context.Context, userID uuid.UUID) ([]models.UserConsent, error) {
|
|
return s.oauth.GetUserConsents(ctx, userID)
|
|
}
|
|
|
|
func (s *Service) HasUserConsent(ctx context.Context, userId uuid.UUID, clientId uuid.UUID, scopes []string) bool {
|
|
allConsents, err := s.oauth.GetUserConsentsByClientID(ctx, userId, clientId)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
consents := make(map[string]bool)
|
|
for _, scopes := range allConsents {
|
|
for _, scope := range scopes.Scopes {
|
|
consents[scope] = true
|
|
}
|
|
}
|
|
|
|
for _, scope := range scopes {
|
|
if !consents[scope] {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (s *Service) RevokeConsent(ctx context.Context, userID uuid.UUID, clientID string) error {
|
|
client, err := s.oauth.GetOAuthClientByClientID(ctx, clientID)
|
|
if err != nil {
|
|
return models.ErrNotFound
|
|
}
|
|
|
|
if err := s.oauth.RevokeConsent(ctx, userID, client.ID, &userID); err != nil {
|
|
return err
|
|
}
|
|
|
|
tokens, _ := s.redis.SMembers(ctx, fmt.Sprintf("user_client_tokens:%s:%s", userID, client.ID)).Result()
|
|
for _, token := range tokens {
|
|
s.redis.Del(ctx, accessTokenPrefix+token)
|
|
}
|
|
s.redis.Del(ctx, fmt.Sprintf("user_client_tokens:%s:%s", userID, client.ID))
|
|
|
|
return nil
|
|
}
|