|
| 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 | + |
| 5 | +package models |
| 6 | + |
| 7 | +import ( |
| 8 | + "code.gitea.io/gitea/modules/log" |
| 9 | + "code.gitea.io/gitea/modules/util" |
| 10 | + |
| 11 | + "github.com/tstranex/u2f" |
| 12 | +) |
| 13 | + |
| 14 | +// U2FRegistration represents the registration data and counter of a security key |
| 15 | +type U2FRegistration struct { |
| 16 | + ID int64 `xorm:"pk autoincr"` |
| 17 | + Name string |
| 18 | + UserID int64 `xorm:"INDEX"` |
| 19 | + Raw []byte |
| 20 | + Counter uint32 |
| 21 | + CreatedUnix util.TimeStamp `xorm:"INDEX created"` |
| 22 | + UpdatedUnix util.TimeStamp `xorm:"INDEX updated"` |
| 23 | +} |
| 24 | + |
| 25 | +// TableName returns a better table name for U2FRegistration |
| 26 | +func (reg U2FRegistration) TableName() string { |
| 27 | + return "u2f_registration" |
| 28 | +} |
| 29 | + |
| 30 | +// Parse will convert the db entry U2FRegistration to an u2f.Registration struct |
| 31 | +func (reg *U2FRegistration) Parse() (*u2f.Registration, error) { |
| 32 | + r := new(u2f.Registration) |
| 33 | + return r, r.UnmarshalBinary(reg.Raw) |
| 34 | +} |
| 35 | + |
| 36 | +func (reg *U2FRegistration) updateCounter(e Engine) error { |
| 37 | + _, err := e.ID(reg.ID).Cols("counter").Update(reg) |
| 38 | + return err |
| 39 | +} |
| 40 | + |
| 41 | +// UpdateCounter will update the database value of counter |
| 42 | +func (reg *U2FRegistration) UpdateCounter() error { |
| 43 | + return reg.updateCounter(x) |
| 44 | +} |
| 45 | + |
| 46 | +// U2FRegistrationList is a list of *U2FRegistration |
| 47 | +type U2FRegistrationList []*U2FRegistration |
| 48 | + |
| 49 | +// ToRegistrations will convert all U2FRegistrations to u2f.Registrations |
| 50 | +func (list U2FRegistrationList) ToRegistrations() []u2f.Registration { |
| 51 | + regs := make([]u2f.Registration, len(list)) |
| 52 | + for _, reg := range list { |
| 53 | + r, err := reg.Parse() |
| 54 | + if err != nil { |
| 55 | + log.Fatal(4, "parsing u2f registration: %v", err) |
| 56 | + continue |
| 57 | + } |
| 58 | + regs = append(regs, *r) |
| 59 | + } |
| 60 | + |
| 61 | + return regs |
| 62 | +} |
| 63 | + |
| 64 | +func getU2FRegistrationsByUID(e Engine, uid int64) (U2FRegistrationList, error) { |
| 65 | + regs := make(U2FRegistrationList, 0) |
| 66 | + return regs, e.Where("user_id = ?", uid).Find(®s) |
| 67 | +} |
| 68 | + |
| 69 | +// GetU2FRegistrationByID returns U2F registration by id |
| 70 | +func GetU2FRegistrationByID(id int64) (*U2FRegistration, error) { |
| 71 | + return getU2FRegistrationByID(x, id) |
| 72 | +} |
| 73 | + |
| 74 | +func getU2FRegistrationByID(e Engine, id int64) (*U2FRegistration, error) { |
| 75 | + reg := new(U2FRegistration) |
| 76 | + if found, err := e.ID(id).Get(reg); err != nil { |
| 77 | + return nil, err |
| 78 | + } else if !found { |
| 79 | + return nil, ErrU2FRegistrationNotExist{ID: id} |
| 80 | + } |
| 81 | + return reg, nil |
| 82 | +} |
| 83 | + |
| 84 | +// GetU2FRegistrationsByUID returns all U2F registrations of the given user |
| 85 | +func GetU2FRegistrationsByUID(uid int64) (U2FRegistrationList, error) { |
| 86 | + return getU2FRegistrationsByUID(x, uid) |
| 87 | +} |
| 88 | + |
| 89 | +func createRegistration(e Engine, user *User, name string, reg *u2f.Registration) (*U2FRegistration, error) { |
| 90 | + raw, err := reg.MarshalBinary() |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + r := &U2FRegistration{ |
| 95 | + UserID: user.ID, |
| 96 | + Name: name, |
| 97 | + Counter: 0, |
| 98 | + Raw: raw, |
| 99 | + } |
| 100 | + _, err = e.InsertOne(r) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + return r, nil |
| 105 | +} |
| 106 | + |
| 107 | +// CreateRegistration will create a new U2FRegistration from the given Registration |
| 108 | +func CreateRegistration(user *User, name string, reg *u2f.Registration) (*U2FRegistration, error) { |
| 109 | + return createRegistration(x, user, name, reg) |
| 110 | +} |
| 111 | + |
| 112 | +// DeleteRegistration will delete U2FRegistration |
| 113 | +func DeleteRegistration(reg *U2FRegistration) error { |
| 114 | + return deleteRegistration(x, reg) |
| 115 | +} |
| 116 | + |
| 117 | +func deleteRegistration(e Engine, reg *U2FRegistration) error { |
| 118 | + _, err := e.Delete(reg) |
| 119 | + return err |
| 120 | +} |
0 commit comments