Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"cookie_test.go",
"dao_test.go",
"mc_test.go",
"refresh_test.go",
"token_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/passport-auth/conf:go_default_library",
"//app/service/main/passport-auth/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"cookie.go",
"dao.go",
"mc.go",
"refresh.go",
"token.go",
],
importpath = "go-common/app/service/main/passport-auth/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/passport-auth/conf:go_default_library",
"//app/service/main/passport-auth/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,34 @@
package dao
import (
"context"
"encoding/hex"
"fmt"
"time"
"go-common/app/service/main/passport-auth/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_getCookieSessionSQL = "SELECT mid,session,csrf,type,expires FROM user_cookie_%s where session = ? limit 1"
)
// Cookie get cookie by session
func (d *Dao) Cookie(c context.Context, sd []byte, ct time.Time) (res *model.Cookie, session []byte, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_getCookieSessionSQL, formatSuffix(ct)), sd)
res = new(model.Cookie)
var csrf []byte
if err = row.Scan(&res.Mid, &session, &csrf, &res.Type, &res.Expires); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
return
}
res.CSRF = hex.EncodeToString(csrf)
return
}

View File

@@ -0,0 +1,32 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoCookie(t *testing.T) {
var (
c = context.TODO()
sd = []byte("5ebd8ebb,1530838806,2c8d0678")
sdNotExist = []byte("9f1c9145,1536117849,c9fb62a2")
ct, _ = time.Parse("01/02/2006", "07/27/2018")
)
convey.Convey("Cookie", t, func(ctx convey.C) {
res, session, err := d.Cookie(c, sd, ct)
ctx.Convey("Then err should be nil.res,session should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(session, convey.ShouldNotBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
res2, session2, err2 := d.Cookie(c, sdNotExist, ct)
ctx.Convey("Then err should be nil.res,session should be nil.", func(ctx convey.C) {
ctx.So(err2, convey.ShouldBeNil)
ctx.So(session2, convey.ShouldBeNil)
ctx.So(res2, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,47 @@
package dao
import (
"context"
"time"
"go-common/app/service/main/passport-auth/conf"
"go-common/library/cache/memcache"
"go-common/library/database/sql"
)
// Dao dao struct
type Dao struct {
db *sql.DB
mc *memcache.Pool
mcExpire int32
c *conf.Config
}
// New create new dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.Mysql),
mc: memcache.NewPool(c.Memcache.Config),
mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
}
return
}
// Ping check db and mc health.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.Ping(c); err != nil {
return
}
return d.pingMC(c)
}
// Close close connections of mc, redis, db.
func (d *Dao) Close() {
if d.mc != nil {
d.mc.Close()
}
if d.db != nil {
d.db.Close()
}
}

View File

@@ -0,0 +1,35 @@
package dao
import (
"flag"
"go-common/app/service/main/passport-auth/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.passport.passport-auth-service")
flag.Set("conf_token", "3f34a05a9bf3bc0add9c135c1f83ff2f")
flag.Set("tree_id", "35706")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "/Users/bilibili/gopath/src/go-common/app/service/main/passport-auth/cmd/passport-auth-service.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,199 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/main/passport-auth/model"
"go-common/library/cache/memcache"
"go-common/library/log"
)
func ckKey(session string) string {
return fmt.Sprintf("ck_%s", session)
}
func akKey(token string) string {
return fmt.Sprintf("ak_%s", token)
}
func rkKey(refresh string) string {
return fmt.Sprintf("rk_%s", refresh)
}
// SetCookieCache set cookie info to cache
func (d *Dao) SetCookieCache(c context.Context, session string, res *model.Cookie) (err error) {
key := ckKey(session)
conn := d.mc.Get(c)
defer conn.Close()
if res.Expires < 0 {
log.Error("auth expire error(expires:%d)", res.Expires)
return
}
item := &memcache.Item{Key: key, Object: res, Flags: memcache.FlagProtobuf, Expiration: int32(res.Expires)}
if err = conn.Set(item); err != nil {
log.Error("auth set error(%s,%d,%v)", key, res.Expires, err)
}
return
}
// CookieCache get cookie info from cache
func (d *Dao) CookieCache(c context.Context, session string) (res *model.Cookie, err error) {
key := ckKey(session)
conn := d.mc.Get(c)
defer conn.Close()
var item *memcache.Item
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", key, err)
return
}
res = new(model.Cookie)
if err = conn.Scan(item, res); err != nil {
log.Error("conn.Scan(%v) error(%v)", string(item.Value), err)
}
return
}
// DelCookieCache del cache.
func (d *Dao) DelCookieCache(c context.Context, session string) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(ckKey(session)); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Delete(%s) error(%v)", ckKey(session), err)
}
return
}
// SetTokenCache set token to cache
func (d *Dao) SetTokenCache(c context.Context, k string, res *model.Token) (err error) {
key := akKey(k)
conn := d.mc.Get(c)
defer conn.Close()
if res.Expires < 0 {
log.Error("auth expire error(expires:%d)", res.Expires)
return
}
if err = conn.Set(&memcache.Item{
Key: key,
Object: res,
Flags: memcache.FlagProtobuf,
Expiration: int32(res.Expires),
}); err != nil {
log.Error("set token cache error(%s,%d,%v)", key, res.Expires, err)
}
return
}
// TokenCache get token from cache
func (d *Dao) TokenCache(c context.Context, sd string) (res *model.Token, err error) {
key := akKey(sd)
conn := d.mc.Get(c)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", key, err)
return
}
res = new(model.Token)
if err = conn.Scan(r, res); err != nil {
log.Error("conn.Scan(%v) error(%v)", string(r.Value), err)
}
return
}
// DelTokenCache del cache.
func (d *Dao) DelTokenCache(c context.Context, token string) (err error) {
key := akKey(token)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Delete(%s) error(%v)", key, err)
}
return
}
// SetRefreshCache set refresh token to cache .
func (d *Dao) SetRefreshCache(c context.Context, refresh *model.Refresh) (err error) {
key := rkKey(refresh.Refresh)
conn := d.mc.Get(c)
defer conn.Close()
if refresh.Expires < 0 {
log.Error("auth expire error(expires:%d)", refresh.Expires)
return
}
if err := conn.Set(&memcache.Item{
Key: key,
Object: refresh,
Flags: memcache.FlagProtobuf,
Expiration: int32(refresh.Expires),
}); err != nil {
log.Error("auth set error(%s,%d,%v)", key, refresh.Expires, err)
}
return
}
// RefreshCache get refresh token from cache
func (d *Dao) RefreshCache(c context.Context, refresh string) (res *model.Refresh, err error) {
key := rkKey(refresh)
conn := d.mc.Get(c)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", key, err)
return
}
res = new(model.Refresh)
if err = conn.Scan(r, res); err != nil {
log.Error("conn.Scan(%v) error(%v)", string(r.Value), err)
}
return
}
// DelRefreshCache del refresh token from cache
func (d *Dao) DelRefreshCache(c context.Context, refresh string) (err error) {
key := akKey(refresh)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Delete(%s) error(%v)", key, err)
}
return
}
// pingMC ping memcache.
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Set(&memcache.Item{
Key: "ping",
Value: []byte{1},
Expiration: d.mcExpire,
}); err != nil {
log.Error("conn.Set(ping, 1) error(%v)", err)
}
return
}

