Skip to content

Commit

Permalink
Remove vestigial privilege token stuff that's now in a library; clean…
Browse files Browse the repository at this point in the history
… up some names
  • Loading branch information
elffjs committed Dec 1, 2023
1 parent 5bba8f9 commit f29f9ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 74 deletions.
18 changes: 8 additions & 10 deletions cmd/devices-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
pb "github.com/DIMO-Network/devices-api/pkg/grpc"
"github.com/DIMO-Network/shared"
pbuser "github.com/DIMO-Network/shared/api/users"
pr "github.com/DIMO-Network/shared/middleware/privilegetoken"
"github.com/DIMO-Network/shared/middleware/privilegetoken"
"github.com/DIMO-Network/zflogger"
"github.com/Shopify/sarama"
"github.com/aws/aws-sdk-go-v2/service/s3"
Expand Down Expand Up @@ -196,21 +196,19 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, pdb db.Store,

vPriv := app.Group("/v1/vehicle/:tokenID", privilegeAuth)

tk := pr.New(pr.Config{
Log: &logger,
})
privTokenWare := privilegetoken.New(privilegetoken.Config{Log: &logger})

vehicleAddr := common.HexToAddress(settings.VehicleNFTAddress)

// vehicle command privileges
vPriv.Get("/status", tk.OneOf(vehicleAddr, []int64{controllers.NonLocationData, controllers.CurrentLocation, controllers.AllTimeLocation}), nftController.GetVehicleStatus)
vPriv.Get("/status", privTokenWare.OneOf(vehicleAddr, []int64{controllers.NonLocationData, controllers.CurrentLocation, controllers.AllTimeLocation}), nftController.GetVehicleStatus)
if !settings.IsProduction() {
vPriv.Get("/vin-credential", tk.OneOf(vehicleAddr, []int64{controllers.VinCredential}), nftController.GetVinCredential)
vPriv.Get("/vin-credential", privTokenWare.OneOf(vehicleAddr, []int64{controllers.VinCredential}), nftController.GetVinCredential)
}
vPriv.Post("/commands/doors/unlock", tk.OneOf(vehicleAddr, []int64{controllers.Commands}), nftController.UnlockDoors)
vPriv.Post("/commands/doors/lock", tk.OneOf(vehicleAddr, []int64{controllers.Commands}), nftController.LockDoors)
vPriv.Post("/commands/trunk/open", tk.OneOf(vehicleAddr, []int64{controllers.Commands}), nftController.OpenTrunk)
vPriv.Post("/commands/frunk/open", tk.OneOf(vehicleAddr, []int64{controllers.Commands}), nftController.OpenFrunk)
vPriv.Post("/commands/doors/unlock", privTokenWare.OneOf(vehicleAddr, []int64{controllers.Commands}), nftController.UnlockDoors)
vPriv.Post("/commands/doors/lock", privTokenWare.OneOf(vehicleAddr, []int64{controllers.Commands}), nftController.LockDoors)
vPriv.Post("/commands/trunk/open", privTokenWare.OneOf(vehicleAddr, []int64{controllers.Commands}), nftController.OpenTrunk)
vPriv.Post("/commands/frunk/open", privTokenWare.OneOf(vehicleAddr, []int64{controllers.Commands}), nftController.OpenFrunk)

// Traditional tokens

Expand Down
64 changes: 0 additions & 64 deletions internal/controllers/helpers/handlers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package helpers

import (
"encoding/json"
"fmt"
"strconv"

Expand All @@ -18,17 +17,6 @@ import (
//d "github.com/dexidp/dex/api/v2"
)

type CustomClaims struct {
ContractAddress common.Address `json:"contract_address"`
TokenID string `json:"token_id"`
PrivilegeIDs []int64 `json:"privilege_ids"`
}

type Token struct {
jwt.RegisteredClaims
CustomClaims
}

// ErrorResponseHandler is deprecated. it doesn't log. We prefer to return an err and have the ErrorHandler in api.go handle stuff.
func ErrorResponseHandler(c *fiber.Ctx, err error, status int) error {
msg := ""
Expand All @@ -47,58 +35,6 @@ func GetUserID(c *fiber.Ctx) string {
return userID
}

type VehicleTokenClaims struct {
VehicleTokenID string
UserEthAddress string
Privileges []int64
}

type VehicleTokenClaimsResponseRaw struct {
Sub string
UserID string
Privileges []int64
}

func GetVehicleTokenClaims(c *fiber.Ctx) (VehicleTokenClaims, error) {
token := c.Locals("user").(*jwt.Token)
claims := token.Claims.(jwt.MapClaims)

jsonbody, err := json.Marshal(claims)
if err != nil {
return VehicleTokenClaims{}, err
}

p := VehicleTokenClaimsResponseRaw{}

if err := json.Unmarshal(jsonbody, &p); err != nil {
return VehicleTokenClaims{}, err
}

return VehicleTokenClaims{
VehicleTokenID: p.Sub,
UserEthAddress: p.UserID,
Privileges: p.Privileges,
}, nil
}

func GetPrivilegeTokenClaims(c *fiber.Ctx) (Token, error) {
token := c.Locals("user").(*jwt.Token)
claims := token.Claims.(jwt.MapClaims)

jsonbody, err := json.Marshal(claims)
if err != nil {
return Token{}, err
}

var t Token
err = json.Unmarshal(jsonbody, &t)
if err != nil {
return Token{}, err
}

return t, nil
}

// CreateResponse is a generic response with an ID of the created entity
type CreateResponse struct {
ID string `json:"id"`
Expand Down

0 comments on commit f29f9ac

Please sign in to comment.