Merge branch 'dev'
This commit is contained in:
commit
d7016ad386
@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ClientCredential struct {
|
type ClientCredential struct {
|
||||||
request.Error
|
request.Error `gorm:"-"`
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
ExpiresIn int32 `json:"expires_in"`
|
ExpiresIn int32 `json:"expires_in"`
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Token struct {
|
type Token struct {
|
||||||
request.Error
|
request.Error `gorm:"-"`
|
||||||
AccessToken string `json:"access_token"` //获取到的凭证
|
AccessToken string `json:"access_token"` //获取到的凭证
|
||||||
ExpiresIn int32 `json:"expires_in"` //凭证有效时间,单位:秒
|
ExpiresIn int32 `json:"expires_in"` //凭证有效时间,单位:秒
|
||||||
RefreshToken string `json:"refresh_token"` //有效期为30天,当失效之后,需要用户重新授
|
RefreshToken string `json:"refresh_token"` //有效期为30天,当失效之后,需要用户重新授
|
||||||
OpenID string `json:"openid" gorm:"index"`
|
OpenID string `json:"openid" gorm:"index"`
|
||||||
Scope string `json:"scope"`
|
Scope string `json:"scope"`
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Userinfo struct {
|
type Userinfo struct {
|
||||||
request.Error
|
request.Error `gorm:"-"`
|
||||||
OpenID string `json:"openid" gorm:"index"`
|
OpenID string `json:"openid" gorm:"index"`
|
||||||
NickName string `json:"nickname"`
|
NickName string `json:"nickname"`
|
||||||
Sex int32 `json:"sex"`
|
Sex int32 `json:"sex"`
|
||||||
Province string `json:"province"`
|
Province string `json:"province"`
|
||||||
City string `json:"city"`
|
City string `json:"city"`
|
||||||
Country string `json:"country"`
|
Country string `json:"country"`
|
||||||
HeadImgURL string `json:"headimgurl"`
|
HeadImgURL string `json:"headimgurl"`
|
||||||
Privilege []string `json:"privilege" gorm:"type:text[]"`
|
Privilege []string `json:"privilege" gorm:"type:text[]"`
|
||||||
UnionID string `json:"unionid"`
|
UnionID string `json:"unionid"`
|
||||||
}
|
}
|
||||||
|
113
cmd/mp-auth-http/main.go
Normal file
113
cmd/mp-auth-http/main.go
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
pb "git.esin.io/lab/weixin/protobuf/clientapi/mp/auth"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
lisPort string
|
||||||
|
authServerEndpoint string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(&lisPort, "port", "3000", "server listen port")
|
||||||
|
flag.StringVar(&authServerEndpoint, "auth.server", "localhost:13721", "auth grpc server endpoint")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if err := run(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func run() error {
|
||||||
|
grpcDialOpts := []grpc.DialOption{
|
||||||
|
grpc.WithInsecure(),
|
||||||
|
grpc.WithBlock(),
|
||||||
|
}
|
||||||
|
grpcConn, err := grpc.Dial(authServerEndpoint, grpcDialOpts...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
h := NewHandler(pb.NewAuthServiceClient(grpcConn))
|
||||||
|
|
||||||
|
http.HandleFunc("/url", h.getCodeURL)
|
||||||
|
http.HandleFunc("/token", h.exchangeToken)
|
||||||
|
http.HandleFunc("/userinfo", h.getUserinfo)
|
||||||
|
http.HandleFunc("/userinfo/sync", h.syncUserinfo)
|
||||||
|
|
||||||
|
return http.ListenAndServe(":"+lisPort, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
type restHandler struct {
|
||||||
|
client pb.AuthServiceClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(client pb.AuthServiceClient) *restHandler {
|
||||||
|
return &restHandler{
|
||||||
|
client: client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h restHandler) getCodeURL(w http.ResponseWriter, r *http.Request) {
|
||||||
|
resp, err := h.client.GetCodeURL(r.Context(), &pb.GetCodeURLRequest{
|
||||||
|
RedirectUrl: r.FormValue("redirect_url"),
|
||||||
|
State: r.FormValue("state"),
|
||||||
|
Scope: pb.GetCodeURLRequest_snsapi_base,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
encoder := json.NewEncoder(w)
|
||||||
|
encoder.SetEscapeHTML(false)
|
||||||
|
encoder.Encode(resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h restHandler) exchangeToken(w http.ResponseWriter, r *http.Request) {
|
||||||
|
code := r.FormValue("code")
|
||||||
|
|
||||||
|
resp, err := h.client.ExchangeToken(r.Context(), &pb.ExchangeTokenRequest{
|
||||||
|
Code: code,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
json.NewEncoder(w).Encode(resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h restHandler) getUserinfo(w http.ResponseWriter, r *http.Request) {
|
||||||
|
openid := r.FormValue("openid")
|
||||||
|
resp, err := h.client.GetUserinfo(r.Context(), &pb.GetUserinfoRequest{
|
||||||
|
OpenId: openid,
|
||||||
|
Lang: pb.GetUserinfoRequest_zh_CN,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h restHandler) syncUserinfo(w http.ResponseWriter, r *http.Request) {
|
||||||
|
openid := r.FormValue("openid")
|
||||||
|
resp, err := h.client.SyncUserinfo(r.Context(), &pb.SyncUserinfoRequest{
|
||||||
|
OpenId: openid,
|
||||||
|
Lang: pb.SyncUserinfoRequest_zh_CN,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(resp)
|
||||||
|
}
|
@ -53,7 +53,7 @@ func (srv Service) PublishEvent(ctx context.Context, subject string, message int
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv Service) GetAuthCodeURL(ctx context.Context, req *pb.GetCodeURLRequest) (*pb.GetCodeURLResponse, error) {
|
func (srv Service) GetCodeURL(ctx context.Context, req *pb.GetCodeURLRequest) (*pb.GetCodeURLResponse, error) {
|
||||||
resp := srv.client.GetCodeURL(req.RedirectUrl, req.State, mpauth.Scope(req.Scope.String()))
|
resp := srv.client.GetCodeURL(req.RedirectUrl, req.State, mpauth.Scope(req.Scope.String()))
|
||||||
|
|
||||||
return &pb.GetCodeURLResponse{
|
return &pb.GetCodeURLResponse{
|
||||||
@ -163,7 +163,7 @@ func (srv Service) SyncUserinfo(ctx context.Context, req *pb.SyncUserinfoRequest
|
|||||||
return nil, status.Errorf(codes.Internal, errors.Wrap(err, "get userinfo token from weixin failed").Error())
|
return nil, status.Errorf(codes.Internal, errors.Wrap(err, "get userinfo token from weixin failed").Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
userinfo = Userinfo{Userinfo: *resp}
|
userinfo.Userinfo = *resp
|
||||||
if err := srv.db.Save(&userinfo).Error; err != nil {
|
if err := srv.db.Save(&userinfo).Error; err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, errors.Wrap(err, "sync userinfo and save to database failed").Error())
|
return nil, status.Errorf(codes.Internal, errors.Wrap(err, "sync userinfo and save to database failed").Error())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user