2021-08-17 16:08:32 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
2021-10-10 11:08:05 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-08-17 16:08:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getAPIKeys(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
limit, offset, order, err := getSearchFilters(w, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKeys, err := dataprovider.GetAPIKeys(limit, offset, order)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
render.JSON(w, r, apiKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAPIKeyByID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
keyID := getURLParam(r, "id")
|
|
|
|
apiKey, err := dataprovider.APIKeyExists(keyID)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiKey.HideConfidentialData()
|
|
|
|
|
|
|
|
render.JSON(w, r, apiKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addAPIKey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
var apiKey dataprovider.APIKey
|
2021-10-10 11:08:05 +00:00
|
|
|
err = render.DecodeJSON(r.Body, &apiKey)
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiKey.ID = 0
|
|
|
|
apiKey.KeyID = ""
|
|
|
|
apiKey.Key = ""
|
2021-11-06 13:13:20 +00:00
|
|
|
apiKey.LastUseAt = 0
|
2021-10-10 11:08:05 +00:00
|
|
|
err = dataprovider.AddAPIKey(&apiKey, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
response := make(map[string]string)
|
|
|
|
response["message"] = "API key created. This is the only time the API key is visible, please save it."
|
|
|
|
response["key"] = apiKey.DisplayKey()
|
|
|
|
w.Header().Add("Location", fmt.Sprintf("%v/%v", apiKeysPath, apiKey.KeyID))
|
|
|
|
w.Header().Add("X-Object-ID", apiKey.KeyID)
|
|
|
|
ctx := context.WithValue(r.Context(), render.StatusCtxKey, http.StatusCreated)
|
|
|
|
render.JSON(w, r.WithContext(ctx), response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateAPIKey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
keyID := getURLParam(r, "id")
|
|
|
|
apiKey, err := dataprovider.APIKeyExists(keyID)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = render.DecodeJSON(r.Body, &apiKey)
|
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKey.KeyID = keyID
|
2021-10-10 11:08:05 +00:00
|
|
|
if err := dataprovider.UpdateAPIKey(&apiKey, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr)); err != nil {
|
2021-08-17 16:08:32 +00:00
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, nil, "API key updated", http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteAPIKey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
keyID := getURLParam(r, "id")
|
2021-10-10 11:08:05 +00:00
|
|
|
claims, err := getTokenClaims(r)
|
|
|
|
if err != nil || claims.Username == "" {
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-08-17 16:08:32 +00:00
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
err = dataprovider.DeleteAPIKey(keyID, claims.Username, util.GetIPFromRemoteAddress(r.RemoteAddr))
|
2021-08-17 16:08:32 +00:00
|
|
|
if err != nil {
|
|
|
|
sendAPIResponse(w, r, err, "", getRespStatus(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendAPIResponse(w, r, err, "API key deleted", http.StatusOK)
|
|
|
|
}
|