2021-07-24 06:16:34 -04:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-02-22 02:14:37 -05:00
|
|
|
|
|
|
|
package oauth2
|
|
|
|
|
|
|
|
import (
|
2021-11-24 04:49:20 -05:00
|
|
|
"errors"
|
2020-08-30 18:55:01 -04:00
|
|
|
"net/url"
|
2021-07-24 06:16:34 -04:00
|
|
|
"sort"
|
2017-03-03 03:21:31 -05:00
|
|
|
|
2022-01-02 08:12:35 -05:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2017-02-22 02:14:37 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-03-03 03:21:31 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2017-02-22 02:14:37 -05:00
|
|
|
"github.com/markbates/goth"
|
|
|
|
)
|
|
|
|
|
2021-08-05 21:11:08 -04:00
|
|
|
// Provider is an interface for describing a single OAuth2 provider
|
|
|
|
type Provider interface {
|
|
|
|
Name() string
|
|
|
|
DisplayName() string
|
|
|
|
Image() string
|
|
|
|
CustomURLSettings() *CustomURLSettings
|
|
|
|
}
|
|
|
|
|
|
|
|
// GothProviderCreator provides a function to create a goth.Provider
|
|
|
|
type GothProviderCreator interface {
|
|
|
|
CreateGothProvider(providerName, callbackURL string, source *Source) (goth.Provider, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GothProvider is an interface for describing a single OAuth2 provider
|
|
|
|
type GothProvider interface {
|
|
|
|
Provider
|
|
|
|
GothProviderCreator
|
|
|
|
}
|
|
|
|
|
Fix various typos (#20338)
* Fix various typos
Found via `codespell -q 3 -S ./options/locale,./options/license,./public/vendor -L actived,allways,attachements,ba,befores,commiter,pullrequest,pullrequests,readby,splitted,te,unknwon`
Co-authored-by: zeripath <art27@cantab.net>
2022-07-12 17:32:37 -04:00
|
|
|
// ImagedProvider provide an overridden image setting for the provider
|
2021-08-05 21:11:08 -04:00
|
|
|
type ImagedProvider struct {
|
|
|
|
GothProvider
|
|
|
|
image string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Image returns the image path for this provider
|
|
|
|
func (i *ImagedProvider) Image() string {
|
|
|
|
return i.image
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewImagedProvider is a constructor function for the ImagedProvider
|
|
|
|
func NewImagedProvider(image string, provider GothProvider) *ImagedProvider {
|
|
|
|
return &ImagedProvider{
|
|
|
|
GothProvider: provider,
|
|
|
|
image: image,
|
|
|
|
}
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
2017-02-22 02:14:37 -05:00
|
|
|
|
2021-07-24 06:16:34 -04:00
|
|
|
// Providers contains the map of registered OAuth2 providers in Gitea (based on goth)
|
2022-01-02 08:12:35 -05:00
|
|
|
// key is used to map the OAuth2Provider with the goth provider type (also in AuthSource.OAuth2Config.Provider)
|
2021-07-24 06:16:34 -04:00
|
|
|
// value is used to store display data
|
2021-08-05 21:11:08 -04:00
|
|
|
var gothProviders = map[string]GothProvider{}
|
|
|
|
|
|
|
|
// RegisterGothProvider registers a GothProvider
|
|
|
|
func RegisterGothProvider(provider GothProvider) {
|
|
|
|
if _, has := gothProviders[provider.Name()]; has {
|
|
|
|
log.Fatal("Duplicate oauth2provider type provided: %s", provider.Name())
|
|
|
|
}
|
|
|
|
gothProviders[provider.Name()] = provider
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOAuth2Providers returns the map of unconfigured OAuth2 providers
|
|
|
|
// key is used as technical name (like in the callbackURL)
|
|
|
|
// values to display
|
|
|
|
func GetOAuth2Providers() []Provider {
|
|
|
|
providers := make([]Provider, 0, len(gothProviders))
|
|
|
|
|
|
|
|
for _, provider := range gothProviders {
|
|
|
|
providers = append(providers, provider)
|
|
|
|
}
|
|
|
|
sort.Slice(providers, func(i, j int) bool {
|
|
|
|
return providers[i].Name() < providers[j].Name()
|
|
|
|
})
|
|
|
|
return providers
|
2017-05-01 09:26:53 -04:00
|
|
|
}
|
|
|
|
|
2021-07-24 06:16:34 -04:00
|
|
|
// GetActiveOAuth2Providers returns the map of configured active OAuth2 providers
|
|
|
|
// key is used as technical name (like in the callbackURL)
|
|
|
|
// values to display
|
|
|
|
func GetActiveOAuth2Providers() ([]string, map[string]Provider, error) {
|
|
|
|
// Maybe also separate used and unused providers so we can force the registration of only 1 active provider for each type
|
2017-02-22 02:14:37 -05:00
|
|
|
|
2022-01-02 08:12:35 -05:00
|
|
|
authSources, err := auth.GetActiveOAuth2ProviderSources()
|
2018-04-29 02:09:24 -04:00
|
|
|
if err != nil {
|
2021-07-24 06:16:34 -04:00
|
|
|
return nil, nil, err
|
2017-02-22 02:14:37 -05:00
|
|
|
}
|
|
|
|
|
2021-07-24 06:16:34 -04:00
|
|
|
var orderedKeys []string
|
|
|
|
providers := make(map[string]Provider)
|
2022-01-02 08:12:35 -05:00
|
|
|
for _, source := range authSources {
|
2021-08-05 21:11:08 -04:00
|
|
|
prov := gothProviders[source.Cfg.(*Source).Provider]
|
2021-07-24 06:16:34 -04:00
|
|
|
if source.Cfg.(*Source).IconURL != "" {
|
2021-08-05 21:11:08 -04:00
|
|
|
prov = &ImagedProvider{prov, source.Cfg.(*Source).IconURL}
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
providers[source.Name] = prov
|
|
|
|
orderedKeys = append(orderedKeys, source.Name)
|
2017-02-22 02:14:37 -05:00
|
|
|
}
|
|
|
|
|
2021-07-24 06:16:34 -04:00
|
|
|
sort.Strings(orderedKeys)
|
2017-02-22 02:14:37 -05:00
|
|
|
|
2021-07-24 06:16:34 -04:00
|
|
|
return orderedKeys, providers, nil
|
2017-02-22 02:14:37 -05:00
|
|
|
}
|
|
|
|
|
2021-08-05 21:11:08 -04:00
|
|
|
// RegisterProviderWithGothic register a OAuth2 provider in goth lib
|
|
|
|
func RegisterProviderWithGothic(providerName string, source *Source) error {
|
|
|
|
provider, err := createProvider(providerName, source)
|
2017-02-22 02:14:37 -05:00
|
|
|
|
2017-05-01 09:26:53 -04:00
|
|
|
if err == nil && provider != nil {
|
2021-07-29 13:53:18 -04:00
|
|
|
gothRWMutex.Lock()
|
|
|
|
defer gothRWMutex.Unlock()
|
|
|
|
|
2017-02-22 02:14:37 -05:00
|
|
|
goth.UseProviders(provider)
|
|
|
|
}
|
2017-05-01 09:26:53 -04:00
|
|
|
|
|
|
|
return err
|
2017-02-22 02:14:37 -05:00
|
|
|
}
|
|
|
|
|
2021-08-05 21:11:08 -04:00
|
|
|
// RemoveProviderFromGothic removes the given OAuth2 provider from the goth lib
|
|
|
|
func RemoveProviderFromGothic(providerName string) {
|
2021-07-29 13:53:18 -04:00
|
|
|
gothRWMutex.Lock()
|
|
|
|
defer gothRWMutex.Unlock()
|
|
|
|
|
2017-02-22 02:14:37 -05:00
|
|
|
delete(goth.GetProviders(), providerName)
|
|
|
|
}
|
|
|
|
|
2020-12-24 14:47:17 -05:00
|
|
|
// ClearProviders clears all OAuth2 providers from the goth lib
|
|
|
|
func ClearProviders() {
|
2021-07-29 13:53:18 -04:00
|
|
|
gothRWMutex.Lock()
|
|
|
|
defer gothRWMutex.Unlock()
|
|
|
|
|
2020-12-24 14:47:17 -05:00
|
|
|
goth.ClearProviders()
|
|
|
|
}
|
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
// ErrAuthSourceNotActived login source is not actived error
|
|
|
|
var ErrAuthSourceNotActived = errors.New("auth source is not actived")
|
2021-11-24 04:49:20 -05:00
|
|
|
|
2017-02-22 02:14:37 -05:00
|
|
|
// used to create different types of goth providers
|
2021-08-05 21:11:08 -04:00
|
|
|
func createProvider(providerName string, source *Source) (goth.Provider, error) {
|
2020-08-30 18:55:01 -04:00
|
|
|
callbackURL := setting.AppURL + "user/oauth2/" + url.PathEscape(providerName) + "/callback"
|
2017-02-22 02:14:37 -05:00
|
|
|
|
|
|
|
var provider goth.Provider
|
2017-05-01 09:26:53 -04:00
|
|
|
var err error
|
2017-02-22 02:14:37 -05:00
|
|
|
|
2021-08-05 21:11:08 -04:00
|
|
|
p, ok := gothProviders[source.Provider]
|
|
|
|
if !ok {
|
2022-01-02 08:12:35 -05:00
|
|
|
return nil, ErrAuthSourceNotActived
|
2021-08-05 21:11:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
provider, err = p.CreateGothProvider(providerName, callbackURL, source)
|
|
|
|
if err != nil {
|
|
|
|
return provider, err
|
2017-02-22 02:14:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// always set the name if provider is created so we can support multiple setups of 1 provider
|
2017-05-01 09:26:53 -04:00
|
|
|
if err == nil && provider != nil {
|
2017-02-22 02:14:37 -05:00
|
|
|
provider.SetName(providerName)
|
|
|
|
}
|
|
|
|
|
2017-05-01 09:26:53 -04:00
|
|
|
return provider, err
|
|
|
|
}
|