forked from longtao.wu/droboticflow
46 lines
1.8 KiB
Go
46 lines
1.8 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
const objectSyncServerVersion = "1.0.7"
|
|
const objectSyncAuthenticationProtocol = "activation-v1"
|
|
|
|
func (server *server) handleExternalObjectSyncAuthCapabilities(w http.ResponseWriter, r *http.Request) {
|
|
renderJSON(w, http.StatusOK, map[string]any{"authenticationProtocol": objectSyncAuthenticationProtocol, "serverVersion": objectSyncServerVersion, "activationRequired": true})
|
|
}
|
|
|
|
func (server *server) handleExternalObjectSyncActivate(w http.ResponseWriter, r *http.Request) {
|
|
target, ok := server.objectSyncStore(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
principal, ok := currentObjectSyncCredential(r)
|
|
if !ok {
|
|
renderObjectSyncError(w, r, http.StatusUnauthorized, "credential_required", "Pending credential is required", false, nil)
|
|
return
|
|
}
|
|
var input struct {
|
|
ActivationID string `json:"activationId"`
|
|
}
|
|
if !decodeJSON(w, r, &input) {
|
|
return
|
|
}
|
|
if strings.TrimSpace(input.ActivationID) == "" || input.ActivationID != principal.Binding.ActivationID {
|
|
renderObjectSyncError(w, r, http.StatusConflict, "activation_mismatch", "Activation does not belong to this device credential", false, nil)
|
|
return
|
|
}
|
|
activated, err := target.ActivateObjectSyncToken(r.Context(), principal.Credential.ID, principal.Device.ID, principal.TokenHash, input.ActivationID)
|
|
if err != nil {
|
|
renderObjectSyncError(w, r, http.StatusServiceUnavailable, "activation_store_failed", "Credential activation could not be confirmed; retry with the same activation", true, nil)
|
|
return
|
|
}
|
|
if !activated {
|
|
renderObjectSyncError(w, r, http.StatusConflict, "activation_expired", "Credential was revoked, expired or superseded; authenticate again", false, nil)
|
|
return
|
|
}
|
|
renderJSON(w, http.StatusOK, map[string]any{"activated": true, "activationId": input.ActivationID, "credentialId": principal.Credential.ID, "deviceId": principal.Device.ID})
|
|
}
|