1

I have defined a UserLogin security check extending the UserAuthenticationSecurityCheck class. Once the user is authenticated I need to set some attributes when creating the user in the protected AuthenticatedUser createUser() function in order to use them later as headers in my Java Adapter.

My problem is that I'm not able to get the Active User information from my adapter resource secured by @OAuthSecurity(scope = "UserLogin"). I have tried defining in my securitycheck the following function:

public AuthenticatedUser getUser() {

        return authorizationContext.getActiveUser();

     }

and then calling it on this way from the adapter:

UserLogin userLogin;
logger.info("Logging info message..."+userLogin.getUser().getId());

But it returns a null pointer exception.

How am I suppose to get active user information from Java Adapters?

Maria Borbonés
  • 153
  • 1
  • 1
  • 12

2 Answers2

3

The AdapterSecurityContext class provides the security context of an adapter REST call.

Inside your Java adapter class, add the following at the class level:
@Context AdapterSecurityContext securityContext;

You can then get the current AuthenticatedUser using:
AuthenticatedUser currentUser = securityContext.getAuthenticatedUser();

You can find an example in ResourceAdapter sample (under the transactions enpoint).

Lior Burg
  • 175
  • 6
  • I have also tried that option, but I have just checked that every API that I include in the adapter @Context, such as, AdapterSecurityContext or AdaptersAPI causes a Null pointer exception within my code. – Maria Borbonés Jun 28 '16 at 15:28
0

You can set your attributes in the protected AuthenticatedUser createUser() function in UserLogin security class:

private Map<String, Object> attributes = new HashMap<String, Object>();
    @Override
    protected AuthenticatedUser createUser() {
        return new AuthenticatedUser(userId, displayName, this.getName(),attributes);
    }

You can get the Active user information in ResourceAdapter class using:

@Context
    AdapterSecurityContext securityContext;

public String getActiveUser(){

AuthenticatedUser currentUser = securityContext.getAuthenticatedUser();

return "Active user informations are:" +currentUser.getDisplayName()+" "+currentUser.getAuthenticatedBy()+" "+currentUser.getAttributes()+ " "+currentUser.getAuthenticatedAt();

 }

Click here for more information related to Class AuthenticatedUser.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97