@Override public void validate(final TokenCredentials credentials, final WebContext webContext) { val token = credentials.getToken().trim(); val at = this.ticketRegistry.getTicket(token, AccessToken.class); if (at == null || at.isExpired()) { val err = String.format("Access token is not found or has expired. Unable to authenticate requesting party access token %s", token); throw new CredentialsException(err); } if (!at.getScopes().contains(getRequiredScope())) { val err = String.format("Missing scope [%s]. Unable to authenticate requesting party access token %s", OAuth20Constants.UMA_PERMISSION_URL, token); throw new CredentialsException(err); } val profile = new CommonProfile(); val authentication = at.getAuthentication(); val principal = authentication.getPrincipal(); profile.setId(principal.getId()); val attributes = new LinkedHashMap<String, Object>(authentication.getAttributes()); attributes.putAll(principal.getAttributes()); profile.addAttributes(attributes); profile.addPermissions(at.getScopes()); profile.addAttribute(AccessToken.class.getName(), at); LOGGER.debug("Authenticated access token [{}]", profile); credentials.setUserProfile(profile); }
val profile = new CommonProfile(); profile.setId(username); entry.getAttributes().forEach(a -> profile.addAttribute(a.getName(), a.getStringValues()));
@Override public void validate(final UsernamePasswordCredentials credentials, final WebContext context) { if (credentials == null) { throw new CredentialsException("No credential"); } String username = credentials.getUsername(); String password = credentials.getPassword(); if (CommonHelper.isBlank(username)) { throw new CredentialsException("Username cannot be blank"); } if (CommonHelper.isBlank(password)) { throw new CredentialsException("Password cannot be blank"); } if (CommonHelper.areNotEquals(username, password)) { throw new CredentialsException("Username : '" + username + "' does not match password"); } final CommonProfile profile = new CommonProfile(); profile.setId(username); profile.addAttribute(Pac4jConstants.USERNAME, username); credentials.setUserProfile(profile); } }
@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()); } }