Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Pushing code for ben
- Loading branch information
Showing
10 changed files
with
399 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
package org.fcrepo.auth.oauth; | ||
|
||
public interface Constants { | ||
|
||
public static final String OAUTH_WORKSPACE = "oauth"; | ||
|
||
public static final String CLIENT_PROPERTY = "oauth:client"; | ||
|
||
public static final String PRINCIPAL_PROPERTY = "oauth:principal"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
package org.fcrepo.auth.oauth; | ||
|
||
import java.security.Principal; | ||
|
||
import org.apache.oltu.oauth2.rsfilter.OAuthClient; | ||
import org.apache.oltu.oauth2.rsfilter.OAuthDecision; | ||
|
||
public class Decision implements OAuthDecision { | ||
|
||
private OAuthClient oAuthClient; | ||
|
||
private Principal principal; | ||
|
||
private boolean isAuthorized; | ||
|
||
public Decision(final String client, final String principal) { | ||
this.oAuthClient = new OAuthClient() { | ||
|
||
@Override | ||
public String getClientId() { | ||
return client; | ||
} | ||
|
||
}; | ||
this.principal = new Principal() { | ||
|
||
@Override | ||
public String getName() { | ||
return principal; | ||
} | ||
|
||
}; | ||
} | ||
|
||
@Override | ||
public OAuthClient getOAuthClient() { | ||
return oAuthClient; | ||
} | ||
|
||
@Override | ||
public Principal getPrincipal() { | ||
return principal; | ||
} | ||
|
||
@Override | ||
public boolean isAuthorized() { | ||
return isAuthorized; | ||
} | ||
|
||
public void setAuthorized(final boolean isAuthorized) { | ||
this.isAuthorized = isAuthorized; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/org/fcrepo/auth/oauth/DefaultOAuthResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
package org.fcrepo.auth.oauth; | ||
|
||
import static com.google.common.base.Throwables.propagate; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
import javax.jcr.Node; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.apache.oltu.oauth2.common.exception.OAuthProblemException; | ||
import org.apache.oltu.oauth2.common.exception.OAuthRuntimeException; | ||
import org.apache.oltu.oauth2.rsfilter.OAuthDecision; | ||
import org.apache.oltu.oauth2.rsfilter.OAuthRSProvider; | ||
import org.fcrepo.session.SessionFactory; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import static org.fcrepo.auth.oauth.Constants.*; | ||
|
||
@Component | ||
public class DefaultOAuthResourceProvider implements OAuthRSProvider { | ||
|
||
@Autowired | ||
private SessionFactory sessionFactory; | ||
|
||
private static final Logger LOGGER = | ||
getLogger(DefaultOAuthResourceProvider.class); | ||
|
||
@Override | ||
public OAuthDecision validateRequest(final String rsId, final String token, | ||
final HttpServletRequest req) throws OAuthProblemException { | ||
// first check validity of token | ||
try { | ||
final Session session = sessionFactory.getSession(OAUTH_WORKSPACE); | ||
try { | ||
if (!session.itemExists("/tokens/" + token)) { | ||
throw new OAuthRuntimeException("Invalid token!"); | ||
} else { | ||
final Node tokenNode = session.getNode("/tokens/" + token); | ||
LOGGER.debug("Retrieved token from: {}", tokenNode | ||
.getPath()); | ||
final String client = | ||
tokenNode.getProperty(CLIENT_PROPERTY).getString(); | ||
LOGGER.debug("Retrieved client: {}", client); | ||
final String principal = | ||
tokenNode.getProperty(PRINCIPAL_PROPERTY) | ||
.getString(); | ||
LOGGER.debug("Retrieved principal: {}", principal); | ||
return new Decision(client, principal); | ||
} | ||
} finally { | ||
session.logout(); | ||
} | ||
} catch (final RepositoryException e) { | ||
propagate(e); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public void setSessionFactory(final SessionFactory sessionFactory) { | ||
this.sessionFactory = sessionFactory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.