2017-05-01 13:26:53 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
2017-05-04 05:54:56 +00:00
|
|
|
|
2017-05-01 13:26:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/auth/oauth2"
|
2021-02-26 03:20:58 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2017-05-01 13:26:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// OAuth2Provider describes the display values of a single OAuth2 provider
|
|
|
|
type OAuth2Provider struct {
|
2017-05-04 05:54:56 +00:00
|
|
|
Name string
|
|
|
|
DisplayName string
|
|
|
|
Image string
|
|
|
|
CustomURLMapping *oauth2.CustomURLMapping
|
2017-05-01 13:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2Providers contains the map of registered OAuth2 providers in Gitea (based on goth)
|
|
|
|
// key is used to map the OAuth2Provider with the goth provider type (also in LoginSource.OAuth2Config.Provider)
|
|
|
|
// value is used to store display data
|
|
|
|
var OAuth2Providers = map[string]OAuth2Provider{
|
2021-04-28 12:35:06 +00:00
|
|
|
"bitbucket": {Name: "bitbucket", DisplayName: "Bitbucket", Image: "/assets/img/auth/bitbucket.png"},
|
|
|
|
"dropbox": {Name: "dropbox", DisplayName: "Dropbox", Image: "/assets/img/auth/dropbox.png"},
|
|
|
|
"facebook": {Name: "facebook", DisplayName: "Facebook", Image: "/assets/img/auth/facebook.png"},
|
2021-03-14 18:52:12 +00:00
|
|
|
"github": {
|
2021-04-28 12:35:06 +00:00
|
|
|
Name: "github", DisplayName: "GitHub", Image: "/assets/img/auth/github.png",
|
2017-05-01 13:26:53 +00:00
|
|
|
CustomURLMapping: &oauth2.CustomURLMapping{
|
|
|
|
TokenURL: oauth2.GetDefaultTokenURL("github"),
|
|
|
|
AuthURL: oauth2.GetDefaultAuthURL("github"),
|
|
|
|
ProfileURL: oauth2.GetDefaultProfileURL("github"),
|
|
|
|
EmailURL: oauth2.GetDefaultEmailURL("github"),
|
|
|
|
},
|
|
|
|
},
|
2021-03-14 18:52:12 +00:00
|
|
|
"gitlab": {
|
2021-04-28 12:35:06 +00:00
|
|
|
Name: "gitlab", DisplayName: "GitLab", Image: "/assets/img/auth/gitlab.png",
|
2017-05-01 13:26:53 +00:00
|
|
|
CustomURLMapping: &oauth2.CustomURLMapping{
|
|
|
|
TokenURL: oauth2.GetDefaultTokenURL("gitlab"),
|
|
|
|
AuthURL: oauth2.GetDefaultAuthURL("gitlab"),
|
|
|
|
ProfileURL: oauth2.GetDefaultProfileURL("gitlab"),
|
|
|
|
},
|
|
|
|
},
|
2021-04-28 12:35:06 +00:00
|
|
|
"gplus": {Name: "gplus", DisplayName: "Google", Image: "/assets/img/auth/google.png"},
|
|
|
|
"openidConnect": {Name: "openidConnect", DisplayName: "OpenID Connect", Image: "/assets/img/auth/openid_connect.svg"},
|
|
|
|
"twitter": {Name: "twitter", DisplayName: "Twitter", Image: "/assets/img/auth/twitter.png"},
|
|
|
|
"discord": {Name: "discord", DisplayName: "Discord", Image: "/assets/img/auth/discord.png"},
|
2021-03-14 18:52:12 +00:00
|
|
|
"gitea": {
|
2021-04-28 12:35:06 +00:00
|
|
|
Name: "gitea", DisplayName: "Gitea", Image: "/assets/img/auth/gitea.png",
|
2019-09-13 02:15:36 +00:00
|
|
|
CustomURLMapping: &oauth2.CustomURLMapping{
|
|
|
|
TokenURL: oauth2.GetDefaultTokenURL("gitea"),
|
|
|
|
AuthURL: oauth2.GetDefaultAuthURL("gitea"),
|
|
|
|
ProfileURL: oauth2.GetDefaultProfileURL("gitea"),
|
|
|
|
},
|
|
|
|
},
|
2021-03-14 18:52:12 +00:00
|
|
|
"nextcloud": {
|
2021-04-28 12:35:06 +00:00
|
|
|
Name: "nextcloud", DisplayName: "Nextcloud", Image: "/assets/img/auth/nextcloud.png",
|
2020-03-03 03:11:45 +00:00
|
|
|
CustomURLMapping: &oauth2.CustomURLMapping{
|
|
|
|
TokenURL: oauth2.GetDefaultTokenURL("nextcloud"),
|
|
|
|
AuthURL: oauth2.GetDefaultAuthURL("nextcloud"),
|
|
|
|
ProfileURL: oauth2.GetDefaultProfileURL("nextcloud"),
|
|
|
|
},
|
|
|
|
},
|
2021-04-28 12:35:06 +00:00
|
|
|
"yandex": {Name: "yandex", DisplayName: "Yandex", Image: "/assets/img/auth/yandex.png"},
|
2021-03-14 18:52:12 +00:00
|
|
|
"mastodon": {
|
2021-04-28 12:35:06 +00:00
|
|
|
Name: "mastodon", DisplayName: "Mastodon", Image: "/assets/img/auth/mastodon.png",
|
2020-10-25 18:21:49 +00:00
|
|
|
CustomURLMapping: &oauth2.CustomURLMapping{
|
|
|
|
AuthURL: oauth2.GetDefaultAuthURL("mastodon"),
|
|
|
|
},
|
|
|
|
},
|
2017-05-01 13:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2DefaultCustomURLMappings contains the map of default URL's for OAuth2 providers that are allowed to have custom urls
|
|
|
|
// key is used to map the OAuth2Provider
|
|
|
|
// value is the mapping as defined for the OAuth2Provider
|
2017-05-04 05:54:56 +00:00
|
|
|
var OAuth2DefaultCustomURLMappings = map[string]*oauth2.CustomURLMapping{
|
2020-03-03 03:11:45 +00:00
|
|
|
"github": OAuth2Providers["github"].CustomURLMapping,
|
|
|
|
"gitlab": OAuth2Providers["gitlab"].CustomURLMapping,
|
|
|
|
"gitea": OAuth2Providers["gitea"].CustomURLMapping,
|
|
|
|
"nextcloud": OAuth2Providers["nextcloud"].CustomURLMapping,
|
2020-10-25 18:21:49 +00:00
|
|
|
"mastodon": OAuth2Providers["mastodon"].CustomURLMapping,
|
2017-05-01 13:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetActiveOAuth2ProviderLoginSources returns all actived LoginOAuth2 sources
|
|
|
|
func GetActiveOAuth2ProviderLoginSources() ([]*LoginSource, error) {
|
|
|
|
sources := make([]*LoginSource, 0, 1)
|
2017-08-01 05:47:31 +00:00
|
|
|
if err := x.Where("is_actived = ? and type = ?", true, LoginOAuth2).Find(&sources); err != nil {
|
2017-05-01 13:26:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetActiveOAuth2LoginSourceByName returns a OAuth2 LoginSource based on the given name
|
|
|
|
func GetActiveOAuth2LoginSourceByName(name string) (*LoginSource, error) {
|
2017-08-01 05:47:31 +00:00
|
|
|
loginSource := new(LoginSource)
|
|
|
|
has, err := x.Where("name = ? and type = ? and is_actived = ?", name, LoginOAuth2, true).Get(loginSource)
|
2017-05-01 13:26:53 +00:00
|
|
|
if !has || err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return loginSource, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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]OAuth2Provider, error) {
|
|
|
|
// Maybe also separate used and unused providers so we can force the registration of only 1 active provider for each type
|
|
|
|
|
|
|
|
loginSources, err := GetActiveOAuth2ProviderLoginSources()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var orderedKeys []string
|
|
|
|
providers := make(map[string]OAuth2Provider)
|
|
|
|
for _, source := range loginSources {
|
2020-12-28 02:35:55 +00:00
|
|
|
prov := OAuth2Providers[source.OAuth2().Provider]
|
|
|
|
if source.OAuth2().IconURL != "" {
|
|
|
|
prov.Image = source.OAuth2().IconURL
|
|
|
|
}
|
|
|
|
providers[source.Name] = prov
|
2017-05-01 13:26:53 +00:00
|
|
|
orderedKeys = append(orderedKeys, source.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(orderedKeys)
|
|
|
|
|
|
|
|
return orderedKeys, providers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitOAuth2 initialize the OAuth2 lib and register all active OAuth2 providers in the library
|
2018-04-29 06:09:24 +00:00
|
|
|
func InitOAuth2() error {
|
2021-06-17 21:56:46 +00:00
|
|
|
if err := oauth2.InitSigningKey(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-29 06:09:24 +00:00
|
|
|
if err := oauth2.Init(x); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-24 19:47:17 +00:00
|
|
|
return initOAuth2LoginSources()
|
|
|
|
}
|
2017-05-01 13:26:53 +00:00
|
|
|
|
2020-12-24 19:47:17 +00:00
|
|
|
// ResetOAuth2 clears existing OAuth2 providers and loads them from DB
|
|
|
|
func ResetOAuth2() error {
|
|
|
|
oauth2.ClearProviders()
|
|
|
|
return initOAuth2LoginSources()
|
|
|
|
}
|
|
|
|
|
|
|
|
// initOAuth2LoginSources is used to load and register all active OAuth2 providers
|
|
|
|
func initOAuth2LoginSources() error {
|
|
|
|
loginSources, _ := GetActiveOAuth2ProviderLoginSources()
|
2017-05-01 13:26:53 +00:00
|
|
|
for _, source := range loginSources {
|
|
|
|
oAuth2Config := source.OAuth2()
|
2019-06-12 19:41:28 +00:00
|
|
|
err := oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping)
|
|
|
|
if err != nil {
|
2021-02-26 03:20:58 +00:00
|
|
|
log.Critical("Unable to register source: %s due to Error: %v. This source will be disabled.", source.Name, err)
|
|
|
|
source.IsActived = false
|
|
|
|
if err = UpdateSource(source); err != nil {
|
|
|
|
log.Critical("Unable to update source %s to disable it. Error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-06-12 19:41:28 +00:00
|
|
|
}
|
2017-05-01 13:26:53 +00:00
|
|
|
}
|
2018-04-29 06:09:24 +00:00
|
|
|
return nil
|
2017-05-01 13:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrapOpenIDConnectInitializeError is used to wrap the error but this cannot be done in modules/auth/oauth2
|
|
|
|
// inside oauth2: import cycle not allowed models -> modules/auth/oauth2 -> models
|
|
|
|
func wrapOpenIDConnectInitializeError(err error, providerName string, oAuth2Config *OAuth2Config) error {
|
|
|
|
if err != nil && "openidConnect" == oAuth2Config.Provider {
|
|
|
|
err = ErrOpenIDConnectInitialize{ProviderName: providerName, OpenIDConnectAutoDiscoveryURL: oAuth2Config.OpenIDConnectAutoDiscoveryURL, Cause: err}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|