@SuppressWarnings("unchecked") protected AuthenticationInfo internalClientGetAuthenticationInfo(final AuthenticationToken authenticationToken) { final UsernamePasswordAndClientToken clientToken = (UsernamePasswordAndClientToken) authenticationToken; log.debug("clientToken : {}", clientToken); if (clientToken == null) { return null; } final Credentials credentials = (Credentials) clientToken.getCredentials(); log.debug("credentials : {}", credentials); final Client<Credentials, CommonProfile> client = this.clients.findClient(clientToken.getClientName()); log.debug("client : {}", client); final CommonProfile profile = client.getUserProfile(credentials, clientToken.getContext()); log.debug("profile : {}", profile); if (profile == null) { final String message = "No profile retrieved from authentication using client : " + client + " and credentials : " + credentials; log.info(message); throw new AuthenticationException(message); } // refresh authentication token with user id final String userId = profile.getTypedId(); clientToken.setUserId(userId); // set rememberMe status clientToken.setRememberMe(profile.isRemembered()); return internalClientGetAuthenticationInfo(profile, credentials); }
protected JWTClaimsSet buildJwtClaimsSet(final U profile) { // claims builder with subject and issue time final JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder() .subject(profile.getTypedId()) .issueTime(new Date()); if (this.expirationTime != null) { builder.expirationTime(this.expirationTime); } // add attributes final Map<String, Object> attributes = profile.getAttributes(); for (final Map.Entry<String, Object> entry : attributes.entrySet()) { builder.claim(entry.getKey(), entry.getValue()); } builder.claim(INTERNAL_ROLES, profile.getRoles()); builder.claim(INTERNAL_PERMISSIONS, profile.getPermissions()); // claims return builder.build(); }
@Test public void testGetUserProfile() { final DirectFormClient formClient = getFormClient(); formClient.setProfileCreator((credentials, context) -> { String username = credentials.getUsername(); final CommonProfile profile = new CommonProfile(); profile.setId(username); profile.addAttribute(Pac4jConstants.USERNAME, username); return profile; }); final MockWebContext context = MockWebContext.create(); final CommonProfile profile = formClient.getUserProfile(new UsernamePasswordCredentials(USERNAME, USERNAME), context); assertEquals(USERNAME, profile.getId()); assertEquals(CommonProfile.class.getName() + CommonProfile.SEPARATOR + USERNAME, profile.getTypedId()); assertTrue(ProfileHelper.isTypedIdOf(profile.getTypedId(), CommonProfile.class)); assertEquals(USERNAME, profile.getUsername()); assertEquals(1, profile.getAttributes().size()); } }
@Test public void testGetUserProfile() { final FormClient formClient = getFormClient(); formClient.setProfileCreator((credentials, context) -> { String username = credentials.getUsername(); final CommonProfile profile = new CommonProfile(); profile.setId(username); profile.addAttribute(Pac4jConstants.USERNAME, username); return profile; }); final MockWebContext context = MockWebContext.create(); final CommonProfile profile = formClient.getUserProfile(new UsernamePasswordCredentials(USERNAME, USERNAME), context); assertEquals(USERNAME, profile.getId()); assertEquals(CommonProfile.class.getName() + CommonProfile.SEPARATOR + USERNAME, profile.getTypedId()); assertTrue(ProfileHelper.isTypedIdOf(profile.getTypedId(), CommonProfile.class)); assertEquals(USERNAME, profile.getUsername()); assertEquals(1, profile.getAttributes().size()); } }