本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.3KB

  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. "github.com/tstranex/u2f"
  9. )
  10. // U2FRegistration represents the registration data and counter of a security key
  11. type U2FRegistration struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. Name string
  14. UserID int64 `xorm:"INDEX"`
  15. Raw []byte
  16. Counter uint32 `xorm:"BIGINT"`
  17. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  18. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  19. }
  20. // TableName returns a better table name for U2FRegistration
  21. func (reg U2FRegistration) TableName() string {
  22. return "u2f_registration"
  23. }
  24. // Parse will convert the db entry U2FRegistration to an u2f.Registration struct
  25. func (reg *U2FRegistration) Parse() (*u2f.Registration, error) {
  26. r := new(u2f.Registration)
  27. return r, r.UnmarshalBinary(reg.Raw)
  28. }
  29. func (reg *U2FRegistration) updateCounter(e Engine) error {
  30. _, err := e.ID(reg.ID).Cols("counter").Update(reg)
  31. return err
  32. }
  33. // UpdateCounter will update the database value of counter
  34. func (reg *U2FRegistration) UpdateCounter() error {
  35. return reg.updateCounter(x)
  36. }
  37. // U2FRegistrationList is a list of *U2FRegistration
  38. type U2FRegistrationList []*U2FRegistration
  39. // ToRegistrations will convert all U2FRegistrations to u2f.Registrations
  40. func (list U2FRegistrationList) ToRegistrations() []u2f.Registration {
  41. regs := make([]u2f.Registration, 0, len(list))
  42. for _, reg := range list {
  43. r, err := reg.Parse()
  44. if err != nil {
  45. log.Fatal("parsing u2f registration: %v", err)
  46. continue
  47. }
  48. regs = append(regs, *r)
  49. }
  50. return regs
  51. }
  52. func getU2FRegistrationsByUID(e Engine, uid int64) (U2FRegistrationList, error) {
  53. regs := make(U2FRegistrationList, 0)
  54. return regs, e.Where("user_id = ?", uid).Find(&regs)
  55. }
  56. // GetU2FRegistrationByID returns U2F registration by id
  57. func GetU2FRegistrationByID(id int64) (*U2FRegistration, error) {
  58. return getU2FRegistrationByID(x, id)
  59. }
  60. func getU2FRegistrationByID(e Engine, id int64) (*U2FRegistration, error) {
  61. reg := new(U2FRegistration)
  62. if found, err := e.ID(id).Get(reg); err != nil {
  63. return nil, err
  64. } else if !found {
  65. return nil, ErrU2FRegistrationNotExist{ID: id}
  66. }
  67. return reg, nil
  68. }
  69. // GetU2FRegistrationsByUID returns all U2F registrations of the given user
  70. func GetU2FRegistrationsByUID(uid int64) (U2FRegistrationList, error) {
  71. return getU2FRegistrationsByUID(x, uid)
  72. }
  73. func createRegistration(e Engine, user *User, name string, reg *u2f.Registration) (*U2FRegistration, error) {
  74. raw, err := reg.MarshalBinary()
  75. if err != nil {
  76. return nil, err
  77. }
  78. r := &U2FRegistration{
  79. UserID: user.ID,
  80. Name: name,
  81. Counter: 0,
  82. Raw: raw,
  83. }
  84. _, err = e.InsertOne(r)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return r, nil
  89. }
  90. // CreateRegistration will create a new U2FRegistration from the given Registration
  91. func CreateRegistration(user *User, name string, reg *u2f.Registration) (*U2FRegistration, error) {
  92. return createRegistration(x, user, name, reg)
  93. }
  94. // DeleteRegistration will delete U2FRegistration
  95. func DeleteRegistration(reg *U2FRegistration) error {
  96. return deleteRegistration(x, reg)
  97. }
  98. func deleteRegistration(e Engine, reg *U2FRegistration) error {
  99. _, err := e.Delete(reg)
  100. return err
  101. }
上海开阖软件有限公司 沪ICP备12045867号-1