permit.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package dao
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "encoding/hex"
  6. "net/url"
  7. "go-common/library/ecode"
  8. "go-common/library/net/http/blademaster/middleware/permit"
  9. )
  10. const (
  11. _sessionlen = 32
  12. _sessionLife = 2592000
  13. _dsbCaller = "manager-go"
  14. _dsbVerifyURL = "http://dashboard-mng.bilibili.co/api/session/verify"
  15. )
  16. // VerifyDsb .
  17. func (d *Dao) VerifyDsb(ctx context.Context, sid string) (res string, err error) {
  18. params := url.Values{}
  19. params.Set("session_id", sid)
  20. params.Set("encrypt", "md5")
  21. params.Set("caller", _dsbCaller)
  22. var dsbRes struct {
  23. Code int `json:"code"`
  24. UserName string `json:"username"`
  25. }
  26. if err = d.dsbClient.Get(ctx, _dsbVerifyURL, "", params, &dsbRes); err != nil {
  27. return
  28. }
  29. if ecode.Int(dsbRes.Code) != ecode.OK {
  30. err = ecode.Int(dsbRes.Code)
  31. return
  32. }
  33. res = dsbRes.UserName
  34. return
  35. }
  36. // NewSession .
  37. func (d *Dao) NewSession(ctx context.Context) (res *permit.Session) {
  38. b := make([]byte, _sessionlen)
  39. n, err := rand.Read(b)
  40. if n != len(b) || err != nil {
  41. return
  42. }
  43. res = &permit.Session{
  44. Sid: hex.EncodeToString(b),
  45. Values: make(map[string]interface{}),
  46. }
  47. return
  48. }