org.pac4j.core.profile.UserProfile Java Examples

The following examples show how to use org.pac4j.core.profile.UserProfile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClientAuthenticationHandler.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected HandlerResult createResult(final ClientCredential credentials, final UserProfile profile)
    throws GeneralSecurityException, PreventedException {
    final String id;
    if (typedIdUsed) {
        id = profile.getTypedId();
    } else {
        id = profile.getId();
    }
    if (StringUtils.isNotBlank(id)) {
        credentials.setUserProfile(profile);
        return new DefaultHandlerResult(
            this,
            new BasicCredentialMetaData(credentials),
            this.principalFactory.createPrincipal(id, profile.getAttributes()));
    }
    throw new FailedLoginException("No identifier found for this user profile: " + profile);
}
 
Example #2
Source File: AuthenticationFilter.java    From minnal with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
protected User retrieveProfile(Session session) {
	Object profile = session.getAttribute(PRINCIPAL);
	if (profile == null) {
		return null;
	}
	Client client = getClient(session);
	Class<UserProfile> type = Generics.getTypeParameter(client.getClass(), UserProfile.class);
	if (type.isAssignableFrom(profile.getClass())) {
		return new User((UserProfile) profile);
	}
	if (profile instanceof Map) {
		String buffer = Serializer.DEFAULT_JSON_SERIALIZER.serialize(profile);
		profile = Serializer.DEFAULT_JSON_SERIALIZER.deserialize(buffer, type);
		User user = new User((UserProfile) profile);
		session.addAttribute(PRINCIPAL, profile);
		return user;
	}
	// Can't come here 
	return null;
}
 
Example #3
Source File: SimpleAuthorizerTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setup() {
	userRoleMapper = mock(UserRoleMapper.class);
	when(userRoleMapper.getRoles(user)).thenReturn(Arrays.asList("role1", "role2"));
	rolePermissionMapper = mock(RolePermissionMapper.class);
	when(rolePermissionMapper.getPermissions("role1")).thenReturn(Arrays.asList("permission1", "permission2"));
	when(rolePermissionMapper.getPermissions("role2")).thenReturn(Arrays.asList("permission2"));
	authorizer = new SimpleAuthorizer(rolePermissionMapper, userRoleMapper);
	UserProfile profile = new UserProfile();
	profile.setId("name1");
	user = new User(profile);
}
 
Example #4
Source File: SimpleUserRoleMapperTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnEmptyListForUserWithoutRole() {
	UserProfile profile = new UserProfile();
	profile.setId("user3");
	User user = new User(profile);
	assertEquals(mapper.getRoles(user).size(), 0);
}
 
Example #5
Source File: SimpleUserRoleMapperTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setup() {
	mapper = new SimpleUserRoleMapper();
	UserProfile profile = new UserProfile();
	profile.setId("user1");
	user = new User(profile);
}
 
Example #6
Source File: OauthPersonDirectoryPrincipalResolver.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
@Override
public Principal resolve(Credential credential) {
    logger.debug("Attempting to resolve a principal...");

    if (credential instanceof ClientCredential){
        // do nothing
    } else {
        throw new RuntimeException("用户数据转换异常!");
    }

    ClientCredential oauthCredential = (ClientCredential) credential;
    UserProfile userProfile = oauthCredential.getUserProfile();
    logger.info("userProfile = {}", userProfile);


    //String principalId = oauthCredential.getUserProfile().getId();
    String principalId = oauthCredential.getId();
    if (principalId == null) {
        logger.debug("Got null for extracted principal ID; returning null.");
        return null;
    }

    logger.debug("Creating SimplePrincipal for [{}]", principalId);
    //UserProfile userProfile = oauthCredential.getUserProfile();
    final Map<String, Object> attributes = userProfile.getAttributes();

    if (attributes == null & !this.returnNullIfNoAttributes) {
        return new SimplePrincipal(principalId);
    }

    if (attributes == null) {
        return null;
    }

    return new SimplePrincipal(principalId, attributes);
}
 
