-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
config_auth.go
216 lines (176 loc) Β· 4.89 KB
/
config_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
package config
import (
"regexp"
"time"
"golang.org/x/crypto/bcrypt"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/rnd"
"github.com/photoprism/photoprism/pkg/txt"
)
const (
AuthModePublic = "public"
AuthModePasswd = "password"
)
func isBcrypt(s string) bool {
b, err := regexp.MatchString(`^\$2[ayb]\$.{56}$`, s)
if err != nil {
return false
}
return b
}
// SetAuthMode changes the authentication mode (for use in tests only).
func (c *Config) SetAuthMode(mode string) {
if !c.Debug() {
return
}
switch mode {
case AuthModePublic:
c.options.AuthMode = AuthModePublic
c.options.Public = true
entity.ValidateTokens = false
default:
c.options.AuthMode = AuthModePasswd
c.options.Public = false
entity.ValidateTokens = true
}
}
// Auth checks if authentication is required.
func (c *Config) Auth() bool {
return !c.Public()
}
// AuthMode returns the authentication mode.
func (c *Config) AuthMode() string {
if c.options.Public || c.Demo() {
return AuthModePublic
}
switch c.options.AuthMode {
case AuthModePublic:
return AuthModePublic
default:
return AuthModePasswd
}
}
// Public checks if app runs in public mode and requires no authentication.
func (c *Config) Public() bool {
return c.AuthMode() == AuthModePublic
}
// AdminUser returns the admin auth name.
func (c *Config) AdminUser() string {
c.options.AdminUser = clean.Username(c.options.AdminUser)
if c.options.AdminUser == "" {
c.options.AdminUser = "admin"
}
return c.options.AdminUser
}
// AdminPassword returns the initial admin password.
func (c *Config) AdminPassword() string {
return clean.Password(c.options.AdminPassword)
}
// PasswordLength returns the minimum password length in characters.
func (c *Config) PasswordLength() int {
if c.Public() {
return 0
} else if c.options.PasswordLength < 1 {
return entity.PasswordLengthDefault
} else if c.options.PasswordLength > txt.ClipPassword {
return txt.ClipPassword
}
return c.options.PasswordLength
}
// CheckPassword compares given password p with the admin password
func (c *Config) CheckPassword(p string) bool {
ap := c.AdminPassword()
if isBcrypt(ap) {
err := bcrypt.CompareHashAndPassword([]byte(ap), []byte(p))
return err == nil
}
return ap == p
}
// PasswordResetUri returns the password reset page URI, if any.
func (c *Config) PasswordResetUri() string {
if c.Public() {
return ""
}
return c.options.PasswordResetUri
}
// RegisterUri returns the user registration page URI, if any.
func (c *Config) RegisterUri() string {
if c.Public() {
return ""
}
return c.options.RegisterUri
}
// LoginUri returns the user authentication page URI.
func (c *Config) LoginUri() string {
if c.Public() {
return c.BaseUri("/library/browse")
}
if c.options.LoginUri == "" {
return c.BaseUri("/library/login")
}
return c.options.LoginUri
}
// SessionMaxAge returns the standard session expiration time in seconds.
func (c *Config) SessionMaxAge() int64 {
if c.options.SessionMaxAge < 0 {
return 0
} else if c.options.SessionMaxAge == 0 {
return DefaultSessionMaxAge
}
return c.options.SessionMaxAge
}
// SessionTimeout returns the standard session idle time in seconds.
func (c *Config) SessionTimeout() int64 {
if c.options.SessionTimeout < 0 {
return 0
} else if c.options.SessionTimeout == 0 {
return DefaultSessionTimeout
}
return c.options.SessionTimeout
}
// SessionCache returns the default session cache duration in seconds.
func (c *Config) SessionCache() int64 {
if c.options.SessionCache == 0 {
return DefaultSessionCache
} else if c.options.SessionCache < 60 {
return 60
} else if c.options.SessionCache > 3600 {
return 3600
}
return c.options.SessionCache
}
// SessionCacheDuration returns the default session cache duration.
func (c *Config) SessionCacheDuration() time.Duration {
return time.Duration(c.SessionCache()) * time.Second
}
// DownloadToken returns the DOWNLOAD api token (you can optionally use a static value for permanent caching).
func (c *Config) DownloadToken() string {
if c.Public() {
return entity.TokenPublic
} else if c.options.DownloadToken == "" {
c.options.DownloadToken = rnd.Base36(8)
}
return c.options.DownloadToken
}
// InvalidDownloadToken checks if the token is invalid.
func (c *Config) InvalidDownloadToken(t string) bool {
return entity.InvalidDownloadToken(t)
}
// PreviewToken returns the preview image api token (based on the unique storage serial by default).
func (c *Config) PreviewToken() string {
if c.Public() {
return entity.TokenPublic
} else if c.options.PreviewToken == "" {
if c.Serial() == "" {
return "********"
} else {
c.options.PreviewToken = c.SerialChecksum()
}
}
return c.options.PreviewToken
}
// InvalidPreviewToken checks if the preview token is invalid.
func (c *Config) InvalidPreviewToken(t string) bool {
return entity.InvalidPreviewToken(t)
}