2021-03-16 19:43:02 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
2022-11-21 18:58:42 +00:00
|
|
|
"storj.io/storj/private/web"
|
2021-03-16 19:43:02 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrAPIKeysAPI - console api keys api error type.
|
2021-06-24 16:49:15 +01:00
|
|
|
ErrAPIKeysAPI = errs.Class("console api keys")
|
2021-03-16 19:43:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// APIKeys is an api controller that exposes all api keys related functionality.
|
|
|
|
type APIKeys struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *console.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPIKeys is a constructor for api api keys controller.
|
|
|
|
func NewAPIKeys(log *zap.Logger, service *console.Service) *APIKeys {
|
|
|
|
return &APIKeys{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteByNameAndProjectID deletes specific api key by it's name and project ID.
|
|
|
|
func (keys *APIKeys) DeleteByNameAndProjectID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
name := r.URL.Query().Get("name")
|
|
|
|
projectIDString := r.URL.Query().Get("projectID")
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
keys.serveJSONError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.FromString(projectIDString)
|
|
|
|
if err != nil {
|
|
|
|
keys.serveJSONError(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = keys.service.DeleteAPIKeyByNameAndProjectID(ctx, name, projectID)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
keys.serveJSONError(w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-23 20:23:27 +00:00
|
|
|
if console.ErrNoAPIKey.Has(err) {
|
|
|
|
keys.serveJSONError(w, http.StatusNoContent, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:43:02 +00:00
|
|
|
keys.serveJSONError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveJSONError writes JSON error to response output stream.
|
|
|
|
func (keys *APIKeys) serveJSONError(w http.ResponseWriter, status int, err error) {
|
2022-11-21 18:58:42 +00:00
|
|
|
web.ServeJSONError(keys.log, w, status, err)
|
2021-03-16 19:43:02 +00:00
|
|
|
}
|