Example #7
Source File: ClientAuthenticationHandler.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
@Override
protected HandlerResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException {
    final ClientCredential clientCredentials = (ClientCredential) credential;
    logger.debug("clientCredentials : {}", clientCredentials);

    final String clientName = clientCredentials.getCredentials().getClientName();
    logger.debug("clientName : {}", clientName);

    // get client
    final Client<org.pac4j.core.credentials.Credentials, UserProfile> client = this.clients.findClient(clientName);
    logger.debug("client : {}", client);

    // web context
    final ServletExternalContext servletExternalContext = (ServletExternalContext) ExternalContextHolder.getExternalContext();
    final HttpServletRequest request = (HttpServletRequest) servletExternalContext.getNativeRequest();
    final HttpServletResponse response = (HttpServletResponse) servletExternalContext.getNativeResponse();
    final WebContext webContext = new J2EContext(request, response);

    // get user profile
    final UserProfile userProfile = client.getUserProfile(clientCredentials.getCredentials(), webContext);
    logger.debug("userProfile : {}", userProfile);

    if (userProfile != null && StringUtils.isNotBlank(userProfile.getTypedId())) {
        clientCredentials.setUserProfile(userProfile);
        return new HandlerResult(
                this,
                new BasicCredentialMetaData(credential),
                new SimplePrincipal(userProfile.getTypedId(), userProfile.getAttributes()));
    }

    throw new FailedLoginException("Provider did not produce profile for " + clientCredentials);
}
 
Example #8
Source File: CallbackFilter.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext request) {
	URI uri = URI.create(getClients().getCallbackUrl());
	if (! HttpUtil.structureUrl(request.getUriInfo().getPath()).equalsIgnoreCase(uri.getPath())) {
	    logger.debug("Request path {} doesn't match callback url. Skipping", request.getUriInfo().getPath());
		return;
	}
	
	Session session = getSession(request, true);
	JaxrsWebContext context = getContext(request, session);
	Client client = getClient(session);
	if (client == null) {
	    client = getClient(context);
	}
	if (client == null) {
		context.setResponseStatus(422);
		if (listener != null) {
		    listener.authFailed(session);
		}
	} else {
		try {
			Credentials credentials = client.getCredentials(context);
			UserProfile userProfile = client.getUserProfile(credentials, context);
			session.addAttribute(Clients.DEFAULT_CLIENT_NAME_PARAMETER, client.getName());
			session.addAttribute(PRINCIPAL, userProfile);
			if (listener != null) {
                listener.authSuccess(session, userProfile);
			}
			getConfiguration().getSessionStore().save(session);
			context.setResponseStatus(Response.Status.OK.getStatusCode());
		} catch (RequiresHttpAction e) {
			context.setResponseStatus(e.getCode());
			if (listener != null) {
                listener.authFailed(session);
            }
		}
	}
	request.abortWith(context.getResponse());
}
 
Example #9
Source File: MinnalSecurityContext.java    From minnal with Apache License 2.0 5 votes vote down vote up
@Override
public User getUserPrincipal() {
	UserProfile profile = session.getAttribute(AuthenticationFilter.PRINCIPAL);
	if (profile != null) {
		return new User(profile);
	}
	return null;
}
 
