Search in sources :

Example 1 with ReportPortalUser

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);
}
Also used : Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) UPSTREAM_TOKEN(com.epam.reportportal.auth.integration.github.ExternalOauth2TokenConverter.UPSTREAM_TOKEN) Supplier(java.util.function.Supplier) ResourceServerTokenServices(org.springframework.security.oauth2.provider.token.ResourceServerTokenServices) Serializable(java.io.Serializable) List(java.util.List) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Map(java.util.Map) AuthenticationException(org.springframework.security.core.AuthenticationException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) OAuthRegistrationResource(com.epam.ta.reportportal.ws.model.settings.OAuthRegistrationResource) Splitter(com.google.common.base.Splitter) InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) Collections(java.util.Collections) OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) Serializable(java.io.Serializable) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) OAuthRegistrationResource(com.epam.ta.reportportal.ws.model.settings.OAuthRegistrationResource) OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser)

Example 2 with ReportPortalUser

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);
}
Also used : Project(com.epam.ta.reportportal.entity.project.Project) PersonalProjectService(com.epam.ta.reportportal.util.PersonalProjectService) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ErrorType(com.epam.ta.reportportal.ws.model.ErrorType) StringUtils(org.apache.commons.lang3.StringUtils) Strings(com.google.common.base.Strings) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) UserRole(com.epam.ta.reportportal.entity.user.UserRole) ZoneOffset(java.time.ZoneOffset) UserType(com.epam.ta.reportportal.entity.user.UserType) UserSynchronizationException(com.epam.reportportal.auth.oauth.UserSynchronizationException) Resource(org.springframework.core.io.Resource) BinaryData(com.epam.ta.reportportal.entity.attachment.BinaryData) AbstractUserReplicator(com.epam.reportportal.auth.integration.AbstractUserReplicator) ContentTypeResolver(com.epam.reportportal.commons.ContentTypeResolver) BusinessRule(com.epam.ta.reportportal.commons.validation.BusinessRule) Optional.ofNullable(java.util.Optional.ofNullable) User(com.epam.ta.reportportal.entity.user.User) UserRepository(com.epam.ta.reportportal.dao.UserRepository) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) Objects(java.util.Objects) Metadata(com.epam.ta.reportportal.entity.Metadata) Component(org.springframework.stereotype.Component) EntityUtils.normalizeId(com.epam.ta.reportportal.commons.EntityUtils.normalizeId) ProjectRepository(com.epam.ta.reportportal.dao.ProjectRepository) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) UserBinaryDataService(com.epam.ta.reportportal.binary.UserBinaryDataService) Transactional(org.springframework.transaction.annotation.Transactional) InputStream(java.io.InputStream) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) User(com.epam.ta.reportportal.entity.user.User) UserSynchronizationException(com.epam.reportportal.auth.oauth.UserSynchronizationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ReportPortalUser

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();
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ReportPortalUser (com.epam.ta.reportportal.commons.ReportPortalUser)3 Optional.ofNullable (java.util.Optional.ofNullable)2 Transactional (org.springframework.transaction.annotation.Transactional)2 AbstractUserReplicator (com.epam.reportportal.auth.integration.AbstractUserReplicator)1 UPSTREAM_TOKEN (com.epam.reportportal.auth.integration.github.ExternalOauth2TokenConverter.UPSTREAM_TOKEN)1 UserSynchronizationException (com.epam.reportportal.auth.oauth.UserSynchronizationException)1 ContentTypeResolver (com.epam.reportportal.commons.ContentTypeResolver)1 UserBinaryDataService (com.epam.ta.reportportal.binary.UserBinaryDataService)1 EntityUtils.normalizeId (com.epam.ta.reportportal.commons.EntityUtils.normalizeId)1 BusinessRule (com.epam.ta.reportportal.commons.validation.BusinessRule)1 ProjectRepository (com.epam.ta.reportportal.dao.ProjectRepository)1 UserRepository (com.epam.ta.reportportal.dao.UserRepository)1 Metadata (com.epam.ta.reportportal.entity.Metadata)1 BinaryData (com.epam.ta.reportportal.entity.attachment.BinaryData)1 Project (com.epam.ta.reportportal.entity.project.Project)1 User (com.epam.ta.reportportal.entity.user.User)1 UserRole (com.epam.ta.reportportal.entity.user.UserRole)1 UserType (com.epam.ta.reportportal.entity.user.UserType)1 PersonalProjectService (com.epam.ta.reportportal.util.PersonalProjectService)1 ErrorType (com.epam.ta.reportportal.ws.model.ErrorType)1