aboutsummaryrefslogtreecommitdiff
path: root/util/verification.go
blob: c64b54d7828d8f2bdee4c267dff94322da96a58a (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package util

import (
	"fmt"
	"math/rand"
	"net/smtp"
	"os"
	"os/exec"
	"strings"
	"time"

	"github.com/FChannel0/FChannel-Server/config"
	"github.com/gofiber/fiber/v2"
	_ "github.com/lib/pq"
)

type Verify struct {
	Type       string
	Identifier string
	Code       string
	Created    string
	Board      string
}

type VerifyCooldown struct {
	Identifier string
	Code       string
	Time       int
}

type Signature struct {
	KeyId     string
	Headers   []string
	Signature string
	Algorithm string
}

func (verify Verify) Create() error {
	query := `insert into verification (type, identifier, code, created) values ($1, $2, $3, $4)`
	_, err := config.DB.Exec(query, verify.Type, verify.Identifier, verify.Code, time.Now().UTC().Format(time.RFC3339))

	return MakeError(err, "Create")
}

func (verify Verify) CreateBoardAccess() error {
	hasAccess, err := verify.HasBoardAccess()

	if err != nil {
		return MakeError(err, "CreateBoardAccess")
	}

	if !hasAccess {
		query := `insert into boardaccess (identifier, board) values($1, $2)`
		_, err := config.DB.Exec(query, verify.Identifier, verify.Board)

		return MakeError(err, "CreateBoardAccess")
	}

	return nil
}

func (verify Verify) CreateBoardMod() error {
	var pass string
	var err error

	if pass, err = CreateKey(50); err != nil {
		return MakeError(err, "CreateBoardMod")
	}

	var code string

	query := `select code from verification where identifier=$1 and type=$2`
	if err := config.DB.QueryRow(query, verify.Board, verify.Type).Scan(&code); err != nil {
		return nil
	}

	var ident string

	query = `select identifier from boardaccess where identifier=$1 and board=$2`
	if err := config.DB.QueryRow(query, verify.Identifier, verify.Board).Scan(&ident); err != nil {
		return nil
	}

	if ident != verify.Identifier {
		query := `insert into crossverification (verificationcode, code) values ($1, $2)`
		if _, err := config.DB.Exec(query, code, pass); err != nil {
			return MakeError(err, "CreateBoardMod")
		}

		query = `insert into boardaccess (identifier, code, board, type) values ($1, $2, $3, $4)`
		if _, err = config.DB.Exec(query, verify.Identifier, pass, verify.Board, verify.Type); err != nil {
			return MakeError(err, "CreateBoardMod")
		}

		config.Log.Printf("Board access - Board: %s, Identifier: %s, Code: %s\n", verify.Board, verify.Identifier, pass)
	}

	return nil
}

func (verify Verify) DeleteBoardMod() error {
	var code string

	query := `select code from boardaccess where identifier=$1 and board=$1`
	if err := config.DB.QueryRow(query, verify.Identifier, verify.Board).Scan(&code); err != nil {
		return nil
	}

	query = `delete from crossverification where code=$1`
	if _, err := config.DB.Exec(query, code); err != nil {
		return MakeError(err, "DeleteBoardMod")
	}

	query = `delete from boardaccess where identifier=$1 and board=$2`
	if _, err := config.DB.Exec(query, verify.Identifier, verify.Board); err != nil {
		return MakeError(err, "DeleteBoardMod")
	}

	return nil
}

func (verify Verify) GetBoardMod() (Verify, error) {
	var nVerify Verify

	query := `select code, board, type, identifier from boardaccess where identifier=$1`
	if err := config.DB.QueryRow(query, verify.Identifier).Scan(&nVerify.Code, &nVerify.Board, &nVerify.Type, &nVerify.Identifier); err != nil {
		return nVerify, MakeError(err, "GetBoardMod")
	}

	return nVerify, nil
}

func (verify Verify) GetCode() (Verify, error) {
	var nVerify Verify

	query := `select type, identifier, code, board from boardaccess where identifier=$1 and board=$2`
	if err := config.DB.QueryRow(query, verify.Identifier, verify.Board).Scan(&nVerify.Type, &nVerify.Identifier, &nVerify.Code, &nVerify.Board); err != nil {
		return verify, nil
	}

	return nVerify, nil
}

func (verify Verify) HasBoardAccess() (bool, error) {
	var count int

	query := `select count(*) from boardaccess where identifier=$1 and board=$2`
	if err := config.DB.QueryRow(query, verify.Identifier, verify.Board).Scan(&count); err != nil {
		return false, nil
	}

	return true, nil
}

func (verify Verify) SendVerification() error {
	config.Log.Println("sending email")

	from := config.SiteEmail
	pass := config.SiteEmailPassword
	to := verify.Identifier
	body := fmt.Sprintf("You can use either\r\nEmail: %s \r\n Verfication Code: %s\r\n for the board %s", verify.Identifier, verify.Code, verify.Board)

	msg := "From: " + from + "\n" +
		"To: " + to + "\n" +
		"Subject: Image Board Verification\n\n" +
		body

	err := smtp.SendMail(config.SiteEmailServer+":"+config.SiteEmailPort,
		smtp.PlainAuth("", from, pass, config.SiteEmailServer),
		from, []string{to}, []byte(msg))

	return MakeError(err, "SendVerification")
}

func (verify Verify) VerifyCooldownAdd() error {
	query := `insert into verficationcooldown (identifier, code) values ($1, $2)`
	_, err := config.DB.Exec(query, verify.Identifier, verify.Code)

	return MakeError(err, "VerifyCooldownAdd")
}

func BoardHasAuthType(board string, auth string) (bool, error) {
	authTypes, err := GetBoardAuth(board)

	if err != nil {
		return false, MakeError(err, "BoardHasAuthType")
	}

	for _, e := range authTypes {
		if e == auth {
			return true, nil
		}
	}

	return false, nil
}

func Captcha() string {
	rand.Seed(time.Now().UTC().UnixNano())
	domain := "ABEFHKMNPQRSUVWXYZ#$&"
	rng := 4
	newID := ""

	for i := 0; i < rng; i++ {
		newID += string(domain[rand.Intn(len(domain))])
	}

	return newID
}

func CreateNewCaptcha() error {
	id := RandomID(8)
	file := "public/" + id + ".png"

	for true {
		if _, err := os.Stat("./" + file); err == nil {
			id = RandomID(8)
			file = "public/" + id + ".png"
		} else {
			break
		}
	}

	var pattern string

	captcha := Captcha()
	rnd := fmt.Sprintf("%d", rand.Intn(3))
	srnd := string(rnd)

	switch srnd {
	case "0":
		pattern = "pattern:verticalbricks"
		break

	case "1":
		pattern = "pattern:verticalsaw"
		break

	case "2":
		pattern = "pattern:hs_cross"
		break

	}

	cmd := exec.Command("convert", "-size", "200x98", pattern, "-transparent", "white", file)
	cmd.Stderr = os.Stderr

	if err := cmd.Run(); err != nil {
		return MakeError(err, "CreateNewCaptcha")
	}

	cmd = exec.Command("convert", file, "-fill", "blue", "-pointsize", "62", "-annotate", "+0+70", captcha, "-tile", "pattern:left30", "-gravity", "center", "-transparent", "white", file)
	cmd.Stderr = os.Stderr

	if err := cmd.Run(); err != nil {
		return MakeError(err, "CreateNewCaptcha")
	}

	rnd = fmt.Sprintf("%d", rand.Intn(24)-12)
	cmd = exec.Command("convert", file, "-rotate", rnd, "-wave", "5x35", "-distort", "Arc", "20", "-wave", "2x35", "-transparent", "white", file)
	cmd.Stderr = os.Stderr

	if err := cmd.Run(); err != nil {
		return MakeError(err, "CreateNewCaptcha")
	}

	var verification Verify

	verification.Type = "captcha"
	verification.Code = captcha
	verification.Identifier = file

	return verification.Create()
}

func GetRandomCaptcha() (string, error) {
	var verify string

	query := `select identifier from verification where type='captcha' order by random() limit 1`
	if err := config.DB.QueryRow(query).Scan(&verify); err != nil {
		return verify, MakeError(err, "GetRandomCaptcha")
	}

	return verify, nil
}

func GetCaptchaTotal() (int, error) {
	var count int

	query := `select count(*) from verification where type='captcha'`
	if err := config.DB.QueryRow(query).Scan(&count); err != nil {
		return count, MakeError(err, "GetCaptchaTotal")
	}

	return count, nil
}

func GetCaptchaCode(verify string) (string, error) {
	var code string

	query := `select code from verification where identifier=$1 limit 1`
	if err := config.DB.QueryRow(query, verify).Scan(&code); err != nil {
		return code, MakeError(err, "GetCaptchaCodeDB")
	}

	return code, nil
}

func DeleteCaptchaCode(verify string) error {
	query := `delete from verification where identifier=$1`
	_, err := config.DB.Exec(query, verify)

	if err != nil {
		return MakeError(err, "DeleteCaptchaCode")
	}

	err = os.Remove("./" + verify)
	return MakeError(err, "DeleteCaptchaCode")
}

func GetVerificationByCode(code string) (Verify, error) {
	// TODO: this only needs to select one row.

	var verify Verify

	query := `select type, identifier, code, board from boardaccess where code=$1`

	rows, err := config.DB.Query(query, code)
	if err != nil {
		return verify, MakeError(err, "GetVerificationByCode")
	}

	defer rows.Close()

	for rows.Next() {
		if err := rows.Scan(&verify.Type, &verify.Identifier, &verify.Code, &verify.Board); err != nil {
			return verify, MakeError(err, "GetVerificationByCode")
		}
	}

	return verify, nil
}

func GetVerificationByEmail(email string) (Verify, error) {
	var verify Verify

	query := `select type, identifier, code, board from boardaccess where identifier=$1`
	if err := config.DB.QueryRow(query, email).Scan(&verify.Type, &verify.Identifier, &verify.Code, &verify.Board); err != nil {
		return verify, nil
	}

	return verify, nil
}

func GetVerify(access string) (Verify, error) {
	verify, err := GetVerificationByCode(access)

	if err != nil {
		return verify, MakeError(err, "GetVerify")
	}

	if verify.Identifier == "" {
		verify, err = GetVerificationByEmail(access)
	}

	return verify, MakeError(err, "GetVerify")
}

func HasAuthCooldown(auth string) (bool, error) {
	var current VerifyCooldown
	var err error

	if current, err = VerifyCooldownCurrent(auth); err != nil {
		return false, MakeError(err, "HasAuthCooldown")
	}

	if current.Time > 0 {
		return true, nil
	}

	return false, nil
}

func HasAuth(code string, board string) (bool, error) {
	verify, err := GetVerificationByCode(code)
	if err != nil {
		return false, MakeError(err, "HasAuth")
	}

	if res, err := verify.HasBoardAccess(); err == nil && (verify.Board == config.Domain || (res && verify.Board == board)) {
		return true, nil
	} else {
		return false, MakeError(err, "HasAuth")
	}

	return false, nil
}

func IsEmailSetup() bool {
	return config.SiteEmail != "" || config.SiteEmailPassword != "" || config.SiteEmailServer != "" || config.SiteEmailPort != ""
}

func VerficationCooldown() error {
	query := `select identifier, code, time from verificationcooldown`
	rows, err := config.DB.Query(query)

	if err != nil {
		return MakeError(err, "VerficationCooldown")
	}

	defer rows.Close()
	for rows.Next() {
		var verify VerifyCooldown

		if err := rows.Scan(&verify.Identifier, &verify.Code, &verify.Time); err != nil {
			return MakeError(err, "VerficationCooldown")
		}

		nTime := verify.Time - 1
		query = `update set time=$1 where identifier=$2`

		if _, err := config.DB.Exec(query, nTime, verify.Identifier); err != nil {
			return MakeError(err, "VerficationCooldown")
		}

		VerficationCooldownRemove()
	}

	return nil
}

func VerficationCooldownRemove() error {
	query := `delete from verificationcooldown where time < 1`
	_, err := config.DB.Exec(query)

	return MakeError(err, "VerficationCooldownRemove")
}

func VerifyCooldownCurrent(auth string) (VerifyCooldown, error) {
	var current VerifyCooldown

	query := `select identifier, code, time from verificationcooldown where code=$1`
	if err := config.DB.QueryRow(query, auth).Scan(&current.Identifier, &current.Code, &current.Time); err != nil {
		query := `select identifier, code, time from verificationcooldown where identifier=$1`
		if err := config.DB.QueryRow(query, auth).Scan(&current.Identifier, &current.Code, &current.Time); err != nil {
			return current, nil
		}

		return current, nil
	}

	return current, nil
}

func GetPasswordFromSession(ctx *fiber.Ctx) (string, string) {
	cookie := ctx.Cookies("session_token")
	parts := strings.Split(cookie, "|")

	if len(parts) > 1 {
		return parts[0], parts[1]
	}

	return "", ""
}

func MakeCaptchas(total int) error {
	dbtotal, err := GetCaptchaTotal()

	if err != nil {
		return MakeError(err, "MakeCaptchas")
	}

	difference := total - dbtotal

	for i := 0; i < difference; i++ {
		if err := CreateNewCaptcha(); err != nil {
			return MakeError(err, "MakeCaptchas")
		}
	}

	return nil
}