aboutsummaryrefslogtreecommitdiffstats
path: root/services/auth/source/oauth2/providers.go
diff options
context:
space:
mode:
Diffstat (limited to 'services/auth/source/oauth2/providers.go')
-rw-r--r--services/auth/source/oauth2/providers.go44
1 files changed, 25 insertions, 19 deletions
diff --git a/services/auth/source/oauth2/providers.go b/services/auth/source/oauth2/providers.go
index 7ba370855a..0aa83ba494 100644
--- a/services/auth/source/oauth2/providers.go
+++ b/services/auth/source/oauth2/providers.go
@@ -19,7 +19,7 @@ import (
type Provider interface {
Name() string
DisplayName() string
- Image() string
+ IconURL() string
CustomURLSettings() *CustomURLSettings
}
@@ -34,23 +34,28 @@ type GothProvider interface {
GothProviderCreator
}
-// ImagedProvider provide an overridden image setting for the provider
-type ImagedProvider struct {
+// AuthSourceProvider provides a provider for an AuthSource. Multiple auth sources could use the same registered GothProvider
+// So each auth source should have its own DisplayName and IconURL for display.
+// The Name is the GothProvider's name, to help to find the GothProvider to sign in.
+// The DisplayName is the auth source config's name, site admin set it on the admin page, the IconURL can also be set there.
+type AuthSourceProvider struct {
GothProvider
- image string
+ sourceName, iconURL string
}
-// Image returns the image path for this provider
-func (i *ImagedProvider) Image() string {
- return i.image
+func (p *AuthSourceProvider) Name() string {
+ return p.GothProvider.Name()
}
-// NewImagedProvider is a constructor function for the ImagedProvider
-func NewImagedProvider(image string, provider GothProvider) *ImagedProvider {
- return &ImagedProvider{
- GothProvider: provider,
- image: image,
+func (p *AuthSourceProvider) DisplayName() string {
+ return p.sourceName
+}
+
+func (p *AuthSourceProvider) IconURL() string {
+ if p.iconURL != "" {
+ return p.iconURL
}
+ return p.GothProvider.IconURL()
}
// Providers contains the map of registered OAuth2 providers in Gitea (based on goth)
@@ -95,11 +100,13 @@ func GetActiveOAuth2Providers() ([]string, map[string]Provider, error) {
var orderedKeys []string
providers := make(map[string]Provider)
for _, source := range authSources {
- prov := gothProviders[source.Cfg.(*Source).Provider]
- if source.Cfg.(*Source).IconURL != "" {
- prov = &ImagedProvider{prov, source.Cfg.(*Source).IconURL}
+ oauth2Cfg, ok := source.Cfg.(*Source)
+ if !ok {
+ log.Error("Invalid OAuth2 source config: %v", oauth2Cfg)
+ continue
}
- providers[source.Name] = prov
+ gothProv := gothProviders[oauth2Cfg.Provider]
+ providers[source.Name] = &AuthSourceProvider{GothProvider: gothProv, sourceName: source.Name, iconURL: oauth2Cfg.IconURL}
orderedKeys = append(orderedKeys, source.Name)
}
@@ -138,8 +145,7 @@ func ClearProviders() {
goth.ClearProviders()
}
-// ErrAuthSourceNotActived login source is not actived error
-var ErrAuthSourceNotActived = errors.New("auth source is not actived")
+var ErrAuthSourceNotActivated = errors.New("auth source is not activated")
// used to create different types of goth providers
func createProvider(providerName string, source *Source) (goth.Provider, error) {
@@ -150,7 +156,7 @@ func createProvider(providerName string, source *Source) (goth.Provider, error)
p, ok := gothProviders[source.Provider]
if !ok {
- return nil, ErrAuthSourceNotActived
+ return nil, ErrAuthSourceNotActivated
}
provider, err = p.CreateGothProvider(providerName, callbackURL, source)