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

Beyond Compare 3 #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions api/v1/oauth/oauth_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package oauth

import (
"github.com/gogf/gf/v2/frame/g"
)

// 获取 平台 sso 授权配置信息
type ListProviderReq struct {
g.Meta `path:"/provider" method:"get" summary:"sso授权配置信息" tags:"授权登录"`
//Name string `json:"name" description:"sso授权名称"`
//Status int `json:"status" description:"是否开启"`
}

type Provider struct {
Name string `json:"name" description:"sso授权名称"`
Logo string `json:"logo" description:"图标logo地址"`
Status int `json:"status" description:"是否开启 0 关闭 1 启用"`
}

type ListProviderRes struct {
g.Meta `mime:"application/json"`
Providers []*Provider `json:"providers" dc:"授权配置"`
}
54 changes: 54 additions & 0 deletions api/v1/oauth/oauth_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package oauth

import (
"github.com/gogf/gf/v2/frame/g"
"sagooiot/internal/model/entity"
)

type AuthLoginReq struct {
g.Meta `path:"/login" method:"get" summary:"sso授权" tags:"授权登录"`
Provider string `json:"provider" description:"授权对象 qq | wechat" v:"required#provider不能为空"`
}

type AuthLoginRes struct{}

type AuthCallbackReq struct {
g.Meta `path:"/callback" method:"get" summary:"sso授权登录回调" tags:"授权登录"`
Code string `json:"code" description:"授权凭证" v:"required#code不能为空"`
Provider string `json:"provider" description:"授权对象 qq | wechat" v:"required#provider不能为空"`
State string `json:"state"`
}

type AuthCallbackRes struct {
OauthUser *entity.OauthUser `json:"oauthUser"`
UserInfo *LoginUserRes `json:"loginUser"`
}

type LoginUserRes struct {
UserNickname string `orm:"user_nickname" json:"userNickname"` // 用户昵称
Avatar string `orm:"avatar" json:"avatar"` //头像
Token string `json:"token"`
}

// 获取 系统用户绑定授权信息
type GetUserBindingReq struct {
g.Meta `path:"/users/{user_id}" method:"get" summary:"获取系统账户授权绑定信息" tags:"授权登录"`
Provider string `json:"provider" description:"授权对象 qq | wechat"`
UserId int `json:"user_id" description:"系统用户身份id" v:"required#user_id不能为空"`
}

type GetUserBindingRes struct {
g.Meta `mime:"application/json"`
Users []*entity.OauthUser `json:"users" dc:"授权用户列表"`
}

// 系统用户登录授权绑定
type UserBindingReq struct {
g.Meta `path:"/binding" method:"post" summary:"系统用户登录绑定授权用户" tags:"授权登录"`
Provider string `json:"provider" description:"授权对象 qq | wechat" v:"required#provider不能为空"`
Openid string `json:"openid" description:"授权用户身份id" v:"required#openid不能为空"`
}
type UserBindingRes struct {
g.Meta `mime:"application/json"`
*entity.OauthUser `json:"user" dc:"授权用户列表"`
}
18 changes: 18 additions & 0 deletions internal/cmd/router/oauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package router

import (
"context"
"github.com/gogf/gf/v2/net/ghttp"
oauthController "sagooiot/internal/controller/oauth"
)

// Analysis 分析统计相关的接口
func OAuth(ctx context.Context, group *ghttp.RouterGroup) {
group.Group("/oauth", func(group *ghttp.RouterGroup) {
//group.Middleware(service.Middleware().Auth)
group.Bind(
oauthController.OProvider, // 第三方授权配置提供
oauthController.OUser, // 第三方授权用户登录
)
})
}
1 change: 1 addition & 0 deletions internal/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func RunServer(ctx context.Context, stopSignal chan os.Signal) {
router.Iot(ctx, group) //Iot功能的路由
router.Analysis(ctx, group) //分析统计功能的路由
module.Router(ctx, group) //加载模块的路由
router.OAuth(ctx, group) //第三方授权登录

})

Expand Down
16 changes: 16 additions & 0 deletions internal/consts/oauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package consts

