Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auth nil pointer #2725

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions gateway/enforcer/internal/authorization/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,13 @@ import (

// Validate performs the authorization.
func Validate(rch *requestconfig.Holder, subAppDataStore *datastore.SubscriptionApplicationDataStore, cfg *config.Server) *dto.ImmediateResponse {
if immediateResponse := ValidateScopes(*rch.JWTValidationInfo.Scopes, rch.MatchedResource.Scopes, rch.MatchedResource.Path); immediateResponse != nil {
if immediateResponse := ValidateScopes(rch); immediateResponse != nil {
return immediateResponse
}
cfg.Logger.Info(fmt.Sprintf("Scope validation successful for the request: %s", rch.MatchedResource.Path))
if rch.MatchedAPI.SubscriptionValidation {
appID := rch.ExternalProcessingEnvoyAttributes.ApplicationID
if appID == "" && rch.JWTValidationInfo.ClientID != "" {
appID = getAppIDUsingConsumerKey(rch.JWTValidationInfo.ClientID, subAppDataStore, rch.MatchedAPI, "")
} else {
return &dto.ImmediateResponse{
StatusCode: 403,
Message: "Application ID not found",
}
}
return validateSubscription(appID, subAppDataStore, rch)

return validateSubscription(subAppDataStore, rch)
}
return nil
}
35 changes: 23 additions & 12 deletions gateway/enforcer/internal/authorization/scope_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,37 @@ package authorization

import (
"fmt"

"github.com/wso2/apk/gateway/enforcer/internal/dto"
"github.com/wso2/apk/gateway/enforcer/internal/requestconfig"
)

// ValidateScopes validates the scopes of the user against the required scopes.
func ValidateScopes(scopes []string, requiredScopes []string, path string) *dto.ImmediateResponse {
for _, requiredScope := range requiredScopes {
found := false
for _, scope := range scopes {
if requiredScope == scope {
found = true
break
}
}
if !found {
func ValidateScopes(rch *requestconfig.Holder) *dto.ImmediateResponse {
if rch.MatchedResource != nil {
if rch.JWTValidationInfo.Scopes == nil && len(rch.MatchedResource.Scopes) > 0 {
return &dto.ImmediateResponse{
StatusCode: 403,
Message: fmt.Sprintf("User is NOT authorized to access the Resource: %s. Scope validation failed.", path),
Message: fmt.Sprintf("User is NOT authorized to access the Resource: %s. Scope validation failed.", rch.MatchedResource.Path),
}
}
// scopes []string, requiredScopes []string, path string
for _, requiredScope := range rch.MatchedResource.Scopes {
found := false
for _, scope := range *rch.JWTValidationInfo.Scopes {
if requiredScope == scope {
found = true
break
}
}
if !found {
return &dto.ImmediateResponse{
StatusCode: 403,
Message: fmt.Sprintf("User is NOT authorized to access the Resource: %s. Scope validation failed.", rch.MatchedResource.Path),
}
}
}
}

return nil
}

17 changes: 16 additions & 1 deletion gateway/enforcer/internal/authorization/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,22 @@ const (
)

// validateSubscription validates the subscription.
func validateSubscription(appID string, subAppDatastore *datastore.SubscriptionApplicationDataStore, rch *requestconfig.Holder) *dto.ImmediateResponse{
func validateSubscription(subAppDatastore *datastore.SubscriptionApplicationDataStore, rch *requestconfig.Holder) *dto.ImmediateResponse{
if rch.JWTValidationInfo == nil {
return &dto.ImmediateResponse{
StatusCode: 403,
Message: "JWT validation info not found",
}
}
appID := rch.ExternalProcessingEnvoyAttributes.ApplicationID
if appID == "" && rch.JWTValidationInfo.ClientID != "" {
appID = getAppIDUsingConsumerKey(rch.JWTValidationInfo.ClientID, subAppDatastore, rch.MatchedAPI, "")
} else {
return &dto.ImmediateResponse{
StatusCode: 403,
Message: "Application ID not found",
}
}
api := rch.MatchedAPI
appMaps := subAppDatastore.GetApplicationMappings(api.OrganizationID, appID)
for _, appMap := range appMaps {
Expand Down