Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes privileges enum. #55

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions middleware/privilegetoken/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

"github.com/DIMO-Network/shared/privileges"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/gofiber/fiber/v2"
Expand All @@ -12,9 +13,9 @@ import (
)

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

type Token struct {
Expand Down
7 changes: 4 additions & 3 deletions middleware/privilegetoken/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package privilegetoken
import (
"fmt"

"github.com/DIMO-Network/shared/privileges"
"github.com/ethereum/go-ethereum/common"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog"
"golang.org/x/exp/slices"
)

type IVerifyPrivilegeToken interface {
OneOf(contract common.Address, privilegeIDs []int64) fiber.Handler
OneOf(contract common.Address, privilegeIDs []privileges.Privilege) fiber.Handler
}

type Config struct {
Expand All @@ -27,7 +28,7 @@ func New(cfg Config) IVerifyPrivilegeToken {
}
}

func (p *verifyPrivilegeToken) OneOf(contract common.Address, privilegeIDs []int64) fiber.Handler {
func (p *verifyPrivilegeToken) OneOf(contract common.Address, privilegeIDs []privileges.Privilege) fiber.Handler {
return func(c *fiber.Ctx) error {
return p.checkPrivilege(c, contract, privilegeIDs)
}
Expand Down Expand Up @@ -55,7 +56,7 @@ func (p *verifyPrivilegeToken) getClaims(c *fiber.Ctx, logger zerolog.Logger) (C
return claims, nil
}

func (p *verifyPrivilegeToken) checkPrivilege(c *fiber.Ctx, contract common.Address, privilegeIDs []int64) error {
func (p *verifyPrivilegeToken) checkPrivilege(c *fiber.Ctx, contract common.Address, privilegeIDs []privileges.Privilege) error {
logger := p.cfg.Log.With().Str("src", "mw.shared.privilegetoken").Logger()

// This checks that the privileges are for the token specified by the path variable :tokenID
Expand Down
27 changes: 11 additions & 16 deletions middleware/privilegetoken/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"testing"

"github.com/DIMO-Network/shared/privileges"
"github.com/ethereum/go-ethereum/common"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
Expand Down Expand Up @@ -46,12 +47,6 @@ func initTestHelper(t *testing.T) testHelper {
func (t testHelper) signToken(p jwt.MapClaims) *jwt.Token {
return jwt.NewWithClaims(jwt.SigningMethodHS256, p)
}

const (
Commands = 1
AllTimeLocation = 2
)

func TestSuccessOnValidSinglePrivilege(t *testing.T) {
th := initTestHelper(t)

Expand All @@ -67,14 +62,14 @@ func TestSuccessOnValidSinglePrivilege(t *testing.T) {
token := th.signToken((jwt.MapClaims{
"token_id": "1",
"contract_address": vehicleAddr,
"privilege_ids": []int64{Commands},
"privilege_ids": []int64{int64(privileges.VehicleCommands)},
}))

c.Locals("user", token)
return c.Next()
})

th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []int64{Commands}), func(c *fiber.Ctx) error {
th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleCommands}), func(c *fiber.Ctx) error {
return c.SendString("Ok")
})

Expand All @@ -101,14 +96,14 @@ func TestSuccessOnValidTokenPrivilegeOnMany(t *testing.T) {
token := th.signToken((jwt.MapClaims{
"token_id": "1",
"contract_address": vehicleAddr,
"privilege_ids": []int64{Commands},
"privilege_ids": []int64{int64(privileges.VehicleCommands)},
}))

c.Locals("user", token)
return c.Next()
})

th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []int64{Commands, AllTimeLocation}), func(c *fiber.Ctx) error {
th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleCommands, privileges.VehicleAllTimeLocation}), func(c *fiber.Ctx) error {

return c.SendString("Ok")
})
Expand All @@ -134,7 +129,7 @@ func TestMiddlewareWriteClaimsToContext(t *testing.T) {
cClaims := CustomClaims{
ContractAddress: vehicleAddr,
TokenID: "1",
PrivilegeIDs: []int64{Commands},
PrivilegeIDs: []privileges.Privilege{privileges.VehicleCommands},
}
th.app.Use(func(c *fiber.Ctx) error {
token := th.signToken((jwt.MapClaims{
Expand All @@ -147,7 +142,7 @@ func TestMiddlewareWriteClaimsToContext(t *testing.T) {
return c.Next()
})

th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []int64{Commands, AllTimeLocation}), func(c *fiber.Ctx) error {
th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleAllTimeLocation, privileges.VehicleCommands}), func(c *fiber.Ctx) error {
cl := c.Locals("tokenClaims").(CustomClaims)
th.assert.Equal(cl, cClaims)
return c.SendString("Ok")
Expand Down Expand Up @@ -176,14 +171,14 @@ func TestFailureOnInvalidPrivilegeInToken(t *testing.T) {
token := th.signToken((jwt.MapClaims{
"token_id": "1",
"contract_address": vehicleAddr,
"privilege_ids": []int64{Commands},
"privilege_ids": []int64{int64(privileges.VehicleCommands)},
}))

c.Locals("user", token)
return c.Next()
})

th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []int64{AllTimeLocation}), func(c *fiber.Ctx) error {
th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleAllTimeLocation}), func(c *fiber.Ctx) error {
return c.SendString("Ok")
})

