Search in sources :

Example 11 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project cas by apereo.

the class X509CredentialFactoryTests method createDefaultCredential.

@Test
public void createDefaultCredential() {
    final MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
    requestBody.add("username", "name");
    requestBody.add("password", "passwd");
    final Credential cred = factory.fromRequestBody(requestBody);
    assertTrue(cred instanceof UsernamePasswordCredential);
}
Also used : X509CertificateCredential(org.apereo.cas.adaptors.x509.authentication.principal.X509CertificateCredential) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) Credential(org.apereo.cas.authentication.Credential) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) Test(org.junit.Test)

Example 12 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project cas by apereo.

the class X509CredentialFactoryTests method createX509Credential.

@Test
public void createX509Credential() throws IOException {
    final MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
    final Scanner scan = new Scanner(new ClassPathResource("ldap-crl.crt").getFile());
    final String certStr = scan.useDelimiter("\\Z").next();
    scan.close();
    requestBody.add("cert", certStr);
    final Credential cred = factory.fromRequestBody(requestBody);
    assertTrue(cred instanceof X509CertificateCredential);
}
Also used : Scanner(java.util.Scanner) X509CertificateCredential(org.apereo.cas.adaptors.x509.authentication.principal.X509CertificateCredential) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) Credential(org.apereo.cas.authentication.Credential) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) X509CertificateCredential(org.apereo.cas.adaptors.x509.authentication.principal.X509CertificateCredential) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 13 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project cas by apereo.

the class OAuth20UserProfileControllerController method handleRequestInternal.

/**
     * Handle request internal response entity.
     *
     * @param request  the request
     * @param response the response
     * @return the response entity
     * @throws Exception the exception
     */
@GetMapping(path = OAuthConstants.BASE_OAUTH20_URL + '/' + OAuthConstants.PROFILE_URL, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    String accessToken = request.getParameter(OAuthConstants.ACCESS_TOKEN);
    if (StringUtils.isBlank(accessToken)) {
        final String authHeader = request.getHeader(HttpConstants.AUTHORIZATION_HEADER);
        if (StringUtils.isNotBlank(authHeader) && authHeader.toLowerCase().startsWith(OAuthConstants.BEARER_TOKEN.toLowerCase() + ' ')) {
            accessToken = authHeader.substring(OAuthConstants.BEARER_TOKEN.length() + 1);
        }
    }
    LOGGER.debug("[{}]: [{}]", OAuthConstants.ACCESS_TOKEN, accessToken);
    if (StringUtils.isBlank(accessToken)) {
        LOGGER.error("Missing [{}]", OAuthConstants.ACCESS_TOKEN);
        final LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>(1);
        map.add(OAuthConstants.ERROR, OAuthConstants.MISSING_ACCESS_TOKEN);
        final String value = OAuthUtils.jsonify(map);
        return new ResponseEntity<>(value, HttpStatus.UNAUTHORIZED);
    }
    final AccessToken accessTokenTicket = getTicketRegistry().getTicket(accessToken, AccessToken.class);
    if (accessTokenTicket == null || accessTokenTicket.isExpired()) {
        LOGGER.error("Expired access token: [{}]", OAuthConstants.ACCESS_TOKEN);
        final LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>(1);
        map.add(OAuthConstants.ERROR, OAuthConstants.EXPIRED_ACCESS_TOKEN);
        final String value = OAuthUtils.jsonify(map);
        return new ResponseEntity<>(value, HttpStatus.UNAUTHORIZED);
    }
    final Map<String, Object> map = writeOutProfileResponse(accessTokenTicket.getAuthentication(), accessTokenTicket.getAuthentication().getPrincipal());
    final String value = OAuthUtils.jsonify(map);
    LOGGER.debug("Final user profile is [{}]", value);
    return new ResponseEntity<>(value, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) AccessToken(org.apereo.cas.ticket.accesstoken.AccessToken) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 14 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project spring-security-oauth by spring-projects.

the class ClientCredentialsProviderTests method testHardCodedAuthenticationWrongClient.

@Test
public void testHardCodedAuthenticationWrongClient() {
    RestTemplate restTemplate = new RestTemplate();
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("grant_type", "client_credentials");
    params.add("client_id", "my-trusted-client");
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    RequestEntity<MultiValueMap<String, String>> req = new RequestEntity<MultiValueMap<String, String>>(params, headers, HttpMethod.POST, tokenUri);
    try {
        restTemplate.exchange(req, Map.class);
        fail("Expected HTTP 401");
    } catch (HttpStatusCodeException e) {
        assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) RequestEntity(org.springframework.http.RequestEntity) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 15 with LinkedMultiValueMap

use of org.springframework.util.LinkedMultiValueMap in project spring-security-oauth by spring-projects.

the class CustomProviderTests method invalidGrant.

@Test
public void invalidGrant() throws Exception {
    LinkedMultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("grant_type", "foo");
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Basic " + new String(Base64.encode(("my-trusted-client:").getBytes())));
    @SuppressWarnings("rawtypes") ResponseEntity<Map> response = http.postForMap("/oauth/token", headers, form);
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Aggregations

LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)413 Test (org.junit.Test)153 HttpHeaders (org.springframework.http.HttpHeaders)126 MultiValueMap (org.springframework.util.MultiValueMap)94 Test (org.junit.jupiter.api.Test)88 HttpEntity (org.springframework.http.HttpEntity)60 List (java.util.List)42 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)40 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)37 HashMap (java.util.HashMap)36 MediaType (org.springframework.http.MediaType)34 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)33 URI (java.net.URI)33 Map (java.util.Map)31 AbstractReadWriteDtoControllerRestTest (eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest)30 ArrayList (java.util.ArrayList)27 UUID (java.util.UUID)27 lombok.val (lombok.val)27 IOException (java.io.IOException)26 Assert (org.junit.Assert)25