Skip to content

Commit

Permalink
Add #436 Support API key name
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 6, 2023
1 parent b87f184 commit cc521b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
10 changes: 10 additions & 0 deletions internal/handler/v2/config/apikey/config_create_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions internal/handler/v2/config/apikey/config_update_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions internal/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,6 +13,7 @@ import (
func Migrate() {
migrateThirdPartyDataSharingToTrueInPolicyCollection()
migrateThirdPartyDataSharingToTrueInDataAgreementsCollection()
migrateNameInApiKeyCollection()
}

func migrateThirdPartyDataSharingToTrueInPolicyCollection() {
Expand All @@ -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)
}
}

0 comments on commit cc521b2

Please sign in to comment.