Skip to content

Commit

Permalink
Support for roles
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jul 18, 2017
1 parent 91e0fb9 commit a36abf4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Portcullis Go Library
#### Usage
FromContext() accepts the context from your GRPC request
```go
import "github.com/cubex/portcullis-go"
import "github.com/kubex/portcullis-go"

project := portcullis.FromContext(ctx).ProjectID
```
Expand Down
7 changes: 7 additions & 0 deletions keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const (

firstNameKey = "first-name"
lastNameKey = "last-name"

rolesKey = "roles"
)

// GetKeyPrefix returns portcullis key prefix
Expand Down Expand Up @@ -61,6 +63,11 @@ func GetLastNameKey() string {
return keyprefix + lastNameKey
}

// GetRolesKey key for retrieving roles from the request
func GetRolesKey() string {
return keyprefix + rolesKey
}

// GetGenericKeyForString retrieves key for given generic value
func GetGenericKeyForString(in string) string {
key := strings.Replace(in, " ", "-", -1)
Expand Down
26 changes: 25 additions & 1 deletion portcullis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"

"github.com/cubex/portcullis-go/keys"
"github.com/kubex/portcullis-go/keys"
)

// ReqInfo is the structure for deserialised request information
Expand All @@ -25,6 +25,7 @@ type ReqInfo struct {
LastName string
signature string
meta metadata.MD
Roles []string
}

// Verify checks that the request signature matches using signature key
Expand Down Expand Up @@ -60,6 +61,16 @@ func (r *ReqInfo) GlobalAppID() string {
return fmt.Sprintf("%s/%s", r.VendorID, r.AppID)
}

// HasRole check if the user has a specific role
func (r *ReqInfo) HasRole(checkRole string) bool {
for _, role := range r.Roles {
if role == checkRole {
return true
}
}
return false
}

// FromContext retrieves request info from given request context
func FromContext(ctx context.Context) ReqInfo {
md, _ := metadata.FromContext(ctx)
Expand All @@ -72,6 +83,7 @@ func FromContext(ctx context.Context) ReqInfo {
AppID: safeGetMetaValString(keys.GetAppIDKey(), md),
VendorID: safeGetMetaValString(keys.GetAppVendorKey(), md),
signature: safeGetMetaValString(keys.GetSignatureKey(), md),
Roles: safeGetMetaValStringSlice(keys.GetRolesKey(), md),
meta: md,
}
return res
Expand All @@ -86,3 +98,15 @@ func safeGetMetaValString(key string, md metadata.MD) string {
}
return result
}

func safeGetMetaValStringSlice(key string, md metadata.MD) []string {
result := []string{}
if md != nil {
if sliceKeys, hasKey := md[key]; hasKey {
for _, sliceValue := range sliceKeys {
result = append(result, sliceValue)
}
}
}
return result
}
17 changes: 15 additions & 2 deletions portcullis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"

"github.com/cubex/portcullis-go"
"github.com/cubex/portcullis-go/keys"
"github.com/kubex/portcullis-go"
"github.com/kubex/portcullis-go/keys"
)

const (
Expand All @@ -30,13 +30,21 @@ func TestAuthDataExtraction(t *testing.T) {
metamap[keys.GetAppVendorKey()] = testVendor

meta := metadata.New(metamap)

meta[keys.GetRolesKey()] = append(meta[keys.GetRolesKey()], "role2")
meta[keys.GetRolesKey()] = append(meta[keys.GetRolesKey()], "role1")

ctx := metadata.NewContext(context.Background(), meta)
in := portcullis.FromContext(ctx)

if in.GlobalAppID() != fmt.Sprintf("%s/%s", testVendor, testAppID) {
t.Error("Global app ID does not contain expected value")
}

if !in.HasRole("role1") || !in.HasRole("role2") {
t.Error("Roles do not contain expected values")
}

if in.ProjectID != testProject {
t.Error("Project does not contain expected value")
}
Expand All @@ -60,6 +68,7 @@ func TestAuthDataExtractionWithMissingFields(t *testing.T) {
project := portcullis.FromContext(ctx).ProjectID
username := portcullis.FromContext(ctx).Username
userID := portcullis.FromContext(ctx).UserID
roles := portcullis.FromContext(ctx).Roles

if username != testUsername {
t.Error("Username does not contain expected value")
Expand All @@ -72,6 +81,10 @@ func TestAuthDataExtractionWithMissingFields(t *testing.T) {
if userID != "" {
t.Error("userID does not contain expected value")
}

if len(roles) != 0 {
t.Error("roles do not contain expected value")
}
}

// TestExtractionWithInvalidContext tests extraction result with context contains no metadata
Expand Down

0 comments on commit a36abf4

Please sign in to comment.