diff --git a/internal/apikey/apikey.go b/internal/apikey/apikey.go index 9c661c0..d2c4bb2 100644 --- a/internal/apikey/apikey.go +++ b/internal/apikey/apikey.go @@ -12,6 +12,7 @@ import ( type ApiKey struct { Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` + Name string `json:"name"` Scopes []string `json:"scopes" valid:"required"` Apikey string `json:"apiKey"` ExpiryInDays int `json:"expiryInDays"` diff --git a/internal/handler/v2/config/apikey/config_create_apikey.go b/internal/handler/v2/config/apikey/config_create_apikey.go index ecd469b..6f56142 100644 --- a/internal/handler/v2/config/apikey/config_create_apikey.go +++ b/internal/handler/v2/config/apikey/config_create_apikey.go @@ -7,6 +7,7 @@ import ( "net/http" "time" + "github.com/asaskevich/govalidator" "github.com/bb-consent/api/internal/apikey" "github.com/bb-consent/api/internal/common" "github.com/bb-consent/api/internal/config" @@ -43,6 +44,14 @@ func ConfigCreateApiKey(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() json.Unmarshal(b, &apiKeyReq) + // validating request payload + valid, err := govalidator.ValidateStruct(apiKeyReq) + if !valid { + m := "missing mandatory params for creating api key" + common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + return + } + // Repository apiKeyRepo := apikey.ApiKeyRepository{} apiKeyRepo.Init(organisationId) @@ -65,6 +74,7 @@ func ConfigCreateApiKey(w http.ResponseWriter, r *http.Request) { var newApiKey apikey.ApiKey newApiKey.Id = primitive.NewObjectID() + newApiKey.Name = apiKeyReq.Apikey.Name newApiKey.Scopes = apiKeyReq.Apikey.Scopes newApiKey.Apikey = key newApiKey.ExpiryInDays = apiKeyReq.Apikey.ExpiryInDays diff --git a/internal/handler/v2/config/apikey/config_update_apikey.go b/internal/handler/v2/config/apikey/config_update_apikey.go index 3ee149d..a606282 100644 --- a/internal/handler/v2/config/apikey/config_update_apikey.go +++ b/internal/handler/v2/config/apikey/config_update_apikey.go @@ -7,6 +7,7 @@ import ( "net/http" "time" + "github.com/asaskevich/govalidator" "github.com/bb-consent/api/internal/apikey" "github.com/bb-consent/api/internal/common" "github.com/bb-consent/api/internal/config" @@ -45,6 +46,14 @@ func ConfigUpdateApiKey(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() json.Unmarshal(b, &apiKeyReq) + // validating request payload + valid, err := govalidator.ValidateStruct(apiKeyReq) + if !valid { + m := "missing mandatory params for updating api key" + common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + return + } + // Repository apiKeyRepo := apikey.ApiKeyRepository{} apiKeyRepo.Init(organisationId) @@ -69,6 +78,7 @@ func ConfigUpdateApiKey(w http.ResponseWriter, r *http.Request) { common.HandleError(w, http.StatusInternalServerError, m, err) return } + toBeUpdatedApiKey.Name = apiKeyReq.Apikey.Name toBeUpdatedApiKey.Apikey = currentApiKey toBeUpdatedApiKey.ExpiryInDays = apiKeyReq.Apikey.ExpiryInDays toBeUpdatedApiKey.Scopes = apiKeyReq.Apikey.Scopes diff --git a/internal/migrate/migrate.go b/internal/migrate/migrate.go index faf6ff7..f7543bf 100644 --- a/internal/migrate/migrate.go +++ b/internal/migrate/migrate.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/bb-consent/api/internal/apikey" "github.com/bb-consent/api/internal/dataagreement" "github.com/bb-consent/api/internal/policy" "go.mongodb.org/mongo-driver/bson" @@ -12,6 +13,7 @@ import ( func Migrate() { migrateThirdPartyDataSharingToTrueInPolicyCollection() migrateThirdPartyDataSharingToTrueInDataAgreementsCollection() + migrateNameInApiKeyCollection() } func migrateThirdPartyDataSharingToTrueInPolicyCollection() { @@ -37,3 +39,15 @@ func migrateThirdPartyDataSharingToTrueInDataAgreementsCollection() { } // TODO: Handle impact towards revisions } + +func migrateNameInApiKeyCollection() { + apiKeyCollection := apikey.Collection() + + filter := bson.M{"name": bson.M{"$exists": false}} + update := bson.M{"$set": bson.M{"name": ""}} + + _, err := apiKeyCollection.UpdateMany(context.TODO(), filter, update) + if err != nil { + fmt.Println(err) + } +}