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,62 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"clean_cache.go",
"cookie.go",
"dao.go",
"refresh.go",
"token.go",
],
importpath = "go-common/app/job/main/passport-auth/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/passport-auth/conf:go_default_library",
"//app/job/main/passport-auth/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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"],
)
go_test(
name = "go_default_test",
srcs = [
"clean_cache_test.go",
"cookie_test.go",
"dao_test.go",
"refresh_test.go",
"token_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/passport-auth/conf:go_default_library",
"//app/job/main/passport-auth/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,36 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/library/ecode"
"go-common/library/log"
)
// AsoCleanCache aso clean cache
func (d *Dao) AsoCleanCache(c context.Context, token, session string, mid int64) (err error) {
log.Info("aso clean cache,mid = %d,token = %s, session = %s", mid, token, session)
params := url.Values{}
params.Set("token", token)
params.Set("session", session)
params.Set("mid", strconv.Itoa(int(mid)))
res := &struct {
Code int `json:"code"`
}{}
if err = d.httpClient.Get(c, d.c.AuthJobConfig.AsoCleanURL, "127.0.0.2", params, res); err != nil {
log.Error("AsoCleanCache HTTP request err %v,token = %s,session = %s,mid = %d", err, token, session, mid)
return
}
if res.Code != 0 {
log.Error("AsoCleanCache server err_code %d,token = %s,session = %s,mid=%d", res.Code, token, session, mid)
if res.Code == ecode.RequestErr.Code() {
err = nil
return
}
err = ecode.Int(res.Code)
return
}
return
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAsoCleanCache(t *testing.T) {
convey.Convey("AsoCleanCache", t, func(ctx convey.C) {
var (
c = context.Background()
token = ""
session = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
mid = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.AsoCleanCache(c, token, session, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,48 @@
package dao
import (
"context"
"database/sql"
"fmt"
"time"
"go-common/app/job/main/passport-auth/model"
"go-common/library/log"
)
const (
_addCookieSQL = "INSERT IGNORE INTO user_cookie_%s (mid,session,csrf,type,expires) VALUES (?,?,?,?,?)"
_delCookieBySessionSQL = "DELETE FROM user_cookie_%s where session = ?"
_addCookieDeletedSQL = "INSERT IGNORE INTO user_cookie_deleted_%s (mid,session,csrf,type,expires,ctime) VALUES (?,?,?,?,?,?)"
)
// AddCookie save cookie
func (d *Dao) AddCookie(c context.Context, cookie *model.Cookie, session, csrf []byte, ct time.Time) (affected int64, err error) {
row, err := d.db.Exec(c, fmt.Sprintf(_addCookieSQL, formatSuffix(ct)), cookie.Mid, session, csrf, cookie.Type, cookie.Expires)
if err != nil {
log.Error("dao.db.Exec(%v) err(%v)", cookie, err)
return
}
return row.RowsAffected()
}
// DelCookie del cookie by session
func (d *Dao) DelCookie(c context.Context, session []byte, ct time.Time) (affected int64, err error) {
var res sql.Result
if res, err = d.db.Exec(c, fmt.Sprintf(_delCookieBySessionSQL, formatSuffix(ct)), session); err != nil {
log.Error("del cookie by session , dao.db.Exec(%s) error(%v)", session, err)
return
}
return res.RowsAffected()
}
// AddCookieDeleted save cookie deleted
func (d *Dao) AddCookieDeleted(c context.Context, cookie *model.Cookie, session, csrf []byte, ct time.Time) (affected int64, err error) {
row, err := d.db.Exec(c, fmt.Sprintf(_addCookieDeletedSQL, formatSuffix(ct)), cookie.Mid, session, csrf, cookie.Type, cookie.Expires, cookie.Ctime)
if err != nil {
log.Error("fail to add cookie deleted, cookie(%+v), tx.Exec() error(%+v)", cookie, err)
return
}
return row.RowsAffected()
}

View File

@@ -0,0 +1,66 @@
package dao
import (
"context"
"testing"
"time"
"go-common/app/job/main/passport-auth/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddCookie(t *testing.T) {
convey.Convey("AddCookie", t, func(ctx convey.C) {
var (
c = context.Background()
cookie = &model.Cookie{}
session = []byte("712b7a22,1535703191,c07e44d8")
csrf = []byte("0273f9216fa8d6d77c3dd5499a1d0d4a")
ct = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.AddCookie(c, cookie, session, csrf, ct)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDelCookie(t *testing.T) {
convey.Convey("DelCookie", t, func(ctx convey.C) {
var (
c = context.Background()
session = []byte("712b7a22,1535703191,c07e44d8")
ct = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.DelCookie(c, session, ct)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAddCookieDeleted(t *testing.T) {
convey.Convey("AddCookieDeleted", t, func(ctx convey.C) {
var (
c = context.Background()
cookie = &model.Cookie{}
session = []byte("712b7a22,1535703191,c07e44d8")
csrf = []byte("0273f9216fa8d6d77c3dd5499a1d0d4a")
ct = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.AddCookieDeleted(c, cookie, session, csrf, ct)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,46 @@
package dao
import (
"context"
"go-common/app/job/main/passport-auth/conf"
xsql "go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
)
// Dao dao
type Dao struct {
c *conf.Config
db *xsql.DB
olddb *xsql.DB
// httpClient
httpClient *bm.Client
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
db: xsql.NewMySQL(c.MySQL),
olddb: xsql.NewMySQL(c.OldMySQL),
// httpClient
httpClient: bm.NewClient(c.HTTPClientConfig),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.db.Close()
d.olddb.Close()
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) error {
return d.pingMC(c)
}
// pingMc ping
func (d *Dao) pingMC(c context.Context) (err error) {
return
}

View File

@@ -0,0 +1,34 @@
package dao
import (
"flag"
"go-common/app/job/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-job")
flag.Set("conf_token", "ebcb53159bf293283b853091f57aa254")
flag.Set("tree_id", "35674")
flag.Set("conf_version", "uat-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", "../cmd/passport-auth-job.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,48 @@
package dao
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"time"
"go-common/app/job/main/passport-auth/model"
"go-common/library/log"
)
const (
_addRefreshSQL = "INSERT IGNORE INTO user_refresh_%s (mid,appid,refresh,token,expires) VALUES (?,?,?,?,?)"
_delRefreshSQL = "DELETE FROM user_refresh_%s WHERE refresh = ?"
)
// AddRefresh save token
func (d *Dao) AddRefresh(c context.Context, t *model.Refresh, refresh, token []byte, ct time.Time) (affected int64, err error) {
var row sql.Result
if row, err = d.db.Exec(c, fmt.Sprintf(_addRefreshSQL, formatRefreshSuffix(ct)), t.Mid, t.AppID, refresh, token, t.Expires); err != nil {
log.Error("d.AddToken(%v) err(%v)", t, err)
return
}
return row.RowsAffected()
}
// DelRefresh del token
func (d *Dao) DelRefresh(c context.Context, refresh []byte, ct time.Time) (affected int64, err error) {
var res sql.Result
if res, err = d.db.Exec(c, fmt.Sprintf(_delRefreshSQL, formatRefreshSuffix(ct)), refresh); err != nil {
log.Error("del token failed, dao.db.Exec(%s) error(%v)", hex.EncodeToString(refresh), err)
return
}
return res.RowsAffected()
}
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,75 @@
package dao
import (
"context"
"go-common/app/job/main/passport-auth/model"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddRefresh(t *testing.T) {
convey.Convey("AddRefresh", t, func(ctx convey.C) {
var (
c = context.Background()
no = &model.Refresh{}
refresh = []byte("9df38fe4b94a47baad001ad823b84110")
token = []byte("61c13e530b1418653e2fdc265b3f0fe6")
ct = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.AddRefresh(c, no, refresh, token, ct)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDelRefresh(t *testing.T) {
convey.Convey("DelRefresh", t, func(ctx convey.C) {
var (
c = context.Background()
refresh = []byte("9df38fe4b94a47baad001ad823b84110")
ct = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.DelRefresh(c, refresh, ct)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoformatRefreshSuffix(t *testing.T) {
convey.Convey("formatRefreshSuffix", t, func(ctx convey.C) {
var (
no = time.Now()
)
ctx.Convey("When everything gose positive", 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) {
convey.Convey("formatByDate", t, func(ctx convey.C) {
var (
year = int(0)
month = int(0)
)
ctx.Convey("When everything gose positive", 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,53 @@
package dao
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"time"
"go-common/app/job/main/passport-auth/model"
"go-common/library/log"
)
const (
_addTokenSQL = "INSERT IGNORE INTO user_token_%s (mid,appid,token,expires,type) VALUES (?,?,?,?,?)"
_delTokenSQL = "DELETE FROM user_token_%s where token = ?"
_addTokenDeletedSQL = "INSERT IGNORE INTO user_token_deleted_%s (mid,appid,token,expires,type,ctime) VALUES (?,?,?,?,?,?)"
)
// AddToken save token
func (d *Dao) AddToken(c context.Context, t *model.Token, token []byte, ct time.Time) (affected int64, err error) {
var row sql.Result
if row, err = d.db.Exec(c, fmt.Sprintf(_addTokenSQL, formatSuffix(ct)), t.Mid, t.AppID, token, t.Expires, t.Type); err != nil {
log.Error("d.AddToken(%v) err(%v)", t, err)
return
}
return row.RowsAffected()
}
// DelToken del token
func (d *Dao) DelToken(c context.Context, token []byte, ct time.Time) (affected int64, err error) {
var res sql.Result
if res, err = d.db.Exec(c, fmt.Sprintf(_delTokenSQL, formatSuffix(ct)), token); err != nil {
log.Error("del token failed, dao.db.Exec(%s) error(%v)", hex.EncodeToString(token), err)
return
}
return res.RowsAffected()
}
// AddTokenDeleted save token deleted
func (d *Dao) AddTokenDeleted(c context.Context, t *model.Token, token []byte, ct time.Time) (affected int64, err error) {
row, err := d.db.Exec(c, fmt.Sprintf(_addTokenDeletedSQL, formatSuffix(ct)), t.Mid, t.AppID, token, t.Expires, t.Type, t.Ctime)
if err != nil {
log.Error("fail to add token deleted, token(%+v), tx.Exec() error(%+v)", t, err)
return
}
return row.RowsAffected()
}
func formatSuffix(t time.Time) string {
return t.Format("200601")
}

View File

@@ -0,0 +1,78 @@
package dao
import (
"context"
"testing"
"time"
"go-common/app/job/main/passport-auth/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddToken(t *testing.T) {
convey.Convey("AddToken", t, func(ctx convey.C) {
var (
c = context.Background()
no = &model.Token{}
token = []byte("9df38fe4b94a47baad001ad823b84110")
ct = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.AddToken(c, no, token, ct)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDelToken(t *testing.T) {
convey.Convey("DelToken", t, func(ctx convey.C) {
var (
c = context.Background()
token = []byte("9df38fe4b94a47baad001ad823b84110")
ct = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.DelToken(c, token, ct)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAddTokenDeleted(t *testing.T) {
convey.Convey("AddTokenDeleted", t, func(ctx convey.C) {
var (
c = context.Background()
no = &model.Token{}
token = []byte("9df38fe4b94a47baad001ad823b84110")
ct = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
affected, err := d.AddTokenDeleted(c, no, token, ct)
ctx.Convey("Then err should be nil.affected should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoformatSuffix(t *testing.T) {
convey.Convey("formatSuffix", t, func(ctx convey.C) {
var (
no = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := formatSuffix(no)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}