/** * Called when the dialog has been validated by the user, when the OK button has been pressed or when enter has * been pressed in a text field. */ private void setCredentialMapping() { if(guestRadioButton!=null && guestRadioButton.isSelected()) { guestCredentialsSelected = true; selectedCredentialsMapping = new CredentialsMapping(fileURL.getGuestCredentials(), fileURL, false); } else { Credentials enteredCredentials = new Credentials(loginField.getText(), new String(passwordField.getPassword())); guestCredentialsSelected = false; boolean isPersistent = saveCredentialsCheckBox.isSelected(); selectedCredentialsMapping = new CredentialsMapping(enteredCredentials, fileURL, isPersistent); // Look for an existing matching CredentialsMapping instance to re-use the realm which may contain // connection properties. int nbCredentials = credentialsMappings.length; CredentialsMapping cm; for(int i=0; i<nbCredentials; i++) { cm = credentialsMappings[i]; if(cm.getCredentials().equals(enteredCredentials, true)) { // Comparison must be password-sensitive // Create a new CredentialsMapping instance in case the 'isPersistent' flag has changed. // (original credentials may have originally been added as 'volatile' and then made persistent by // ticking the checkbox, or vice-versa) selectedCredentialsMapping = new CredentialsMapping(cm.getCredentials(), cm.getRealm(), isPersistent); break; } } } }
/** * Looks for the best set of credentials matching the given location (if any) and use it to authenticate the * URL by calling {@link #authenticate(com.mucommander.commons.file.FileURL, CredentialsMapping)}. * Returns <code>true</code> if a set of credentials was found and used to authenticate the URL, <code>false</code> * otherwise. * * <p>Credentials are first looked for using {@link #getMatchingCredentials(com.mucommander.commons.file.FileURL)}. * If there is no match, guest credentials are retrieved from the URL and used (if any).</p> * * @param location the FileURL to authenticate */ private static void authenticateImplicit(FileURL location) { LOGGER.trace("called, fileURL="+ location +" containsCredentials="+ location.containsCredentials()); CredentialsMapping creds[] = getMatchingCredentials(location); if(creds.length>0) { authenticate(location, creds[0]); } else { Credentials guestCredentials = location.getGuestCredentials(); if(guestCredentials!=null) { authenticate(location, new CredentialsMapping(guestCredentials, location.getRealm(), false)); } } }
/** * Ensures that the values returned by {@link FileURL#getGuestCredentials()} and {@link SchemeHandler#getGuestCredentials()} * match the expected one returned by {@link #getGuestCredentials()}. * * @throws MalformedURLException should not happen */ @Test public void testGuestCredentials() throws MalformedURLException { FileURL url = getRootURL(); Credentials expectedGuestCredentials = getGuestCredentials(); // Assert that the guest credentials values returned by the FileURL and its handler match the expected one // and are consistent if(expectedGuestCredentials == null) { assert url.getGuestCredentials() == null; assert url.getHandler().getGuestCredentials() == null; } else { assert expectedGuestCredentials.equals(url.getGuestCredentials()); assert expectedGuestCredentials.equals(url.getHandler().getGuestCredentials()); } }
Credentials guestCredentials = fileURL.getGuestCredentials();
/** * Ensures that the values returned by {@link FileURL#getAuthenticationType()} ()} and {@link SchemeHandler#getAuthenticationType()} * match the expected value returned by {@link #getAuthenticationType()}, and that the value is one of the constants * defined in {@link AuthenticationType}. * If the authentication type is {@link AuthenticationType#NO_AUTHENTICATION}, verifies that * {@link #getGuestCredentials()} returns <code>null</code>. * * @throws MalformedURLException should not happen */ @Test public void testAuthenticationType() throws MalformedURLException { FileURL url = getRootURL(); AuthenticationType expectedAuthenticationType = getAuthenticationType(); assert expectedAuthenticationType.equals(url.getAuthenticationType()); assert expectedAuthenticationType.equals(url.getHandler().getAuthenticationType()); assert expectedAuthenticationType==AuthenticationType.NO_AUTHENTICATION || expectedAuthenticationType==AuthenticationType.AUTHENTICATION_REQUIRED || expectedAuthenticationType==AuthenticationType.AUTHENTICATION_OPTIONAL; if(expectedAuthenticationType== AuthenticationType.NO_AUTHENTICATION) assert url.getGuestCredentials() == null; }