-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseHandler.go
170 lines (149 loc) · 4.05 KB
/
databaseHandler.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
// DatabaseHandler is entry point to databases.
// Stores pointers to databases.
type DatabaseHandler struct {
db *sql.DB
}
// QueryResults is alias for return type of database functions.
type QueryResults int
const (
// RegisterOK - Register was successfull.
RegisterOK QueryResults = 0
// RegisterLoginExists - Login already exists.
RegisterLoginExists QueryResults = 1
// RegisterEmailExists - E-mail already exists.
RegisterEmailExists QueryResults = 2
// RegisterPasswordError - bcrypt returned an error.
RegisterPasswordError QueryResults = 3
// LoginOK - Login was successfull.
LoginOK QueryResults = 0
// LoginWrongLogin - Login is unknown or unregistered.
LoginWrongLogin QueryResults = 1
// LoginWrongPassword - Login is registered, but passworld doesn't match.
LoginWrongPassword QueryResults = 2
)
// init is DatabaseHandler constructor.
/*
- opens `SQL` database(s).
- initializes database's tables if they aren't present
**Note:** database must be closed on program exit. */
func (p *DatabaseHandler) init(s *Server) error {
db, err := sql.Open("sqlite3", s.srcDir+"/database.db")
if err != nil {
fmt.Println(err.Error())
return StringError{"ERROR: Some of databases weren't opened!"}
}
p.db = db
p.createTable()
return nil
}
// UserRecord is single user in `userData` database.
type UserRecord struct {
hash string
login string
pass string
email string
group string
class string
}
// Creates userData table
func (p *DatabaseHandler) createTable() error {
var table SQLiteTable
table.setName("userData")
table.addColumn([]SQLColumn{
SQLiteColumn{name: "id", sqlType: "INTEGER", args: "PRIMARY KEY"},
SQLiteColumn{name: "login", sqlType: "VARCHAR(20)", args: ""},
SQLiteColumn{name: "password", sqlType: "VARCHAR(128)", args: ""},
SQLiteColumn{name: "email", sqlType: "VARCHAR(128)", args: ""},
SQLiteColumn{name: "account_type", sqlType: "VARCHAR(20)", args: ""},
SQLiteColumn{name: "assigned_class", sqlType: "VARCHAR(20)", args: ""}})
_, err := table.create(p.db)
if err != nil {
fmt.Println(err.Error())
}
return err
}
func (p *DatabaseHandler) register(login string, email string, pass string) QueryResults {
if p.userExist(login, LoginLoginMethod) {
return RegisterLoginExists
}
if p.userExist(email, EmailLoginMethod) {
return RegisterEmailExists
}
passHash, err := HashPassword(pass)
if err != nil {
println(err.Error())
return RegisterPasswordError
}
p.createRecord(UserRecord{
hash: "0",
login: login,
pass: passHash,
email: email,
group: "undefined",
class: "undefined"})
return RegisterOK
}
func (p *DatabaseHandler) createRecord(r UserRecord) {
register := "INSERT INTO userData " +
"(login, password, email, account_type, assigned_class)" +
"VALUES (?, ?, ?, ?, ?);"
_, err := p.db.Exec(register, r.login, r.pass, r.email, r.group, r.class)
if err != nil {
fmt.Println(err.Error())
}
}
type LoginMethod string
const (
LoginLoginMethod LoginMethod = "login"
EmailLoginMethod LoginMethod = "email"
)
func (p *DatabaseHandler) userExist(username string, method LoginMethod) bool {
userExist := "SELECT ? " +
"FROM userData " +
"WHERE ? = ?;"
result, err := p.db.Query(userExist, method, method, username)
if err != nil {
fmt.Println(err.Error())
return false
}
for result.Next() {
return true
}
defer result.Close()
return false
}
func (p *DatabaseHandler) login(username string, pass string, method LoginMethod) bool {
var login string
if method == LoginLoginMethod {
login = "SELECT login, password " +
"FROM userData " +
"WHERE login = ?"
} else if method == EmailLoginMethod {
login = "SELECT email, password " +
"FROM userData " +
"WHERE email = ?"
}
result, err := p.db.Query(login, username)
if err != nil {
fmt.Println(err.Error())
return false
}
for result.Next() {
var login string
var passHash string
result.Scan(&login, &passHash)
if CheckPasswordHash(pass, passHash) {
return true
} else {
return false
}
}
defer result.Close()
return false
}