auth.go 803 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package controllers
  2. import (
  3. "encoding/hex"
  4. "time"
  5. "ehang.io/nps/lib/crypt"
  6. "github.com/astaxie/beego"
  7. )
  8. type AuthController struct {
  9. beego.Controller
  10. }
  11. func (s *AuthController) GetAuthKey() {
  12. m := make(map[string]interface{})
  13. defer func() {
  14. s.Data["json"] = m
  15. s.ServeJSON()
  16. }()
  17. if cryptKey := beego.AppConfig.String("auth_crypt_key"); len(cryptKey) != 16 {
  18. m["status"] = 0
  19. return
  20. } else {
  21. b, err := crypt.AesEncrypt([]byte(beego.AppConfig.String("auth_key")), []byte(cryptKey))
  22. if err != nil {
  23. m["status"] = 0
  24. return
  25. }
  26. m["status"] = 1
  27. m["crypt_auth_key"] = hex.EncodeToString(b)
  28. m["crypt_type"] = "aes cbc"
  29. return
  30. }
  31. }
  32. func (s *AuthController) GetTime() {
  33. m := make(map[string]interface{})
  34. m["time"] = time.Now().Unix()
  35. s.Data["json"] = m
  36. s.ServeJSON()
  37. }