Search in sources :

Example 1 with ShaPasswordEncoder

use of org.springframework.security.authentication.encoding.ShaPasswordEncoder in project spring-security by spring-projects.

the class AuthenticationProviderBeanDefinitionParserTests method providerWithSha256PasswordEncoderIsSupported.

@Test
public void providerWithSha256PasswordEncoderIsSupported() throws Exception {
    setContext(" <authentication-provider>" + "        <password-encoder hash='sha-256'/>" + "        <user-service>" + "            <user name='bob' password='notused' authorities='ROLE_A' />" + "        </user-service>" + "    </authentication-provider>");
    ShaPasswordEncoder encoder = (ShaPasswordEncoder) FieldUtils.getFieldValue(getProvider(), "passwordEncoder");
    assertThat(encoder.getAlgorithm()).isEqualTo("SHA-256");
}
Also used : ShaPasswordEncoder(org.springframework.security.authentication.encoding.ShaPasswordEncoder) Test(org.junit.Test)

Example 2 with ShaPasswordEncoder

use of org.springframework.security.authentication.encoding.ShaPasswordEncoder in project OpenClinica by OpenClinica.

the class EnketoAPI method getEditURL.

public EnketoURLResponse getEditURL(FormLayout formLayout, String flavor, String instance, String ecid, String redirect, boolean markComplete, String studyOid, List<FormLayoutMedia> mediaList, String goTo) {
    String crfOid = formLayout.getOcOid() + flavor;
    if (enketoURL == null)
        return null;
    try {
        // Build instanceId to cache populated instance at Enketo with
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        String hashString = ecid + "." + String.valueOf(cal.getTimeInMillis());
        ShaPasswordEncoder encoder = new ShaPasswordEncoder(256);
        String instanceId = encoder.encodePassword(hashString, null);
        URL eURL = new URL(enketoURL + "/api/v2/instance/fieldsubmission/iframe");
        // URL eURL = new URL(enketoURL + "/api/v2/instance/iframe");
        String userPasswdCombo = new String(Base64.encodeBase64((token + ":").getBytes()));
        InstanceAttachment attachment = new InstanceAttachment();
        for (FormLayoutMedia media : mediaList) {
            String fileName = media.getName();
            String baseUrl = CoreResources.getField("sysURL.base") + "rest2/openrosa/" + studyOid;
            String downLoadUrl = baseUrl + "/downloadMedia?formLayoutMediaId=" + media.getFormLayoutMediaId();
            attachment.setAdditionalProperty(fileName, downLoadUrl);
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization", "Basic " + userPasswdCombo);
        headers.add("Accept-Charset", "UTF-8");
        EnketoEditURLRequest body = new EnketoEditURLRequest(ocURL, crfOid, instanceId, redirect, instance, String.valueOf(markComplete), attachment, goTo);
        HttpEntity<EnketoEditURLRequest> request = new HttpEntity<EnketoEditURLRequest>(body, headers);
        RestTemplate rest = new RestTemplate();
        ResponseEntity<EnketoURLResponse> response = rest.postForEntity(eURL.toString(), request, EnketoURLResponse.class);
        if (response != null)
            return response.getBody();
        else
            return null;
    } catch (Exception e) {
        logger.error(e.getMessage());
        logger.error(ExceptionUtils.getStackTrace(e));
    }
    return null;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ShaPasswordEncoder(org.springframework.security.authentication.encoding.ShaPasswordEncoder) HttpEntity(org.springframework.http.HttpEntity) Calendar(java.util.Calendar) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) Date(java.util.Date) URL(java.net.URL) RestTemplate(org.springframework.web.client.RestTemplate)

Example 3 with ShaPasswordEncoder

use of org.springframework.security.authentication.encoding.ShaPasswordEncoder in project spring-security by spring-projects.

the class ShaPasswordEncoderTests method test256.

@Test
public void test256() throws Exception {
    ShaPasswordEncoder pe = new ShaPasswordEncoder(256);
    String encoded = pe.encodePassword("abc123", null);
    assertThat(encoded).isEqualTo("6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090");
    String encodedWithSalt = pe.encodePassword("abc123", "THIS_IS_A_SALT");
    assertThat(encodedWithSalt).isEqualTo("4b79b7de23eb23b78cc5ede227d532b8a51f89b2ec166f808af76b0dbedc47d7");
}
Also used : ShaPasswordEncoder(org.springframework.security.authentication.encoding.ShaPasswordEncoder) Test(org.junit.Test)

Example 4 with ShaPasswordEncoder

use of org.springframework.security.authentication.encoding.ShaPasswordEncoder in project spring-security by spring-projects.

the class DaoAuthenticationProviderTests method testGettersSetters.

@Test
public void testGettersSetters() {
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setPasswordEncoder(new ShaPasswordEncoder());
    assertThat(provider.getPasswordEncoder().getClass()).isEqualTo(ShaPasswordEncoder.class);
    provider.setSaltSource(new SystemWideSaltSource());
    assertThat(provider.getSaltSource().getClass()).isEqualTo(SystemWideSaltSource.class);
    provider.setUserCache(new EhCacheBasedUserCache());
    assertThat(provider.getUserCache().getClass()).isEqualTo(EhCacheBasedUserCache.class);
    assertThat(provider.isForcePrincipalAsString()).isFalse();
    provider.setForcePrincipalAsString(true);
    assertThat(provider.isForcePrincipalAsString()).isTrue();
}
Also used : EhCacheBasedUserCache(org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache) ShaPasswordEncoder(org.springframework.security.authentication.encoding.ShaPasswordEncoder) Test(org.junit.Test)

Example 5 with ShaPasswordEncoder

use of org.springframework.security.authentication.encoding.ShaPasswordEncoder in project spring-security by spring-projects.

the class ShaPasswordEncoderTests method testBasicFunctionality.

// ~ Methods
// ========================================================================================================
@Test
public void testBasicFunctionality() {
    ShaPasswordEncoder pe = new ShaPasswordEncoder();
    String raw = "abc123";
    String badRaw = "abc321";
    String salt = "THIS_IS_A_SALT";
    String encoded = pe.encodePassword(raw, salt);
    assertThat(pe.isPasswordValid(encoded, raw, salt)).isTrue();
    assertThat(pe.isPasswordValid(encoded, badRaw, salt)).isFalse();
    assertThat(encoded).isEqualTo("b2f50ffcbd3407fe9415c062d55f54731f340d32");
}
Also used : ShaPasswordEncoder(org.springframework.security.authentication.encoding.ShaPasswordEncoder) Test(org.junit.Test)

Aggregations

ShaPasswordEncoder (org.springframework.security.authentication.encoding.ShaPasswordEncoder)9 Test (org.junit.Test)5 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Student (ba.isss.models.Student)1 URL (java.net.URL)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 FormLayoutMedia (org.akaza.openclinica.domain.datamap.FormLayoutMedia)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 EhCacheBasedUserCache (org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache)1 RestTemplate (org.springframework.web.client.RestTemplate)1