use of com.epam.ta.reportportal.commons.ReportPortalUser in project service-authorization by reportportal.
the class GitHubTokenServices method loadAuthentication.
@Override
public OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException {
GitHubClient gitHubClient = GitHubClient.withAccessToken(accessToken);
UserResource gitHubUser = gitHubClient.getUser();
OAuthRegistrationResource oAuthRegistrationResource = oAuthRegistrationSupplier.get();
List<String> allowedOrganizations = ofNullable(oAuthRegistrationResource.getRestrictions()).flatMap(restrictions -> ofNullable(restrictions.get("organizations"))).map(it -> Splitter.on(",").omitEmptyStrings().splitToList(it)).orElse(emptyList());
if (!allowedOrganizations.isEmpty()) {
boolean assignedToOrganization = gitHubClient.getUserOrganizations(gitHubUser.getLogin()).stream().map(OrganizationResource::getLogin).anyMatch(allowedOrganizations::contains);
if (!assignedToOrganization) {
throw new InsufficientOrganizationException("User '" + gitHubUser.getLogin() + "' does not belong to allowed GitHUB organization");
}
}
ReportPortalUser user = replicator.replicateUser(gitHubUser, gitHubClient);
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, "N/A", user.getAuthorities());
Map<String, Serializable> extensionProperties = Collections.singletonMap(UPSTREAM_TOKEN, accessToken);
OAuth2Request request = new OAuth2Request(null, oAuthRegistrationResource.getClientId(), null, true, null, null, null, null, extensionProperties);
return new OAuth2Authentication(request, token);
}
use of com.epam.ta.reportportal.commons.ReportPortalUser in project service-authorization by reportportal.
the class GitHubUserReplicator method replicateUser.
/**
* Replicates GitHub user to internal database (if does NOT exist). Updates if exist. Creates personal project for that user
*
* @param userResource GitHub user to be replicated
* @param gitHubClient Configured github client
* @return Internal User representation
*/
@Transactional
public ReportPortalUser replicateUser(UserResource userResource, GitHubClient gitHubClient) {
String login = normalizeId(userResource.getLogin());
User user = userRepository.findByLogin(login).map(u -> {
if (UserType.GITHUB.equals(u.getUserType())) {
updateUser(u, userResource, gitHubClient);
} else {
// if user with such login exists, but it's not GitHub user than throw an exception
throw new UserSynchronizationException("User with login '" + u.getLogin() + "' already exists");
}
return u;
}).orElseGet(() -> userRepository.save(createUser(userResource, gitHubClient)));
return ReportPortalUser.userBuilder().fromUser(user);
}
use of com.epam.ta.reportportal.commons.ReportPortalUser in project service-authorization by reportportal.
the class DatabaseUserDetailsService method loadUserByUsername.
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
ReportPortalUser user = userRepository.findUserDetails(normalizeId(username)).orElseThrow(() -> new UsernameNotFoundException("User not found"));
UserDetails userDetails = org.springframework.security.core.userdetails.User.builder().username(user.getUsername()).password(user.getPassword() == null ? "" : user.getPassword()).authorities(AuthUtils.AS_AUTHORITIES.apply(user.getUserRole())).build();
return ReportPortalUser.userBuilder().withUserDetails(userDetails).withProjectDetails(user.getProjectDetails()).withUserId(user.getUserId()).withUserRole(user.getUserRole()).withEmail(user.getEmail()).build();
}
Aggregations