verify.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "database/sql"
  18. "fmt"
  19. "math/rand"
  20. "time"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/notify/options"
  25. )
  26. type SVerificationManager struct {
  27. db.SStandaloneResourceBaseManager
  28. }
  29. var VerificationManager *SVerificationManager
  30. func init() {
  31. VerificationManager = &SVerificationManager{
  32. SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager(
  33. SVerification{},
  34. "verification_tbl",
  35. "verification",
  36. "verifications",
  37. ),
  38. }
  39. VerificationManager.SetVirtualObject(VerificationManager)
  40. }
  41. // +onecloud:swagger-gen-ignore
  42. type SVerification struct {
  43. db.SStandaloneResourceBase
  44. ReceiverId string `width:"128" nullable:"false"`
  45. ContactType string `width:"16" nullable:"false"`
  46. Token string `width:"200" nullable:"false"`
  47. }
  48. var ErrVerifyFrequently = errors.Wrap(httperrors.ErrTooManyRequests, "Send validation messages too frequently")
  49. func (vm *SVerificationManager) generateVerifyToken() string {
  50. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  51. token := fmt.Sprintf("%06v", rnd.Int31n(1000000))
  52. return token
  53. }
  54. func (vm *SVerificationManager) Create(ctx context.Context, receiverId, contactType string) (*SVerification, error) {
  55. // try to reuse
  56. ret, err := vm.Get(receiverId, contactType)
  57. if err != nil && errors.Cause(err) != sql.ErrNoRows {
  58. return nil, err
  59. }
  60. if ret == nil {
  61. ret = &SVerification{
  62. ReceiverId: receiverId,
  63. ContactType: contactType,
  64. Token: vm.generateVerifyToken(),
  65. }
  66. err := vm.TableSpec().Insert(ctx, ret)
  67. if err != nil {
  68. return nil, err
  69. }
  70. } else {
  71. now := time.Now()
  72. if now.Before(ret.CreatedAt.Add(time.Duration(options.Options.VerifyExpireInterval) * time.Minute)) {
  73. return nil, ErrVerifyFrequently
  74. }
  75. _, err := db.Update(ret, func() error {
  76. ret.Token = vm.generateVerifyToken()
  77. ret.CreatedAt = now
  78. ret.UpdatedAt = now
  79. return nil
  80. })
  81. if err != nil {
  82. return nil, err
  83. }
  84. }
  85. return ret, nil
  86. }
  87. func (vm *SVerificationManager) Get(receiverId, contactType string) (*SVerification, error) {
  88. q := vm.Query().Equals("receiver_id", receiverId).Equals("contact_type", contactType)
  89. var verification SVerification
  90. err := q.First(&verification)
  91. if err != nil {
  92. return nil, err
  93. }
  94. verification.SetModelManager(vm, &verification)
  95. return &verification, nil
  96. }