diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-09-09 18:37:42 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-09-09 18:37:42 +0700 |
| commit | 3f18324f5607cd103383365d2cb330639d6d2e36 (patch) | |
| tree | 297eb55d265d7ca35bfa27de2aea4d528b4dd852 | |
| parent | a02995e07ca6322689aa333563eb685f3e21f61f (diff) | |
| download | pakakeh.go-3f18324f5607cd103383365d2cb330639d6d2e36.tar.xz | |
paseto: check for empty key ID and private key on NewPublicMode
| -rw-r--r-- | lib/paseto/example_public_mode_test.go | 10 | ||||
| -rw-r--r-- | lib/paseto/public_mode.go | 17 | ||||
| -rw-r--r-- | lib/paseto/public_mode_test.go | 5 |
3 files changed, 27 insertions, 5 deletions
diff --git a/lib/paseto/example_public_mode_test.go b/lib/paseto/example_public_mode_test.go index 6868a8d1..705be1ae 100644 --- a/lib/paseto/example_public_mode_test.go +++ b/lib/paseto/example_public_mode_test.go @@ -37,7 +37,10 @@ func ExamplePublicMode() { // In the sender part, we register the sender key and the public key // of receiver in the list of peers. // - sender := NewPublicMode(senderKey) + sender, err := NewPublicMode(senderKey) + if err != nil { + log.Fatal(err) + } sender.AddPeer(receiverKey) footer := map[string]interface{}{ @@ -59,7 +62,10 @@ func ExamplePublicMode() { // In the receiver part, we register the receiver key and the public key // of sender in the list of peers. // - receiver := NewPublicMode(receiverKey) + receiver, err := NewPublicMode(receiverKey) + if err != nil { + log.Fatal(err) + } receiver.AddPeer(senderKey) // receiver receive the token from sender and unpack it ... diff --git a/lib/paseto/public_mode.go b/lib/paseto/public_mode.go index 9e33b1ee..36b1f535 100644 --- a/lib/paseto/public_mode.go +++ b/lib/paseto/public_mode.go @@ -41,12 +41,18 @@ type PublicMode struct { // NewPublicMode create new PublicMode with our private key for signing // outgoing token. // -func NewPublicMode(our Key) (auth *PublicMode) { +func NewPublicMode(our Key) (auth *PublicMode, err error) { + if len(our.ID) == 0 { + return nil, fmt.Errorf("empty key ID") + } + if len(our.Private) == 0 { + return nil, fmt.Errorf("empty private key") + } auth = &PublicMode{ our: our, peers: newKeys(), } - return auth + return auth, nil } // @@ -99,6 +105,13 @@ func (auth *PublicMode) AddPeer(k Key) (err error) { } // +// GetPeerKey get the peer's key based on key ID. +// +func (auth *PublicMode) GetPeerKey(id string) (k Key, ok bool) { + return auth.peers.get(id) +} + +// // RemovePeer remove peer's key from list. // func (auth *PublicMode) RemovePeer(id string) { diff --git a/lib/paseto/public_mode_test.go b/lib/paseto/public_mode_test.go index bed46e61..b9f95c02 100644 --- a/lib/paseto/public_mode_test.go +++ b/lib/paseto/public_mode_test.go @@ -27,7 +27,10 @@ func TestPublicMode_UnpackHTTPRequest(t *testing.T) { }, } - auth := NewPublicMode(ourKey) + auth, err := NewPublicMode(ourKey) + if err != nil { + t.Fatal(err) + } auth.AddPeer(ourKey) data := []byte("This is a signed message") |
