- auth for weixin.
This commit is contained in:
79
business/jxcallback/auth/auth.go
Normal file
79
business/jxcallback/auth/auth.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
)
|
||||
|
||||
const (
|
||||
DefTokenDuration = 7 * 24 * time.Hour // 7天
|
||||
)
|
||||
|
||||
type IAuther interface {
|
||||
Login(id, secret string) error
|
||||
Logout(id string) error
|
||||
}
|
||||
|
||||
var (
|
||||
authers map[string]IAuther
|
||||
)
|
||||
|
||||
var (
|
||||
ErrLoginTypeNotSupported = errors.New("不支持指定的登录类型")
|
||||
)
|
||||
|
||||
type LoginInfo struct {
|
||||
ID string
|
||||
LoginType string
|
||||
ExpiresIn int64
|
||||
Token string
|
||||
}
|
||||
|
||||
func init() {
|
||||
authers = make(map[string]IAuther)
|
||||
}
|
||||
|
||||
func RegisterAuther(loginType string, handler IAuther) {
|
||||
authers[loginType] = handler
|
||||
}
|
||||
|
||||
func Login(id, loginType, secret string) (loginInfo *LoginInfo, err error) {
|
||||
if handler := authers[loginType]; handler != nil {
|
||||
if err = handler.Login(id, secret); err == nil {
|
||||
token := utils.GetUUID()
|
||||
loginInfo = &LoginInfo{
|
||||
ID: id,
|
||||
LoginType: loginType,
|
||||
ExpiresIn: time.Now().Add(DefTokenDuration).Unix(),
|
||||
Token: token,
|
||||
}
|
||||
globals.Cacher.Set(token, loginInfo, DefTokenDuration)
|
||||
return loginInfo, nil
|
||||
}
|
||||
} else {
|
||||
err = ErrLoginTypeNotSupported
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func Logout(token string) (err error) {
|
||||
loginInfo := new(LoginInfo)
|
||||
if err = globals.Cacher.GetAs(token, loginInfo); err == nil {
|
||||
if handler := authers[loginInfo.LoginType]; handler != nil {
|
||||
err = handler.Logout(loginInfo.ID)
|
||||
}
|
||||
globals.Cacher.Del(token)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func GetUserInfo(token string) (loginInfo *LoginInfo, err error) {
|
||||
loginInfo = new(LoginInfo)
|
||||
if err = globals.Cacher.GetAs(token, loginInfo); err == nil {
|
||||
return loginInfo, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -1,25 +1,70 @@
|
||||
package weixin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi/weixinsnsapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
)
|
||||
|
||||
const (
|
||||
LoginType = "weixinsns"
|
||||
DefTempPasswordDuration = 5 * time.Minute // 登录时间限制在5分钟内
|
||||
)
|
||||
|
||||
var (
|
||||
ErrLoginFailed = errors.New("登录失败")
|
||||
StrStateIsWrong = "state:%s状态不对"
|
||||
)
|
||||
|
||||
func GetUserInfo(code string, state string) (token *weixinsnsapi.UserInfo, err error) {
|
||||
type Auther struct {
|
||||
}
|
||||
|
||||
type UserInfoExt struct {
|
||||
weixinsnsapi.UserInfo
|
||||
TempPassword string `json:"tempPassword"` // 一段时间有效的登录密码
|
||||
}
|
||||
|
||||
func init() {
|
||||
auth.RegisterAuther(LoginType, new(Auther))
|
||||
}
|
||||
|
||||
func GetUserInfo(code string, state string) (token *UserInfoExt, err error) {
|
||||
if state == "" {
|
||||
wxapi := weixinsnsapi.New(api.WeixinAPI.GetAppID(), api.WeixinAPI.GetSecret())
|
||||
token, err2 := wxapi.RefreshToken(code)
|
||||
if err = err2; err == nil {
|
||||
return wxapi.GetUserInfo(token.OpenID)
|
||||
wxUserinfo, err2 := wxapi.GetUserInfo(token.OpenID)
|
||||
if err = err2; err == nil {
|
||||
pwd := utils.GetUUID()
|
||||
globals.Cacher.Set(wxUserinfo.OpenID, pwd, DefTempPasswordDuration)
|
||||
return &UserInfoExt{
|
||||
UserInfo: *wxUserinfo,
|
||||
TempPassword: pwd,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf(StrStateIsWrong, state)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (a *Auther) Login(openid, password string) error {
|
||||
if value := globals.Cacher.Get(openid); value != nil {
|
||||
if password == value.(string) {
|
||||
globals.Cacher.Del(openid)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return ErrLoginFailed
|
||||
}
|
||||
|
||||
func (a *Auther) Logout(openid string) error {
|
||||
return globals.Cacher.Del(openid)
|
||||
}
|
||||
|
||||
13
business/jxutils/cache/cache.go
vendored
Normal file
13
business/jxutils/cache/cache.go
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type ICacher interface {
|
||||
FlushAll() error
|
||||
Set(key string, value interface{}, expiration time.Duration) error
|
||||
Del(key string) error
|
||||
Get(key string) interface{}
|
||||
GetAs(key string, ptr interface{}) error
|
||||
}
|
||||
60
business/jxutils/cache/redis/redis.go
vendored
Normal file
60
business/jxutils/cache/redis/redis.go
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"github.com/go-redis/redis"
|
||||
)
|
||||
|
||||
type Cacher struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func New(host string, port int, password string) *Cacher {
|
||||
if port == 0 {
|
||||
port = 6379
|
||||
}
|
||||
return &Cacher{
|
||||
client: redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("%s:%d", host, port),
|
||||
Password: password,
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cacher) FlushAll() error {
|
||||
return c.client.FlushAll().Err()
|
||||
}
|
||||
|
||||
func (c *Cacher) Set(key string, value interface{}, expiration time.Duration) error {
|
||||
strValue := string(utils.MustMarshal(value))
|
||||
return c.client.Set(key, strValue, expiration).Err()
|
||||
}
|
||||
|
||||
func (c *Cacher) Del(key string) error {
|
||||
return c.client.Del(key).Err()
|
||||
}
|
||||
|
||||
func (c *Cacher) Get(key string) interface{} {
|
||||
result, err := c.client.Get(key).Result()
|
||||
if err == nil {
|
||||
var retVal interface{}
|
||||
if err = utils.UnmarshalUseNumber([]byte(result), &retVal); err == nil {
|
||||
return retVal
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cacher) GetAs(key string, ptr interface{}) error {
|
||||
result, err := c.client.Get(key).Result()
|
||||
if err == nil {
|
||||
if err = utils.UnmarshalUseNumber([]byte(result), ptr); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
48
business/jxutils/cache/redis/redis_test.go
vendored
Normal file
48
business/jxutils/cache/redis/redis_test.go
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"git.rosy.net.cn/jx-callback/globals/beegodb"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//E:/goprojects/src/git.rosy.net.cn/jx-callback/conf/app.conf
|
||||
///Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf
|
||||
beego.InitBeegoBeforeTest("/Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf")
|
||||
beego.BConfig.RunMode = "dev" // InitBeegoBeforeTest会将runmode设置为test
|
||||
|
||||
globals.Init()
|
||||
beegodb.Init()
|
||||
api.Init()
|
||||
}
|
||||
|
||||
type FuckYou struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
func TestIt(t *testing.T) {
|
||||
cacher := New("localhost", 6379, "")
|
||||
cacher.FlushAll()
|
||||
if cacher.Get("key") != nil {
|
||||
t.Fatal("should get nothing")
|
||||
}
|
||||
if err := cacher.Set("key", map[string]interface{}{
|
||||
"key": "value",
|
||||
}, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if cacher.Get("key") == nil {
|
||||
t.Fatal("should not get nothing")
|
||||
}
|
||||
|
||||
result := new(FuckYou)
|
||||
cacher.GetAs("key", result)
|
||||
if result.Key != "value" {
|
||||
t.Fatal("should get value")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user