Skip to content

Commit

Permalink
Merge pull request #64 from roikku/master
Browse files Browse the repository at this point in the history
Added support for External authentication method
  • Loading branch information
roikku committed Jun 2, 2014
2 parents e1a5710 + 43eff50 commit 0f960ec
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 8 deletions.
Expand Up @@ -174,9 +174,14 @@ public class AccountConfig {
* password must be passed. Ideally, tenant ID and/or name are passed as well. JOSS can auto-
* discover the tenant if none is passed and if it can be resolved (one tenant for user).
* </li>
* <li>
* <b>EXTERNAL</b>; an implementation of the interface AccessProvider must be provided.
* </li>
* </ul>
*/
private AuthenticationMethod authenticationMethod = KEYSTONE;

private AuthenticationMethod.AccessProvider accessProvider = null ;

public void setTenantName(String tenantName) {
this.tenantName = tenantName;
Expand Down Expand Up @@ -345,10 +350,18 @@ public void setDisableSslValidation(boolean disableSslValidation) {
public AuthenticationMethod getAuthenticationMethod() {
return authenticationMethod;
}

public void setAuthenticationMethod(AuthenticationMethod authenticationMethod) {
this.authenticationMethod = authenticationMethod;
}

public AuthenticationMethod.AccessProvider getAccessProvider () {
return accessProvider ;
}

public void setAccessProvider (AuthenticationMethod.AccessProvider accessProvider) {
this.accessProvider = accessProvider ;
}

public void setAuthenticationMethod(String authenticationMethod) {
setAuthenticationMethod(AuthenticationMethod.valueOf(authenticationMethod));
Expand Down
@@ -1,5 +1,7 @@
package org.javaswift.joss.client.factory;

import org.javaswift.joss.model.Access;

/**
* <ul>
* <li>
Expand All @@ -14,10 +16,19 @@
* <a href="https://github.com/openstack/swift/blob/master/swift/common/middleware/tempauth.py">TEMPAUTH</a>:
* if you have TempAuth enabled, you will use this option. Looks very similar to Basic authentication
* </li>
* <li>
* EXTERNAL:
* an implementation of the interface AccessProvider must be provided
* </li>
* </ul>
*/
public enum AuthenticationMethod {
BASIC,
KEYSTONE,
TEMPAUTH
TEMPAUTH,
EXTERNAL;

public static interface AccessProvider {
public Access authenticate () ;
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/javaswift/joss/client/impl/ClientImpl.java
Expand Up @@ -88,7 +88,8 @@ protected AccountImpl createAccount() {
accountConfig.getTenantName(),
accountConfig.getTenantId(),
accountConfig.getUsername(),
accountConfig.getPassword());
accountConfig.getPassword(),
accountConfig.getAccessProvider());
LOG.info(
"JOSS / Attempting authentication with tenant name: " + accountConfig.getTenantName()+
", tenant ID: "+accountConfig.getTenantId()+
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/javaswift/joss/client/mock/ClientMock.java
Expand Up @@ -66,7 +66,8 @@ protected AccountMock createAccount() {
accountConfig.getTenantName(),
accountConfig.getTenantId(),
accountConfig.getUsername(),
accountConfig.getPassword()).call();
accountConfig.getPassword(),
accountConfig.getAccessProvider()).call();
}
return new AccountMock(swift);
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import org.apache.http.client.HttpClient;
import org.javaswift.joss.client.factory.AuthenticationMethod;
import org.javaswift.joss.command.impl.identity.BasicAuthenticationCommandImpl;
import org.javaswift.joss.command.impl.identity.ExternalAuthenticationCommandImpl;
import org.javaswift.joss.command.impl.identity.KeystoneAuthenticationCommandImpl;
import org.javaswift.joss.command.impl.identity.TempAuthAuthenticationCommandImpl;
import org.javaswift.joss.command.shared.factory.AuthenticationCommandFactory;
Expand All @@ -15,11 +16,13 @@ public class AuthenticationCommandFactoryImpl implements AuthenticationCommandFa
@Override
public AuthenticationCommand createAuthenticationCommand(HttpClient httpClient, AuthenticationMethod authenticationMethod,
String url, String tenantName, String tenantId,
String username, String password) {
String username, String password, AuthenticationMethod.AccessProvider accessProvier) {
if (authenticationMethod == BASIC) {
return new BasicAuthenticationCommandImpl(httpClient, url, username, password, tenantName);
} else if (authenticationMethod == TEMPAUTH) {
return new TempAuthAuthenticationCommandImpl(httpClient, url, username, password, tenantName);
} else if (authenticationMethod == EXTERNAL) {
return new ExternalAuthenticationCommandImpl (httpClient, url, accessProvier) ;
} else { // KEYSTONE
return new KeystoneAuthenticationCommandImpl(httpClient, url, tenantName, tenantId, username, password);
}
Expand Down
@@ -0,0 +1,23 @@
package org.javaswift.joss.command.impl.identity;

import org.apache.http.client.HttpClient;
import org.javaswift.joss.client.factory.AuthenticationMethod;
import org.javaswift.joss.model.Access;

public class ExternalAuthenticationCommandImpl extends AbstractSimpleAuthenticationCommandImpl {

private final AuthenticationMethod.AccessProvider accessProvier ;

public ExternalAuthenticationCommandImpl(HttpClient httpClient, String url, AuthenticationMethod.AccessProvider accessProvier) {
super(httpClient, url);
if (accessProvier == null)
throw new NullPointerException () ;
this.accessProvier = accessProvier ;
}

@Override
public Access call() {
return accessProvier.authenticate() ;
}
}

Expand Up @@ -18,7 +18,7 @@ public AuthenticationCommandFactoryMock(Swift swift) {
@Override
public AuthenticationCommand createAuthenticationCommand(HttpClient httpClient, AuthenticationMethod authenticationMethod,
String url, String tenantName, String tenantId,
String username, String password) {
String username, String password, AuthenticationMethod.AccessProvider accessProvier) {
return new AuthenticationCommandMock(swift, url, tenantName, tenantId, username, password);
}

Expand Down
Expand Up @@ -8,6 +8,6 @@ public interface AuthenticationCommandFactory {

AuthenticationCommand createAuthenticationCommand(HttpClient httpClient, AuthenticationMethod authenticationMethod,
String url, String tenantName, String tenantId,
String username, String password);
String username, String password, AuthenticationMethod.AccessProvider accessProvier);

}
Expand Up @@ -2,13 +2,16 @@

import org.javaswift.joss.client.factory.AuthenticationMethod;
import org.javaswift.joss.command.impl.identity.BasicAuthenticationCommandImpl;
import org.javaswift.joss.command.impl.identity.ExternalAuthenticationCommandImpl;
import org.javaswift.joss.command.impl.identity.KeystoneAuthenticationCommandImpl;
import org.javaswift.joss.command.impl.identity.TempAuthAuthenticationCommandImpl;
import org.javaswift.joss.model.Access;
import org.junit.Test;

import static org.javaswift.joss.client.factory.AuthenticationMethod.BASIC;
import static org.javaswift.joss.client.factory.AuthenticationMethod.KEYSTONE;
import static org.javaswift.joss.client.factory.AuthenticationMethod.TEMPAUTH;
import static org.javaswift.joss.client.factory.AuthenticationMethod.EXTERNAL;
import static org.junit.Assert.assertEquals;

public class AuthenticationCommandFactoryImplTest {
Expand All @@ -24,7 +27,17 @@ public void variousAuthenticationMechanisms() {

protected void assertAuthenticationMechanism(AuthenticationCommandFactoryImpl factory,
AuthenticationMethod authenticationMethod, Class instance) {
assertEquals(instance, factory.createAuthenticationCommand(null, authenticationMethod, "http://nowhere.com", null, null, null, null).getClass());
assertEquals(instance, factory.createAuthenticationCommand(null, authenticationMethod, "http://nowhere.com", null, null, null, null, null).getClass());
}

@Test
public void externalAuthenticationMechanisms() {
AuthenticationCommandFactoryImpl factory = new AuthenticationCommandFactoryImpl();
assertEquals(ExternalAuthenticationCommandImpl.class, factory.createAuthenticationCommand(null, EXTERNAL, "http://nowhere.com", null, null, null, null, new AuthenticationMethod.AccessProvider() {
@Override
public Access authenticate() {
return null ;
}
}).getClass());
}
}

0 comments on commit 0f960ec

Please sign in to comment.