-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
225 lines (205 loc) · 5.83 KB
/
auth.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
*
*
*
* @author John Aran (Ilyas Aranzhanovich Toxanbayev)
* @version 1.0.0
* @based on
* @email il.aranov@gmail.com
* @link
* @github https://github.com/ilyaran/github.com/ilyaran/Malina
* @license MIT License Copyright (c) 2017 John Aran (Ilyas Toxanbayev)
*/ package models
import (
"database/sql"
"strconv"
"github.com/ilyaran/Malina/entity"
"github.com/ilyaran/Malina/app"
)
var AuthModel = new(authModel)
type authModel struct{
}
// param must have format example: CheckDetails("email = ?","ilyaran@mail.ru")
func (this *authModel)CheckDetails(param, value string)int64{
var all int64
query := `SELECT count(*) FROM account WHERE `+param+` LIMIT 1`
err := CrudGeneral.DB.QueryRow(query, value).Scan(&all)
if err == sql.ErrNoRows {
panic(err)
return -1
}
if err != nil {
panic(err)
return -2
}
return all
}
func (this *authModel) AddActivation(email,pass,ip_address string) string {
var activation_key string
query := `
INSERT INTO activation
(activation_email,activation_password,activation_key,activation_last_ip)
VALUES
($1,$2,uuid_generate_v4(),$3)
ON CONFLICT (activation_email) DO UPDATE
SET activation_password = $2,
activation_key = uuid_generate_v4(),
activation_last_ip = $3,
activation_created = date_part('epoch',CURRENT_TIMESTAMP)::bigint
RETURNING activation_key`
err:=CrudGeneral.DB.QueryRow(query,email,pass,ip_address).Scan(&activation_key)
if err == sql.ErrNoRows {
panic(err)
return ""
}
if err != nil {
panic(err)
return ""
}
return activation_key
}
func (s *authModel)GetActivation(activation_key string) *entity.Account{
var user_password string
var user_email string
err := CrudGeneral.DB.QueryRow(`
WITH t AS (
DELETE FROM activation WHERE
date_part('epoch',CURRENT_TIMESTAMP)::bigint - activation_created > `+strconv.FormatInt(app.Email_activation_expire,10)+`
)
SELECT activation_email, activation_password
FROM activation WHERE activation_key = $1 LIMIT 1
`, activation_key).Scan(&user_email,&user_password)
if err == sql.ErrNoRows{
//panic(err)
return nil
}
if err != nil {
panic(err)
return nil
}
go s.RemoveFromActivateTable(user_email)
return &entity.Account{Email:user_email,Newpass:user_password}
}
func (this *authModel) RemoveFromActivateTable(user_email string) {
CrudGeneral.Delete(`DELETE FROM activation WHERE activation_email = $1`, user_email)
}
func (this *authModel)AddAccount(q string,exec... interface{})int64{
return CrudGeneral.Insert(`INSERT INTO account `+q +` RETURNING account_id`, exec...)
}
func (this *authModel)DeleteSession(cookieValue string) bool {
if CrudGeneral.Delete(`DELETE FROM session WHERE session_id = $1`, cookieValue) > 0 {
return true
}
return false
}
func (this *authModel) CountLoginAttempts(ip_address string) int64{
var all int64
err := CrudGeneral.DB.QueryRow(`SELECT COUNT(*) FROM attempt WHERE attempt_ip = $1`, ip_address).Scan(&all)
if err == sql.ErrNoRows {
all = 0
} else if err != nil {
panic(err)
all = -1
}
return all
}
func (this *authModel) IncreaseLoginAttempts(ip_address string) bool {
query := `
WITH t AS (
DELETE FROM attempt
WHERE attempt_time + ` + strconv.Itoa(app.Login_attempts_period) + ` < date_part('epoch',CURRENT_TIMESTAMP)::bigint
)
INSERT INTO attempt (attempt_ip) VALUES ($1) RETURNING attempt_id`
if CrudGeneral.Insert(query, ip_address) > 0 {
return true
}
return false
}
func (this *authModel) ClearLoginAttempts(ip_address string) bool {
if CrudGeneral.Delete(`DELETE FROM attempt WHERE attempt_ip = $1`, ip_address) > 0 {
return true
}
return false
}
func (this *authModel)ForgotPass(email, cryptedNewPass string) (string, bool) {
if CrudGeneral.Update(`
UPDATE account SET account_password = $1 WHERE account_email = $2
`, cryptedNewPass, email) > 0 {
return email, true
}
return "", false
}
func (this *authModel) DeleteUserByEmailPassword(email,password string) {
if password != ""{
CrudGeneral.Delete(`DELETE FROM account WHERE account_email = $1 AND account_password = $2`, email,password)
return
}else {
CrudGeneral.Delete(`DELETE FROM account WHERE account_email = $1`, email)
return
}
}
func (this *authModel) ChangePass(email, cryptedOldPass, cryptedNewPass string) (string, bool) {
var userId int64
query := `SELECT account_id FROM account
WHERE account_email = $1 AND account_password = $2 LIMIT 1`
err := CrudGeneral.DB.QueryRow(query, email, cryptedOldPass).Scan(&userId)
if err == sql.ErrNoRows {
return "no_rows", false
} else if err != nil {
panic(err)
return "server_error", false
} else {
query := `
UPDATE account SET account_password = $1
WHERE account_email = $2 AND account_password = $3
`
if CrudGeneral.Update(query, cryptedNewPass, email, cryptedOldPass) > 0 {
return ``, true
}
}
return ``, false
}
func (this *authModel)GetAccountByEmail(id int64,email,sqlSelectFields string)*entity.Account{
var query string
var row *sql.Row
if email != "" {
query = `
SELECT `+sqlSelectFields+`
FROM account
WHERE account_email = $1 LIMIT 1`
row = CrudGeneral.DB.QueryRow(query, email)
}
if id > 0 {
query = `
SELECT `+sqlSelectFields+`
FROM account
WHERE account_id = $1 LIMIT 1`
row = CrudGeneral.DB.QueryRow(query, id)
}
account:=&entity.Account{}
if account.Scanning(row,nil) == 'o'{
return account
}
return nil
}
func (this *authModel) CheckIsAllreadySentToActivation(email,nick,phone string) bool {
var all int64
var where string
var exec []interface{}
if email != "" {
where = "activation_email = $1"
exec = []interface{}{email}
}else if nick != "" {
where = "activation_nick = $1"
exec = []interface{}{nick}
}
query := `SELECT count(*) FROM activation WHERE ` + where
err := CrudGeneral.DB.QueryRow(query, exec).Scan(&all)
if err == sql.ErrNoRows {
return false
} else if err != nil {
panic(err)
return false
}
return all > 0
}