cancel
Showing results for 
Search instead for 
Did you mean: 

CdsRuntimeError - While implementing Custom UserInfoProvider

naresh12
Associate
Associate

errorlog.txtIn our application, we are implementing Instance based Authorization(https://cap.cloud.sap/docs/guides/authorization#instance-based-auth) so that user can have access only to respective organization records. To implement instance based authorization, we get organization Id via SAML attributes (attribute : sapBpidOrg).

The actual problem is with values obtained from XSUAA UserInfo class for the attribute “sapBpidOrg”. For example, user has been associated to organization 12345, the value obtained after decoding XSUAA UserInfo “[12345]” but the expected value [“12345”].

Therefore, we are trying to modify attribute(“sapBpidOrg”) value obtained from XSUAA UserInfo by overriding the class UserInfoProvider. However, we are stuck with error “Error creating bean with name 'com.sap.cds.framework.spring.config.runtime.CdsRuntimeInitializer': Invocation of init method failed;”.

    @Autowired
    UserInfo userInfo;

    @Autowired
    CdsRuntime cdsRuntime;

    @Override
    public UserInfo get() {
        LOG.debug("User Information Statement {}", cdsRuntime.getProvidedUserInfo().getAttributeValues("uid"));
        cdsRuntime.requestContext().privilegedUser().run(context -> {
            LOG.debug("User Information {}", context.getUserInfo().getAttributeValues("uid"));
        });
        
        List<String> value = new ArrayList<>();
        final Pattern pattern = Pattern.compile("\\[[^\\]]*\\]");

        if (userInfo != null) {
            
            cdsRuntime.requestContext().privilegedUser().run(context -> {
                List<String> organisationId = userInfo.getAttributeValues("sapBpidOrg");
                List<String> xtractedVals = null;

                if (organisationId != null) {
                    for (String customerId : organisationId) {

                        if (pattern.matcher(customerId).matches()) {
                            String str = customerId.substring(1, customerId.length() - 1);
                            xtractedVals = Stream.of(str.split(",")).collect(Collectors.toList());
                        }

                        value.addAll(xtractedVals);

                    }
                    LOG.debug("User roles of user [{}] from db: {}", userInfo.getName(), value);
                }
            });
        }

        final ModifiableUserInfo modifiableUserInfo = userInfo.copy();
        modifiableUserInfo.setAttributeValues("sapBpidOrg", value);

        LOG.debug("Modified attributes of user [{}]: {}", userInfo.getName(),
                modifiableUserInfo.getAttributeValues("sapBpidOrg"));

        return modifiableUserInfo.as(UserInfo.class);
    }    
}<br>

Accepted Solutions (0)

Answers (0)