Expand All @@ -210,14 +205,14 @@ func TestFailureOnInvalidContractAddress(t *testing.T) {
token := th.signToken((jwt.MapClaims{
"token_id": "1",
"contract_address": common.BytesToAddress([]byte{uint8(2)}),
"privilege_ids": []int64{Commands},
"privilege_ids": []int64{int64(privileges.VehicleCommands)},
}))

c.Locals("user", token)
return c.Next()
})

th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []int64{AllTimeLocation}), func(c *fiber.Ctx) error {
th.app.Get("/v1/test/:tokenID", th.privilegeMiddleware.OneOf(vehicleAddr, []privileges.Privilege{privileges.VehicleAllTimeLocation}), func(c *fiber.Ctx) error {
return c.SendString("Ok")
})

Expand Down
41 changes: 24 additions & 17 deletions privileges/privileges.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
// Package privileges contains the different privileges that can be given to a user.
// Privilege names and values defined here come from values stored on chain.
package privileges

// Privilege is a type for the different privileges that can be given.
// Privilege is an enum that represents the different privileges that can be given to a user.
type Privilege int64

const (
// NonLocationData provides access to all data excluding location data.
NonLocationData Privilege = 1
// ManufacureMintDevice provides access to minting a device.
ManufacureMintDevice Privilege = 1
// ManufactureDistributeDevice provides access to distributing a device.
ManufactureDistributeDevice Privilege = 2
// ManufactureFactoryReset provides access to factory resetting a device.
ManufactureFactoryReset Privilege = 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manufacturer? (misspelled in the first line)


// Commands provides access to all commands that can be sent to the device.
Commands Privilege = 2
// Vehicle Privileges.

// CurrentLocation provides access to the current location of the device.
CurrentLocation Privilege = 3

// AllTimeLocation provives access to current and historical location data.
AllTimeLocation Privilege = 4
// VehicleNonLocationData provides access to all data excluding location data.
VehicleNonLocationData Privilege = 1
// VehicleCommands provides access to all commands that can be sent to the device.
VehicleCommands Privilege = 2
// VehicleCurrentLocation provides access to the current location of the device.
VehicleCurrentLocation Privilege = 3
// VehicleAllTimeLocation provives access to current and historical location data.
VehicleAllTimeLocation Privilege = 4
)

// String returns the string representation of a privilege.
func (p Privilege) String() string {
switch p {
case NonLocationData:
// String returns the string representation of a VehiclePrivilege.
func (v Privilege) String() string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add cases for the manufacturer ones?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I am going to get rid of it since we can't distinguish ManufacturerMintDevice and VehicleNonLocationData in any meaningful way.

switch v {
case VehicleNonLocationData:
return "NonLocationData"
case Commands:
case VehicleCommands:
return "Commands"
case CurrentLocation:
case VehicleCurrentLocation:
return "CurrentLocation"
case AllTimeLocation:
case VehicleAllTimeLocation:
return "AllTimeLocation"
default:
return "Unknown"
Expand Down
Loading