Search in sources :

Example 1 with UPSTREAM_TOKEN

use of com.epam.reportportal.auth.integration.github.ExternalOauth2TokenConverter.UPSTREAM_TOKEN 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)

Aggregations

UPSTREAM_TOKEN (com.epam.reportportal.auth.integration.github.ExternalOauth2TokenConverter.UPSTREAM_TOKEN)1 ReportPortalUser (com.epam.ta.reportportal.commons.ReportPortalUser)1 OAuthRegistrationResource (com.epam.ta.reportportal.ws.model.settings.OAuthRegistrationResource)1 Splitter (com.google.common.base.Splitter)1 Serializable (java.io.Serializable)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Map (java.util.Map)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Supplier (java.util.function.Supplier)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)1 InvalidTokenException (org.springframework.security.oauth2.common.exceptions.InvalidTokenException)1 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)1 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)1 ResourceServerTokenServices (org.springframework.security.oauth2.provider.token.ResourceServerTokenServices)1