com.sap.ipe.ble.remotehandler.authentication.CustomAuthenticationInfoProvider Maven / Gradle / Ivy
package com.sap.ipe.ble.remotehandler.authentication;
import com.sap.cds.services.authentication.AuthenticationInfo;
import com.sap.cds.services.authentication.JwtTokenAuthenticationInfo;
import com.sap.cds.services.impl.request.RequestContextImpl;
import com.sap.cds.services.request.RequestContext;
import com.sap.cds.services.runtime.AuthenticationInfoProvider;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/***
* The CustomAuthenticationInfoProvider provides the AuthenticationInfo based on the RequestContext
*/
@Component
@Order(value = Ordered.LOWEST_PRECEDENCE - 1000)
public class CustomAuthenticationInfoProvider implements AuthenticationInfoProvider {
private AuthenticationInfoProvider defaultProvider;
private final ThreadLocal jwt = new ThreadLocal<>();
/***
* Set the user-claim credentials
* @param accessToken user-claim credentials
*/
public void setJwt(String accessToken) {
jwt.set(accessToken);
}
/***
* Removes the user-claim credentials
*/
public void removeJwt() {
jwt.remove();
}
/***
* Returns the AuthenticationInfo based on the RequestContext
* @return Returns the AuthenticationInfo based on the RequestContext
*/
@Override
public AuthenticationInfo get() {
RequestContext parentRequestContext = RequestContextImpl.getCurrentInternal();
if(parentRequestContext == null) {
return defaultProvider.get();
} else {
return new JwtTokenAuthenticationInfo(jwt.get());
}
}
/***
* Set the previously registered provider
* @param prev Set the previously registered provider.
*/
@Override
public void setPrevious(AuthenticationInfoProvider prev) {
this.defaultProvider = prev;
}
}