Example #10
Source File: AbstractClientAuthenticationHandler.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Override
protected HandlerResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException {
    final ClientCredential clientCredentials = (ClientCredential) credential;
    logger.debug("clientCredentials : {}", clientCredentials);

    final Credentials credentials = clientCredentials.getCredentials();
    final String clientName = credentials.getClientName();
    logger.debug("clientName : {}", clientName);

    // get client
    final Client<Credentials, UserProfile> client = this.clients.findClient(clientName);
    logger.debug("client : {}", client);

    // web context
    final ServletExternalContext servletExternalContext = (ServletExternalContext) ExternalContextHolder.getExternalContext();
    final HttpServletRequest request = (HttpServletRequest) servletExternalContext.getNativeRequest();
    final HttpServletResponse response = (HttpServletResponse) servletExternalContext.getNativeResponse();
    final WebContext webContext = new J2EContext(request, response);
    
    // get user profile
    final UserProfile userProfile = client.getUserProfile(credentials, webContext);
    logger.debug("userProfile : {}", userProfile);

    if (userProfile != null) {
        return createResult(clientCredentials, userProfile);
    }

    throw new FailedLoginException("Provider did not produce a user profile for: " + clientCredentials);
}
 
Example #11
Source File: Pac4JHttpServletRequestWrapper.java    From jee-pac4j with Apache License 2.0 4 votes vote down vote up
private Optional<UserProfile> getProfile() {
    return ProfileHelper.flatIntoOneProfile(profiles);
}
 
Example #12
Source File: User.java    From minnal with Apache License 2.0 4 votes vote down vote up
/**
 * @return the profile
 */
public UserProfile getProfile() {
	return profile;
}
 
Example #13
Source File: User.java    From minnal with Apache License 2.0 4 votes vote down vote up
/**
 * @param profile
 */
public User(UserProfile profile) {
	this.profile = profile;
}
 
Example #14
Source File: Pac4JHttpServletRequestWrapper.java    From jee-pac4j with Apache License 2.0 4 votes vote down vote up
private Optional<Principal> getPrincipal() {
    return getProfile().map(UserProfile::asPrincipal);
}
 
Example #15
Source File: Pac4JHttpServletRequestWrapper.java    From jee-pac4j with Apache License 2.0 4 votes vote down vote up
public Pac4JHttpServletRequestWrapper(HttpServletRequest request, Collection<UserProfile> profiles) {
    super(request);
    this.profiles = profiles;
}
 
Example #16
Source File: ClientCredential.java    From cas4.0.x-server-wechat with Apache License 2.0 2 votes vote down vote up
/**
 * Define the user profile.
 *
 * @param theUserProfile The user profile
 */
public void setUserProfile(final UserProfile theUserProfile) {
    this.userProfile = theUserProfile;
}
 
Example #17
Source File: ClientCredential.java    From cas4.0.x-server-wechat with Apache License 2.0 2 votes vote down vote up
/**
 * Return the profile of the authenticated user.
 *
 * @return the profile of the authenticated user
 */
public UserProfile getUserProfile() {
    return userProfile;
}
 
Example #18
Source File: ClientCredential.java    From springboot-shiro-cas-mybatis with MIT License 2 votes vote down vote up
/**
 * Define the user profile.
 *
 * @param theUserProfile The user profile
 */
public void setUserProfile(final UserProfile theUserProfile) {
    this.userProfile = theUserProfile;
}
 
Example #19
Source File: ClientCredential.java    From springboot-shiro-cas-mybatis with MIT License 2 votes vote down vote up
/**
 * Return the profile of the authenticated user.
 *
 * @return the profile of the authenticated user
 */
public UserProfile getUserProfile() {
    return userProfile;
}
 
Example #20
Source File: AbstractClientAuthenticationHandler.java    From springboot-shiro-cas-mybatis with MIT License 2 votes vote down vote up
/**
 * Build the handler result.
 *
 * @param credentials the provided credentials
 * @param profile the retrieved user profile
 * @return the built handler result
 * @throws GeneralSecurityException On authentication failure.
 * @throws PreventedException On the indeterminate case when authentication is prevented.
 */
protected abstract HandlerResult createResult(final ClientCredential credentials, final UserProfile profile)
    throws GeneralSecurityException, PreventedException;
 
Example #21
Source File: AuthenticationListener.java    From minnal with Apache License 2.0 votes vote down vote up
void authSuccess(Session session, UserProfile profile);