Skip to content

Commit 8418ced

Browse files
fix: case insensitive arguments (#9)
The spec clearly expects most values to be case-sensitive especially scopes.
1 parent 52c9e90 commit 8418ced

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

arguments.go

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,39 @@
33

44
package oauth2
55

6-
import (
7-
"strings"
8-
)
9-
106
type Arguments []string
117

12-
// Matches performs an case-insensitive, out-of-order check that the items
8+
// Matches performs an case-sensitive, out-of-order check that the items
9+
// provided exist and equal all of the args in arguments.
10+
func (r Arguments) Matches(items ...string) bool {
11+
if len(r) != len(items) {
12+
return false
13+
}
14+
15+
found := make(map[string]bool)
16+
for _, item := range items {
17+
if !StringInSlice(item, r) {
18+
return false
19+
}
20+
found[item] = true
21+
}
22+
23+
return len(found) == len(r)
24+
}
25+
26+
// MatchesFold performs an case-insensitive, out-of-order check that the items
1327
// provided exist and equal all of the args in arguments.
1428
// Note:
1529
// - Providing a list that includes duplicate string-case items will return not
1630
// matched.
17-
func (r Arguments) Matches(items ...string) bool {
31+
func (r Arguments) MatchesFold(items ...string) bool {
1832
if len(r) != len(items) {
1933
return false
2034
}
2135

2236
found := make(map[string]bool)
2337
for _, item := range items {
24-
if !StringInSlice(item, r) {
38+
if !StringInSliceFold(item, r) {
2539
return false
2640
}
2741
found[item] = true
@@ -30,7 +44,7 @@ func (r Arguments) Matches(items ...string) bool {
3044
return len(found) == len(r)
3145
}
3246

33-
// Has checks, in a case-insensitive manner, that all of the items
47+
// Has checks, in a case-sensitive manner, that all of the items
3448
// provided exists in arguments.
3549
func (r Arguments) Has(items ...string) bool {
3650
for _, item := range items {
@@ -42,7 +56,19 @@ func (r Arguments) Has(items ...string) bool {
4256
return true
4357
}
4458

45-
// HasOneOf checks, in a case-insensitive manner, that one of the items
59+
// HasFold checks, in a case-insensitive manner, that all of the items
60+
// provided exists in arguments.
61+
func (r Arguments) HasFold(items ...string) bool {
62+
for _, item := range items {
63+
if !StringInSliceFold(item, r) {
64+
return false
65+
}
66+
}
67+
68+
return true
69+
}
70+
71+
// HasOneOf checks, in a case-sensitive manner, that one of the items
4672
// provided exists in arguments.
4773
func (r Arguments) HasOneOf(items ...string) bool {
4874
for _, item := range items {
@@ -54,9 +80,16 @@ func (r Arguments) HasOneOf(items ...string) bool {
5480
return false
5581
}
5682

57-
// Deprecated: Use ExactOne, Matches or MatchesExact
58-
func (r Arguments) Exact(name string) bool {
59-
return name == strings.Join(r, " ")
83+
// HasOneOfFold checks, in a case-sensitive manner, that one of the items
84+
// provided exists in arguments.
85+
func (r Arguments) HasOneOfFold(items ...string) bool {
86+
for _, item := range items {
87+
if StringInSliceFold(item, r) {
88+
return true
89+
}
90+
}
91+
92+
return false
6093
}
6194

6295
// ExactOne checks, by string case, that a single argument equals the provided

arguments_test.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,6 @@ var exactTests = []exactTestCase{
4343
},
4444
}
4545

46-
func TestArgumentsExact(t *testing.T) {
47-
testCases := append(exactTests, []exactTestCase{
48-
{
49-
args: Arguments{"foo", "bar"},
50-
exact: "foo bar",
51-
expect: true,
52-
},
53-
}...)
54-
55-
for k, c := range testCases {
56-
assert.Equal(t, c.expect, c.args.Exact(c.exact), "%d", k)
57-
t.Logf("Passed test case %d", k)
58-
}
59-
}
60-
6146
func TestArgumentsExactOne(t *testing.T) {
6247
testCases := append(exactTests, []exactTestCase{
6348
{
@@ -219,11 +204,11 @@ func TestArgumentsMatches(t *testing.T) {
219204
is: []string{"bar", "foo"},
220205
expect: true,
221206
},
222-
// should allow case-insensitive matching.
207+
// should not allow case-insensitive matching.
223208
{
224209
args: Arguments{"fOo", "bar"},
225210
is: []string{"foo", "BaR"},
226-
expect: true,
211+
expect: false,
227212
},
228213
// should return non-matching if duplicate items exist.
229214
{

helper.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import (
1010

1111
// StringInSlice returns true if needle exists in haystack
1212
func StringInSlice(needle string, haystack []string) bool {
13+
for _, b := range haystack {
14+
if b == needle {
15+
return true
16+
}
17+
}
18+
return false
19+
}
20+
21+
// StringInSliceFold returns true if needle exists in haystack (case-insensitive).
22+
func StringInSliceFold(needle string, haystack []string) bool {
1323
for _, b := range haystack {
1424
if strings.EqualFold(b, needle) {
1525
return true

0 commit comments

Comments
 (0)