/
models.go
89 lines (78 loc) · 2.04 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package models
import (
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/duo-labs/webauthn.io/config"
log "github.com/duo-labs/webauthn.io/logger"
_ "github.com/go-sql-driver/mysql" // Blank import needed to import mysql
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3
)
var db *gorm.DB
var err error
// ErrUsernameTaken is thrown when a user attempts to register a username that is taken.
var ErrUsernameTaken = errors.New("username already taken")
// Copy of auth.GenerateSecureKey to prevent cyclic import with auth library
func generateSecureKey() string {
k := make([]byte, 32)
io.ReadFull(rand.Reader, k)
return fmt.Sprintf("%x", k)
}
// BytesToID converts a byte slice to a uint. This is needed because the
// WebAuthn specification deals with byte buffers, while the primary keys in
// our database are uints.
func BytesToID(buf []byte) uint {
// TODO: Probably want to catch the number of bytes converted in production
id, _ := binary.Uvarint(buf)
return uint(id)
}
// Setup initializes the Conn object
// It also populates the Config object
func Setup(config *config.Config) error {
createDb := false
if _, err = os.Stat(config.DBPath); err != nil || config.DBPath == ":memory:" {
createDb = true
}
// Open our database connection
db, err = gorm.Open(config.DBName, config.DBPath)
if err != nil {
return err
}
db.LogMode(false)
db.SetLogger(log.Logger)
db.DB().SetMaxOpenConns(1)
if err != nil {
return err
}
// Migrate up to the latest version
//If the database didn't exist, we need to create the admin user
err := db.AutoMigrate(
&User{},
&Credential{},
&Authenticator{},
).Error
if err != nil {
return err
}
gorm.NowFunc = func() time.Time {
return time.Now().UTC()
}
if createDb {
// Create the default user
initUser := User{
Username: "admin",
DisplayName: "Example Admin",
}
err = db.Save(&initUser).Error
if err != nil {
log.Infof("error creating initial user: %s", err)
return err
}
}
return nil
}