Skip to content

Commit

Permalink
Merge pull request #52 from pubnub/CE-3286-grant-multiple-auth-keys
Browse files Browse the repository at this point in the history
CE 3286 grant with multiple auth keys
  • Loading branch information
crimsonred committed Sep 4, 2018
2 parents 5b9018f + f2a356d commit 8e61f89
Show file tree
Hide file tree
Showing 15 changed files with 497 additions and 233 deletions.
13 changes: 11 additions & 2 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
---
changelog:
-
changes:
-
text: "Multiple auth keys in grant fix"
type: bug
-
text: "Presence timetoken float64 fix"
type: bug
date: Sep 4, 18
version: v4.1.3
-
changes:
-
Expand Down Expand Up @@ -255,7 +265,6 @@ scm: github.com/pubnub/go
supported-platforms:
-
editors:
- "1.7.1"
- "1.7.6"
- "1.8.7"
- "1.9.7"
Expand All @@ -266,4 +275,4 @@ supported-platforms:
- "Mac OS X 10.8 or later, amd64"
- "Windows 7 or later, amd64, 386"
version: "PubNub Go SDK"
version: v4.1.2
version: v4.1.3
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: go

go:
- 1.7.1
- 1.7.6
- 1.8.7
- 1.9.7
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

# PubNub 4.1.2 client for Go
* Go (1.7.1+)
# PubNub 4.1.3 client for Go
* Go (1.7.6+)

# Please direct all Support Questions and Concerns to Support@PubNub.com

