@@ -121,9 +121,11 @@ func (cfg *PAMConfig) ToDB() ([]byte, error) {
121121
122122// OAuth2Config holds configuration for the OAuth2 login source.
123123type OAuth2Config struct {
124- Provider string
125- ClientID string
126- ClientSecret string
124+ Provider string
125+ ClientID string
126+ ClientSecret string
127+ OpenIDConnectAutoDiscoveryURL string
128+ CustomURLMapping * oauth2.CustomURLMapping
127129}
128130
129131// FromDB fills up an OAuth2Config from serialized format.
@@ -294,9 +296,15 @@ func CreateLoginSource(source *LoginSource) error {
294296 }
295297
296298 _ , err = x .Insert (source )
297- if err == nil && source .IsOAuth2 () {
299+ if err == nil && source .IsOAuth2 () && source . IsActived {
298300 oAuth2Config := source .OAuth2 ()
299- oauth2 .RegisterProvider (source .Name , oAuth2Config .Provider , oAuth2Config .ClientID , oAuth2Config .ClientSecret )
301+ err = oauth2 .RegisterProvider (source .Name , oAuth2Config .Provider , oAuth2Config .ClientID , oAuth2Config .ClientSecret , oAuth2Config .OpenIDConnectAutoDiscoveryURL , oAuth2Config .CustomURLMapping )
302+ err = wrapOpenIDConnectInitializeError (err , source .Name , oAuth2Config )
303+
304+ if err != nil {
305+ // remove the LoginSource in case of errors while registering OAuth2 providers
306+ x .Delete (source )
307+ }
300308 }
301309 return err
302310}
@@ -321,11 +329,25 @@ func GetLoginSourceByID(id int64) (*LoginSource, error) {
321329
322330// UpdateSource updates a LoginSource record in DB.
323331func UpdateSource (source * LoginSource ) error {
332+ var originalLoginSource * LoginSource
333+ if source .IsOAuth2 () {
334+ // keep track of the original values so we can restore in case of errors while registering OAuth2 providers
335+ var err error
336+ if originalLoginSource , err = GetLoginSourceByID (source .ID ); err != nil {
337+ return err
338+ }
339+ }
340+
324341 _ , err := x .Id (source .ID ).AllCols ().Update (source )
325- if err == nil && source .IsOAuth2 () {
342+ if err == nil && source .IsOAuth2 () && source . IsActived {
326343 oAuth2Config := source .OAuth2 ()
327- oauth2 .RemoveProvider (source .Name )
328- oauth2 .RegisterProvider (source .Name , oAuth2Config .Provider , oAuth2Config .ClientID , oAuth2Config .ClientSecret )
344+ err = oauth2 .RegisterProvider (source .Name , oAuth2Config .Provider , oAuth2Config .ClientID , oAuth2Config .ClientSecret , oAuth2Config .OpenIDConnectAutoDiscoveryURL , oAuth2Config .CustomURLMapping )
345+ err = wrapOpenIDConnectInitializeError (err , source .Name , oAuth2Config )
346+
347+ if err != nil {
348+ // restore original values since we cannot update the provider it self
349+ x .Id (source .ID ).AllCols ().Update (originalLoginSource )
350+ }
329351 }
330352 return err
331353}
@@ -580,27 +602,6 @@ func LoginViaPAM(user *User, login, password string, sourceID int64, cfg *PAMCon
580602 return user , CreateUser (user )
581603}
582604
583- // ________ _____ __ .__ ________
584- // \_____ \ / _ \ __ ___/ |_| |__ \_____ \
585- // / | \ / /_\ \| | \ __\ | \ / ____/
586- // / | \/ | \ | /| | | Y \/ \
587- // \_______ /\____|__ /____/ |__| |___| /\_______ \
588- // \/ \/ \/ \/
589-
590- // OAuth2Provider describes the display values of a single OAuth2 provider
591- type OAuth2Provider struct {
592- Name string
593- DisplayName string
594- Image string
595- }
596-
597- // OAuth2Providers contains the map of registered OAuth2 providers in Gitea (based on goth)
598- // key is used to map the OAuth2Provider with the goth provider type (also in LoginSource.OAuth2Config.Provider)
599- // value is used to store display data
600- var OAuth2Providers = map [string ]OAuth2Provider {
601- "github" : {Name : "github" , DisplayName : "GitHub" , Image : "/img/github.png" },
602- }
603-
604605// ExternalUserLogin attempts a login using external source types.
605606func ExternalUserLogin (user * User , login , password string , source * LoginSource , autoRegister bool ) (* User , error ) {
606607 if ! source .IsActived {
@@ -684,59 +685,4 @@ func UserSignIn(username, password string) (*User, error) {
684685 }
685686
686687 return nil , ErrUserNotExist {user .ID , user .Name , 0 }
687- }
688-
689- // GetActiveOAuth2ProviderLoginSources returns all actived LoginOAuth2 sources
690- func GetActiveOAuth2ProviderLoginSources () ([]* LoginSource , error ) {
691- sources := make ([]* LoginSource , 0 , 1 )
692- if err := x .UseBool ().Find (& sources , & LoginSource {IsActived : true , Type : LoginOAuth2 }); err != nil {
693- return nil , err
694- }
695- return sources , nil
696- }
697-
698- // GetActiveOAuth2LoginSourceByName returns a OAuth2 LoginSource based on the given name
699- func GetActiveOAuth2LoginSourceByName (name string ) (* LoginSource , error ) {
700- loginSource := & LoginSource {
701- Name : name ,
702- Type : LoginOAuth2 ,
703- IsActived : true ,
704- }
705-
706- has , err := x .UseBool ().Get (loginSource )
707- if ! has || err != nil {
708- return nil , err
709- }
710-
711- return loginSource , nil
712- }
713-
714- // GetActiveOAuth2Providers returns the map of configured active OAuth2 providers
715- // key is used as technical name (like in the callbackURL)
716- // values to display
717- func GetActiveOAuth2Providers () (map [string ]OAuth2Provider , error ) {
718- // Maybe also separate used and unused providers so we can force the registration of only 1 active provider for each type
719-
720- loginSources , err := GetActiveOAuth2ProviderLoginSources ()
721- if err != nil {
722- return nil , err
723- }
724-
725- providers := make (map [string ]OAuth2Provider )
726- for _ , source := range loginSources {
727- providers [source .Name ] = OAuth2Providers [source .OAuth2 ().Provider ]
728- }
729-
730- return providers , nil
731- }
732-
733- // InitOAuth2 initialize the OAuth2 lib and register all active OAuth2 providers in the library
734- func InitOAuth2 () {
735- oauth2 .Init ()
736- loginSources , _ := GetActiveOAuth2ProviderLoginSources ()
737-
738- for _ , source := range loginSources {
739- oAuth2Config := source .OAuth2 ()
740- oauth2 .RegisterProvider (source .Name , oAuth2Config .Provider , oAuth2Config .ClientID , oAuth2Config .ClientSecret )
741- }
742- }
688+ }
0 commit comments