Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock goth/gothic and Re-attempt OAuth2 registration on login if registration failed at startup (#16564) #16570

Merged
merged 3 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions models/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ func initOAuth2LoginSources() error {
err := oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping)
if err != nil {
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
}
}
}
return nil
Expand Down
21 changes: 21 additions & 0 deletions modules/auth/oauth2/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package oauth2
import (
"net/http"
"net/url"
"sync"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -34,6 +35,7 @@ import (
var (
sessionUsersStoreKey = "gitea-oauth2-sessions"
providerHeaderKey = "gitea-oauth2-provider"
gothRWMutex = sync.RWMutex{}
)

// CustomURLMapping describes the urls values to use when customizing OAuth2 provider URLs
Expand All @@ -60,6 +62,10 @@ func Init(x *xorm.Engine) error {

// Note, when using the FilesystemStore only the session.ID is written to a browser cookie, so this is explicit for the storage on disk
store.MaxLength(setting.OAuth2.MaxTokenLength)

gothRWMutex.Lock()
defer gothRWMutex.Unlock()

gothic.Store = store

gothic.SetState = func(req *http.Request) string {
Expand All @@ -82,6 +88,9 @@ func Auth(provider string, request *http.Request, response http.ResponseWriter)
// normally the gothic library will write some custom stuff to the response instead of our own nice error page
//gothic.BeginAuthHandler(response, request)

gothRWMutex.RLock()
defer gothRWMutex.RUnlock()

url, err := gothic.GetAuthURL(response, request)
if err == nil {
http.Redirect(response, request, url, http.StatusTemporaryRedirect)
Expand All @@ -95,6 +104,9 @@ func ProviderCallback(provider string, request *http.Request, response http.Resp
// not sure if goth is thread safe (?) when using multiple providers
request.Header.Set(providerHeaderKey, provider)

gothRWMutex.RLock()
defer gothRWMutex.RUnlock()

user, err := gothic.CompleteUserAuth(response, request)
if err != nil {
return user, err
Expand All @@ -108,6 +120,9 @@ func RegisterProvider(providerName, providerType, clientID, clientSecret, openID
provider, err := createProvider(providerName, providerType, clientID, clientSecret, openIDConnectAutoDiscoveryURL, customURLMapping)

if err == nil && provider != nil {
gothRWMutex.Lock()
defer gothRWMutex.Unlock()

goth.UseProviders(provider)
}

Expand All @@ -116,11 +131,17 @@ func RegisterProvider(providerName, providerType, clientID, clientSecret, openID

// RemoveProvider removes the given OAuth2 provider from the goth lib
func RemoveProvider(providerName string) {
gothRWMutex.Lock()
defer gothRWMutex.Unlock()

delete(goth.GetProviders(), providerName)
}

// ClearProviders clears all OAuth2 providers from the goth lib
func ClearProviders() {
gothRWMutex.Lock()
defer gothRWMutex.Unlock()

goth.ClearProviders()
}

Expand Down