diff --git a/obp-api/src/main/scala/code/api/util/ApiSession.scala b/obp-api/src/main/scala/code/api/util/ApiSession.scala index 59f4af5606..d6f5774911 100644 --- a/obp-api/src/main/scala/code/api/util/ApiSession.scala +++ b/obp-api/src/main/scala/code/api/util/ApiSession.scala @@ -30,7 +30,7 @@ case class CallContext( dauthResponseHeader: Option[String] = None, spelling: Option[String] = None, user: Box[User] = Empty, - onBehalfOfUser: Option[User] = None, + onBehalfOfUser: Box[User] = Empty, consenter: Box[User] = Empty, consumer: Box[Consumer] = Empty, ipAddress: String = "", diff --git a/obp-api/src/main/scala/code/api/util/ConsentUtil.scala b/obp-api/src/main/scala/code/api/util/ConsentUtil.scala index c2e907ee2b..7e7d4e294b 100644 --- a/obp-api/src/main/scala/code/api/util/ConsentUtil.scala +++ b/obp-api/src/main/scala/code/api/util/ConsentUtil.scala @@ -431,33 +431,38 @@ object Consent extends MdcLoggable { def applyConsentRules(consent: ConsentJWT): Future[(Box[User], Option[CallContext])] = { val cc = callContext - if(consent.createdByUserId.nonEmpty) { + if(consent.createdByUserId.nonEmpty) { // Populate on behalf of user in case of OBP Consent val onBehalfOfUser = Users.users.vend.getUserByUserId(consent.createdByUserId) cc.copy(onBehalfOfUser = onBehalfOfUser.toOption) } - // 1. Get or Create a User - getOrCreateUser(consent.sub, consent.iss, Some(consent.jti), None, None) map { - case (Full(user), newUser) => - // 2. Assign entitlements to the User - addEntitlements(user, consent) match { - case (Full(user)) => - // 3. Copy Auth Context to the User - copyAuthContextOfConsentToUser(consent.jti, user.userId, newUser) match { - case Full(_) => - // 4. Assign views to the User - (grantAccessToViews(user, consent), Some(cc)) - case failure@Failure(_, _, _) => // Handled errors - (failure, Some(callContext)) - case _ => - (Failure(ErrorMessages.UnknownError), Some(cc)) - } - case failure@Failure(msg, exp, chain) => // Handled errors - (Failure(msg), Some(cc)) - case _ => - (Failure(CannotAddEntitlement + consentAsJwt), Some(cc)) - } - case _ => - (Failure(CannotGetOrCreateUser + consentAsJwt), Some(cc)) + if (cc.onBehalfOfUser.nonEmpty && + APIUtil.getPropsAsBoolValue(nameOfProperty = "experimental_become_user_that_created_consent", defaultValue = false)) { + Future(cc.onBehalfOfUser, Some(cc)) // Just propagate on behalf of user back + } else { + // 1. Get or Create a User + getOrCreateUser(consent.sub, consent.iss, Some(consent.jti), None, None) map { + case (Full(user), newUser) => + // 2. Assign entitlements to the User + addEntitlements(user, consent) match { + case (Full(user)) => + // 3. Copy Auth Context to the User + copyAuthContextOfConsentToUser(consent.jti, user.userId, newUser) match { + case Full(_) => + // 4. Assign views to the User + (grantAccessToViews(user, consent), Some(cc)) + case failure@Failure(_, _, _) => // Handled errors + (failure, Some(callContext)) + case _ => + (Failure(ErrorMessages.UnknownError), Some(cc)) + } + case failure@Failure(msg, exp, chain) => // Handled errors + (Failure(msg), Some(cc)) + case _ => + (Failure(CannotAddEntitlement + consentAsJwt), Some(cc)) + } + case _ => + (Failure(CannotGetOrCreateUser + consentAsJwt), Some(cc)) + } } } diff --git a/obp-api/src/main/scala/code/api/v6_0_0/APIMethods600.scala b/obp-api/src/main/scala/code/api/v6_0_0/APIMethods600.scala index 727f0f50c2..318bb47d55 100644 --- a/obp-api/src/main/scala/code/api/v6_0_0/APIMethods600.scala +++ b/obp-api/src/main/scala/code/api/v6_0_0/APIMethods600.scala @@ -65,9 +65,10 @@ trait APIMethods600 { val permissions: Option[Permission] = Views.views.vend.getPermissionForUser(u).toOption val currentUser = UserV600(u, entitlements, permissions) val onBehalfOfUser = if(cc.onBehalfOfUser.isDefined) { - val entitlements = Entitlement.entitlement.vend.getEntitlementsByUserId(cc.onBehalfOfUser.get.userId).headOption.toList.flatten - val permissions: Option[Permission] = Views.views.vend.getPermissionForUser(cc.onBehalfOfUser.get).toOption - Some(UserV600(cc.onBehalfOfUser.get, entitlements, permissions)) + val user = cc.onBehalfOfUser.toOption.get + val entitlements = Entitlement.entitlement.vend.getEntitlementsByUserId(user.userId).headOption.toList.flatten + val permissions: Option[Permission] = Views.views.vend.getPermissionForUser(user).toOption + Some(UserV600(user, entitlements, permissions)) } else { None }