feat: scope API MCP OAuth by principal
This commit is contained in:
@@ -17,7 +17,7 @@ const clientRegistrationName = "WeKnora"
|
||||
|
||||
// OAuthManager orchestrates the MCP OAuth2 authorization-code flow:
|
||||
// discovery, dynamic client registration, the authorize redirect, and the
|
||||
// callback code exchange. Tokens are persisted per (tenant, user, service);
|
||||
// callback code exchange. Tokens are persisted per (tenant, principal, service);
|
||||
// the registered client is persisted per (tenant, service) and reused.
|
||||
type OAuthManager struct {
|
||||
repo interfaces.MCPOAuthRepository
|
||||
@@ -39,9 +39,9 @@ func NewOAuthManager(
|
||||
}
|
||||
}
|
||||
|
||||
// newHandler builds an OAuth handler bound to a service + per-user token store.
|
||||
// newHandler builds an OAuth handler bound to a service + per-principal token store.
|
||||
func (m *OAuthManager) newHandler(
|
||||
ctx context.Context, service *types.MCPService, tenantID uint64, userID, redirectURI string,
|
||||
ctx context.Context, service *types.MCPService, tenantID uint64, principal types.Principal, redirectURI string,
|
||||
) (*transport.OAuthHandler, error) {
|
||||
if service.URL == nil || *service.URL == "" {
|
||||
return nil, fmt.Errorf("MCP service URL is required for OAuth")
|
||||
@@ -49,7 +49,7 @@ func (m *OAuthManager) newHandler(
|
||||
cfg := transport.OAuthConfig{
|
||||
RedirectURI: redirectURI,
|
||||
Scopes: service.AuthConfig.Scopes,
|
||||
TokenStore: newDBTokenStore(m.repo, tenantID, userID, service.ID),
|
||||
TokenStore: newDBTokenStore(m.repo, tenantID, principal, service.ID),
|
||||
PKCEEnabled: true,
|
||||
AuthServerMetadataURL: service.AuthConfig.AuthServerMetadataURL,
|
||||
}
|
||||
@@ -70,13 +70,18 @@ func (m *OAuthManager) StartAuthorization(
|
||||
ctx context.Context,
|
||||
service *types.MCPService,
|
||||
tenantID uint64,
|
||||
userID, redirectURI, frontendRedirect string,
|
||||
principal types.Principal,
|
||||
redirectURI, frontendRedirect string,
|
||||
) (string, error) {
|
||||
if !service.AuthConfig.IsOAuth() {
|
||||
return "", fmt.Errorf("MCP service %s does not use OAuth", service.ID)
|
||||
}
|
||||
principal = principal.Normalize()
|
||||
if !principal.Valid() {
|
||||
return "", fmt.Errorf("principal context is required to authorize OAuth MCP service %s", service.ID)
|
||||
}
|
||||
|
||||
h, err := m.newHandler(ctx, service, tenantID, userID, redirectURI)
|
||||
h, err := m.newHandler(ctx, service, tenantID, principal, redirectURI)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -118,7 +123,8 @@ func (m *OAuthManager) StartAuthorization(
|
||||
|
||||
if err := m.states.Put(ctx, state, OAuthState{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
UserID: principal.StorageID(),
|
||||
Principal: principal,
|
||||
ServiceID: service.ID,
|
||||
CodeVerifier: verifier,
|
||||
ClientID: h.GetClientID(),
|
||||
@@ -142,6 +148,13 @@ func (m *OAuthManager) CompleteAuthorization(
|
||||
return "", err
|
||||
}
|
||||
frontendRedirect = st.FrontendRedirect
|
||||
principal := st.Principal.Normalize()
|
||||
if !principal.Valid() && st.UserID != "" {
|
||||
principal = types.Principal{Type: types.PrincipalWebUser, ID: st.UserID}.Normalize()
|
||||
}
|
||||
if !principal.Valid() {
|
||||
return frontendRedirect, fmt.Errorf("principal context is missing from OAuth state")
|
||||
}
|
||||
|
||||
service, err := m.serviceRepo.GetByID(ctx, st.TenantID, st.ServiceID)
|
||||
if err != nil {
|
||||
@@ -151,7 +164,7 @@ func (m *OAuthManager) CompleteAuthorization(
|
||||
return frontendRedirect, fmt.Errorf("MCP service not found")
|
||||
}
|
||||
|
||||
h, err := m.newHandler(ctx, service, st.TenantID, st.UserID, st.RedirectURI)
|
||||
h, err := m.newHandler(ctx, service, st.TenantID, principal, st.RedirectURI)
|
||||
if err != nil {
|
||||
return frontendRedirect, err
|
||||
}
|
||||
@@ -164,26 +177,26 @@ func (m *OAuthManager) CompleteAuthorization(
|
||||
}
|
||||
// ProcessAuthorizationResponse persists the token via the TokenStore.
|
||||
logger.GetLogger(ctx).Infof(
|
||||
"MCP OAuth authorized: service=%s user=%s", st.ServiceID, st.UserID,
|
||||
"MCP OAuth authorized: service=%s principal=%s", st.ServiceID, principal.StorageID(),
|
||||
)
|
||||
return frontendRedirect, nil
|
||||
}
|
||||
|
||||
// IsAuthorized reports whether the given user has a stored (non-empty) token
|
||||
// IsAuthorized reports whether the given principal has a stored (non-empty) token
|
||||
// for the service.
|
||||
func (m *OAuthManager) IsAuthorized(
|
||||
ctx context.Context, tenantID uint64, userID, serviceID string,
|
||||
ctx context.Context, tenantID uint64, principal types.Principal, serviceID string,
|
||||
) (bool, error) {
|
||||
tok, err := m.repo.GetToken(ctx, tenantID, userID, serviceID)
|
||||
tok, err := m.repo.GetTokenForPrincipal(ctx, tenantID, principal, serviceID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return tok != nil && tok.AccessToken != "", nil
|
||||
}
|
||||
|
||||
// Revoke removes the user's stored token for the service.
|
||||
// Revoke removes the principal's stored token for the service.
|
||||
func (m *OAuthManager) Revoke(
|
||||
ctx context.Context, tenantID uint64, userID, serviceID string,
|
||||
ctx context.Context, tenantID uint64, principal types.Principal, serviceID string,
|
||||
) error {
|
||||
return m.repo.DeleteToken(ctx, tenantID, userID, serviceID)
|
||||
return m.repo.DeleteTokenForPrincipal(ctx, tenantID, principal, serviceID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user