Skip to content

Commit 584ad92

Browse files
authored
Merge pull request #49 from rgritti/fix-scope-check-function
Fix scope check function
2 parents 8f27dda + 61a99ce commit 584ad92

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

zalando/zalando.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ func ScopeCheck(name string, scopes ...string) func(tc *ginoauth2.TokenContainer
131131
return func(tc *ginoauth2.TokenContainer, ctx *gin.Context) bool {
132132
scopesFromToken := make([]string, 0)
133133
for _, s := range configuredScopes {
134-
if cur, ok := tc.Scopes[s].(string); ok {
135-
glog.V(2).Infof("Found configured scope %s", cur)
136-
scopesFromToken = append(scopesFromToken, cur)
134+
if cur, ok := tc.Scopes[s]; ok {
135+
glog.V(2).Infof("Found configured scope %s", s)
136+
scopesFromToken = append(scopesFromToken, s)
137137
ctx.Set(s, cur) // set value from token of configured scope to the context, which you can use in your application.
138138
}
139139
}
@@ -150,9 +150,9 @@ func ScopeAndCheck(name string, scopes ...string) func(tc *ginoauth2.TokenContai
150150
return func(tc *ginoauth2.TokenContainer, ctx *gin.Context) bool {
151151
scopesFromToken := make([]string, 0)
152152
for _, s := range configuredScopes {
153-
if cur, ok := tc.Scopes[s].(string); ok {
154-
glog.V(2).Infof("Found configured scope %s", cur)
155-
scopesFromToken = append(scopesFromToken, cur)
153+
if cur, ok := tc.Scopes[s]; ok {
154+
glog.V(2).Infof("Found configured scope %s", s)
155+
scopesFromToken = append(scopesFromToken, s)
156156
ctx.Set(s, cur) // set value from token of configured scope to the context, which you can use in your application.
157157
} else {
158158
return false

zalando/zalando_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import (
44
"encoding/json"
55
"fmt"
66
"io/ioutil"
7+
"net/http/httptest"
78
"os"
89
"testing"
910
"time"
1011

12+
"github.com/stretchr/testify/assert"
13+
14+
"github.com/gin-gonic/gin"
15+
1116
"github.com/zalando/gin-oauth2"
1217
"golang.org/x/oauth2"
1318
)
@@ -61,3 +66,66 @@ func TestRequestTeamInfo(t *testing.T) {
6166
}
6267
fmt.Printf("%+v\n", data)
6368
}
69+
70+
func TestScopeCheck(t *testing.T) {
71+
// given
72+
tc := &ginoauth2.TokenContainer{
73+
Token: &oauth2.Token{
74+
AccessToken: "sdgergSgadGSAHBSHsagsdv.",
75+
TokenType: "Bearer",
76+
RefreshToken: "",
77+
},
78+
Scopes: map[string]interface{}{
79+
"my-scope-1": true,
80+
"my-scope-2": true,
81+
"uid": "stups_marilyn-updater",
82+
},
83+
GrantType: "password",
84+
Realm: "/services",
85+
}
86+
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
87+
88+
// when
89+
checkFn := ScopeCheck("name", "my-scope-1")
90+
result := checkFn(tc, ctx)
91+
92+
// then
93+
assert.True(t, result)
94+
95+
scopeVal, scopeOk := ctx.Get("my-scope-1")
96+
assert.True(t, scopeOk)
97+
assert.Equal(t, true, scopeVal)
98+
}
99+
100+
func TestScopeAndCheck(t *testing.T) {
101+
// given
102+
tc := &ginoauth2.TokenContainer{
103+
Token: &oauth2.Token{
104+
AccessToken: "sdgergSgadGSAHBSHsagsdv.",
105+
TokenType: "Bearer",
106+
RefreshToken: "",
107+
},
108+
Scopes: map[string]interface{}{
109+
"my-scope-1": true,
110+
"my-scope-2": true,
111+
"uid": "stups_marilyn-updater",
112+
},
113+
GrantType: "password",
114+
Realm: "/services",
115+
}
116+
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
117+
118+
// when
119+
checkFn := ScopeAndCheck("name", "uid", "my-scope-2")
120+
result := checkFn(tc, ctx)
121+
122+
// then
123+
assert.True(t, result)
124+
125+
uidVal, uidOk := ctx.Get("uid")
126+
scopeVal, scopeOk := ctx.Get("my-scope-2")
127+
assert.True(t, uidOk)
128+
assert.Equal(t, "stups_marilyn-updater", uidVal)
129+
assert.True(t, scopeOk)
130+
assert.Equal(t, true, scopeVal)
131+
}

0 commit comments

Comments
 (0)