yao/openapi/oauth/providers/user/default.go
Max e19aa4b8df Remove deprecated OAuth interfaces and types
- Deleted the OAuth interface and related types that were previously defined in the `interfaces.go` and `types.go` files, streamlining the codebase.
- Updated the `oauth.go` file to integrate user and client providers directly, enhancing the service's functionality and reducing complexity.
- Refactored the user information retrieval method to utilize the new user provider structure, ensuring compatibility with the updated architecture.
2025-07-17 17:19:22 +08:00

52 lines
2.2 KiB
Go

package user
import (
"context"
"github.com/yaoapp/yao/openapi/oauth/types"
)
// DefaultUserProvider provides a default implementation of UserProvider
type DefaultUserProvider struct {
getUserByAccessTokenFunc func(ctx context.Context, accessToken string) (interface{}, error)
getUserBySubjectFunc func(ctx context.Context, subject string) (interface{}, error)
validateUserScopeFunc func(ctx context.Context, userID string, scopes []string) (bool, error)
}
// NewDefaultUserProvider creates a new DefaultUserProvider with the given functions
func NewDefaultUserProvider(
getUserByAccessTokenFunc func(ctx context.Context, accessToken string) (interface{}, error),
getUserBySubjectFunc func(ctx context.Context, subject string) (interface{}, error),
validateUserScopeFunc func(ctx context.Context, userID string, scopes []string) (bool, error),
) *DefaultUserProvider {
return &DefaultUserProvider{
getUserByAccessTokenFunc: getUserByAccessTokenFunc,
getUserBySubjectFunc: getUserBySubjectFunc,
validateUserScopeFunc: validateUserScopeFunc,
}
}
// GetUserByAccessToken retrieves user information using an access token
func (p *DefaultUserProvider) GetUserByAccessToken(ctx context.Context, accessToken string) (interface{}, error) {
if p.getUserByAccessTokenFunc == nil {
return nil, &types.ErrorResponse{Code: "not_implemented", ErrorDescription: "GetUserByAccessToken is not implemented"}
}
return p.getUserByAccessTokenFunc(ctx, accessToken)
}
// GetUserBySubject retrieves user information using a subject identifier
func (p *DefaultUserProvider) GetUserBySubject(ctx context.Context, subject string) (interface{}, error) {
if p.getUserBySubjectFunc == nil {
return nil, &types.ErrorResponse{Code: "not_implemented", ErrorDescription: "GetUserBySubject is not implemented"}
}
return p.getUserBySubjectFunc(ctx, subject)
}
// ValidateUserScope validates if a user has access to requested scopes
func (p *DefaultUserProvider) ValidateUserScope(ctx context.Context, userID string, scopes []string) (bool, error) {
if p.validateUserScopeFunc == nil {
// Default implementation: allow all scopes
return true, nil
}
return p.validateUserScopeFunc(ctx, userID, scopes)
}