simple_aaa.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package hhc_telws
  2. import (
  3. "fmt"
  4. "github.com/pascaldekloe/jwt"
  5. "golang.org/x/crypto/bcrypt"
  6. "time"
  7. )
  8. type TelwsAuthSimpleAAA struct {
  9. jwtKey []byte
  10. userDatabase map[string]string
  11. authLogFunc func(isSuccess bool, msg string)
  12. jwtttl time.Duration
  13. }
  14. func NewTelwsAuthSimpleAAA() *TelwsAuthSimpleAAA {
  15. ah := &TelwsAuthSimpleAAA{
  16. jwtKey: []byte{},
  17. userDatabase: make(map[string]string),
  18. authLogFunc: func(isSuccess bool, msg string) {},
  19. }
  20. return ah
  21. }
  22. func (ah *TelwsAuthSimpleAAA) SetAuthLogFunc(authLogFunc func(isSuccess bool, msg string)) {
  23. ah.authLogFunc = authLogFunc
  24. }
  25. func (ah *TelwsAuthSimpleAAA) SimpleAAAAddSimple(username, password string, cost int) error {
  26. hash, err := bcrypt.GenerateFromPassword([]byte(password), cost)
  27. if err != nil {
  28. return err
  29. }
  30. ah.userDatabase[username] = string(hash)
  31. return nil
  32. }
  33. func (ah *TelwsAuthSimpleAAA) SimpleAAAAddBcrypt(username, hash string) {
  34. ah.userDatabase[username] = hash
  35. }
  36. func (ah *TelwsAuthSimpleAAA) Register(jwtkey []byte, jwtttl time.Duration) (ahname string) {
  37. ah.jwtKey = jwtkey
  38. ah.jwtttl = jwtttl
  39. return "simple-aaa"
  40. }
  41. func (ah *TelwsAuthSimpleAAA) Login(authData map[string]string) (isSuccess bool, jwtdata string, emsg string) {
  42. amet, ok := authData["method"]
  43. if !ok {
  44. ah.authLogFunc(false, "no auth method specified")
  45. return false, "", "no auth method specified"
  46. }
  47. if amet != "simple-aaa" {
  48. ah.authLogFunc(false, "auth method not support")
  49. return false, "", "auth method not support"
  50. }
  51. username, ok := authData["username"]
  52. if !ok {
  53. ah.authLogFunc(false, "no username specified")
  54. return false, "", "no username specified"
  55. }
  56. password, ok := authData["password"]
  57. if !ok {
  58. ah.authLogFunc(false, "no password specified")
  59. return false, "", "no password specified"
  60. }
  61. if username == "" {
  62. ah.authLogFunc(false, "username can not be null")
  63. return false, "", "username can not be null"
  64. }
  65. ubc, ok := ah.userDatabase[username]
  66. if !ok {
  67. ah.authLogFunc(false, fmt.Sprintf("user '%s' not exist", username))
  68. return false, "", "username or password not match"
  69. }
  70. err := bcrypt.CompareHashAndPassword([]byte(ubc), []byte(password))
  71. if err != nil {
  72. ah.authLogFunc(false, fmt.Sprintf("user '%s' auth failed with error: %s", username, err.Error()))
  73. return false, "", "username or password not match"
  74. }
  75. clm := jwt.Claims{}
  76. clm.Issuer = "simple-aaa"
  77. clm.Subject = "telws-auth"
  78. ntm := jwt.NewNumericTime(time.Now())
  79. clm.Issued = ntm
  80. clm.NotBefore = ntm
  81. clm.Expires = jwt.NewNumericTime(time.Now().Add(ah.jwtttl))
  82. clm.Set = map[string]interface{}{
  83. "username": username,
  84. }
  85. tok, err := clm.HMACSign(jwt.HS256, ah.jwtKey)
  86. if err != nil {
  87. ah.authLogFunc(false, fmt.Sprintf("user '%s' auth success but generate jwt error: %s", username, err.Error()))
  88. return false, "", "internal error"
  89. }
  90. ah.authLogFunc(false, fmt.Sprintf("user '%s' auth success", username))
  91. return true, string(tok), ""
  92. }