Polish OAuth SSO
This commit is contained in:
@@ -1,13 +1,4 @@
|
||||
package sample;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
|
||||
package sample.secure.oauth2;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -28,8 +19,20 @@ import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import sample.secure.oauth2.SampleSecureOAuth2Application;
|
||||
import sample.secure.oauth2.Flight;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
|
||||
|
||||
/**
|
||||
* Series of automated integration tests to verify proper behavior of auto-configured,
|
||||
* OAuth2-secured system
|
||||
@@ -38,12 +41,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@SpringApplicationConfiguration(classes = SampleSecureOAuth2Application.class)
|
||||
@IntegrationTest("server.port:0")
|
||||
public class ApplicationTests {
|
||||
public class SampleSecureOAuth2ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
WebApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
FilterChainProxy filterChain;
|
||||
|
||||
@@ -53,85 +57,51 @@ public class ApplicationTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
this.mvc = webAppContextSetup(this.context).addFilters(this.filterChain).build();
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void everythingIsSecuredByDefault() throws Exception {
|
||||
|
||||
this.mvc.perform(get("/").//
|
||||
accept(MediaTypes.HAL_JSON)).// /
|
||||
andExpect(status().isUnauthorized()).//
|
||||
andDo(print());
|
||||
|
||||
this.mvc.perform(get("/flights").//
|
||||
accept(MediaTypes.HAL_JSON)).// /
|
||||
andExpect(status().isUnauthorized()).//
|
||||
andDo(print());
|
||||
|
||||
this.mvc.perform(get("/flights/1").//
|
||||
accept(MediaTypes.HAL_JSON)).// /
|
||||
andExpect(status().isUnauthorized()).//
|
||||
andDo(print());
|
||||
|
||||
this.mvc.perform(get("/alps").//
|
||||
accept(MediaTypes.HAL_JSON)).// /
|
||||
andExpect(status().isUnauthorized()).//
|
||||
andDo(print());
|
||||
this.mvc.perform(get("/").accept(MediaTypes.HAL_JSON))
|
||||
.andExpect(status().isUnauthorized()).andDo(print());
|
||||
this.mvc.perform(get("/flights").accept(MediaTypes.HAL_JSON))
|
||||
.andExpect(status().isUnauthorized()).andDo(print());
|
||||
this.mvc.perform(get("/flights/1").accept(MediaTypes.HAL_JSON))
|
||||
.andExpect(status().isUnauthorized()).andDo(print());
|
||||
this.mvc.perform(get("/alps").accept(MediaTypes.HAL_JSON))
|
||||
.andExpect(status().isUnauthorized()).andDo(print());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
// TODO: maybe show mixed basic + token auth on different resources?
|
||||
public void accessingRootUriPossibleWithUserAccount() throws Exception {
|
||||
|
||||
String header = "Basic " + new String(Base64.encode("greg:turnquist".getBytes()));
|
||||
this.mvc.perform(
|
||||
get("/").//
|
||||
accept(MediaTypes.HAL_JSON).//
|
||||
header("Authorization",
|
||||
"Basic "
|
||||
+ new String(Base64.encode("greg:turnquist"
|
||||
.getBytes()))))
|
||||
.//
|
||||
andExpect(header().string("Content-Type", MediaTypes.HAL_JSON.toString()))
|
||||
.//
|
||||
andExpect(status().isOk()).//
|
||||
andDo(print());
|
||||
get("/").accept(MediaTypes.HAL_JSON).header("Authorization", header))
|
||||
.andExpect(
|
||||
header().string("Content-Type", MediaTypes.HAL_JSON.toString()))
|
||||
.andExpect(status().isOk()).andDo(print());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useAppSecretsPlusUserAccountToGetBearerToken() throws Exception {
|
||||
|
||||
// @formatter:off
|
||||
String header = "Basic " + new String(Base64.encode("foo:bar".getBytes()));
|
||||
MvcResult result = this.mvc
|
||||
.perform(
|
||||
post("/oauth/token").
|
||||
header("Authorization",
|
||||
"Basic " + new String(Base64.encode("foo:bar".getBytes()))).
|
||||
param("grant_type", "password").
|
||||
param("scope", "read").
|
||||
param("username", "greg").
|
||||
param("password", "turnquist")).
|
||||
andExpect(status().isOk()).
|
||||
andDo(print()).
|
||||
andReturn();
|
||||
// @formatter:on
|
||||
|
||||
post("/oauth/token").header("Authorization", header)
|
||||
.param("grant_type", "password").param("scope", "read")
|
||||
.param("username", "greg").param("password", "turnquist"))
|
||||
.andExpect(status().isOk()).andDo(print()).andReturn();
|
||||
Object accessToken = this.objectMapper.readValue(
|
||||
result.getResponse().getContentAsString(), Map.class).get("access_token");
|
||||
|
||||
MvcResult flightsAction = this.mvc
|
||||
.perform(get("/flights/1").//
|
||||
accept(MediaTypes.HAL_JSON).//
|
||||
header("Authorization", "Bearer " + accessToken))
|
||||
.//
|
||||
andExpect(header().string("Content-Type", MediaTypes.HAL_JSON.toString()))
|
||||
.//
|
||||
andExpect(status().isOk()).//
|
||||
andDo(print()).//
|
||||
andReturn();
|
||||
.perform(
|
||||
get("/flights/1").accept(MediaTypes.HAL_JSON).header(
|
||||
"Authorization", "Bearer " + accessToken))
|
||||
.andExpect(
|
||||
header().string("Content-Type", MediaTypes.HAL_JSON.toString()))
|
||||
.andExpect(status().isOk()).andDo(print()).andReturn();
|
||||
|
||||
Flight flight = this.objectMapper.readValue(flightsAction.getResponse()
|
||||
.getContentAsString(), Flight.class);
|
||||
Reference in New Issue
Block a user