View File

@@ -0,0 +1,185 @@
package dao
import (
"context"
"go-common/app/service/main/passport-auth/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaockKey(t *testing.T) {
var (
session = "9f1c9145,1536117849,c9fb62a9"
)
convey.Convey("ckKey", t, func(ctx convey.C) {
p1 := ckKey(session)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaoakKey(t *testing.T) {
var (
token = "b8b544c602557c27d454911d7ecc006c"
)
convey.Convey("akKey", t, func(ctx convey.C) {
p1 := akKey(token)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaorkKey(t *testing.T) {
var (
refresh = "5f263d1297aa40ea0252c0963e29c6eb"
)
convey.Convey("rkKey", t, func(ctx convey.C) {
p1 := rkKey(refresh)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaoSetCookieCache(t *testing.T) {
var (
c = context.TODO()
session = "9f1c9145,1536117849,c9fb62a9"
res = &model.Cookie{}
)
convey.Convey("SetCookieCache", t, func(ctx convey.C) {
err := d.SetCookieCache(c, session, res)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoCookieCache(t *testing.T) {
var (
c = context.TODO()
session = "9f1c9145,1536117849,c9fb62a9"
)
convey.Convey("CookieCache", t, func(ctx convey.C) {
res, err := d.CookieCache(c, session)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}
func TestDaoDelCookieCache(t *testing.T) {
var (
c = context.TODO()
session = "9f1c9145,1536117849,c9fb62a9"
)
convey.Convey("DelCookieCache", t, func(ctx convey.C) {
err := d.DelCookieCache(c, session)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoSetTokenCache(t *testing.T) {
var (
c = context.TODO()
k = "5f263d1297aa40ea0252c0963e29c6eb"
res = &model.Token{}
)
convey.Convey("SetTokenCache", t, func(ctx convey.C) {
err := d.SetTokenCache(c, k, res)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoTokenCache(t *testing.T) {
var (
c = context.TODO()
sd = "5f263d1297aa40ea0252c0963e29c6eb"
)
convey.Convey("TokenCache", t, func(ctx convey.C) {
res, err := d.TokenCache(c, sd)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}
func TestDaoDelTokenCache(t *testing.T) {
var (
c = context.TODO()
token = "5f263d1297aa40ea0252c0963e29c6eb"
)
convey.Convey("DelTokenCache", t, func(ctx convey.C) {
err := d.DelTokenCache(c, token)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoSetRefreshCache(t *testing.T) {
var (
c = context.TODO()
refresh = &model.Refresh{
Mid: 123,
AppID: 430,
Token: "b8b544c602557c27d454911d7ecc006c",
Refresh: "5f263d1297aa40ea0252c0963e29c6e1",
Expires: 1850953187,
}
)
convey.Convey("SetRefreshCache", t, func(ctx convey.C) {
err := d.SetRefreshCache(c, refresh)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaoRefreshCache(t *testing.T) {
var (
c = context.TODO()
refresh = "5f263d1297aa40ea0252c0963e29c6e1"
)
convey.Convey("RefreshCache", t, func(ctx convey.C) {
res, err := d.RefreshCache(c, refresh)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}
func TestDaoDelRefreshCache(t *testing.T) {
var (
c = context.TODO()
refresh = "5f263d1297aa40ea0252c0963e29c6e0"
)
convey.Convey("DelRefreshCache", t, func(ctx convey.C) {
err := d.DelRefreshCache(c, refresh)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestDaopingMC(t *testing.T) {
var (
c = context.TODO()
)
convey.Convey("pingMC", t, func(ctx convey.C) {
err := d.pingMC(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,46 @@
package dao
import (
"context"
"encoding/hex"
"fmt"
"time"
"go-common/app/service/main/passport-auth/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_getRefreshSQL = "SELECT mid,appid,refresh,token,expires FROM user_refresh_%s WHERE refresh = ? limit 1"
)
// Refresh get token by access_token
func (d *Dao) Refresh(c context.Context, rk []byte, ct time.Time) (res *model.Refresh, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_getRefreshSQL, formatRefreshSuffix(ct)), rk)
res = new(model.Refresh)
var refresh, token []byte
if err = row.Scan(&res.Mid, &res.AppID, &refresh, &token, &res.Expires); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
return
}
res.Refresh = hex.EncodeToString(refresh)
res.Token = hex.EncodeToString(token)
return
}
func formatRefreshSuffix(t time.Time) string {
return formatByDate(t.Year(), int(t.Month()))
}
func formatByDate(year, month int) string {
if month%2 == 1 {
return fmt.Sprintf("%4d%02d", year, month)
}
return fmt.Sprintf("%4d%02d", year, month-1)
}

View File

@@ -0,0 +1,56 @@
package dao
import (
"context"
"encoding/hex"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoRefresh(t *testing.T) {
var (
c = context.TODO()
rk, _ = hex.DecodeString("5f1813d287eb4238f2eadf225bda5d84")
rkNotExist, _ = hex.DecodeString("5f1813d287eb4238f2eadf225bda5d81")
ct, _ = time.Parse("01/02/2006", "08/27/2018")
)
convey.Convey("Refresh", t, func(ctx convey.C) {
res, err := d.Refresh(c, rk, ct)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
res2, err2 := d.Refresh(c, rkNotExist, ct)
ctx.Convey("Then err should be nil.res should be nil.", func(ctx convey.C) {
ctx.So(err2, convey.ShouldBeNil)
ctx.So(res2, convey.ShouldBeNil)
})
})
}
func TestDaoformatRefreshSuffix(t *testing.T) {
var (
no = time.Now()
)
convey.Convey("formatRefreshSuffix", t, func(ctx convey.C) {
p1 := formatRefreshSuffix(no)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestDaoformatByDate(t *testing.T) {
var (
year = int(2018)
month = int(8)
)
convey.Convey("formatByDate", t, func(ctx convey.C) {
p1 := formatByDate(year, month)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,38 @@
package dao
import (
"context"
"encoding/hex"
"fmt"
"time"
"go-common/app/service/main/passport-auth/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_getTokenSQL = "SELECT mid,appid,token,expires,type FROM user_token_%s where token = ? limit 1"
)
// Token get token by access_token
func (d *Dao) Token(c context.Context, token []byte, ct time.Time) (res *model.Token, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_getTokenSQL, formatSuffix(ct)), token)
res = new(model.Token)
var tokenByte []byte
if err = row.Scan(&res.Mid, &res.AppID, &tokenByte, &res.Expires, &res.Type); err != nil {
if err == xsql.ErrNoRows {
res = nil
err = nil
} else {
log.Error("row.Scan() error(%v)", err)
}
return
}
res.Token = hex.EncodeToString(tokenByte)
return
}
func formatSuffix(t time.Time) string {
return t.Format("200601")
}

View File

@@ -0,0 +1,43 @@
package dao
import (
"context"
"encoding/hex"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoToken(t *testing.T) {
var (
c = context.TODO()
token, _ = hex.DecodeString("baa3443180f346db244780ba6d0c6f6c")
tokenNotExist, _ = hex.DecodeString("baa3443180f346db244780ba6d0c6f61")
ct, _ = time.Parse("01/02/2006", "10/27/2018")
)
convey.Convey("Token", t, func(ctx convey.C) {
res, err := d.Token(c, token, ct)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
res2, err2 := d.Token(c, tokenNotExist, ct)
ctx.Convey("Then err should be nil.res should be nil.", func(ctx convey.C) {
ctx.So(err2, convey.ShouldBeNil)
ctx.So(res2, convey.ShouldBeNil)
})
})
}
func TestDaoformatSuffix(t *testing.T) {
var (
no = time.Now()
)
convey.Convey("formatSuffix", t, func(ctx convey.C) {
p1 := formatSuffix(no)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}