/
models.go
80 lines (67 loc) · 1.54 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
package mail
import (
"crypto/rand"
"crypto/sha512"
"encoding/base64"
"github.com/kapmahc/air/web"
)
// https://www.linode.com/docs/email/postfix/email-with-postfix-dovecot-and-mysql
// http://wiki.dovecot.org/Authentication/PasswordSchemes
// https://mad9scientist.com/dovecot-password-creation-php/
// Domain domain
type Domain struct {
web.Model
Name string
}
// TableName table name
func (Domain) TableName() string {
return "mail_domains"
}
// User user
type User struct {
web.Model
FullName string
Email string
Password string
DomainID uint
Enable bool
Domain Domain
}
// TableName table name
func (User) TableName() string {
return "mail_users"
}
func (p *User) sum(password string, salt []byte) string {
buf := sha512.Sum512(append([]byte(password), salt...))
return base64.StdEncoding.EncodeToString(append(buf[:], salt...))
}
// SetPassword set password (SSHA512-CRYPT)
func (p *User) SetPassword(password string) error {
salt := make([]byte, 16)
_, err := rand.Read(salt)
if err != nil {
return err
}
p.Password = p.sum(password, salt)
return nil
}
// ChkPassword check password
func (p *User) ChkPassword(password string) bool {
buf, err := base64.StdEncoding.DecodeString(p.Password)
if err != nil {
return false
}
return len(buf) > sha512.Size && p.Password == p.sum(password, buf[sha512.Size:])
}
// Alias alias
type Alias struct {
web.Model
Source string
Destination string
DomainID uint
Domain Domain
}
// TableName table name
func (Alias) TableName() string {
return "mail_aliases"
}