This repository has been archived by the owner on May 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
“aud” claim can be string or []string
- Loading branch information
Showing
4 changed files
with
123 additions
and
13 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,37 @@ | ||
package jwt | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
) | ||
|
||
// ClaimStrings is used for parsing claim properties that | ||
// can be either a string or array of strings | ||
type ClaimStrings []string | ||
|
||
// UnmarshalJSON implements the json package's Unmarshaler interface | ||
func (c *ClaimStrings) UnmarshalJSON(data []byte) error { | ||
var value interface{} | ||
err := json.Unmarshal(data, &value) | ||
if err != nil { | ||
return err | ||
} | ||
switch v := value.(type) { | ||
case string: | ||
*c = ClaimStrings{v} | ||
case []interface{}: | ||
result := make(ClaimStrings, 0, len(v)) | ||
for i, vv := range v { | ||
if x, ok := vv.(string); ok { | ||
result = append(result, x) | ||
} else { | ||
return &json.UnsupportedTypeError{Type: reflect.TypeOf(v[i])} | ||
} | ||
} | ||
*c = result | ||
case nil: | ||
default: | ||
return &json.UnsupportedTypeError{Type: reflect.TypeOf(v)} | ||
} | ||
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,60 @@ | ||
package jwt_test | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/dgrijalva/jwt-go/v4" | ||
) | ||
|
||
var claimStringsTestData = []struct { | ||
name string | ||
input interface{} | ||
output jwt.ClaimStrings | ||
err error | ||
}{ | ||
{ | ||
name: "null", | ||
input: nil, | ||
output: nil, | ||
}, | ||
{ | ||
name: "single", | ||
input: "foo", | ||
output: jwt.ClaimStrings{"foo"}, | ||
}, | ||
{ | ||
name: "multi", | ||
input: []string{"foo", "bar"}, | ||
output: jwt.ClaimStrings{"foo", "bar"}, | ||
}, | ||
{ | ||
name: "invalid", | ||
input: float64(42), | ||
output: nil, | ||
err: &json.UnsupportedTypeError{Type: reflect.TypeOf(float64(42))}, | ||
}, | ||
{ | ||
name: "invalid multi", | ||
input: []interface{}{"foo", float64(42)}, | ||
output: nil, | ||
err: &json.UnsupportedTypeError{Type: reflect.TypeOf(float64(42))}, | ||
}, | ||
} | ||
|
||
func TestClaimStrings(t *testing.T) { | ||
for _, test := range claimStringsTestData { | ||
var r *struct { | ||
Value jwt.ClaimStrings `json:"value"` | ||
} | ||
data, _ := json.Marshal(map[string]interface{}{"value": test.input}) | ||
err := json.Unmarshal(data, &r) | ||
if !reflect.DeepEqual(err, test.err) { | ||
t.Errorf("[%v] Error didn't match expectation: %v != %v", test.name, test.err, err) | ||
} | ||
if !reflect.DeepEqual(test.output, r.Value) { | ||
t.Errorf("[%v] Unmarshaled value didn't match expectation: %v != %v", test.name, test.output, r.Value) | ||
} | ||
} | ||
} |
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