2020-12-04 13:10:32 +01:00
|
|
|
package hub
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/sha1"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-10-07 12:47:12 +02:00
|
|
|
"errors"
|
2020-10-03 13:50:30 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-10-07 12:22:45 +02:00
|
|
|
"path/filepath"
|
2020-10-03 13:50:30 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/hub/places"
|
2020-10-03 13:50:30 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2021-12-14 20:01:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2020-10-03 13:50:30 +02:00
|
|
|
)
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
// Config represents backend api credentials for maps & geodata.
|
2020-10-03 13:50:30 +02:00
|
|
|
type Config struct {
|
2022-03-02 14:16:49 +01:00
|
|
|
Version string `json:"version" yaml:"Version"`
|
|
|
|
FileName string `json:"-" yaml:"-"`
|
2022-01-05 18:15:39 +01:00
|
|
|
Key string `json:"key" yaml:"Key"`
|
|
|
|
Secret string `json:"secret" yaml:"Secret"`
|
|
|
|
Session string `json:"session" yaml:"Session"`
|
|
|
|
Status string `json:"status" yaml:"Status"`
|
|
|
|
Serial string `json:"serial" yaml:"Serial"`
|
2022-03-02 14:16:49 +01:00
|
|
|
Env string `json:"-" yaml:"-"`
|
|
|
|
UserAgent string `json:"-" yaml:"-"`
|
2022-01-05 18:15:39 +01:00
|
|
|
PartnerID string `json:"-" yaml:"-"`
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
// NewConfig creates a new backend api credentials instance.
|
2022-03-02 14:16:49 +01:00
|
|
|
func NewConfig(version, fileName, serial, env, userAgent, partnerId string) *Config {
|
2020-10-03 13:50:30 +02:00
|
|
|
return &Config{
|
2022-03-02 14:16:49 +01:00
|
|
|
Version: version,
|
|
|
|
FileName: fileName,
|
2022-01-05 18:15:39 +01:00
|
|
|
Key: "",
|
|
|
|
Secret: "",
|
|
|
|
Session: "",
|
|
|
|
Status: "",
|
|
|
|
Serial: serial,
|
2022-03-02 14:16:49 +01:00
|
|
|
Env: env,
|
|
|
|
UserAgent: userAgent,
|
|
|
|
PartnerID: partnerId,
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
// MapKey returns the maps api key.
|
|
|
|
func (c *Config) MapKey() string {
|
|
|
|
if sess, err := c.DecodeSession(); err != nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return sess.MapKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
// Propagate updates backend api credentials in other packages.
|
2020-10-04 04:47:54 +02:00
|
|
|
func (c *Config) Propagate() {
|
|
|
|
places.Key = c.Key
|
|
|
|
places.Secret = c.Secret
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
// Sanitize verifies and sanitizes backend api credentials.
|
2020-10-04 04:47:54 +02:00
|
|
|
func (c *Config) Sanitize() {
|
|
|
|
c.Key = strings.ToLower(c.Key)
|
|
|
|
|
|
|
|
if c.Secret != "" {
|
|
|
|
if c.Key != fmt.Sprintf("%x", sha1.Sum([]byte(c.Secret))) {
|
|
|
|
c.Key = ""
|
|
|
|
c.Secret = ""
|
|
|
|
c.Session = ""
|
|
|
|
c.Status = ""
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
// DecodeSession decodes backend api session data.
|
2020-10-04 04:47:54 +02:00
|
|
|
func (c *Config) DecodeSession() (Session, error) {
|
|
|
|
c.Sanitize()
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
result := Session{}
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
if c.Session == "" {
|
2020-10-03 13:50:30 +02:00
|
|
|
return result, fmt.Errorf("empty session")
|
|
|
|
}
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
s, err := hex.DecodeString(c.Session)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := sha256.New()
|
2021-07-15 17:47:43 +02:00
|
|
|
if _, err := hash.Write([]byte(c.Secret)); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
var b []byte
|
|
|
|
|
|
|
|
block, err := aes.NewCipher(hash.Sum(b))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
iv := s[:aes.BlockSize]
|
|
|
|
|
|
|
|
plaintext := make([]byte, len(s))
|
|
|
|
|
|
|
|
stream := cipher.NewCTR(block, iv)
|
|
|
|
stream.XORKeyStream(plaintext, s[aes.BlockSize:])
|
|
|
|
|
|
|
|
plaintext = bytes.Trim(plaintext, "\x00")
|
|
|
|
|
|
|
|
if err := json.Unmarshal(plaintext, &result); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
// Refresh updates backend api credentials.
|
2020-10-04 04:47:54 +02:00
|
|
|
func (c *Config) Refresh() (err error) {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := os.MkdirAll(filepath.Dir(c.FileName), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
c.Sanitize()
|
2021-11-20 19:14:00 +01:00
|
|
|
|
2022-03-27 21:37:11 +02:00
|
|
|
// Create new http.Client instance.
|
|
|
|
//
|
|
|
|
// NOTE: Timeout specifies a time limit for requests made by
|
|
|
|
// this Client. The timeout includes connection time, any
|
|
|
|
// redirects, and reading the response body. The timer remains
|
|
|
|
// running after Get, Head, Post, or Do return and will
|
|
|
|
// interrupt reading of the Response.Body.
|
2020-10-03 13:50:30 +02:00
|
|
|
client := &http.Client{Timeout: 60 * time.Second}
|
2022-03-27 21:37:11 +02:00
|
|
|
|
2020-12-04 13:10:32 +01:00
|
|
|
url := ServiceURL
|
2020-10-03 13:50:30 +02:00
|
|
|
method := http.MethodPost
|
2021-11-20 19:14:00 +01:00
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
var req *http.Request
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
if c.Key != "" {
|
2020-12-04 13:10:32 +01:00
|
|
|
url = fmt.Sprintf(ServiceURL+"/%s", c.Key)
|
2020-10-03 13:50:30 +02:00
|
|
|
method = http.MethodPut
|
2022-01-05 18:15:39 +01:00
|
|
|
log.Debugf("config: requesting updated api key for maps and places")
|
2020-10-03 13:50:30 +02:00
|
|
|
} else {
|
2022-01-05 18:15:39 +01:00
|
|
|
log.Debugf("config: requesting new api key for maps and places")
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
// Create request.
|
|
|
|
if j, err := json.Marshal(NewRequest(c.Version, c.Serial, c.Env, c.PartnerID)); err != nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
return err
|
|
|
|
} else if req, err = http.NewRequest(method, url, bytes.NewReader(j)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
// Set user agent.
|
|
|
|
if c.UserAgent != "" {
|
|
|
|
req.Header.Set("User-Agent", c.UserAgent)
|
|
|
|
} else {
|
|
|
|
req.Header.Set("User-Agent", "PhotoPrism/Test")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add Content-Type header.
|
2020-10-03 13:50:30 +02:00
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
var r *http.Response
|
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
// Send request.
|
2020-10-03 13:50:30 +02:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
r, err = client.Do(req)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
// Ok?
|
2020-10-03 13:50:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if r.StatusCode >= 400 {
|
2022-01-05 18:15:39 +01:00
|
|
|
err = fmt.Errorf("fetching api key from %s failed (error %d)", ApiHost(), r.StatusCode)
|
2020-10-03 13:50:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-02 14:16:49 +01:00
|
|
|
// Decode JSON response.
|
2020-10-04 04:47:54 +02:00
|
|
|
err = json.NewDecoder(r.Body).Decode(c)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
// Load backend api credentials from a YAML file.
|
2020-10-07 12:22:45 +02:00
|
|
|
func (c *Config) Load() error {
|
|
|
|
if !fs.FileExists(c.FileName) {
|
2021-12-14 20:01:39 +01:00
|
|
|
return fmt.Errorf("settings file not found: %s", sanitize.Log(c.FileName))
|
2020-10-03 13:50:30 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
2021-10-06 07:10:50 +02:00
|
|
|
yamlConfig, err := os.ReadFile(c.FileName)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
if err := yaml.Unmarshal(yamlConfig, c); err != nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
c.Sanitize()
|
|
|
|
c.Propagate()
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-10-07 12:47:12 +02:00
|
|
|
if sess, err := c.DecodeSession(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if sess.Expired() {
|
|
|
|
return errors.New("session expired")
|
|
|
|
}
|
|
|
|
|
2020-10-03 13:50:30 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:02:19 +01:00
|
|
|
// Save backend api credentials to a YAML file.
|
2020-10-07 12:22:45 +02:00
|
|
|
func (c *Config) Save() error {
|
2020-10-04 04:47:54 +02:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
|
|
c.Sanitize()
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
data, err := yaml.Marshal(c)
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
c.Propagate()
|
2020-10-03 13:50:30 +02:00
|
|
|
|
2020-10-07 12:22:45 +02:00
|
|
|
if err := os.MkdirAll(filepath.Dir(c.FileName), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-06 07:10:50 +02:00
|
|
|
if err := os.WriteFile(c.FileName, data, os.ModePerm); err != nil {
|
2020-10-03 13:50:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-04 04:47:54 +02:00
|
|
|
c.Propagate()
|
2020-10-03 13:50:30 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|