Skip to content

Commit

Permalink
Nonfunctional code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Feb 25, 2014
1 parent 440a2bc commit 306b021
Show file tree
Hide file tree
Showing 41 changed files with 248 additions and 277 deletions.
Expand Up @@ -16,6 +16,8 @@

package org.fcrepo.auth.common;

import static org.fcrepo.auth.common.ServletContainerAuthenticationProvider.EVERYONE;

import java.security.Principal;
import java.util.Set;

Expand Down Expand Up @@ -117,9 +119,8 @@ public final boolean hasRole(final String roleName) {
public Principal getEffectiveUserPrincipal() {
if (this.loggedIn && this.userPrincipal != null) {
return this.userPrincipal;
} else {
return ServletContainerAuthenticationProvider.EVERYONE;
}
return EVERYONE;
}

/**
Expand Down Expand Up @@ -159,8 +160,7 @@ public boolean hasPermission(final Context context, final Path absPath,
if (pep != null) {
return pep.hasModeShapePermission(absPath, actions,
this.principals, getEffectiveUserPrincipal());
} else {
return false;
}
return false;
}
}
Expand Up @@ -89,11 +89,10 @@ public String getName() {
public static synchronized AuthenticationProvider getInstance() {
if (_instance != null) {
return _instance;
} else {
_instance = new ServletContainerAuthenticationProvider();
LOGGER.warn("Security is MINIMAL, no Policy Enforcement Point configured.");
return _instance;
}
_instance = new ServletContainerAuthenticationProvider();
LOGGER.warn("Security is MINIMAL, no Policy Enforcement Point configured.");
return _instance;
}

/**
Expand Down Expand Up @@ -137,7 +136,7 @@ public ExecutionContext authenticate(final Credentials credentials,
}

// add base public principals
final Set<Principal> principals = new HashSet<Principal>();
final Set<Principal> principals = new HashSet<>();
principals.add(EVERYONE); // all sessions have this principal

// request fedora user role to add user principal
Expand Down
Expand Up @@ -69,9 +69,8 @@ protected static HttpPost postObjMethod(final String pid,
final String query) {
if (query.equals("")) {
return new HttpPost(serverAddress + pid);
} else {
return new HttpPost(serverAddress + pid + "?" + query);
}
return new HttpPost(serverAddress + pid + "?" + query);
}

protected static HttpPost postDSMethod(final String pid,
Expand Down
Expand Up @@ -70,23 +70,22 @@ public OAuthDecision validateRequest(final String rsId, final String token,
try {
if (!session.itemExists(getTokenPath(token))) {
throw new OAuthRuntimeException("Invalid token!");
} else {
final Node tokenNode = session.getNode(getTokenPath(token));
LOGGER.debug("Retrieved token from: {}", tokenNode.getPath());
}
final Node tokenNode = session.getNode(getTokenPath(token));
LOGGER.debug("Retrieved token from: {}", tokenNode.getPath());

final String client =
tokenNode.getProperty(CLIENT_PROPERTY).getString();
LOGGER.debug("Retrieved client: {}", client);
final String client =
tokenNode.getProperty(CLIENT_PROPERTY).getString();
LOGGER.debug("Retrieved client: {}", client);

final String principal;
if (tokenNode.hasProperty(PRINCIPAL_PROPERTY)) {
principal = tokenNode.getProperty(PRINCIPAL_PROPERTY).getString();
} else {
principal = null;
}
LOGGER.debug("Retrieved principal: {}", principal);
return new Decision(client, principal);
final String principal;
if (tokenNode.hasProperty(PRINCIPAL_PROPERTY)) {
principal = tokenNode.getProperty(PRINCIPAL_PROPERTY).getString();
} else {
principal = null;
}
LOGGER.debug("Retrieved principal: {}", principal);
return new Decision(client, principal);
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException("Exception validating OAuth request", e);
} finally {
Expand All @@ -96,7 +95,7 @@ public OAuthDecision validateRequest(final String rsId, final String token,

}

private String getTokenPath(String token) {
private static String getTokenPath(final String token) {
return "/tokens/" + token;
}

Expand Down
Expand Up @@ -53,7 +53,7 @@ public class DefaultTokenRequestValidations implements TokenRequestValidations {
* accessible via the given session factory.
* @param sessions
*/
public DefaultTokenRequestValidations(SessionFactory sessions) {
public DefaultTokenRequestValidations(final SessionFactory sessions) {
this.sessions = sessions;
}

Expand All @@ -64,7 +64,7 @@ public DefaultTokenRequestValidations(SessionFactory sessions) {
* @throws RepositoryException
*/
@Override
public boolean isValidAuthCode(OAuthTokenRequest oauthRequest)
public boolean isValidAuthCode(final OAuthTokenRequest oauthRequest)
throws RepositoryException {
final String client = oauthRequest.getClientId();
LOGGER.debug("Request has authorization client: {}", client);
Expand All @@ -91,9 +91,8 @@ public boolean isValidAuthCode(OAuthTokenRequest oauthRequest)
value2string));
// and if there is at least one scope in common
return (storedScopes.isEmpty() || !intersection(storedScopes, scopes).isEmpty());
} else {
return true;
}
return true;
}
} catch (final PathNotFoundException e) {
// this wasn't a code we stored
Expand Down
Expand Up @@ -61,7 +61,7 @@ public class DefaultOAuthResourceProviderTest {
DefaultOAuthResourceProvider testObj;

@Before
public void setUp() throws RepositoryException, NoSuchFieldException {
public void setUp() throws RepositoryException {
initMocks(this);
when(mockSessions.getInternalSession("oauth")).thenReturn(mockSession);
when(mockSession.itemExists("/tokens/" + INVALID_TOKEN))
Expand Down Expand Up @@ -90,7 +90,7 @@ public void testAcceptsExistingTokenRequest()

@Test(expected=OAuthRuntimeException.class)
public void testRejectsNonexistentTokenRequest()
throws PathNotFoundException, RepositoryException, OAuthProblemException {
throws OAuthProblemException {
testObj.validateRequest(DUMMY_RSID, INVALID_TOKEN, mockRequest);
verify(mockSession).logout();
}
Expand Down
Expand Up @@ -64,7 +64,7 @@ public class AuthzEndpointTest {
private AuthzEndpoint testObj;

@Before
public void setUp() throws RepositoryException, NoSuchFieldException {
public void setUp() throws RepositoryException {
initMocks(this);
when(mockRootNode.getNode(startsWith("authorization-codes/")))
.thenReturn(mockCodeNode);
Expand Down
Expand Up @@ -47,36 +47,36 @@
import org.mockito.Mock;

public class TokenEndpointTest {

private static final String AUTH_CODES = "/authorization-codes";

private final static String DUMMY_AUTH_CODE = "mockAuthorizationCode";

@Mock
private HttpServletRequest mockRequest;

@Mock
TokenRequestValidations mockValidations;

@Mock
SessionFactory mockSessions;

@Mock
Session mockSession;

@Mock
Node mockTokenNode;

@Mock
Node mockTokenRootNode;

@Mock
Node mockRootNode;

private TokenEndpoint testObj;

@Before
public void setUp() throws NoSuchFieldException, RepositoryException {
public void setUp() throws RepositoryException {
initMocks(this);
testObj = new TokenEndpoint();
setField(testObj, "requestValidator", mockValidations);
Expand All @@ -96,14 +96,14 @@ public void setUp() throws NoSuchFieldException, RepositoryException {
when(mockSession.getNode(startsWith("/tokens/")))
.thenReturn(mockTokenNode);
}

private void initForAuthCode() throws RepositoryException {
String authCodePath = (AUTH_CODES + "/" + DUMMY_AUTH_CODE);
Node mockAuthCode = mock(Node.class);
final String authCodePath = (AUTH_CODES + "/" + DUMMY_AUTH_CODE);
final Node mockAuthCode = mock(Node.class);
when(mockAuthCode.getPath()).thenReturn(authCodePath);
when(mockRootNode.getNode(authCodePath.substring(1)))
.thenReturn(mockAuthCode);
Property mockProp = mock(Property.class);
final Property mockProp = mock(Property.class);
when(mockProp.getString()).thenReturn("bond");
when(mockAuthCode.getProperty("oauth-client")).thenReturn(mockProp);
when(mockRequest.getParameter(OAuth.OAUTH_GRANT_TYPE))
Expand All @@ -115,7 +115,7 @@ private void initForAuthCode() throws RepositoryException {
when(mockSession.getNode(authCodePath))
.thenReturn(mockAuthCode);
}

private void initForPassword() {
when(mockRequest.getParameter(OAuth.OAUTH_GRANT_TYPE))
.thenReturn(GrantType.PASSWORD.toString());
Expand All @@ -124,14 +124,14 @@ private void initForPassword() {
when(mockRequest.getParameter(OAuth.OAUTH_PASSWORD))
.thenReturn("testPassword");
}

private void initForRefreshToken() {
when(mockRequest.getParameter(OAuth.OAUTH_GRANT_TYPE))
.thenReturn(GrantType.REFRESH_TOKEN.toString());
when(mockRequest.getParameter(OAuth.OAUTH_REFRESH_TOKEN))
.thenReturn("dummyData");
}

@Test
public void testValidRequest()
throws OAuthSystemException, RepositoryException {
Expand All @@ -144,7 +144,7 @@ public void testValidRequest()
.thenReturn(true);
when(mockValidations.isValidCredentials(any(OAuthTokenRequest.class)))
.thenReturn(true);
Response actual = testObj.getToken(mockRequest);
final Response actual = testObj.getToken(mockRequest);
if (actual.getStatus() != 200) {
System.out.println(actual.getEntity());
}
Expand All @@ -155,7 +155,7 @@ public void testValidRequest()
verify(mockSession).save();
verify(mockSession).logout();
}

@Test
public void testBadClient()
throws RepositoryException, OAuthSystemException {
Expand All @@ -168,10 +168,10 @@ public void testBadClient()
.thenReturn(true);
when(mockValidations.isValidCredentials(any(OAuthTokenRequest.class)))
.thenReturn(true);
Response actual = testObj.getToken(mockRequest);
final Response actual = testObj.getToken(mockRequest);
assertEquals(400, actual.getStatus());
System.out.println(actual.getEntity());
JsonObject json = JSON.parse(actual.getEntity().toString());
final JsonObject json = JSON.parse(actual.getEntity().toString());
assertEquals("invalid_client", json.get("error").getAsString().value());
}

Expand All @@ -187,10 +187,10 @@ public void testBadSecret()
.thenReturn(false);
when(mockValidations.isValidCredentials(any(OAuthTokenRequest.class)))
.thenReturn(true);
Response actual = testObj.getToken(mockRequest);
final Response actual = testObj.getToken(mockRequest);
assertEquals(401, actual.getStatus());
System.out.println(actual.getEntity());
JsonObject json = JSON.parse(actual.getEntity().toString());
final JsonObject json = JSON.parse(actual.getEntity().toString());
assertEquals("unauthorized_client", json.get("error").getAsString().value());
}

Expand All @@ -206,10 +206,10 @@ public void testBadAuthCode()
.thenReturn(true);
when(mockValidations.isValidCredentials(any(OAuthTokenRequest.class)))
.thenReturn(true);
Response actual = testObj.getToken(mockRequest);
final Response actual = testObj.getToken(mockRequest);
assertEquals(400, actual.getStatus());
System.out.println(actual.getEntity());
JsonObject json = JSON.parse(actual.getEntity().toString());
final JsonObject json = JSON.parse(actual.getEntity().toString());
assertEquals("invalid_grant", json.get("error").getAsString().value());
}

Expand All @@ -225,10 +225,10 @@ public void testBadPasswordCredentials()
.thenReturn(true);
when(mockValidations.isValidCredentials(any(OAuthTokenRequest.class)))
.thenReturn(false);
Response actual = testObj.getToken(mockRequest);
final Response actual = testObj.getToken(mockRequest);
assertEquals(400, actual.getStatus());
System.out.println(actual.getEntity());
JsonObject json = JSON.parse(actual.getEntity().toString());
final JsonObject json = JSON.parse(actual.getEntity().toString());
assertEquals("invalid_grant", json.get("error").getAsString().value());
}

Expand All @@ -240,10 +240,10 @@ public void testRefreshNotSupported()
.thenReturn(true);
when(mockValidations.isValidSecret(any(OAuthTokenRequest.class)))
.thenReturn(true);
Response actual = testObj.getToken(mockRequest);
final Response actual = testObj.getToken(mockRequest);
assertEquals(400, actual.getStatus());
System.out.println(actual.getEntity());
JsonObject json = JSON.parse(actual.getEntity().toString());
final JsonObject json = JSON.parse(actual.getEntity().toString());
assertEquals("invalid_grant", json.get("error").getAsString().value());
}
}

0 comments on commit 306b021

Please sign in to comment.