-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from gbrlsnchs/v3
V3
- Loading branch information
Showing
43 changed files
with
765 additions
and
645 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ sudo: false | |
|
||
language: 'go' | ||
go: | ||
- '1.10' | ||
- '1.11' | ||
- '1.12beta2' | ||
|
||
install: | ||
- 'cd $GOPATH' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package jwt | ||
|
||
import "encoding/json" | ||
|
||
// Audience is a special claim that may either be | ||
// a single string or an array of strings, as per the RFC 7519. | ||
type Audience []string | ||
|
||
// MarshalJSON implements a marshaling function for "aud" claim. | ||
func (a Audience) MarshalJSON() ([]byte, error) { | ||
switch len(a) { | ||
case 0: | ||
return json.Marshal("") // nil or empty slice returns an empty string | ||
case 1: | ||
return json.Marshal(a[0]) | ||
default: | ||
return json.Marshal([]string(a)) | ||
} | ||
} | ||
|
||
// UnmarshalJSON implements an unmarshaling function for "aud" claim. | ||
// TODO(gbrlsnchs): create tests for this method. | ||
func (a *Audience) UnmarshalJSON(b []byte) error { | ||
var ( | ||
v interface{} | ||
err error | ||
) | ||
if err = json.Unmarshal(b, &v); err != nil { | ||
return err | ||
} | ||
switch vv := v.(type) { | ||
case string: | ||
aud := make(Audience, 1) | ||
aud[0] = vv | ||
*a = aud | ||
case []interface{}: | ||
aud := make(Audience, 0, len(vv)) | ||
for i := range vv { | ||
aud[i] = vv[i].(string) | ||
} | ||
*a = aud | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package jwt_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
. "github.com/gbrlsnchs/jwt/v3" | ||
) | ||
|
||
func TestAudienceMarshal(t *testing.T) { | ||
testCases := []struct { | ||
aud Audience | ||
expected string | ||
}{ | ||
{Audience{"foo"}, `"foo"`}, | ||
{Audience{"foo", "bar"}, `["foo","bar"]`}, | ||
{nil, `""`}, | ||
{Audience{}, `""`}, | ||
{Audience{""}, `""`}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.expected, func(t *testing.T) { | ||
b, err := json.Marshal(tc.aud) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if want, got := tc.expected, b; want != string(got) { | ||
t.Errorf("want %s, got %s", want, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAudienceOmitempty(t *testing.T) { | ||
v := struct { | ||
Audience Audience `json:"aud,omitempty"` | ||
}{} | ||
b, err := json.Marshal(v) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if want, got := "{}", b; want != string(got) { | ||
t.Errorf("want %s, got %s", want, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.