コード例 #1
0
  @Test
  public void loginTestForOauthClient() throws MalformedURLException {
    String token = "12345678";
    RestTemplate restTemplate = mock(RestTemplate.class);
    ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
    OAuth2AccessToken oauthToken = mock(OAuth2AccessToken.class);
    when(restTemplate.getRequestFactory()).thenReturn(requestFactory);
    when(restTemplate.execute(
            anyString(),
            any(HttpMethod.class),
            any(RequestCallback.class),
            any(ResponseExtractor.class),
            any(Map.class)))
        .thenReturn(oauthToken);
    when(oauthToken.getValue()).thenReturn(token);
    when(oauthToken.getTokenType()).thenReturn("bearer");

    // Run Test
    OauthClient oauthClient = new OauthClient(new URL("http://uaa.cloud.me"), restTemplate);
    OAuth2AccessToken ouathToken = oauthClient.getToken("*****@*****.**", "passwd");
    String loginToken = oauthToken.getValue();
    assertThat(loginToken, is(token));
  }
コード例 #2
0
 public String login() {
   if (cloudCredentials.getEmail() == null) {
     Assert.hasLength(cloudCredentials.getToken(), "No authentication details provided");
     token = cloudCredentials.getToken();
     return token;
   }
   Assert.hasLength(cloudCredentials.getEmail(), "Email cannot be null or empty");
   Assert.hasLength(cloudCredentials.getPassword(), "Password cannot be null or empty");
   if (oauthClient != null) {
     OAuth2AccessToken token =
         oauthClient.getToken(cloudCredentials.getEmail(), cloudCredentials.getPassword());
     this.token = token.getTokenType() + " " + token.getValue();
     return this.token;
   } else {
     Map<String, String> payload = new HashMap<String, String>();
     payload.put("password", cloudCredentials.getPassword());
     Map<String, String> response =
         getRestTemplate()
             .postForObject(
                 getUrl("users/{id}/tokens"), payload, Map.class, cloudCredentials.getEmail());
     token = response.get("token");
     return token;
   }
 }