go-common/app/admin/main/manager/dao/permit.go
2019-04-22 18:49:16 +08:00

54 lines
1.1 KiB
Go

package dao
import (
"context"
"crypto/rand"
"encoding/hex"
"net/url"
"go-common/library/ecode"
"go-common/library/net/http/blademaster/middleware/permit"
)
const (
_sessionlen = 32
_sessionLife = 2592000
_dsbCaller = "manager-go"
_dsbVerifyURL = "http://dashboard-mng.bilibili.co/api/session/verify"
)
// VerifyDsb .
func (d *Dao) VerifyDsb(ctx context.Context, sid string) (res string, err error) {
params := url.Values{}
params.Set("session_id", sid)
params.Set("encrypt", "md5")
params.Set("caller", _dsbCaller)
var dsbRes struct {
Code int `json:"code"`
UserName string `json:"username"`
}
if err = d.dsbClient.Get(ctx, _dsbVerifyURL, "", params, &dsbRes); err != nil {
return
}
if ecode.Int(dsbRes.Code) != ecode.OK {
err = ecode.Int(dsbRes.Code)
return
}
res = dsbRes.UserName
return
}
// NewSession .
func (d *Dao) NewSession(ctx context.Context) (res *permit.Session) {
b := make([]byte, _sessionlen)
n, err := rand.Read(b)
if n != len(b) || err != nil {
return
}
res = &permit.Session{
Sid: hex.EncodeToString(b),
Values: make(map[string]interface{}),
}
return
}