Skip to content

Commit

Permalink
XSTokenRequest accepts custom RestTemplate SAP#25
Browse files Browse the repository at this point in the history
  • Loading branch information
nenaraab committed Jan 7, 2019
1 parent fa595f4 commit e061c33
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 30 deletions.
8 changes: 7 additions & 1 deletion spring-xsuaa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@
<version>2.6</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Collection;
import java.util.Map;

import com.sap.xs2.security.container.XSTokenRequestImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -51,7 +52,6 @@ public class TokenImpl implements Token {
private final Log logger = LogFactory.getLog(getClass());
private String xsappname = null;
private Jwt jwt;
private RestTemplate restTemplate;

/**
* @param jwt
Expand Down Expand Up @@ -246,6 +246,8 @@ public String requestToken(XSTokenRequest tokenRequest) throws URISyntaxExceptio
Assert.notNull(tokenRequest, "tokenRequest argument is required");
Assert.isTrue(tokenRequest.isValid(), "tokenRequest is not valid");

RestTemplate restTemplate = tokenRequest instanceof XSTokenRequestImpl ? ((XSTokenRequestImpl) tokenRequest).getRestTemplate() : null;

XsuaaTokenExchanger tokenExchanger = new XsuaaTokenExchanger(restTemplate, this);
try {
return tokenExchanger.requestToken(tokenRequest);
Expand All @@ -265,10 +267,6 @@ public boolean hasClaim(String claim) {
return jwt.containsClaim(claim);
}


public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

/**
* For custom access to the claims of the authentication token.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
/**
* Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved.
* This file is licensed under the Apache Software License,
* v. 2 except as noted otherwise in the LICENSE file
* https://github.com/SAP/cloud-security-xsuaa-integration/blob/master/LICENSE
*/
package com.sap.cloud.security.xsuaa.token;

import com.sap.xs2.security.container.UserInfoException;
import com.sap.xs2.security.container.XSTokenRequestImpl;
import com.sap.xsa.security.container.XSTokenRequest;
import net.minidev.json.JSONObject;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand All @@ -12,13 +22,10 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sap.xs2.security.container.UserInfoException;
import com.sap.xsa.security.container.XSTokenRequest;

import net.minidev.json.JSONObject;

public class XsuaaTokenExchanger {
Token token;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved.
* This file is licensed under the Apache Software License,
* v. 2 except as noted otherwise in the LICENSE file
* https://github.com/SAP/cloud-security-xsuaa-integration/blob/master/LICENSE
*/
package com.sap.xs2.security.container;

import java.net.URI;
Expand All @@ -9,6 +15,8 @@
import java.util.Objects;

import com.sap.xsa.security.container.XSTokenRequest;
import org.springframework.lang.Nullable;
import org.springframework.web.client.RestTemplate;

public class XSTokenRequestImpl implements XSTokenRequest {

Expand All @@ -22,6 +30,7 @@ public class XSTokenRequestImpl implements XSTokenRequest {
private int type;
private String clientId;
private String clientSecret;
private RestTemplate restTemplate;

private Map<String, String> additionalAuthorizationAttributes;

Expand Down Expand Up @@ -110,8 +119,7 @@ public Map<String, String> getAdditionalAuthorizationAttributes() {
* @return this mutable object
*/
public XSTokenRequest setAdditionalAuthorizationAttributes(Map<String, String> additionalAuthorizationAttributes) {
this.additionalAuthorizationAttributes = (additionalAuthorizationAttributes == null) ? null
: new HashMap<>(additionalAuthorizationAttributes);
this.additionalAuthorizationAttributes = (additionalAuthorizationAttributes == null) ? null : new HashMap<>(additionalAuthorizationAttributes);
return this;
}

Expand Down Expand Up @@ -169,4 +177,25 @@ private boolean hasAnyNullValues(List<Object> objects) {
}
return objects.stream().filter(o -> Objects.isNull(o)).count() > 0;
}

/**
* Allows to overwrite the default RestTemplate
*
* @param restTemplate
* the custom restTemplate
*/
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

/**
* Returns the custom RestTemplate
*
* @return the custom restTemplate or null
*/
@Nullable
public RestTemplate getRestTemplate() {
return restTemplate;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;

import java.net.URISyntaxException;
import java.net.URI;
import java.util.*;

import com.sap.xs2.security.container.XSTokenRequestImpl;
import com.sap.xsa.security.container.XSTokenRequest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.client.RestTemplate;

import com.nimbusds.jwt.JWTClaimsSet;
import org.springframework.web.client.RestTemplate;
import com.sap.xs2.security.container.XSTokenRequestImpl;
import com.sap.xsa.security.container.XSTokenRequest;

public class TokenImplTest {

Expand Down Expand Up @@ -210,25 +215,30 @@ public void getAppToken() throws Exception {
}

@Test
@Ignore
public void requestToken() throws Exception {
RestTemplate mockRestTemplate = new RestTemplate();
public void requestClientCredentialsToken() throws Exception {
// prepare response
Map<String, String> ccToken = new HashMap<>();
ccToken.put("access_token", "cc_token");

//TODO mock RestTemplate!!!
// mock rest call
// http://myuaa.com/oauth/token?grant_type=client_credentials&authorities=%7B%22az_attr%22:%7B%22a%22:%22b%22,%22c%22:%22d%22%7D%7D
RestTemplate mockRestTemplate = Mockito.mock(RestTemplate.class);
ResponseEntity<Map> response = new ResponseEntity<>(ccToken, HttpStatus.OK);
Mockito.when(mockRestTemplate.postForEntity(any(URI.class), any(HttpEntity.class), eq(Map.class))).thenReturn(response);

token = createToken(claimsSetBuilder);
//token.setRestTemplate(mockRestTemplate);

String mockServerUrl = "http://myuaa.com";
XSTokenRequestImpl tokenRequest = new XSTokenRequestImpl(mockServerUrl);
tokenRequest.setRestTemplate(mockRestTemplate);
tokenRequest.setClientId("c1").setClientSecret("s1").setType(XSTokenRequest.TYPE_CLIENT_CREDENTIALS_TOKEN);

Map<String, String> azMape = new HashMap<>();
azMape.put("a", "b");
azMape.put("c", "d");
tokenRequest.setAdditionalAuthorizationAttributes(azMape);

assertThat(token.requestToken(tokenRequest), startsWith("eyJhbGciOiJSUzI1NiIsInR5"));
assertThat(token.requestToken(tokenRequest), is("cc_token"));
}

private Token createToken(JWTClaimsSet.Builder claimsBuilder) throws Exception {
Expand Down

0 comments on commit e061c33

Please sign in to comment.