2020-01-28 11:04:10 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
2022-07-07 19:12:28 +02:00
|
|
|
"strings"
|
2020-01-28 11:04:10 +01:00
|
|
|
|
2020-05-27 19:38:40 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2020-01-28 11:04:10 +01:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2022-07-07 19:12:28 +02:00
|
|
|
const (
|
2022-07-11 05:08:15 +02:00
|
|
|
AuthModePublic = "public"
|
|
|
|
AuthModePasswd = "passwd"
|
2022-07-07 19:12:28 +02:00
|
|
|
)
|
|
|
|
|
2020-01-28 11:04:10 +01:00
|
|
|
func isBcrypt(s string) bool {
|
|
|
|
b, err := regexp.MatchString(`^\$2[ayb]\$.{56}$`, s)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-07-07 19:12:28 +02:00
|
|
|
// Public checks if app runs in public mode and requires no authentication.
|
|
|
|
func (c *Config) Public() bool {
|
|
|
|
if c.Demo() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.options.Public
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPublic changes authentication while instance is running, for testing purposes only.
|
|
|
|
func (c *Config) SetPublic(enabled bool) {
|
|
|
|
if c.Debug() {
|
|
|
|
c.options.Public = enabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AdminPassword returns the initial admin password.
|
|
|
|
func (c *Config) AdminPassword() string {
|
|
|
|
return c.options.AdminPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthMode returns the authentication mode.
|
|
|
|
func (c *Config) AuthMode() string {
|
|
|
|
if c.Public() {
|
|
|
|
return AuthModePublic
|
|
|
|
}
|
|
|
|
|
2022-07-11 05:08:15 +02:00
|
|
|
mode := strings.ToLower(strings.TrimSpace(c.options.AuthMode))
|
|
|
|
|
|
|
|
switch mode {
|
|
|
|
case AuthModePublic:
|
|
|
|
return AuthModePublic
|
|
|
|
case "", "pw", "pass", "passwd", "password", "passwort", "passwords":
|
|
|
|
return AuthModePasswd
|
|
|
|
default:
|
|
|
|
return AuthModePasswd
|
|
|
|
}
|
2022-07-07 19:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Auth checks if authentication is required.
|
|
|
|
func (c *Config) Auth() bool {
|
|
|
|
return c.AuthMode() != AuthModePublic
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// CheckPassword compares given password p with the admin password
|
2020-01-28 11:04:10 +01:00
|
|
|
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
|
|
|
|
}
|
2020-05-27 19:38:40 +02:00
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// InvalidDownloadToken checks if the token is invalid.
|
2020-05-27 19:38:40 +02:00
|
|
|
func (c *Config) InvalidDownloadToken(t string) bool {
|
|
|
|
return c.DownloadToken() != t
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadToken returns the DOWNLOAD api token (you can optionally use a static value for permanent caching).
|
|
|
|
func (c *Config) DownloadToken() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.DownloadToken == "" {
|
2022-04-15 09:42:07 +02:00
|
|
|
c.options.DownloadToken = rnd.GenerateToken(8)
|
2020-05-27 19:38:40 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.DownloadToken
|
2020-05-27 19:38:40 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:46:41 +02:00
|
|
|
// InvalidPreviewToken checks if the preview token is invalid.
|
2020-06-26 16:11:56 +02:00
|
|
|
func (c *Config) InvalidPreviewToken(t string) bool {
|
2020-05-27 19:56:56 +02:00
|
|
|
return c.PreviewToken() != t && c.DownloadToken() != t
|
2020-05-27 19:38:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-02 15:08:39 +01:00
|
|
|
// PreviewToken returns the preview image api token (based on the unique storage serial by default).
|
2020-05-27 19:56:56 +02:00
|
|
|
func (c *Config) PreviewToken() string {
|
2020-12-18 20:42:12 +01:00
|
|
|
if c.options.PreviewToken == "" {
|
2021-01-08 09:02:30 +01:00
|
|
|
if c.Public() {
|
|
|
|
c.options.PreviewToken = "public"
|
2022-04-06 17:46:41 +02:00
|
|
|
} else if c.Serial() == "" {
|
|
|
|
return "********"
|
2021-01-08 09:02:30 +01:00
|
|
|
} else {
|
|
|
|
c.options.PreviewToken = c.SerialChecksum()
|
|
|
|
}
|
2020-05-27 19:38:40 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
return c.options.PreviewToken
|
2020-05-27 19:38:40 +02:00
|
|
|
}
|