-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
163 lines (124 loc) · 2.8 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
package auth
import (
"crypto/sha256"
"embed"
"hash"
"log/slog"
"strings"
"time"
"github.com/yaitoo/sqle"
"github.com/yaitoo/sqle/migrate"
"github.com/yaitoo/sqle/shardid"
)
var (
//go:embed migration
migration embed.FS
defaultAccessTokenTTL = 1 * time.Minute
defaultRefreshTokenTTL = 1 * time.Hour
defaultTOTPIssuer = "Yaitoo"
defaultTOPTAccountName = "Auth"
defaultDHTEmail = "auth:email"
defaultDHTMobile = "auth:mobile"
defaultLoginCodeLen = 6
defaultLoginCodeTTL = 60 * time.Second
)
var (
noSession Session
noProfileData ProfileData
)
type Auth struct {
db *sqle.DB
prefix string
logger *slog.Logger
hash func() hash.Hash
aesKey []byte
accessTokenTTL time.Duration
refreshTokenTTL time.Duration
jwtSignKey []byte
totpIssuer string
totpAccountName string
loginCodeSize int
loginCodeTTL time.Duration
dhtEmail string
dhtMobile string
genUser *shardid.Generator
genLoginLog *shardid.Generator
genAuditLog *shardid.Generator
}
// New create an auth provider with db and options
// skipcq: GO-R1005
func New(db *sqle.DB, options ...Option) *Auth {
a := &Auth{
db: db,
}
for _, o := range options {
o(a)
}
if a.prefix != "" && !strings.HasSuffix(a.prefix, "_") {
a.prefix = a.prefix + "_"
}
if a.logger == nil {
a.logger = slog.Default()
}
if a.hash == nil {
a.hash = sha256.New
}
if a.genUser == nil {
a.genUser = shardid.New()
}
if a.genLoginLog == nil {
a.genLoginLog = shardid.New()
}
if a.genAuditLog == nil {
a.genAuditLog = shardid.New()
}
if a.accessTokenTTL <= 0 {
a.accessTokenTTL = defaultAccessTokenTTL
}
if a.refreshTokenTTL <= 0 {
a.refreshTokenTTL = defaultRefreshTokenTTL
}
if a.jwtSignKey == nil {
a.jwtSignKey = getJWTKey("")
}
if a.totpIssuer == "" {
a.totpIssuer = defaultTOTPIssuer
}
if a.totpAccountName == "" {
a.totpAccountName = defaultTOPTAccountName
}
if a.dhtEmail == "" {
a.dhtEmail = defaultDHTEmail
}
if a.dhtMobile == "" {
a.dhtMobile = defaultDHTMobile
}
if a.loginCodeSize < 1 {
a.loginCodeSize = defaultLoginCodeLen
}
if a.loginCodeTTL < 1 {
a.loginCodeTTL = defaultLoginCodeTTL
}
return a
}
// CreateMigrator automatically migrate database schema for auth module
func (a *Auth) CreateMigrator(options ...migrate.Option) (*migrate.Migrator, error) {
m := migrate.New(a.db)
options = append(options, migrate.WithModule("auth"))
err := m.Discover(migration, options...)
if err != nil {
return nil, err
}
var vers []migrate.Semver
for _, v := range m.Versions {
var migrations []migrate.Migration
for _, m := range v.Migrations {
m.Scripts = strings.ReplaceAll(m.Scripts, "<prefix>", a.prefix)
migrations = append(migrations, m)
}
v.Migrations = migrations
vers = append(vers, v)
}
m.Versions = vers
return m, nil
}