From 0e189eecaaedc4e2c6ee13882c7b76645af50ccd Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 11 Nov 2021 01:51:09 +0800 Subject: [PATCH] Move oauth2 error to oauth2 service package (#17603) --- models/error_oauth2.go | 24 ------------------- routers/web/admin/auths.go | 3 +-- .../auth/source/oauth2/source_register.go | 21 ++++++++++++++-- 3 files changed, 20 insertions(+), 28 deletions(-) delete mode 100644 models/error_oauth2.go diff --git a/models/error_oauth2.go b/models/error_oauth2.go deleted file mode 100644 index a25be896cc..0000000000 --- a/models/error_oauth2.go +++ /dev/null @@ -1,24 +0,0 @@ -// 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 "fmt" - -// ErrOpenIDConnectInitialize represents a "OpenIDConnectInitialize" kind of error. -type ErrOpenIDConnectInitialize struct { - OpenIDConnectAutoDiscoveryURL string - ProviderName string - Cause error -} - -// IsErrOpenIDConnectInitialize checks if an error is a ExternalLoginUserAlreadyExist. -func IsErrOpenIDConnectInitialize(err error) bool { - _, ok := err.(ErrOpenIDConnectInitialize) - return ok -} - -func (err ErrOpenIDConnectInitialize) Error() string { - return fmt.Sprintf("Failed to initialize OpenID Connect Provider with name '%s' with url '%s': %v", err.ProviderName, err.OpenIDConnectAutoDiscoveryURL, err.Cause) -} diff --git a/routers/web/admin/auths.go b/routers/web/admin/auths.go index 803dcafa28..460b740171 100644 --- a/routers/web/admin/auths.go +++ b/routers/web/admin/auths.go @@ -10,7 +10,6 @@ import ( "net/http" "regexp" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/login" "code.gitea.io/gitea/modules/auth/pam" "code.gitea.io/gitea/modules/base" @@ -386,7 +385,7 @@ func EditAuthSourcePost(ctx *context.Context) { source.IsSyncEnabled = form.IsSyncEnabled source.Cfg = config if err := login.UpdateSource(source); err != nil { - if models.IsErrOpenIDConnectInitialize(err) { + if oauth2.IsErrOpenIDConnectInitialize(err) { ctx.Flash.Error(err.Error(), true) ctx.HTML(http.StatusOK, tplAuthEdit) } else { diff --git a/services/auth/source/oauth2/source_register.go b/services/auth/source/oauth2/source_register.go index 24c61a9a56..71ba288bf4 100644 --- a/services/auth/source/oauth2/source_register.go +++ b/services/auth/source/oauth2/source_register.go @@ -5,7 +5,7 @@ package oauth2 import ( - "code.gitea.io/gitea/models" + "fmt" ) // RegisterSource causes an OAuth2 configuration to be registered @@ -20,11 +20,28 @@ func (source *Source) UnregisterSource() error { return nil } +// ErrOpenIDConnectInitialize represents a "OpenIDConnectInitialize" kind of error. +type ErrOpenIDConnectInitialize struct { + OpenIDConnectAutoDiscoveryURL string + ProviderName string + Cause error +} + +// IsErrOpenIDConnectInitialize checks if an error is a ExternalLoginUserAlreadyExist. +func IsErrOpenIDConnectInitialize(err error) bool { + _, ok := err.(ErrOpenIDConnectInitialize) + return ok +} + +func (err ErrOpenIDConnectInitialize) Error() string { + return fmt.Sprintf("Failed to initialize OpenID Connect Provider with name '%s' with url '%s': %v", err.ProviderName, err.OpenIDConnectAutoDiscoveryURL, err.Cause) +} + // 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, source *Source) error { if err != nil && source.Provider == "openidConnect" { - err = models.ErrOpenIDConnectInitialize{ProviderName: providerName, OpenIDConnectAutoDiscoveryURL: source.OpenIDConnectAutoDiscoveryURL, Cause: err} + err = ErrOpenIDConnectInitialize{ProviderName: providerName, OpenIDConnectAutoDiscoveryURL: source.OpenIDConnectAutoDiscoveryURL, Cause: err} } return err }