Skip to content

Commit bb9d8bb

Browse files
committed
[Misc] Subscription: Saas Scope handling amended
Scope handling in the subscription service has been amended to support either `.Callback` or `.mtCallback` in the JWT token. This allows most apps to transition easily, as one of these usually exists already.
1 parent faba299 commit bb9d8bb

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

cmd/server/internal/handler.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,11 @@ func (s *SubscriptionHandler) checkAuthorization(authHeader string, saasData *ut
315315

316316
token := authHeader[7:]
317317
err := VerifyXSUAAJWTToken(context.TODO(), token, &XSUAAConfig{
318-
UAADomain: saasData.UAADomain,
319-
ClientID: saasData.ClientId,
320-
XSAppName: uaaData.XSAppName,
321-
RequiredScopes: []string{uaaData.XSAppName + ".Callback", uaaData.XSAppName + ".mtcallback"},
318+
UAADomain: saasData.UAADomain,
319+
ClientID: saasData.ClientId,
320+
XSAppName: uaaData.XSAppName,
321+
// `.Callback` is the scope usually used by approuter and `.mtcallback` is used by CAP. Either one of these may be present.
322+
ExpectedScopes: []string{uaaData.XSAppName + ".Callback", uaaData.XSAppName + ".mtcallback"},
322323
}, s.httpClientGenerator.NewHTTPClient())
323324
if err != nil {
324325
util.LogError(err, "failed token validation", step, "checkAuthorization", nil, "XSAppName", uaaData.XSAppName)

cmd/server/internal/jwt.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type XSUAAConfig struct {
2727
// one of xsappname OR clientid must be part of the audience
2828
XSAppName string
2929
ClientID string
30-
// all requested scopes must be fulfilled
31-
RequiredScopes []string
30+
// at least one expected scope must be fulfilled
31+
ExpectedScopes []string
3232
}
3333

3434
type XSUAAJWTClaims struct {
@@ -221,12 +221,12 @@ func adjustForNamespace(s []string, ignoreIfNotNamespaced bool) []string {
221221
func verifyScopes(claims *XSUAAJWTClaims, config *XSUAAConfig) bool {
222222
scope := claims.Scope
223223
tokenScope := convertToMap(scope)
224-
for _, expected := range config.RequiredScopes {
225-
if _, ok := tokenScope[expected]; !ok {
226-
return false // all expected scopes should match
224+
for _, expected := range config.ExpectedScopes {
225+
if _, ok := tokenScope[expected]; ok {
226+
return true // at least 1 expected scope should match
227227
}
228228
}
229-
return true
229+
return false
230230
}
231231

232232
// Create a dummy lookup map

cmd/server/internal/jwt_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func SetupValidTokenAndIssuerForSubscriptionTests(xsappname string) (*http.Clien
6363
UAADomain: jwtTestUAADomain,
6464
XSAppName: xsappname,
6565
ClientID: "some-client-id",
66-
RequiredScopes: []string{xsappname + ".Callback", xsappname + ".mtcallback"},
66+
ExpectedScopes: []string{xsappname + ".Callback"},
6767
}, &jwtTestParameters{})
6868
}
6969

@@ -83,7 +83,7 @@ func setupTokenAndIssuer(config *XSUAAConfig, params *jwtTestParameters) (*http.
8383
return nil, "", fmt.Errorf("error generating rsa key: %s", err.Error())
8484
}
8585
claims := XSUAAJWTClaims{
86-
Scope: config.RequiredScopes,
86+
Scope: config.ExpectedScopes,
8787
ClientID: brokerApp,
8888
AuthorizedParty: brokerApp,
8989
RegisteredClaims: jwt.RegisteredClaims{
@@ -199,7 +199,7 @@ var testXSUAAConfig *XSUAAConfig = &XSUAAConfig{
199199
UAADomain: jwtTestUAADomain,
200200
XSAppName: "myxsappname",
201201
ClientID: "some-client-id",
202-
RequiredScopes: []string{"myxsappname.Callback", "myxsappname.mtcallback"},
202+
ExpectedScopes: []string{"myxsappname.mtcallback"},
203203
}
204204

205205
func testVerifyValidToken(t *testing.T) {
@@ -218,7 +218,7 @@ func testValidTokenWithBrokerClientId(t *testing.T) {
218218
brokerXSUAAConfig := &XSUAAConfig{
219219
UAADomain: testXSUAAConfig.UAADomain,
220220
ClientID: testXSUAAConfig.ClientID,
221-
RequiredScopes: testXSUAAConfig.RequiredScopes,
221+
ExpectedScopes: testXSUAAConfig.ExpectedScopes,
222222
XSAppName: "xsapp!b4711",
223223
}
224224
client, tokenString, err := setupTokenAndIssuer(brokerXSUAAConfig, &jwtTestParameters{clientIsBroker: true})

0 commit comments

Comments
 (0)