Skip to content

Commit

Permalink
Switch Org > Project
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jan 19, 2017
1 parent c4114a5 commit 9e70d52
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FromContext() accepts the context from your GRPC request
```go
import "github.com/cubex/portcullis-go"

org := portcullis.FromContext(ctx).OrganisationID
project := portcullis.FromContext(ctx).ProjectID
```

Dependencies included in [Glide](https://glide.sh/).lock
18 changes: 9 additions & 9 deletions keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import "strings"
const (
keyprefix = "portc-"

organisationKey = "organisation"
usernameKey = "username"
userIDKey = "userid"
appIDkey = "appid"
vendorKey = "appvendor"
sigKey = "signature"
projectKey = "project"
usernameKey = "username"
userIDKey = "userid"
appIDkey = "appid"
vendorKey = "appvendor"
sigKey = "signature"

firstNameKey = "first-name"
lastNameKey = "last-name"
Expand All @@ -36,9 +36,9 @@ func GetAppVendorKey() string {
return keyprefix + vendorKey
}

// GetOrganisationKey retrieves the key used for organisation
func GetOrganisationKey() string {
return keyprefix + organisationKey
// GetProjectKey retrieves the key used for project
func GetProjectKey() string {
return keyprefix + projectKey
}

// GetUsernameKey retrieves the key used for username
Expand Down
36 changes: 18 additions & 18 deletions portcullis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (

// ReqInfo is the structure for deserialised request information
type ReqInfo struct {
OrganisationID string
UserID string
AppID string
VendorID string
Username string
FirstName string
LastName string
signature string
meta metadata.MD
ProjectID string
UserID string
AppID string
VendorID string
Username string
FirstName string
LastName string
signature string
meta metadata.MD
}

// Verify checks that the request signature matches using signature key
Expand Down Expand Up @@ -64,15 +64,15 @@ func (r *ReqInfo) GlobalAppID() string {
func FromContext(ctx context.Context) ReqInfo {
md, _ := metadata.FromContext(ctx)
res := ReqInfo{
OrganisationID: safeGetMetaValString(keys.GetOrganisationKey(), md),
UserID: safeGetMetaValString(keys.GetUserIDKey(), md),
Username: safeGetMetaValString(keys.GetUsernameKey(), md),
FirstName: safeGetMetaValString(keys.GetFirstNameKey(), md),
LastName: safeGetMetaValString(keys.GetLastNameKey(), md),
AppID: safeGetMetaValString(keys.GetAppIDKey(), md),
VendorID: safeGetMetaValString(keys.GetAppVendorKey(), md),
signature: safeGetMetaValString(keys.GetSignatureKey(), md),
meta: md,
ProjectID: safeGetMetaValString(keys.GetProjectKey(), md),
UserID: safeGetMetaValString(keys.GetUserIDKey(), md),
Username: safeGetMetaValString(keys.GetUsernameKey(), md),
FirstName: safeGetMetaValString(keys.GetFirstNameKey(), md),
LastName: safeGetMetaValString(keys.GetLastNameKey(), md),
AppID: safeGetMetaValString(keys.GetAppIDKey(), md),
VendorID: safeGetMetaValString(keys.GetAppVendorKey(), md),
signature: safeGetMetaValString(keys.GetSignatureKey(), md),
meta: md,
}
return res
}
Expand Down
20 changes: 10 additions & 10 deletions portcullis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
testOrg = "this-is-a-test-org-id"
testProject = "this-is-a-test-project-id"
testUserID = "this-is-a-test-user-id"
testUsername = "this-is-a-test-username"

Expand All @@ -23,7 +23,7 @@ const (
// TestAuthDataExtraction tests for valid transaction of portcullis meta data values
func TestAuthDataExtraction(t *testing.T) {
metamap := map[string]string{}
metamap[keys.GetOrganisationKey()] = testOrg
metamap[keys.GetProjectKey()] = testProject
metamap[keys.GetUserIDKey()] = testUserID
metamap[keys.GetUsernameKey()] = testUsername
metamap[keys.GetAppIDKey()] = testAppID
Expand All @@ -37,8 +37,8 @@ func TestAuthDataExtraction(t *testing.T) {
t.Error("Global app ID does not contain expected value")
}

if in.OrganisationID != testOrg {
t.Error("Organisation does not contain expected value")
if in.ProjectID != testProject {
t.Error("Project does not contain expected value")
}

if in.Username != testUsername {
Expand All @@ -57,16 +57,16 @@ func TestAuthDataExtractionWithMissingFields(t *testing.T) {
meta := metadata.New(metamap)
ctx := metadata.NewContext(context.Background(), meta)

org := portcullis.FromContext(ctx).OrganisationID
project := portcullis.FromContext(ctx).ProjectID
username := portcullis.FromContext(ctx).Username
userID := portcullis.FromContext(ctx).UserID

if username != testUsername {
t.Error("Username does not contain expected value")
}

if org != "" {
t.Error("Organisation does not contain expected value")
if project != "" {
t.Error("Project does not contain expected value")
}

if userID != "" {
Expand All @@ -77,9 +77,9 @@ func TestAuthDataExtractionWithMissingFields(t *testing.T) {
// TestExtractionWithInvalidContext tests extraction result with context contains no metadata
func TestExtractionWithInvalidContext(t *testing.T) {
ctx := context.TODO()
org := portcullis.FromContext(ctx).OrganisationID
project := portcullis.FromContext(ctx).ProjectID

if org != "" {
t.Error("Organisation does not contain expected value")
if project != "" {
t.Error("Project does not contain expected value")
}
}

0 comments on commit 9e70d52

Please sign in to comment.