mirror of https://github.com/grafana/grafana
SaveExternalService (OAuth) on plugin load (#69764)
parent
f436364f9b
commit
4ff0abd0d1
@ -0,0 +1,37 @@ |
||||
package oauth |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol" |
||||
) |
||||
|
||||
// SelfCfg is a subset of oauthserver.SelfCfg making some fields optional
|
||||
type SelfCfg struct { |
||||
Enabled *bool `json:"enabled,omitempty"` |
||||
Permissions []accesscontrol.Permission `json:"permissions,omitempty"` |
||||
} |
||||
|
||||
// ImpersonationCfg is a subset of oauthserver.ImpersonationCfg making some fields optional
|
||||
type ImpersonationCfg struct { |
||||
Enabled *bool `json:"enabled,omitempty"` |
||||
Groups *bool `json:"groups,omitempty"` |
||||
Permissions []accesscontrol.Permission `json:"permissions,omitempty"` |
||||
} |
||||
|
||||
// PluginExternalServiceRegistration is a subset of oauthserver.ExternalServiceRegistration
|
||||
// simplified for the plugin use case.
|
||||
type ExternalServiceRegistration struct { |
||||
Impersonation *ImpersonationCfg `json:"impersonation,omitempty"` |
||||
Self *SelfCfg `json:"self,omitempty"` |
||||
} |
||||
|
||||
type ExternalService struct { |
||||
ClientID string `json:"clientId"` |
||||
ClientSecret string `json:"clientSecret"` |
||||
PrivateKey string `json:"privateKey"` |
||||
} |
||||
|
||||
type ExternalServiceRegistry interface { |
||||
RegisterExternalService(ctx context.Context, name string, svc *ExternalServiceRegistration) (*ExternalService, error) |
||||
} |
@ -0,0 +1,62 @@ |
||||
package serviceregistration |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/grafana/grafana/pkg/plugins/oauth" |
||||
"github.com/grafana/grafana/pkg/services/oauthserver" |
||||
) |
||||
|
||||
type Service struct { |
||||
os oauthserver.OAuth2Server |
||||
} |
||||
|
||||
func ProvideService(os oauthserver.OAuth2Server) *Service { |
||||
s := &Service{ |
||||
os: os, |
||||
} |
||||
return s |
||||
} |
||||
|
||||
// RegisterExternalService is a simplified wrapper around SaveExternalService for the plugin use case.
|
||||
func (s *Service) RegisterExternalService(ctx context.Context, svcName string, svc *oauth.ExternalServiceRegistration) (*oauth.ExternalService, error) { |
||||
impersonation := oauthserver.ImpersonationCfg{} |
||||
if svc.Impersonation != nil { |
||||
impersonation.Permissions = svc.Impersonation.Permissions |
||||
if svc.Impersonation.Enabled != nil { |
||||
impersonation.Enabled = *svc.Impersonation.Enabled |
||||
} else { |
||||
impersonation.Enabled = true |
||||
} |
||||
if svc.Impersonation.Groups != nil { |
||||
impersonation.Groups = *svc.Impersonation.Groups |
||||
} else { |
||||
impersonation.Groups = true |
||||
} |
||||
} |
||||
|
||||
self := oauthserver.SelfCfg{} |
||||
if svc.Self != nil { |
||||
self.Permissions = svc.Self.Permissions |
||||
if svc.Self.Enabled != nil { |
||||
self.Enabled = *svc.Self.Enabled |
||||
} else { |
||||
self.Enabled = true |
||||
} |
||||
} |
||||
extSvc, err := s.os.SaveExternalService(ctx, &oauthserver.ExternalServiceRegistration{ |
||||
Name: svcName, |
||||
Impersonation: impersonation, |
||||
Self: self, |
||||
Key: &oauthserver.KeyOption{Generate: true}, |
||||
}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &oauth.ExternalService{ |
||||
ClientID: extSvc.ID, |
||||
ClientSecret: extSvc.Secret, |
||||
PrivateKey: extSvc.KeyResult.PrivatePem, |
||||
}, nil |
||||
} |
Loading…
Reference in new issue