// MINIO
const (
OauthQQName = "oauth.qq.name" //授权对象 QQ
OauthQQLogo = "oauth.qq.logo" //授权对象 QQ图标地址
OauthQQAppid = "oauth.qq.appid" //授权对象 QQ 开发凭证 appid
OauthQQAppsecret = "oauth.qq.appsecret" //授权对象 QQ 开发凭证 appsecret
OauthQQStatus = "oauth.qq.status" //授权对象 QQ 配置状态 0 关闭 1 开启
OauthWechatName = "oauth.wechat.name" //授权对象 微信
OauthWechatLogo = "oauth.wechat.logo" //授权对象 微信 图标地址
OauthWechatAppid = "oauth.wechat.appid" //授权对象 微信 开发凭证 appid
OauthWechatAppsecret = "oauth.wechat.appsecret" //授权对象 微信 开发凭证 appsecret
OauthWechatStatus = "oauth.wechat.status" //授权对象 微信 配置状态 0 关闭 1 开启
OauthCallbackUrl = "oauth.callback.url" //授权回调url
)
30 changes: 30 additions & 0 deletions internal/controller/oauth/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package oauth

import (
"context"
oauthV1 "sagooiot/api/v1/oauth"
"sagooiot/internal/model/entity"
"sagooiot/internal/service"
)

type cProvider struct{}

var OProvider = cProvider{}

func (c *cProvider) ListProvider(ctx context.Context, req *oauthV1.ListProviderReq) (*oauthV1.ListProviderRes, error) {
providers, err := service.OauthProvider().List(ctx, &entity.OauthProvider{})
if err != nil {
return nil, err
}
providersRes := []*oauthV1.Provider{}
for _, value := range providers {
providersRes = append(providersRes, &oauthV1.Provider{
Name: value.Name,
Logo: value.Logo,
Status: value.Status,
})
}
return &oauthV1.ListProviderRes{
Providers: providersRes,
}, nil
}
83 changes: 83 additions & 0 deletions internal/controller/oauth/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package oauth

import (
"context"
"github.com/gogf/gf/v2/frame/g"
oauthV1 "sagooiot/api/v1/oauth"
"sagooiot/internal/model/entity"
"sagooiot/internal/service"
oauth2 "sagooiot/pkg/oauth"
)

var OUser = cUser{}

type cUser struct{}

// 授权登录
func (c *cUser) Login(ctx context.Context, in *oauthV1.AuthLoginReq) (res *oauthV1.AuthLoginRes, err error) {
r := g.RequestFromCtx(ctx)
err = service.OauthProvider().UseProvider(ctx, in.Provider)
if err != nil {
return nil, err
}
oauth2.BeginAuthHandler(r.Response.RawWriter(), r.Request)
return
}

// 授权回调
func (c *cUser) Callback(ctx context.Context, req *oauthV1.AuthCallbackReq) (res *oauthV1.AuthCallbackRes, err error) {
r := g.RequestFromCtx(ctx)
userInfo, err := oauth2.CompleteUserAuth(r.Response.RawWriter(), r.Request)
if err != nil {
return nil, err
}
user, err := service.OauthUser().SaveUser(ctx, &entity.OauthUser{
Nickname: userInfo.NickName,
Provider: req.Provider,
AvatarUrl: userInfo.AvatarURL,
Openid: userInfo.UserID,
})
if err != nil {
return nil, err
}
// 登录
var loginUser *oauthV1.LoginUserRes
if user.UserId > 0 {
sysUser, token, err := service.OauthUser().AuthLogin(ctx, user)
if err != nil {
return nil, err
}
loginUser = &oauthV1.LoginUserRes{
UserNickname: sysUser.UserNickname,
Avatar: sysUser.Avatar,
Token: token,
}
}
return &oauthV1.AuthCallbackRes{
UserInfo: loginUser,
OauthUser: user,
}, err
}

func (c *cUser) ListUser(ctx context.Context, in *oauthV1.GetUserBindingReq) (res *oauthV1.GetUserBindingRes, err error) {
users, err := service.OauthUser().List(ctx, &entity.OauthUser{
UserId: in.UserId,
Provider: in.Provider,
})
if err != nil {
return nil, err
}
return &oauthV1.GetUserBindingRes{
Users: users,
}, nil
}

func (c *cUser) UserBinding(ctx context.Context, in *oauthV1.UserBindingReq) (res *oauthV1.UserBindingRes, err error) {
user, err := service.OauthUser().UserBinding(ctx, in.Provider, in.Openid)
if err != nil {
return nil, err
}
return &oauthV1.UserBindingRes{
OauthUser: user,
}, nil
}
85 changes: 85 additions & 0 deletions internal/dao/internal/oauth_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading