All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.trustedanalytics.cloud.uaa.UaaClient Maven / Gradle / Ivy

There is a newer version: 0.4.42
Show newest version
/**
 * Copyright (c) 2015 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.trustedanalytics.cloud.uaa;

import static java.util.stream.Collectors.joining;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import org.cloudfoundry.identity.uaa.rest.SearchResults;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestOperations;

import org.cloudfoundry.identity.uaa.scim.ScimUser;
import org.trustedanalytics.org.cloudfoundry.identity.uaa.scim.ScimUserFactory;

public class UaaClient implements UaaOperations {

    private final String uaaBaseUrl;

    private final RestOperations uaaRestTemplate;

    public UaaClient(RestOperations uaaRestTemplate, String uaaBaseUrl) {
        this.uaaBaseUrl = uaaBaseUrl;
        this.uaaRestTemplate = uaaRestTemplate;
    }

    @Override
    public ScimUser createUser(String username, String password) {
        return uaaRestTemplate
            .postForObject(uaaBaseUrl + "/Users", ScimUserFactory.newUser(username,
                password), ScimUser.class);
    }

    @Override
    public SearchResults getUsers() {
        ResponseEntity> result = uaaRestTemplate.exchange(
            uaaBaseUrl + "/Users",
            HttpMethod.GET,
            null,
            new ParameterizedTypeReference>() {
            });

        return result.getBody();
    }

    @Override
    public void deleteUser(UUID userGuid) {
        uaaRestTemplate.delete(uaaBaseUrl + "/Users/{id}", userGuid.toString());
    }
    
    @Override
    public Collection findUserNames(Collection users) {
        String filter = users.stream()
                .map(uuid -> "Id eq \"" + uuid + "\"")
                .collect(joining(" or "));
        
        String path = uaaBaseUrl + "/Users?attributes=id,userName&filter=" + filter;
        return uaaRestTemplate.getForObject(path, UserIdNameList.class).getUsers();
    }
    
    @Override
    public void changePassword(UUID guid, ChangePasswordRequest request) {
        uaaRestTemplate.put(uaaBaseUrl + "/Users/{id}/password", request, guid);
    }

    @Override
    public Optional findUserIdByName(String userName) {
        String query = "/Users?attributes=id,userName&filter=userName eq '{name}'";
        Map pathVars = ImmutableMap.of("name", userName);
        UserIdNameList result = uaaRestTemplate.getForObject(uaaBaseUrl + query, UserIdNameList.class, pathVars);
        return Optional.ofNullable(Iterables.getFirst(result.getUsers(), null));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy