2022-07-19 16:58:43 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2022-07-19 16:58:43 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2023-03-27 19:27:19 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2022-07-19 16:58:43 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Connect confirms external service accounts using a token.
|
|
|
|
//
|
|
|
|
// PUT /api/v1/connect/:name
|
|
|
|
func Connect(router *gin.RouterGroup) {
|
|
|
|
router.PUT("/connect/:name", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
name := clean.ID(c.Param("name"))
|
2022-07-19 16:58:43 +02:00
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
log.Errorf("connect: empty service name")
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var f form.Connect
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
|
|
|
log.Warnf("connect: invalid form values (%s)", clean.Log(name))
|
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrAccountConnect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Invalid() {
|
|
|
|
log.Warnf("connect: invalid token %s", clean.Log(f.Token))
|
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrAccountConnect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2022-07-19 16:58:43 +02:00
|
|
|
|
|
|
|
if conf.Public() {
|
|
|
|
Abort(c, http.StatusForbidden, i18n.ErrPublic)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceConfig, acl.ActionUpdate)
|
2022-07-19 16:58:43 +02:00
|
|
|
|
2023-03-29 12:53:51 +02:00
|
|
|
if !s.IsSuperAdmin() {
|
2022-09-28 09:01:17 +02:00
|
|
|
log.Errorf("connect: %s not authorized", clean.Log(s.User().UserName))
|
|
|
|
AbortForbidden(c)
|
2022-07-19 16:58:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2023-03-27 19:27:19 +02:00
|
|
|
var restart bool
|
2022-07-19 16:58:43 +02:00
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "hub":
|
2023-03-27 19:27:19 +02:00
|
|
|
old := conf.Hub().Session
|
2023-10-13 12:43:20 +02:00
|
|
|
err = conf.RenewApiKeysWithToken(f.Token)
|
2023-03-27 19:27:19 +02:00
|
|
|
restart = old != conf.Hub().Session
|
2022-07-19 16:58:43 +02:00
|
|
|
default:
|
|
|
|
log.Errorf("connect: invalid service %s", clean.Log(name))
|
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrAccountConnect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:27:19 +02:00
|
|
|
// Set restart flag and update client config if necessary.
|
|
|
|
if restart {
|
|
|
|
mutex.Restart.Store(true)
|
|
|
|
conf.Propagate()
|
|
|
|
UpdateClientConfig()
|
|
|
|
}
|
|
|
|
|
2022-07-19 16:58:43 +02:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": http.StatusOK})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|