94846c2337
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"regexp"
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func isBcrypt(s string) bool {
|
|
b, err := regexp.MatchString(`^\$2[ayb]\$.{56}$`, s)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return b
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// InvalidDownloadToken returns true if the token is invalid.
|
|
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 {
|
|
if c.params.DownloadToken == "" {
|
|
c.params.DownloadToken = rnd.Token(8)
|
|
}
|
|
|
|
return c.params.DownloadToken
|
|
}
|
|
|
|
// InvalidToken returns true if the token is invalid.
|
|
func (c *Config) InvalidToken(t string) bool {
|
|
return c.PreviewToken() != t && c.DownloadToken() != t
|
|
}
|
|
|
|
// PreviewToken returns the THUMBNAILS api token (you can optionally use a static value for permanent caching).
|
|
func (c *Config) PreviewToken() string {
|
|
if c.params.PreviewToken == "" {
|
|
c.params.PreviewToken = rnd.Token(8)
|
|
}
|
|
|
|
return c.params.PreviewToken
|
|
}
|