[![GoDoc](https://godoc.org/github.com/pubnub/go?status.svg)](https://godoc.org/github.com/pubnub/go)
[![Build Status](https://travis-ci.org/pubnub/go.svg)](https://travis-ci.org/pubnub/go)
[![Build Status](https://api.travis-ci.org/pubnub/go.svg?branch=master)](https://travis-ci.org/pubnub/go)
[![codecov.io](https://codecov.io/github/pubnub/go/coverage.svg)](https://codecov.io/github/pubnub/go)
[![Go Report Card](https://goreportcard.com/badge/github.com/pubnub/go)](https://goreportcard.com/report/github.com/pubnub/go)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.2
4.1.3
2 changes: 1 addition & 1 deletion endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func buildURL(o endpointOpts) (*url.URL, error) {
query.Set("filter-expr", o.config().FilterExpression)
}

if v := o.config().AuthKey; v != "" {
if v := o.config().AuthKey; v != "" && query.Get("auth") == "" {
query.Set("auth", v)
}

Expand Down
1 change: 1 addition & 0 deletions examples/cli/cli_demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func connect() {
config.PublishKey = "demo"
config.SubscribeKey = "demo"
config.SecretKey = "demo"
config.AuthKey = "akey"

config.CipherKey = "enigma"
pn = pubnub.NewPubNub(config)
Expand Down
67 changes: 52 additions & 15 deletions publish_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pubnub

import (
"fmt"
"log"
"net/url"
"testing"

Expand Down Expand Up @@ -69,6 +70,7 @@ func AssertSuccessPublishGet2(t *testing.T, expectedString string, message inter
assert := assert.New(t)

pn := NewPubNub(NewDemoConfig())
pn.Config.AuthKey = "a"

o := newPublishBuilder(pn)
o.Channel("ch")
Expand All @@ -85,18 +87,52 @@ func AssertSuccessPublishGet2(t *testing.T, expectedString string, message inter
path, []int{})

query, err := o.opts.buildQuery()
log.Println(query)

assert.Nil(err)
expected := &url.Values{}
expected.Set("store", "0")
expected.Set("norep", "0")
expected.Set("norep", "10")
expected.Set("seqn", "1")
expected.Set("uuid", pn.Config.UUID)
expected.Set("ttl", "10")
expected.Set("pnsdk", Version)
expected.Set("norep", "true")

h.AssertQueriesEqual(t, expected, query,
[]string{"seqn", "pnsdk", "uuid"}, []string{})

}

func AssertSuccessPublishGetAuth(t *testing.T, expectedString string, message interface{}) {

assert := assert.New(t)

pn := NewPubNub(NewDemoConfig())
pn.Config.AuthKey = "PubAuthKey"

o := newPublishBuilder(pn)
o.Channel("ch")
o.Message(message)
o.TTL(10)
o.ShouldStore(true)
o.DoNotReplicate(true)

path, err := o.opts.buildPath()
assert.Nil(err)

h.AssertPathsEqual(t,
fmt.Sprintf("/publish/demo/demo/0/ch/0/%s", expectedString),
path, []int{})

body, err := o.opts.buildBody()

assert.Nil(err)
assert.Empty(body)
assert.Equal(10, o.opts.TTL)
assert.Equal(true, o.opts.ShouldStore)
assert.Equal(true, o.opts.DoNotReplicate)

}

func AssertSuccessPublishGetMeta(t *testing.T, expectedString string, message interface{}) {
assert := assert.New(t)

Expand Down Expand Up @@ -156,6 +192,7 @@ func TestPublishMixedGet(t *testing.T) {
msgMap["three"] = "hey3"

AssertSuccessPublishGet(t, "12", 12)
AssertSuccessPublishGetAuth(t, "12", 12)
AssertSuccessPublishGet(t, "%22hey%22", "hey")
AssertSuccessPublishGet(t, "true", true)
AssertSuccessPublishGet(t, "%5B%22hey1%22%2C%22hey2%22%2C%22hey3%22%5D",
Expand All @@ -181,18 +218,18 @@ func TestPublishMixedGet(t *testing.T) {
"%7B%22one%22%3A%22hey1%22%2C%22three%22%3A%22hey3%22%2C%22two%22%3A%22hey2%22%7D",
msgMap)

// AssertSuccessPublishGet2(t, "12", 12)
// AssertSuccessPublishGet2(t, "%22hey%22", "hey")
// AssertSuccessPublishGet2(t, "true", true)
// AssertSuccessPublishGet2(t, "%5B%22hey1%22%2C%22hey2%22%2C%22hey3%22%5D",
// []string{"hey1", "hey2", "hey3"})
// AssertSuccessPublishGet2(t, "%5B1%2C2%2C3%5D", []int{1, 2, 3})
// AssertSuccessPublishGet2(t,
// "%7B%22one%22%3A%22hey1%22%2C%22two%22%3A%22hey2%22%2C%22three%22%3A%22hey3%22%7D",
// msgStruct)
// AssertSuccessPublishGet2(t,
// "%7B%22one%22%3A%22hey1%22%2C%22three%22%3A%22hey3%22%2C%22two%22%3A%22hey2%22%7D",
// msgMap)
AssertSuccessPublishGet2(t, "12", 12)
AssertSuccessPublishGet2(t, "%22hey%22", "hey")
AssertSuccessPublishGet2(t, "true", true)
AssertSuccessPublishGet2(t, "%5B%22hey1%22%2C%22hey2%22%2C%22hey3%22%5D",
[]string{"hey1", "hey2", "hey3"})
AssertSuccessPublishGet2(t, "%5B1%2C2%2C3%5D", []int{1, 2, 3})
AssertSuccessPublishGet2(t,
"%7B%22one%22%3A%22hey1%22%2C%22two%22%3A%22hey2%22%2C%22three%22%3A%22hey3%22%7D",
msgStruct)
AssertSuccessPublishGet2(t,
"%7B%22one%22%3A%22hey1%22%2C%22three%22%3A%22hey3%22%2C%22two%22%3A%22hey2%22%7D",
msgMap)
}

func TestPublishMixedPost(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pubnub.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// Default constants
const (
// Version :the version of the SDK
Version = "4.1.2"
Version = "4.1.3"
// MaxSequence for publish messages
MaxSequence = 65535
)
Expand Down
11 changes: 10 additions & 1 deletion subscription_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,16 @@ func processSubscribePayload(m *SubscriptionManager, payload subscribeMessage) {
uuid, _ = presencePayload["uuid"].(string)
occupancy, _ = presencePayload["occupancy"].(int)
if presencePayload["timestamp"] != nil {
timestamp = int64(presencePayload["timestamp"].(int))
m.pubnub.Config.Log.Println("presencePayload['timestamp'] type", reflect.TypeOf(presencePayload["timestamp"]).Kind())
switch presencePayload["timestamp"].(type) {
case int:
timestamp = int64(presencePayload["timestamp"].(int))
break
case float64:
timestamp = int64(presencePayload["timestamp"].(float64))
break
}

}

data = presencePayload["data"]
Expand Down
51 changes: 51 additions & 0 deletions subscription_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,57 @@ func TestProcessSubscribePayload(t *testing.T) {
//pn.Destroy()
}

func TestProcessSubscribePayloadPresence(t *testing.T) {
assert := assert.New(t)
done := make(chan bool)
pn := NewPubNub(NewDemoConfig())
listener := NewListener()

go func() {
for {
select {
case status := <-listener.Status:
assert.Nil(status.Error)
done <- true
break
case _ = <-listener.Message:
assert.Fail("No error")
done <- true
break
case presence := <-listener.Presence:
assert.Equal("join", presence.Event)
assert.Equal("channel", presence.Channel)
assert.Equal(int64(1535709775), presence.Timestamp)
assert.Equal("pn-7b82321a-5359-4780-bfc0-611659105d74", presence.UUID)
assert.Equal(4, presence.Occupancy)
done <- true
break
}
}
}()

pn.AddListener(listener)

//{"action":"join","uuid":"pn-7b82321a-5359-4780-bfc0-611659105d74","timestamp":1535709775,"occupancy":4}
payload := &map[string]interface{}{
"action": "join",
"timestamp": float64(1535709775),
"uuid": "pn-7b82321a-5359-4780-bfc0-611659105d74",
"occupancy": 4,
}

sm := &subscribeMessage{
Shard: "1",
SubscriptionMatch: "channel-pnpres",
Channel: "channel-pnpres",
Payload: *payload,
}

processSubscribePayload(pn.subscriptionManager, *sm)
<-done
//pn.Destroy()
}

func TestProcessSubscribePayloadSubMatch(t *testing.T) {
assert := assert.New(t)
done1 := make(chan bool)
Expand Down
89 changes: 89 additions & 0 deletions tests/e2e/grant_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,102 @@
package e2e

import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"time"

pubnub "github.com/pubnub/go"
"github.com/pubnub/go/tests/stubs"
"github.com/stretchr/testify/assert"
)

func TestGrantParseLogsForAuthKey(t *testing.T) {

assert := assert.New(t)
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

pn := pubnub.NewPubNub(configCopy())
pn.Config.SecretKey = "sec-key"
pn.Config.AuthKey = "myAuthKey"

pn.Config.Log = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)

pn.Grant().
Read(true).Write(true).Manage(true).
Channels([]string{"ch1", "ch2"}).
Execute()

tic := time.NewTicker(time.Duration(timeout) * time.Second)
select {
case <-tic.C:
tic.Stop()
}

w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout

//fmt.Printf("Captured: %s", out)

s := fmt.Sprintf("%s", out)
// //https://ps.pndsn.com/v1/auth/grant/sub-key/sub-c-e41d50d4-43ce-11e8-a433-9e6b275e7b64?w=1&m=1&channel=ch1,ch2&timestamp=1535719943&auth=myAuthKey&pnsdk=PubNub-Go/4.1.3&uuid=pn-621c7b2a-f87c-4362-bd1e-6c6762dfc667&r=1&signature=PntTQe-zBfJa6AvN4bu4u0txG_TOoksHGod7OnijmwM=
// expected := fmt.Sprintf("https://%s/v1/auth/grant/sub-key/%s?&uuid=%sw=1&m=1&channel=ch1,ch2",
// pn.Config.Origin,
// pn.Config.SubscribeKey,
// )

// assert.Contains(s, expected)

//auth=myAuthKey&pnsdk=PubNub-Go/4.1.3
expected2 := fmt.Sprintf("auth=%s",
pn.Config.AuthKey)

assert.Contains(s, expected2)

}

func TestGrantParseLogsForMultipleAuthKeys(t *testing.T) {

assert := assert.New(t)
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

pn := pubnub.NewPubNub(configCopy())
pn.Config.SecretKey = "sec-key"
pn.Config.AuthKey = "myAuthKey"

pn.Config.Log = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)

pn.Grant().
Read(true).Write(true).Manage(true).
AuthKeys([]string{"authkey1", "authkey2"}).
Channels([]string{"ch1", "ch2"}).
Execute()

tic := time.NewTicker(time.Duration(timeout) * time.Second)
select {
case <-tic.C:
tic.Stop()
}

w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout

//fmt.Printf("Captured: %s", out)

s := fmt.Sprintf("%s", out)

//https://ps.pndsn.com/v1/auth/grant/sub-key/sub-c-e41d50d4-43ce-11e8-a433-9e6b275e7b64?m=1&auth=authkey1,authkey2&channel=ch1,ch2&timestamp=1535719219&pnsdk=PubNub-Go/4.1.3&uuid=pn-a83164fe-7ecf-42ab-ba14-d2d8e6eabd7a&r=1&w=1&signature=0SkyfvohAq8_0phVi0YhCL4c2ZRSPBVwCwQ9fANvPmM=
assert.Contains(s, "auth=authkey1,authkey2")
}

func TestGrantSucccessNotStubbed(t *testing.T) {
assert := assert.New(t)

Expand Down
Loading

0 comments on commit 8e61f89

Please sign in to comment.