Search in sources :

Example 1 with OAuthRegistrationResource

use of com.epam.ta.reportportal.ws.model.settings.OAuthRegistrationResource 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 OAuthRegistrationResource

use of com.epam.ta.reportportal.ws.model.settings.OAuthRegistrationResource in project service-authorization by reportportal.

the class CreateAuthIntegrationHandlerImpl method createOrUpdateOauthSettings.

@Override
public OAuthRegistrationResource createOrUpdateOauthSettings(String oauthProviderId, OAuthRegistrationResource clientRegistrationResource) {
    OAuthRegistration oAuthRegistration = OAuthProviderFactory.fillOAuthRegistration(oauthProviderId, clientRegistrationResource);
    OAuthRegistration updatedOauthRegistration = clientRegistrationRepository.findOAuthRegistrationById(oauthProviderId).map(existingRegistration -> {
        clientRegistrationRepository.deleteById(existingRegistration.getId());
        oAuthRegistration.setId(existingRegistration.getId());
        return oAuthRegistration;
    }).orElse(oAuthRegistration);
    return OAuthRegistrationConverters.TO_RESOURCE.apply(clientRegistrationRepository.save(updatedOauthRegistration));
}
Also used : OAuthRegistration(com.epam.ta.reportportal.entity.oauth.OAuthRegistration) AuthIntegrationType(com.epam.reportportal.auth.integration.AuthIntegrationType) OAuthRegistrationConverters(com.epam.reportportal.auth.integration.converter.OAuthRegistrationConverters) UpdateAuthRQ(com.epam.ta.reportportal.ws.model.integration.auth.UpdateAuthRQ) Integration(com.epam.ta.reportportal.entity.integration.Integration) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) Autowired(org.springframework.beans.factory.annotation.Autowired) IntegrationTypeRepository(com.epam.ta.reportportal.dao.IntegrationTypeRepository) AbstractAuthResource(com.epam.ta.reportportal.ws.model.integration.auth.AbstractAuthResource) AuthIntegrationStrategy(com.epam.reportportal.auth.integration.handler.impl.strategy.AuthIntegrationStrategy) ErrorType(com.epam.ta.reportportal.ws.model.ErrorType) AuthIntegrationStrategyProvider(com.epam.reportportal.auth.integration.provider.AuthIntegrationStrategyProvider) CreateAuthIntegrationHandler(com.epam.reportportal.auth.integration.handler.CreateAuthIntegrationHandler) MutableClientRegistrationRepository(com.epam.reportportal.auth.store.MutableClientRegistrationRepository) IntegrationType(com.epam.ta.reportportal.entity.integration.IntegrationType) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) OAuthProviderFactory(com.epam.reportportal.auth.oauth.OAuthProviderFactory) Service(org.springframework.stereotype.Service) OAuthRegistrationResource(com.epam.ta.reportportal.ws.model.settings.OAuthRegistrationResource) OAuthRegistration(com.epam.ta.reportportal.entity.oauth.OAuthRegistration)

Aggregations

ReportPortalUser (com.epam.ta.reportportal.commons.ReportPortalUser)2 OAuthRegistrationResource (com.epam.ta.reportportal.ws.model.settings.OAuthRegistrationResource)2 AuthIntegrationType (com.epam.reportportal.auth.integration.AuthIntegrationType)1 OAuthRegistrationConverters (com.epam.reportportal.auth.integration.converter.OAuthRegistrationConverters)1 UPSTREAM_TOKEN (com.epam.reportportal.auth.integration.github.ExternalOauth2TokenConverter.UPSTREAM_TOKEN)1 CreateAuthIntegrationHandler (com.epam.reportportal.auth.integration.handler.CreateAuthIntegrationHandler)1 AuthIntegrationStrategy (com.epam.reportportal.auth.integration.handler.impl.strategy.AuthIntegrationStrategy)1 AuthIntegrationStrategyProvider (com.epam.reportportal.auth.integration.provider.AuthIntegrationStrategyProvider)1 OAuthProviderFactory (com.epam.reportportal.auth.oauth.OAuthProviderFactory)1 MutableClientRegistrationRepository (com.epam.reportportal.auth.store.MutableClientRegistrationRepository)1 IntegrationTypeRepository (com.epam.ta.reportportal.dao.IntegrationTypeRepository)1 Integration (com.epam.ta.reportportal.entity.integration.Integration)1 IntegrationType (com.epam.ta.reportportal.entity.integration.IntegrationType)1 OAuthRegistration (com.epam.ta.reportportal.entity.oauth.OAuthRegistration)1 ReportPortalException (com.epam.ta.reportportal.exception.ReportPortalException)1 ErrorType (com.epam.ta.reportportal.ws.model.ErrorType)1 AbstractAuthResource (com.epam.ta.reportportal.ws.model.integration.auth.AbstractAuthResource)1 UpdateAuthRQ (com.epam.ta.reportportal.ws.model.integration.auth.UpdateAuthRQ)1 Splitter (com.google.common.base.Splitter)1 Serializable (java.io.Serializable)1