From 1802e6fa2e14ae31a7c6a7c68a0211f427cd00a6 Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Sat, 28 Jan 2023 10:07:13 +0800 Subject: [PATCH] refactor(plugin): Directory movement --- internal/controller/connector_controller.go | 3 +- .../controller_admin/plugin_controller.go | 2 +- internal/plugin/connector/google.go | 124 ------------------ internal/plugin/sample/filter_sample.go | 30 ----- internal/plugin/sample/filter_sample_test.go | 18 --- internal/plugin/sample/sample.go | 18 --- internal/router/plugin_api_router.go | 2 +- .../plugin_common/plugin_common_service.go | 2 +- pkg/checker/reserved_username.go | 2 - {internal/plugin => plugin}/base.go | 0 {internal/plugin => plugin}/config.go | 0 {internal/plugin => plugin}/connector.go | 0 .../plugin => plugin}/connector/github.go | 2 +- plugin/connector/google.go | 118 +++++++++++++++++ {internal/plugin => plugin}/filter.go | 0 {internal/plugin => plugin}/parser.go | 0 {internal/plugin => plugin}/plugin.go | 0 17 files changed, 123 insertions(+), 198 deletions(-) delete mode 100644 internal/plugin/connector/google.go delete mode 100644 internal/plugin/sample/filter_sample.go delete mode 100644 internal/plugin/sample/filter_sample_test.go delete mode 100644 internal/plugin/sample/sample.go rename {internal/plugin => plugin}/base.go (100%) rename {internal/plugin => plugin}/config.go (100%) rename {internal/plugin => plugin}/connector.go (100%) rename {internal/plugin => plugin}/connector/github.go (98%) create mode 100644 plugin/connector/google.go rename {internal/plugin => plugin}/filter.go (100%) rename {internal/plugin => plugin}/parser.go (100%) rename {internal/plugin => plugin}/plugin.go (100%) diff --git a/internal/controller/connector_controller.go b/internal/controller/connector_controller.go index df67b3f5..a736760c 100644 --- a/internal/controller/connector_controller.go +++ b/internal/controller/connector_controller.go @@ -6,12 +6,11 @@ import ( "github.com/answerdev/answer/internal/base/handler" "github.com/answerdev/answer/internal/base/middleware" - "github.com/answerdev/answer/internal/plugin" - _ "github.com/answerdev/answer/internal/plugin/connector" "github.com/answerdev/answer/internal/schema" "github.com/answerdev/answer/internal/service/export" "github.com/answerdev/answer/internal/service/siteinfo_common" "github.com/answerdev/answer/internal/service/user_external_login" + "github.com/answerdev/answer/plugin" "github.com/gin-gonic/gin" "github.com/segmentfault/pacman/log" ) diff --git a/internal/controller_admin/plugin_controller.go b/internal/controller_admin/plugin_controller.go index d22d11d8..e638d442 100644 --- a/internal/controller_admin/plugin_controller.go +++ b/internal/controller_admin/plugin_controller.go @@ -4,9 +4,9 @@ import ( "encoding/json" "github.com/answerdev/answer/internal/base/handler" - "github.com/answerdev/answer/internal/plugin" "github.com/answerdev/answer/internal/schema" "github.com/answerdev/answer/internal/service/plugin_common" + "github.com/answerdev/answer/plugin" "github.com/gin-gonic/gin" ) diff --git a/internal/plugin/connector/google.go b/internal/plugin/connector/google.go deleted file mode 100644 index 7dd2eae6..00000000 --- a/internal/plugin/connector/google.go +++ /dev/null @@ -1,124 +0,0 @@ -package connector - -import ( - "context" - "encoding/json" - "fmt" - "io" - "net/http" - "os" - "time" - - "github.com/answerdev/answer/internal/plugin" - "github.com/go-resty/resty/v2" - "golang.org/x/oauth2" - oauth2Google "golang.org/x/oauth2/google" -) - -type Google struct { - ClientID string - ClientSecret string -} - -func init() { - plugin.Register(&Google{ - ClientID: os.Getenv("GOOGLE_CLIENT_ID"), - ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"), - }) -} - -func (g *Google) Info() plugin.Info { - return plugin.Info{ - Name: "google connector", - SlugName: "google_connector", - Description: "google connector plugin", - Version: "0.0.1", - } -} - -func (g *Google) ConnectorLogo() []byte { - response, err := resty.New().R().Get("https://cdn-icons-png.flaticon.com/32/300/300221.png") - if err != nil { - return nil - } - return response.Body() -} - -func (g *Google) ConnectorLogoContentType() string { - return "image/png" -} - -func (g *Google) ConnectorName() string { - return "Google" -} - -func (g *Google) ConnectorSlugName() string { - return "google" -} - -func (g *Google) ConnectorSender(ctx *plugin.GinContext) { - oauth2Config := &oauth2.Config{ - ClientID: g.ClientID, - ClientSecret: g.ClientSecret, - Endpoint: oauth2Google.Endpoint, - RedirectURL: "http://127.0.0.1:8080/answer/api/v1/oauth/redirect/google", // TODO: Pass by parameter - Scopes: []string{ - "https://www.googleapis.com/auth/userinfo.email", - "https://www.googleapis.com/auth/userinfo.profile", - "openid", - }, - } - ctx.Redirect(http.StatusFound, oauth2Config.AuthCodeURL("state")) -} - -type GoogleAuthUserInfo struct { - Sub string `json:"sub"` - Name string `json:"name"` - GivenName string `json:"given_name"` - FamilyName string `json:"family_name"` - Profile string `json:"profile"` - Picture string `json:"picture"` - Email string `json:"email"` - EmailVerified bool `json:"email_verified"` - Gender string `json:"gender"` -} - -func (g *Google) ConnectorReceiver(ctx *plugin.GinContext) { - code := ctx.Query("code") - oauth2Config := &oauth2.Config{ - ClientID: g.ClientID, - ClientSecret: g.ClientSecret, - Endpoint: oauth2Google.Endpoint, - } - - token, err := oauth2Config.Exchange(context.Background(), code) - if err != nil { - ctx.Redirect(http.StatusFound, "/50x") - return - } - - client := oauth2Config.Client(context.TODO(), token) - client.Timeout = 60 * time.Second - userinfo, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo") - if err != nil { - ctx.Redirect(http.StatusFound, "/50x") - return - } - defer userinfo.Body.Close() - data, _ := io.ReadAll(userinfo.Body) - - userInfo := &GoogleAuthUserInfo{} - if err = json.Unmarshal(data, userInfo); err != nil { - ctx.Redirect(http.StatusFound, "/50x") - return - } - - fmt.Printf("user info is :%+v", userInfo) - - // TODO - // If user email exists, try to login this user. - // If user email not exists, try to register this user. - - ctx.Redirect(http.StatusFound, "/login-success?access_token=token") - return -} diff --git a/internal/plugin/sample/filter_sample.go b/internal/plugin/sample/filter_sample.go deleted file mode 100644 index e50218dd..00000000 --- a/internal/plugin/sample/filter_sample.go +++ /dev/null @@ -1,30 +0,0 @@ -package sample - -import ( - "fmt" - "strings" - - "github.com/answerdev/answer/internal/plugin" -) - -type FilterSample struct { -} - -func (s *FilterSample) Info() plugin.Info { - return plugin.Info{ - Name: "filter sample", - Description: "filter sample plugin", - Version: "0.0.1", - } -} - -func (s *FilterSample) FilterText(data string) (err error) { - if strings.Contains(data, "violent") { - return fmt.Errorf("bloody and violent words cannot appear in this website") - } - return nil -} - -func init() { - plugin.Register(&FilterSample{}) -} diff --git a/internal/plugin/sample/filter_sample_test.go b/internal/plugin/sample/filter_sample_test.go deleted file mode 100644 index ba72cb20..00000000 --- a/internal/plugin/sample/filter_sample_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package sample - -import ( - "fmt" - "testing" - - "github.com/answerdev/answer/internal/plugin" - "github.com/stretchr/testify/assert" -) - -func TestFilterSample_FilterText(t *testing.T) { - // try to call filter plugin for filter text that are not allowed - err := plugin.CallFilter(func(fn plugin.Filter) error { - return fn.FilterText("bloody and violent words") - }) - assert.Error(t, err) - fmt.Println(err) -} diff --git a/internal/plugin/sample/sample.go b/internal/plugin/sample/sample.go deleted file mode 100644 index 42e2b7e9..00000000 --- a/internal/plugin/sample/sample.go +++ /dev/null @@ -1,18 +0,0 @@ -package sample - -import "github.com/answerdev/answer/internal/plugin" - -type Sample struct { -} - -func (s *Sample) Info() plugin.Info { - return plugin.Info{ - Name: "sample", - Description: "sample plugin", - Version: "0.0.1", - } -} - -func init() { - plugin.Register(&Sample{}) -} diff --git a/internal/router/plugin_api_router.go b/internal/router/plugin_api_router.go index cfc975f1..5d55863b 100644 --- a/internal/router/plugin_api_router.go +++ b/internal/router/plugin_api_router.go @@ -2,7 +2,7 @@ package router import ( "github.com/answerdev/answer/internal/controller" - "github.com/answerdev/answer/internal/plugin" + "github.com/answerdev/answer/plugin" "github.com/gin-gonic/gin" ) diff --git a/internal/service/plugin_common/plugin_common_service.go b/internal/service/plugin_common/plugin_common_service.go index d878f98a..c8f7d799 100644 --- a/internal/service/plugin_common/plugin_common_service.go +++ b/internal/service/plugin_common/plugin_common_service.go @@ -7,9 +7,9 @@ import ( "github.com/answerdev/answer/internal/base/constant" "github.com/answerdev/answer/internal/base/reason" "github.com/answerdev/answer/internal/entity" - "github.com/answerdev/answer/internal/plugin" "github.com/answerdev/answer/internal/schema" "github.com/answerdev/answer/internal/service/config" + "github.com/answerdev/answer/plugin" "github.com/segmentfault/pacman/errors" "github.com/segmentfault/pacman/log" ) diff --git a/pkg/checker/reserved_username.go b/pkg/checker/reserved_username.go index 91b5f9ac..01d31997 100644 --- a/pkg/checker/reserved_username.go +++ b/pkg/checker/reserved_username.go @@ -4,7 +4,6 @@ import ( "encoding/json" "github.com/answerdev/answer/configs" - "github.com/segmentfault/pacman/log" ) var ( @@ -14,7 +13,6 @@ var ( func init() { var usernames []string _ = json.Unmarshal(configs.ReservedUsernames, &usernames) - log.Debugf("get reserved usernames %d", len(usernames)) for _, username := range usernames { reservedUsernameMapping[username] = true } diff --git a/internal/plugin/base.go b/plugin/base.go similarity index 100% rename from internal/plugin/base.go rename to plugin/base.go diff --git a/internal/plugin/config.go b/plugin/config.go similarity index 100% rename from internal/plugin/config.go rename to plugin/config.go diff --git a/internal/plugin/connector.go b/plugin/connector.go similarity index 100% rename from internal/plugin/connector.go rename to plugin/connector.go diff --git a/internal/plugin/connector/github.go b/plugin/connector/github.go similarity index 98% rename from internal/plugin/connector/github.go rename to plugin/connector/github.go index 3e79cbdf..863b1205 100644 --- a/internal/plugin/connector/github.go +++ b/plugin/connector/github.go @@ -6,7 +6,7 @@ import ( "fmt" "os" - "github.com/answerdev/answer/internal/plugin" + "github.com/answerdev/answer/plugin" "github.com/google/go-github/v48/github" "golang.org/x/oauth2" oauth2GitHub "golang.org/x/oauth2/github" diff --git a/plugin/connector/google.go b/plugin/connector/google.go new file mode 100644 index 00000000..d0510ac4 --- /dev/null +++ b/plugin/connector/google.go @@ -0,0 +1,118 @@ +package connector + +import ( + "context" + "encoding/json" + "fmt" + "io" + "os" + "time" + + "github.com/answerdev/answer/plugin" + "golang.org/x/oauth2" + oauth2Google "golang.org/x/oauth2/google" +) + +type Google struct { + Config *GoogleConfig +} + +type GoogleConfig struct { + ClientID string `json:"client_id"` + ClientSecret string `json:"client_secret"` +} + +type GoogleAuthUserInfo struct { + ID string `json:"id"` + Sub string `json:"sub"` + Name string `json:"name"` + GivenName string `json:"given_name"` + FamilyName string `json:"family_name"` + Profile string `json:"profile"` + Picture string `json:"picture"` + Email string `json:"email"` + EmailVerified bool `json:"email_verified"` + Gender string `json:"gender"` +} + +func init() { + plugin.Register(&Google{ + Config: &GoogleConfig{ + ClientID: os.Getenv("GOOGLE_CLIENT_ID"), + ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"), + }, + }) +} + +func (g *Google) Info() plugin.Info { + return plugin.Info{ + Name: "google connector", + SlugName: "google_connector", + Description: "google connector plugin", + Version: "0.0.1", + } +} + +func (g *Google) ConnectorLogoSVG() string { + //TODO implement me + panic("implement me") +} + +func (g *Google) ConnectorSender(ctx *plugin.GinContext, receiverURL string) (redirectURL string) { + oauth2Config := &oauth2.Config{ + ClientID: g.Config.ClientID, + ClientSecret: g.Config.ClientSecret, + Endpoint: oauth2Google.Endpoint, + RedirectURL: receiverURL, + Scopes: []string{ + "https://www.googleapis.com/auth/userinfo.email", + "https://www.googleapis.com/auth/userinfo.profile", + "openid", + }, + } + return oauth2Config.AuthCodeURL("state") +} + +func (g *Google) ConnectorReceiver(ctx *plugin.GinContext) (userInfo plugin.ExternalLoginUserInfo, err error) { + code := ctx.Query("code") + oauth2Config := &oauth2.Config{ + ClientID: g.Config.ClientID, + ClientSecret: g.Config.ClientSecret, + Endpoint: oauth2Google.Endpoint, + } + + token, err := oauth2Config.Exchange(context.Background(), code) + if err != nil { + return userInfo, err + } + + client := oauth2Config.Client(context.TODO(), token) + client.Timeout = 15 * time.Second + response, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo") + if err != nil { + return userInfo, err + } + defer response.Body.Close() + data, _ := io.ReadAll(response.Body) + + respGoogleAuthUserInfo := &GoogleAuthUserInfo{} + if err = json.Unmarshal(data, respGoogleAuthUserInfo); err != nil { + return userInfo, fmt.Errorf("parse google oauth user info response failed: %v", err) + } + + userInfo = plugin.ExternalLoginUserInfo{ + ExternalID: respGoogleAuthUserInfo.ID, + Name: respGoogleAuthUserInfo.Name, + Email: respGoogleAuthUserInfo.Email, + MetaInfo: string(data), + } + return userInfo, nil +} + +func (g *Google) ConnectorName() string { + return "Google" +} + +func (g *Google) ConnectorSlugName() string { + return "google" +} diff --git a/internal/plugin/filter.go b/plugin/filter.go similarity index 100% rename from internal/plugin/filter.go rename to plugin/filter.go diff --git a/internal/plugin/parser.go b/plugin/parser.go similarity index 100% rename from internal/plugin/parser.go rename to plugin/parser.go diff --git a/internal/plugin/plugin.go b/plugin/plugin.go similarity index 100% rename from internal/plugin/plugin.go rename to plugin/plugin.go