mirror of https://gitee.com/answerdev/answer.git
refactor(plugin): Directory movement
This commit is contained in:
parent
87f649faf7
commit
1802e6fa2e
|
@ -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"
|
||||
)
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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{})
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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{})
|
||||
}
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"
|
|
@ -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"
|
||||
}
|
Loading…
Reference in New Issue