58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"linkport/internal/cryptotool"
|
|
"linkport/internal/navicat"
|
|
"net/http"
|
|
)
|
|
|
|
func (s *Server) decryptNavicat(w http.ResponseWriter, r *http.Request) {
|
|
if !allowDecryption(w, r) {
|
|
return
|
|
}
|
|
var body struct {
|
|
Version int `json:"version"`
|
|
Ciphertext string `json:"ciphertext"`
|
|
}
|
|
if !decodeLimit(w, r, &body, 64<<10) {
|
|
return
|
|
}
|
|
password, err := navicat.Decrypt(body.Version, body.Ciphertext)
|
|
if err != nil {
|
|
fail(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
respond(w, http.StatusOK, map[string]string{"password": password})
|
|
}
|
|
|
|
func (s *Server) decryptCustom(w http.ResponseWriter, r *http.Request) {
|
|
if !allowDecryption(w, r) {
|
|
return
|
|
}
|
|
var body cryptotool.Options
|
|
if !decodeLimit(w, r, &body, 64<<10) {
|
|
return
|
|
}
|
|
plaintext, err := cryptotool.Decrypt(body)
|
|
if err != nil {
|
|
fail(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
respond(w, http.StatusOK, map[string]string{"plaintext": plaintext})
|
|
}
|
|
|
|
func allowDecryption(w http.ResponseWriter, r *http.Request) bool {
|
|
// Token responses redact passwords; require a management session instead
|
|
// of returning a misleading successful response with an empty password.
|
|
if r.Header.Get("Authorization") != "" {
|
|
fail(w, http.StatusForbidden, "password decryption requires a management session")
|
|
return false
|
|
}
|
|
if r.Method != http.MethodPost {
|
|
w.Header().Set("Allow", http.MethodPost)
|
|
fail(w, http.StatusMethodNotAllowed, "method not allowed")
|
|
return false
|
|
}
|
|
return true
|
|
}
|