finish up work on moving backend into stateless server with resource-oriented api using basic auth for securing api calls.
This commit is contained in:
-93
@@ -1,93 +0,0 @@
|
||||
package org.mifosng.oauth;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
public class ConsumerUserDetails implements UserDetails {
|
||||
|
||||
private final String username;
|
||||
private final Collection<GrantedAuthority> authorities;
|
||||
|
||||
public ConsumerUserDetails(String username, Collection<GrantedAuthority> authorities) {
|
||||
this.username = username;
|
||||
this.authorities = authorities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
return authorities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonLocked() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCredentialsNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean hasNoReportingAuthority() {
|
||||
return !hasReportingAuthority();
|
||||
}
|
||||
|
||||
private boolean hasReportingAuthority() {
|
||||
SimpleGrantedAuthority reportingAuthority = new SimpleGrantedAuthority("REPORTING_SUPER_USER_ROLE");
|
||||
return this.authorities.contains(reportingAuthority);
|
||||
}
|
||||
|
||||
public boolean hasNoAuthorityToSumitLoanApplication() {
|
||||
return !hasAuthorityToSumitLoanApplication();
|
||||
}
|
||||
|
||||
private boolean hasAuthorityToSumitLoanApplication() {
|
||||
return containsAnyOf(portfolioAllAuthority(), sumbitLoanApplicationAuthority(), sumbitHistoricLoanApplicationAuthority());
|
||||
}
|
||||
|
||||
private boolean containsAnyOf(SimpleGrantedAuthority... authorities) {
|
||||
boolean match = false;
|
||||
for (SimpleGrantedAuthority authority : authorities) {
|
||||
match = this.authorities.contains(authority);
|
||||
if (match) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
private SimpleGrantedAuthority portfolioAllAuthority() {
|
||||
return new SimpleGrantedAuthority("PORTFOLIO_MANAGEMENT_SUPER_USER_ROLE");
|
||||
}
|
||||
|
||||
private SimpleGrantedAuthority sumbitLoanApplicationAuthority() {
|
||||
return new SimpleGrantedAuthority("CAN_SUBMIT_NEW_LOAN_APPLICATION_ROLE");
|
||||
}
|
||||
|
||||
private SimpleGrantedAuthority sumbitHistoricLoanApplicationAuthority() {
|
||||
return new SimpleGrantedAuthority("CAN_SUBMIT_HISTORIC_LOAN_APPLICATION_ROLE");
|
||||
}
|
||||
}
|
||||
-271
@@ -1,271 +0,0 @@
|
||||
package org.mifosng.oauth;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.mifosng.configuration.ApplicationConfigurationService;
|
||||
import org.mifosng.data.AuthenticatedUserData;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.context.SecurityContextImpl;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.oauth.common.OAuthProviderParameter;
|
||||
import org.springframework.security.oauth.consumer.OAuthConsumerToken;
|
||||
import org.springframework.security.oauth.consumer.OAuthRequestFailedException;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContextHolder;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContextImpl;
|
||||
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
|
||||
import org.springframework.security.oauth.consumer.client.CoreOAuthConsumerSupport;
|
||||
import org.springframework.security.oauth.consumer.filter.OAuthConsumerContextFilter;
|
||||
import org.springframework.security.web.DefaultRedirectStrategy;
|
||||
import org.springframework.security.web.RedirectStrategy;
|
||||
import org.springframework.web.client.HttpStatusCodeException;
|
||||
|
||||
public class CustomOAuthConsumerContextFilter extends OAuthConsumerContextFilter {
|
||||
|
||||
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||
private final ApplicationConfigurationService applicationConfigurationService;
|
||||
|
||||
@Autowired
|
||||
public CustomOAuthConsumerContextFilter(final ApplicationConfigurationService applicationConfigurationService) {
|
||||
this.applicationConfigurationService = applicationConfigurationService;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
|
||||
HttpServletRequest request = (HttpServletRequest) servletRequest;
|
||||
HttpServletResponse response = (HttpServletResponse) servletResponse;
|
||||
|
||||
OAuthSecurityContextImpl context = new OAuthSecurityContextImpl();
|
||||
context.setDetails(request);
|
||||
|
||||
Map<String, OAuthConsumerToken> rememberedTokens = getRememberMeServices().loadRememberedTokens(request, response);
|
||||
Map<String, OAuthConsumerToken> accessTokens = new TreeMap<String, OAuthConsumerToken>();
|
||||
Map<String, OAuthConsumerToken> requestTokens = new TreeMap<String, OAuthConsumerToken>();
|
||||
if (rememberedTokens != null) {
|
||||
for (Map.Entry<String, OAuthConsumerToken> tokenEntry : rememberedTokens.entrySet()) {
|
||||
OAuthConsumerToken token = tokenEntry.getValue();
|
||||
if (token != null) {
|
||||
if (token.isAccessToken()) {
|
||||
accessTokens.put(tokenEntry.getKey(), token);
|
||||
}
|
||||
else {
|
||||
requestTokens.put(tokenEntry.getKey(), token);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
context.setAccessTokens(accessTokens);
|
||||
OAuthSecurityContextHolder.setContext(context);
|
||||
// if (LOG.isDebugEnabled()) {
|
||||
// LOG.debug("Storing access tokens in request attribute '" + getAccessTokensRequestAttribute() + "'.");
|
||||
// }
|
||||
|
||||
try {
|
||||
try {
|
||||
request.setAttribute(getAccessTokensRequestAttribute(), new ArrayList<OAuthConsumerToken>(accessTokens.values()));
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
catch (Exception e) {
|
||||
try {
|
||||
ProtectedResourceDetails resourceThatNeedsAuthorization = checkForResourceThatNeedsAuthorization(e);
|
||||
String neededResourceId = resourceThatNeedsAuthorization.getId();
|
||||
while (!accessTokens.containsKey(neededResourceId)) {
|
||||
OAuthConsumerToken token = requestTokens.remove(neededResourceId);
|
||||
if (token == null) {
|
||||
token = getTokenServices().getToken(neededResourceId);
|
||||
}
|
||||
|
||||
String verifier = request.getParameter(OAuthProviderParameter.oauth_verifier.toString());
|
||||
// if the token is null OR
|
||||
// if there is NO access token and (we're not using 1.0a or the verifier is not null)
|
||||
if (token == null || (!token.isAccessToken() && (!resourceThatNeedsAuthorization.isUse10a() || verifier == null))) {
|
||||
//no token associated with the resource, start the oauth flow.
|
||||
//if there's a request token, but no verifier, we'll assume that a previous oauth request failed and we need to get a new request token.
|
||||
// if (LOG.isDebugEnabled()) {
|
||||
// LOG.debug("Obtaining request token for resource: " + neededResourceId);
|
||||
// }
|
||||
|
||||
//obtain authorization.
|
||||
String callbackURL = response.encodeRedirectURL(getCallbackURL(request));
|
||||
token = getConsumerSupport().getUnauthorizedRequestToken(neededResourceId, callbackURL);
|
||||
// if (LOG.isDebugEnabled()) {
|
||||
// LOG.debug("Request token obtained for resource " + neededResourceId + ": " + token);
|
||||
// }
|
||||
|
||||
//okay, we've got a request token, now we need to authorize it.
|
||||
requestTokens.put(neededResourceId, token);
|
||||
getTokenServices().storeToken(neededResourceId, token);
|
||||
|
||||
|
||||
try {
|
||||
String baseURL = this.applicationConfigurationService.retrieveOAuthProviderDetails().getUserAuthorizationUrl();
|
||||
StringBuilder builder = new StringBuilder(baseURL);
|
||||
char appendChar = baseURL.indexOf('?') < 0 ? '?' : '&';
|
||||
builder.append(appendChar).append("oauth_token=");
|
||||
builder.append(URLEncoder.encode(token.getValue(), "UTF-8"));
|
||||
builder.append('&').append("oauth_callback=");
|
||||
builder.append(URLEncoder.encode(callbackURL, "UTF-8"));
|
||||
|
||||
String redirect = builder.toString();
|
||||
|
||||
request.setAttribute("org.springframework.security.oauth.consumer.AccessTokenRequiredException", e);
|
||||
this.redirectStrategy.sendRedirect(request, response, redirect);
|
||||
return;
|
||||
}
|
||||
catch (UnsupportedEncodingException use) {
|
||||
throw new IllegalStateException(use);
|
||||
}
|
||||
}
|
||||
else if (!token.isAccessToken()) {
|
||||
//we have a presumably authorized request token, let's try to get an access token with it.
|
||||
// if (LOG.isDebugEnabled()) {
|
||||
// LOG.debug("Obtaining access token for resource: " + neededResourceId);
|
||||
// }
|
||||
|
||||
//authorize the request token and store it.
|
||||
try {
|
||||
token = getConsumerSupport().getAccessToken(token, verifier);
|
||||
}
|
||||
finally {
|
||||
getTokenServices().removeToken(neededResourceId);
|
||||
}
|
||||
|
||||
// if (LOG.isDebugEnabled()) {
|
||||
// LOG.debug("Access token " + token + " obtained for resource " + neededResourceId + ". Now storing and using.");
|
||||
// }
|
||||
|
||||
getTokenServices().storeToken(neededResourceId, token);
|
||||
}
|
||||
|
||||
accessTokens.put(neededResourceId, token);
|
||||
|
||||
// hack here to detect this situation which happens once user is authenticated successfully on provider
|
||||
// and redirected back to this client application
|
||||
String oauthAccessToken = request.getParameter("oauth_token");
|
||||
String oauthVerifier = request.getParameter("oauth_verifier");
|
||||
|
||||
if (StringUtils.isNotBlank(oauthAccessToken) && StringUtils.isNotBlank(oauthVerifier)) {
|
||||
CoreOAuthConsumerSupport support = (CoreOAuthConsumerSupport)getConsumerSupport();
|
||||
|
||||
MifosNgOauthRestTemplate oauthRestTemplate = new MifosNgOauthRestTemplate(support.getProtectedResourceDetailsService());
|
||||
try {
|
||||
URI restUri = URI
|
||||
.create(getBaseServerUrl().concat("api/protected/user/"
|
||||
+ oauthAccessToken));
|
||||
|
||||
ResponseEntity<AuthenticatedUserData> s = oauthRestTemplate.exchange(restUri, HttpMethod.GET, emptyRequest(), AuthenticatedUserData.class);
|
||||
|
||||
AuthenticatedUserData userData = s.getBody();
|
||||
|
||||
SecurityContextHolder.clearContext();
|
||||
SecurityContext springSecurityContext = new SecurityContextImpl();
|
||||
|
||||
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
for (String permissionCode : userData.getPermissions()) {
|
||||
authorities.add(new SimpleGrantedAuthority(permissionCode));
|
||||
}
|
||||
|
||||
UserDetails principal = new ConsumerUserDetails(userData.getUsername(), authorities);
|
||||
Authentication auth = new UsernamePasswordAuthenticationToken(principal, principal, principal.getAuthorities());
|
||||
springSecurityContext.setAuthentication(auth);
|
||||
|
||||
SecurityContextHolder.setContext(springSecurityContext);
|
||||
|
||||
} catch (HttpStatusCodeException ex) {
|
||||
System.err.println(ex.getResponseBodyAsString());
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
//try again
|
||||
if (!response.isCommitted()) {
|
||||
request.setAttribute(getAccessTokensRequestAttribute(), new ArrayList<OAuthConsumerToken>(accessTokens.values()));
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
else {
|
||||
//dang. what do we do now?
|
||||
throw new IllegalStateException("Unable to reprocess filter chain with needed OAuth2 resources because the response is already committed.");
|
||||
}
|
||||
}
|
||||
catch (Exception e1) {
|
||||
resourceThatNeedsAuthorization = checkForResourceThatNeedsAuthorization(e1);
|
||||
neededResourceId = resourceThatNeedsAuthorization.getId();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OAuthRequestFailedException eo) {
|
||||
fail(request, response, eo);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Throwable[] causeChain = getThrowableAnalyzer().determineCauseChain(ex);
|
||||
OAuthRequestFailedException rfe = (OAuthRequestFailedException) getThrowableAnalyzer().getFirstThrowableOfType(OAuthRequestFailedException.class, causeChain);
|
||||
if (rfe != null) {
|
||||
fail(request, response, rfe);
|
||||
}
|
||||
else {
|
||||
// Rethrow ServletExceptions and RuntimeExceptions as-is
|
||||
if (ex instanceof ServletException) {
|
||||
throw (ServletException) ex;
|
||||
}
|
||||
else if (ex instanceof RuntimeException) {
|
||||
throw (RuntimeException) ex;
|
||||
}
|
||||
|
||||
// Wrap other Exceptions. These are not expected to happen
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
OAuthSecurityContextHolder.setContext(null);
|
||||
HashMap<String, OAuthConsumerToken> tokensToRemember = new HashMap<String, OAuthConsumerToken>();
|
||||
tokensToRemember.putAll(requestTokens);
|
||||
tokensToRemember.putAll(accessTokens);
|
||||
getRememberMeServices().rememberTokens(tokensToRemember, request, response);
|
||||
}
|
||||
}
|
||||
|
||||
private HttpEntity<Object> emptyRequest() {
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.set("Accept", "application/xml");
|
||||
requestHeaders.set("Content-Type", "application/xml");
|
||||
|
||||
HttpEntity<Object> requestEntity = new HttpEntity<Object>(
|
||||
requestHeaders);
|
||||
return requestEntity;
|
||||
}
|
||||
|
||||
private String getBaseServerUrl() {
|
||||
return this.applicationConfigurationService.retrieveOAuthProviderDetails().getProviderBaseUrl();
|
||||
}
|
||||
}
|
||||
-67
@@ -1,67 +0,0 @@
|
||||
package org.mifosng.oauth;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.security.authentication.InsufficientAuthenticationException;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.oauth.consumer.AccessTokenRequiredException;
|
||||
import org.springframework.security.oauth.consumer.OAuthConsumerToken;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContext;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContextHolder;
|
||||
import org.springframework.security.oauth.consumer.filter.OAuthConsumerProcessingFilter;
|
||||
|
||||
public class CustomOAuthConsumerProcessingFilter extends
|
||||
OAuthConsumerProcessingFilter {
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest,
|
||||
ServletResponse servletResponse, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
HttpServletRequest request = (HttpServletRequest) servletRequest;
|
||||
HttpServletResponse response = (HttpServletResponse) servletResponse;
|
||||
|
||||
Set<String> accessTokenDeps = getAccessTokenDependencies(request,
|
||||
response, chain);
|
||||
if (!accessTokenDeps.isEmpty()) {
|
||||
Authentication authentication = SecurityContextHolder.getContext()
|
||||
.getAuthentication();
|
||||
if (isRequireAuthenticated() && !authentication.isAuthenticated()) {
|
||||
throw new InsufficientAuthenticationException(
|
||||
"An authenticated principal must be present.");
|
||||
}
|
||||
|
||||
OAuthSecurityContext context = OAuthSecurityContextHolder
|
||||
.getContext();
|
||||
if (context == null) {
|
||||
throw new IllegalStateException(
|
||||
"No OAuth security context has been established. Unable to access resources.");
|
||||
}
|
||||
|
||||
Map<String, OAuthConsumerToken> accessTokens = context
|
||||
.getAccessTokens();
|
||||
|
||||
for (String dependency : accessTokenDeps) {
|
||||
if (!accessTokens.containsKey(dependency)) {
|
||||
throw new AccessTokenRequiredException(
|
||||
getProtectedResourceDetailsService()
|
||||
.loadProtectedResourceDetailsById(
|
||||
dependency));
|
||||
}
|
||||
}
|
||||
|
||||
chain.doFilter(request, response);
|
||||
} else {
|
||||
chain.doFilter(servletRequest, servletResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
-228
@@ -1,228 +0,0 @@
|
||||
package org.mifosng.oauth;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.security.oauth.common.OAuthProviderParameter;
|
||||
import org.springframework.security.oauth.consumer.OAuthConsumerToken;
|
||||
import org.springframework.security.oauth.consumer.OAuthRequestFailedException;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContextHolder;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContextImpl;
|
||||
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
|
||||
import org.springframework.security.oauth.consumer.filter.OAuthConsumerContextFilter;
|
||||
import org.springframework.web.client.DefaultResponseErrorHandler;
|
||||
import org.springframework.web.client.ResponseErrorHandler;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
public class MifosNgOauthConsumerContextFilter extends OAuthConsumerContextFilter {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(MifosNgOauthConsumerContextFilter.class);
|
||||
|
||||
private RestTemplate noAuthRestTemplate;
|
||||
|
||||
@Override
|
||||
public void doFilter(final ServletRequest servletRequest,
|
||||
final ServletResponse servletResponse, final FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
|
||||
ResponseErrorHandler defaultResponseErrorHandler = new DefaultResponseErrorHandler();
|
||||
this.noAuthRestTemplate = new RestTemplate();
|
||||
this.noAuthRestTemplate.setErrorHandler(defaultResponseErrorHandler);
|
||||
|
||||
HttpServletRequest request = (HttpServletRequest) servletRequest;
|
||||
HttpServletResponse response = (HttpServletResponse) servletResponse;
|
||||
OAuthSecurityContextImpl context = new OAuthSecurityContextImpl();
|
||||
context.setDetails(request);
|
||||
|
||||
Map<String, OAuthConsumerToken> rememberedTokens = getRememberMeServices()
|
||||
.loadRememberedTokens(request, response);
|
||||
Map<String, OAuthConsumerToken> accessTokens = new TreeMap<String, OAuthConsumerToken>();
|
||||
Map<String, OAuthConsumerToken> requestTokens = new TreeMap<String, OAuthConsumerToken>();
|
||||
if (rememberedTokens != null) {
|
||||
for (Map.Entry<String, OAuthConsumerToken> tokenEntry : rememberedTokens
|
||||
.entrySet()) {
|
||||
OAuthConsumerToken token = tokenEntry.getValue();
|
||||
if (token != null) {
|
||||
if (token.isAccessToken()) {
|
||||
accessTokens.put(tokenEntry.getKey(), token);
|
||||
} else {
|
||||
requestTokens.put(tokenEntry.getKey(), token);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
context.setAccessTokens(accessTokens);
|
||||
OAuthSecurityContextHolder.setContext(context);
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Storing access tokens in request attribute '"
|
||||
+ getAccessTokensRequestAttribute() + "'.");
|
||||
}
|
||||
|
||||
try {
|
||||
try {
|
||||
request.setAttribute(
|
||||
getAccessTokensRequestAttribute(),
|
||||
new ArrayList<OAuthConsumerToken>(accessTokens.values()));
|
||||
chain.doFilter(request, response);
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
ProtectedResourceDetails resourceThatNeedsAuthorization = checkForResourceThatNeedsAuthorization(e);
|
||||
String neededResourceId = resourceThatNeedsAuthorization
|
||||
.getId();
|
||||
while (!accessTokens.containsKey(neededResourceId)) {
|
||||
OAuthConsumerToken token = requestTokens
|
||||
.remove(neededResourceId);
|
||||
if (token == null) {
|
||||
token = getTokenServices().getToken(
|
||||
neededResourceId);
|
||||
}
|
||||
|
||||
String verifier = request
|
||||
.getParameter(OAuthProviderParameter.oauth_verifier
|
||||
.toString());
|
||||
// if the token is null OR
|
||||
// if there is NO access token and (we're not using 1.0a
|
||||
// or the verifier is not null)
|
||||
if (token == null
|
||||
|| !token.isAccessToken()
|
||||
&& (!resourceThatNeedsAuthorization.isUse10a() || verifier == null)) {
|
||||
// no token associated with the resource, start the
|
||||
// oauth flow.
|
||||
// if there's a request token, but no verifier,
|
||||
// we'll assume that a previous oauth request failed
|
||||
// and we need to get a new request token.
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Obtaining request token for resource: "
|
||||
+ neededResourceId);
|
||||
}
|
||||
|
||||
// obtain authorization.
|
||||
String callbackURL = response
|
||||
.encodeRedirectURL(getCallbackURL(request));
|
||||
token = getConsumerSupport()
|
||||
.getUnauthorizedRequestToken(
|
||||
neededResourceId, callbackURL);
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Request token obtained for resource "
|
||||
+ neededResourceId + ": " + token);
|
||||
}
|
||||
|
||||
// okay, we've got a request token, now we need to
|
||||
// authorize it.
|
||||
requestTokens.put(neededResourceId, token);
|
||||
getTokenServices().storeToken(neededResourceId,
|
||||
token);
|
||||
String redirect = getUserAuthorizationRedirectURL(
|
||||
resourceThatNeedsAuthorization, token,
|
||||
callbackURL);
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Redirecting request to "
|
||||
+ redirect
|
||||
+ " for user authorization of the request token for resource "
|
||||
+ neededResourceId + ".");
|
||||
}
|
||||
|
||||
request.setAttribute(
|
||||
"org.springframework.security.oauth.consumer.AccessTokenRequiredException",
|
||||
e);
|
||||
getRedirectStrategy().sendRedirect(request,
|
||||
response, redirect);
|
||||
return;
|
||||
} else if (!token.isAccessToken()) {
|
||||
// we have a presumably authorized request token,
|
||||
// let's try to get an access token with it.
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Obtaining access token for resource: "
|
||||
+ neededResourceId);
|
||||
}
|
||||
|
||||
// authorize the request token and store it.
|
||||
try {
|
||||
token = getConsumerSupport().getAccessToken(
|
||||
token, verifier);
|
||||
} finally {
|
||||
getTokenServices()
|
||||
.removeToken(neededResourceId);
|
||||
}
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("Access token " + token
|
||||
+ " obtained for resource "
|
||||
+ neededResourceId
|
||||
+ ". Now storing and using.");
|
||||
}
|
||||
|
||||
getTokenServices().storeToken(neededResourceId,
|
||||
token);
|
||||
}
|
||||
|
||||
accessTokens.put(neededResourceId, token);
|
||||
|
||||
try {
|
||||
// try again
|
||||
if (!response.isCommitted()) {
|
||||
request.setAttribute(
|
||||
getAccessTokensRequestAttribute(),
|
||||
new ArrayList<OAuthConsumerToken>(
|
||||
accessTokens.values()));
|
||||
chain.doFilter(request, response);
|
||||
} else {
|
||||
// dang. what do we do now?
|
||||
throw new IllegalStateException(
|
||||
"Unable to reprocess filter chain with needed OAuth2 resources because the response is already committed.");
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
resourceThatNeedsAuthorization = checkForResourceThatNeedsAuthorization(e1);
|
||||
neededResourceId = resourceThatNeedsAuthorization
|
||||
.getId();
|
||||
}
|
||||
}
|
||||
} catch (OAuthRequestFailedException eo) {
|
||||
fail(request, response, eo);
|
||||
} catch (Exception ex) {
|
||||
Throwable[] causeChain = getThrowableAnalyzer()
|
||||
.determineCauseChain(ex);
|
||||
OAuthRequestFailedException rfe = (OAuthRequestFailedException) getThrowableAnalyzer()
|
||||
.getFirstThrowableOfType(
|
||||
OAuthRequestFailedException.class,
|
||||
causeChain);
|
||||
if (rfe != null) {
|
||||
fail(request, response, rfe);
|
||||
} else {
|
||||
// Rethrow ServletExceptions and RuntimeExceptions as-is
|
||||
if (ex instanceof ServletException) {
|
||||
throw (ServletException) ex;
|
||||
} else if (ex instanceof RuntimeException) {
|
||||
throw (RuntimeException) ex;
|
||||
}
|
||||
|
||||
// Wrap other Exceptions. These are not expected to
|
||||
// happen
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
OAuthSecurityContextHolder.setContext(null);
|
||||
HashMap<String, OAuthConsumerToken> tokensToRemember = new HashMap<String, OAuthConsumerToken>();
|
||||
tokensToRemember.putAll(requestTokens);
|
||||
tokensToRemember.putAll(accessTokens);
|
||||
getRememberMeServices().rememberTokens(tokensToRemember, request,
|
||||
response);
|
||||
}
|
||||
}
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package org.mifosng.oauth;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.oauth.consumer.ProtectedResourceDetailsService;
|
||||
import org.springframework.security.oauth.consumer.client.OAuthRestTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service(value="oauthRestServiceTemplate")
|
||||
public class MifosNgOauthRestTemplate extends OAuthRestTemplate {
|
||||
|
||||
@Autowired
|
||||
public MifosNgOauthRestTemplate(ProtectedResourceDetailsService protectedResourceDetailsService) {
|
||||
super(protectedResourceDetailsService.loadProtectedResourceDetailsById("protectedMifosNgServices"));
|
||||
}
|
||||
|
||||
}
|
||||
-69
@@ -1,69 +0,0 @@
|
||||
package org.mifosng.oauth;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.mifosng.configuration.ApplicationConfigurationService;
|
||||
import org.mifosng.configuration.OAuthProviderDetails;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.oauth.common.signature.SharedConsumerSecret;
|
||||
import org.springframework.security.oauth.consumer.BaseProtectedResourceDetails;
|
||||
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
|
||||
import org.springframework.security.oauth.consumer.ProtectedResourceDetailsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service(value="mifosNgInMemoryResourceDetailsService")
|
||||
public class MifosNgProtectedResourceDetailsService implements
|
||||
ProtectedResourceDetailsService, RefreshableProtectedResourceDetailsService {
|
||||
|
||||
private Map<String, ProtectedResourceDetails> resourceDetailsStore = new HashMap<String, ProtectedResourceDetails>();
|
||||
private final ApplicationConfigurationService applicationConfigurationService;
|
||||
|
||||
@Autowired
|
||||
public MifosNgProtectedResourceDetailsService(ApplicationConfigurationService applicationConfigurationService) {
|
||||
this.applicationConfigurationService = applicationConfigurationService;
|
||||
addHardcodedMifosNgResourceIdToStore();
|
||||
}
|
||||
|
||||
private void addHardcodedMifosNgResourceIdToStore() {
|
||||
BaseProtectedResourceDetails individualLendingResourceDetails = new BaseProtectedResourceDetails();
|
||||
individualLendingResourceDetails.setId("protectedMifosNgServices");
|
||||
|
||||
OAuthProviderDetails oauthProviderDetails = applicationConfigurationService
|
||||
.retrieveOAuthProviderDetails();
|
||||
|
||||
individualLendingResourceDetails
|
||||
.setRequestTokenURL(oauthProviderDetails.getRequestTokenUrl());
|
||||
individualLendingResourceDetails
|
||||
.setUserAuthorizationURL(oauthProviderDetails
|
||||
.getUserAuthorizationUrl());
|
||||
individualLendingResourceDetails.setAccessTokenURL(oauthProviderDetails
|
||||
.getAccessTokenUrl());
|
||||
|
||||
individualLendingResourceDetails.setConsumerKey(oauthProviderDetails
|
||||
.getConsumerkey());
|
||||
individualLendingResourceDetails
|
||||
.setSharedSecret(new SharedConsumerSecret(oauthProviderDetails
|
||||
.getSharedSecret()));
|
||||
|
||||
resourceDetailsStore.put(
|
||||
individualLendingResourceDetails.getId(),
|
||||
individualLendingResourceDetails);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProtectedResourceDetails loadProtectedResourceDetailsById(
|
||||
final String id) throws IllegalArgumentException {
|
||||
return resourceDetailsStore.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh() {
|
||||
resourceDetailsStore.clear();
|
||||
addHardcodedMifosNgResourceIdToStore();
|
||||
}
|
||||
|
||||
public ApplicationConfigurationService getApplicationConfigurationService() {
|
||||
return applicationConfigurationService;
|
||||
}
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package org.mifosng.oauth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
import org.springframework.security.access.ConfigAttribute;
|
||||
import org.springframework.security.access.SecurityConfig;
|
||||
import org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource;
|
||||
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
|
||||
import org.springframework.security.web.util.AntPathRequestMatcher;
|
||||
import org.springframework.security.web.util.RequestMatcher;
|
||||
|
||||
public class OAuthConsumerFilterFilterInvocationSecurityMetadataSource
|
||||
implements FilterInvocationSecurityMetadataSource {
|
||||
|
||||
private final FilterInvocationSecurityMetadataSource metadataSource;
|
||||
|
||||
public OAuthConsumerFilterFilterInvocationSecurityMetadataSource() {
|
||||
LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> requestMap = new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>();
|
||||
RequestMatcher key = new AntPathRequestMatcher("/sparklr/**");
|
||||
Collection<ConfigAttribute> value = new ArrayList<ConfigAttribute>();
|
||||
value.add(new SecurityConfig("sparklrPhotos"));
|
||||
requestMap.put(key, value);
|
||||
|
||||
this.metadataSource = new DefaultFilterInvocationSecurityMetadataSource(
|
||||
requestMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ConfigAttribute> getAttributes(final Object object)
|
||||
throws IllegalArgumentException {
|
||||
return this.metadataSource.getAttributes(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<ConfigAttribute> getAllConfigAttributes() {
|
||||
return this.metadataSource.getAllConfigAttributes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(final Class<?> clazz) {
|
||||
return this.metadataSource.supports(clazz);
|
||||
}
|
||||
}
|
||||
-91
@@ -1,91 +0,0 @@
|
||||
package org.mifosng.oauth;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.mifosng.configuration.OAuthProviderDetails;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth.consumer.OAuthConsumerToken;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContextHolder;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContextImpl;
|
||||
import org.springframework.security.oauth.consumer.rememberme.OAuthRememberMeServices;
|
||||
import org.springframework.security.web.authentication.logout.LogoutHandler;
|
||||
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
|
||||
|
||||
/**
|
||||
* Logout Filter process before oauth filters are applied so need create OAuthSercurityContext and populate with 'remembered tokens' first.
|
||||
*/
|
||||
public class OAuthLogoutHandler implements LogoutHandler {
|
||||
|
||||
private final SecurityContextLogoutHandler delegate;
|
||||
private final MifosNgProtectedResourceDetailsService protectedResourceDetailsService;
|
||||
private final OAuthRememberMeServices oAuthRememberMeServices;
|
||||
|
||||
public OAuthLogoutHandler(final SecurityContextLogoutHandler delegate, final MifosNgProtectedResourceDetailsService protectedResourceDetailsService,
|
||||
final OAuthRememberMeServices oAuthRememberMeServices) {
|
||||
this.delegate = delegate;
|
||||
this.protectedResourceDetailsService = protectedResourceDetailsService;
|
||||
this.oAuthRememberMeServices = oAuthRememberMeServices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logout(HttpServletRequest request,
|
||||
HttpServletResponse response, Authentication authentication) {
|
||||
|
||||
this.delegate.logout(request, response, authentication);
|
||||
|
||||
OAuthSecurityContextImpl context = new OAuthSecurityContextImpl();
|
||||
context.setDetails(request);
|
||||
|
||||
Map<String, OAuthConsumerToken> rememberedTokens = this.oAuthRememberMeServices.loadRememberedTokens(request, response);
|
||||
Map<String, OAuthConsumerToken> accessTokens = new TreeMap<String, OAuthConsumerToken>();
|
||||
if (rememberedTokens != null) {
|
||||
for (Map.Entry<String, OAuthConsumerToken> tokenEntry : rememberedTokens.entrySet()) {
|
||||
OAuthConsumerToken token = tokenEntry.getValue();
|
||||
if (token != null) {
|
||||
if (token.isAccessToken()) {
|
||||
accessTokens.put(tokenEntry.getKey(), token);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
context.setAccessTokens(accessTokens);
|
||||
OAuthSecurityContextHolder.setContext(context);
|
||||
|
||||
if (!accessTokens.isEmpty()) {
|
||||
MifosNgOauthRestTemplate oauthRestTemplate = new MifosNgOauthRestTemplate(
|
||||
protectedResourceDetailsService);
|
||||
|
||||
OAuthProviderDetails providerDetails = this.protectedResourceDetailsService
|
||||
.getApplicationConfigurationService()
|
||||
.retrieveOAuthProviderDetails();
|
||||
|
||||
URI restUri = URI.create(providerDetails.getProviderBaseUrl()
|
||||
.concat("api/protected/user/signout"));
|
||||
|
||||
oauthRestTemplate.exchange(restUri, HttpMethod.GET, emptyRequest(),
|
||||
null);
|
||||
}
|
||||
|
||||
OAuthSecurityContextHolder.setContext(null);
|
||||
}
|
||||
|
||||
|
||||
private HttpEntity<Object> emptyRequest() {
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.set("Accept", "application/xml");
|
||||
requestHeaders.set("Content-Type", "application/xml");
|
||||
|
||||
HttpEntity<Object> requestEntity = new HttpEntity<Object>(
|
||||
requestHeaders);
|
||||
return requestEntity;
|
||||
}
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
package org.mifosng.oauth;
|
||||
|
||||
public interface RefreshableProtectedResourceDetailsService {
|
||||
|
||||
void refresh();
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package org.mifosng.ui;
|
||||
|
||||
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
|
||||
|
||||
public interface CommonRestOperations {
|
||||
|
||||
void logout(String accessToken);
|
||||
|
||||
void updateProtectedResource(ProtectedResourceDetails loadProtectedResourceDetailsById);
|
||||
}
|
||||
-57
@@ -1,57 +0,0 @@
|
||||
package org.mifosng.ui;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.mifosng.configuration.ApplicationConfigurationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.oauth.consumer.ProtectedResourceDetails;
|
||||
import org.springframework.security.oauth.consumer.client.OAuthRestTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service(value = "commonRestOperations")
|
||||
public class CommonRestOperationsImpl implements CommonRestOperations {
|
||||
|
||||
private OAuthRestTemplate oauthRestServiceTemplate;
|
||||
private final ApplicationConfigurationService applicationConfigurationService;
|
||||
|
||||
@Autowired
|
||||
public CommonRestOperationsImpl(
|
||||
final OAuthRestTemplate oauthRestServiceTemplate,
|
||||
final ApplicationConfigurationService applicationConfigurationService) {
|
||||
this.oauthRestServiceTemplate = oauthRestServiceTemplate;
|
||||
this.applicationConfigurationService = applicationConfigurationService;
|
||||
}
|
||||
|
||||
private String getBaseServerUrl() {
|
||||
return this.applicationConfigurationService
|
||||
.retrieveOAuthProviderDetails().getProviderBaseUrl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logout(String accessToken) {
|
||||
URI restUri = URI.create(getBaseServerUrl()
|
||||
.concat("api/protected/user/").concat(accessToken)
|
||||
.concat("/signout"));
|
||||
|
||||
oauthRestServiceTemplate.exchange(restUri, HttpMethod.GET,
|
||||
emptyRequest(), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProtectedResource(ProtectedResourceDetails resource) {
|
||||
|
||||
this.oauthRestServiceTemplate = new OAuthRestTemplate(resource);
|
||||
}
|
||||
|
||||
private HttpEntity<Object> emptyRequest() {
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.set("Accept", "application/xml");
|
||||
requestHeaders.set("Content-Type", "application/xml");
|
||||
|
||||
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestHeaders);
|
||||
return requestEntity;
|
||||
}
|
||||
}
|
||||
+6
-25
@@ -1,12 +1,6 @@
|
||||
package org.mifosng.ui.admin;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
import org.mifosng.oauth.ConsumerUserDetails;
|
||||
import org.mifosng.ui.reporting.ReportingRestOperations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
@@ -22,23 +16,10 @@ import org.springframework.web.servlet.ModelAndView;
|
||||
@Controller
|
||||
public class ApplicationPagesRoutesController {
|
||||
|
||||
private final ReportingRestOperations reportingRestOperations;
|
||||
|
||||
@Autowired
|
||||
public ApplicationPagesRoutesController(final ReportingRestOperations reportingRestOperations) {
|
||||
this.reportingRestOperations = reportingRestOperations;
|
||||
}
|
||||
|
||||
@ExceptionHandler(AccessDeniedException.class)
|
||||
public String accessDeniedException() {
|
||||
return "unAuthorizedAction";
|
||||
}
|
||||
|
||||
@RequestMapping(value="/forceOAuth", method = RequestMethod.GET)
|
||||
public String forceOAuth() {
|
||||
this.reportingRestOperations.hackToForceAuthentication();
|
||||
return "redirect:/home";
|
||||
}
|
||||
|
||||
@RequestMapping(value="/", method = RequestMethod.GET)
|
||||
public String redirectToIndexPage() {
|
||||
@@ -77,13 +58,13 @@ public class ApplicationPagesRoutesController {
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/portfolio/client/{clientId}/loan/new", method = RequestMethod.GET)
|
||||
public String loadLoanCreationWorkflow(final Model model, @PathVariable("clientId") final Long clientId, final Principal principal) {
|
||||
public String loadLoanCreationWorkflow(final Model model, @PathVariable("clientId") final Long clientId) {
|
||||
|
||||
UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) principal;
|
||||
ConsumerUserDetails user = (ConsumerUserDetails) authenticationToken.getPrincipal();
|
||||
if (user.hasNoAuthorityToSumitLoanApplication()) {
|
||||
throw new AccessDeniedException("");
|
||||
}
|
||||
// UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) principal;
|
||||
// ConsumerUserDetails user = (ConsumerUserDetails) authenticationToken.getPrincipal();
|
||||
// if (user.hasNoAuthorityToSumitLoanApplication()) {
|
||||
// throw new AccessDeniedException("");
|
||||
// }
|
||||
|
||||
model.addAttribute("clientId", clientId);
|
||||
|
||||
|
||||
-70
@@ -1,70 +0,0 @@
|
||||
package org.mifosng.ui.configuration;
|
||||
|
||||
import org.mifosng.configuration.ApplicationConfigurationService;
|
||||
import org.mifosng.configuration.OAuthProviderDetails;
|
||||
import org.mifosng.oauth.RefreshableProtectedResourceDetailsService;
|
||||
import org.mifosng.ui.CommonRestOperations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.oauth.consumer.ProtectedResourceDetailsService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@SessionAttributes({"oauthSettingsFormBean"})
|
||||
@Controller
|
||||
public class ConfigurationController {
|
||||
|
||||
private final ApplicationConfigurationService applicationConfigurationService;
|
||||
private final RefreshableProtectedResourceDetailsService refreshableProtectedResourceDetailsService;
|
||||
private final CommonRestOperations commonRestOperations;
|
||||
|
||||
@Autowired
|
||||
public ConfigurationController(
|
||||
final CommonRestOperations commonRestOperations, final ApplicationConfigurationService applicationConfigurationService,
|
||||
final RefreshableProtectedResourceDetailsService refreshableProtectedResourceDetailsService) {
|
||||
this.commonRestOperations = commonRestOperations;
|
||||
this.applicationConfigurationService = applicationConfigurationService;
|
||||
this.refreshableProtectedResourceDetailsService = refreshableProtectedResourceDetailsService;
|
||||
}
|
||||
|
||||
@ExceptionHandler(AccessDeniedException.class)
|
||||
public String accessDeniedException() {
|
||||
return "unAuthorizedAction";
|
||||
}
|
||||
|
||||
@ModelAttribute("oauthSettingsFormBean")
|
||||
@RequestMapping(value = "oauth/configuration/edit", method = RequestMethod.GET)
|
||||
public ModelAndView showOAuthConfiguration() {
|
||||
|
||||
OAuthProviderDetails details = this.applicationConfigurationService.retrieveOAuthProviderDetails();
|
||||
|
||||
OauthSettingsFormBean oauthSettingsFormBean = new OauthSettingsFormBean();
|
||||
oauthSettingsFormBean.setConsumerkey(details.getConsumerkey());
|
||||
oauthSettingsFormBean.setSharedSecret(details.getSharedSecret());
|
||||
oauthSettingsFormBean.setOauthProviderUrl(details.getProviderBaseUrl());
|
||||
|
||||
return new ModelAndView("configuration/oauthconfig",
|
||||
"oauthSettingsFormBean", oauthSettingsFormBean);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "oauth/configuration/edit", method = RequestMethod.POST)
|
||||
public ModelAndView updateOAuthConfiguration(@ModelAttribute final OauthSettingsFormBean oauthSettingsFormBean) {
|
||||
|
||||
ModelAndView mav = new ModelAndView("redirect:/");
|
||||
|
||||
OAuthProviderDetails newDetails = new OAuthProviderDetails(oauthSettingsFormBean.getOauthProviderUrl(), oauthSettingsFormBean.getConsumerkey(), oauthSettingsFormBean.getSharedSecret());
|
||||
|
||||
this.applicationConfigurationService.update(newDetails);
|
||||
|
||||
this.refreshableProtectedResourceDetailsService.refresh();
|
||||
|
||||
this.commonRestOperations.updateProtectedResource(((ProtectedResourceDetailsService)refreshableProtectedResourceDetailsService).loadProtectedResourceDetailsById("protectedMifosNgServices"));
|
||||
|
||||
return mav;
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package org.mifosng.ui.configuration;
|
||||
|
||||
public class OauthSettingsFormBean {
|
||||
|
||||
private String oauthProviderUrl;
|
||||
private String sharedSecret;
|
||||
private String consumerkey;
|
||||
|
||||
public String getOauthProviderUrl() {
|
||||
return oauthProviderUrl;
|
||||
}
|
||||
|
||||
public void setOauthProviderUrl(String oauthProviderUrl) {
|
||||
this.oauthProviderUrl = oauthProviderUrl;
|
||||
}
|
||||
|
||||
public String getSharedSecret() {
|
||||
return sharedSecret;
|
||||
}
|
||||
|
||||
public void setSharedSecret(String sharedSecret) {
|
||||
this.sharedSecret = sharedSecret;
|
||||
}
|
||||
|
||||
public String getConsumerkey() {
|
||||
return consumerkey;
|
||||
}
|
||||
|
||||
public void setConsumerkey(String consumerkey) {
|
||||
this.consumerkey = consumerkey;
|
||||
}
|
||||
}
|
||||
-64
@@ -1,64 +0,0 @@
|
||||
package org.mifosng.ui.home;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.mifosng.configuration.ApplicationConfigurationService;
|
||||
import org.mifosng.ui.CommonRestOperations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.oauth.consumer.OAuthConsumerToken;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContext;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
public class LogoutController {
|
||||
|
||||
private final CommonRestOperations commonRestOperations;
|
||||
// private final ApplicationConfigurationService applicationConfigurationService;
|
||||
|
||||
@Autowired
|
||||
public LogoutController(final CommonRestOperations commonRestOperations,
|
||||
ApplicationConfigurationService applicationConfigurationService) {
|
||||
this.commonRestOperations = commonRestOperations;
|
||||
// this.applicationConfigurationService = applicationConfigurationService;
|
||||
}
|
||||
|
||||
@RequestMapping(value="/logoutcontroller", method = RequestMethod.GET)
|
||||
public String forceOAuth() {
|
||||
|
||||
// OAuthProviderDetails oauthDetails = this.applicationConfigurationService
|
||||
// .retrieveOAuthProviderDetails();
|
||||
|
||||
String resourceId = "protectedMifosNgServices";
|
||||
OAuthSecurityContext context = OAuthSecurityContextHolder.getContext();
|
||||
if (context == null) {
|
||||
throw new IllegalStateException(
|
||||
"No OAuth security context has been established. Unable to access resource '"
|
||||
+ resourceId + "'.");
|
||||
}
|
||||
|
||||
Map<String, OAuthConsumerToken> accessTokens = context
|
||||
.getAccessTokens();
|
||||
OAuthConsumerToken accessToken = accessTokens == null ? null
|
||||
: accessTokens.get(resourceId);
|
||||
|
||||
// model.addAttribute("baseUrl", oauthDetails.getProviderBaseUrl());
|
||||
// model.addAttribute("consumerKey", oauthDetails.getConsumerkey());
|
||||
// model.addAttribute("consumerSecret", oauthDetails.getSharedSecret());
|
||||
// if (accessToken != null) {
|
||||
// model.addAttribute("accessToken", accessToken.getValue());
|
||||
// model.addAttribute("tokenSecret", accessToken.getSecret());
|
||||
// }
|
||||
|
||||
String oauthToken = "";
|
||||
if (accessToken != null) {
|
||||
oauthToken = accessToken.getValue();
|
||||
}
|
||||
|
||||
this.commonRestOperations.logout(oauthToken);
|
||||
|
||||
return "redirect:/j_spring_security_logout";
|
||||
}
|
||||
}
|
||||
-57
@@ -1,57 +0,0 @@
|
||||
package org.mifosng.ui.migration;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.mifosng.configuration.ApplicationConfigurationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.oauth.consumer.client.OAuthRestTemplate;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
public class MigrationTriggerController {
|
||||
|
||||
private final OAuthRestTemplate oauthRestServiceTemplate;
|
||||
private final ApplicationConfigurationService applicationConfigurationService;
|
||||
|
||||
@Autowired
|
||||
public MigrationTriggerController(final OAuthRestTemplate oauthRestServiceTemplate, ApplicationConfigurationService applicationConfigurationService) {
|
||||
this.oauthRestServiceTemplate = oauthRestServiceTemplate;
|
||||
this.applicationConfigurationService = applicationConfigurationService;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/migration/trigger/clients/creocore", method = RequestMethod.GET)
|
||||
public String migrateClientInformation() {
|
||||
|
||||
URI restUri = URI
|
||||
.create(getBaseServerUrl().concat("api/protected/import/trigger/clients/creocore"));
|
||||
|
||||
this.oauthRestServiceTemplate.getForEntity(restUri, null);
|
||||
return "redirect:/home";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/migration/trigger/loans/creocore", method = RequestMethod.GET)
|
||||
public String migrateLoanInformation() {
|
||||
|
||||
URI restUri = URI
|
||||
.create(getBaseServerUrl().concat("api/protected/import/trigger/loans/creocore"));
|
||||
|
||||
this.oauthRestServiceTemplate.getForEntity(restUri, null);
|
||||
return "redirect:/home";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/migration/trigger/repayments/creocore", method = RequestMethod.GET)
|
||||
public String migrateLoanRepaymentsInformation() {
|
||||
|
||||
URI restUri = URI
|
||||
.create(getBaseServerUrl().concat("api/protected/import/trigger/repayments/creocore"));
|
||||
|
||||
this.oauthRestServiceTemplate.getForEntity(restUri, null);
|
||||
return "redirect:/home";
|
||||
}
|
||||
|
||||
private String getBaseServerUrl() {
|
||||
return this.applicationConfigurationService.retrieveOAuthProviderDetails().getProviderBaseUrl();
|
||||
}
|
||||
}
|
||||
+10
-39
@@ -1,17 +1,9 @@
|
||||
package org.mifosng.ui.reporting;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.util.Map;
|
||||
|
||||
import org.mifosng.configuration.ApplicationConfigurationService;
|
||||
import org.mifosng.configuration.OAuthProviderDetails;
|
||||
import org.mifosng.oauth.ConsumerUserDetails;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.oauth.consumer.OAuthConsumerToken;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContext;
|
||||
import org.springframework.security.oauth.consumer.OAuthSecurityContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
@@ -22,51 +14,30 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
@RequestMapping("/reporting")
|
||||
public class ReportingController {
|
||||
|
||||
private final ReportingRestOperations reportingRestOperations;
|
||||
private final ApplicationConfigurationService applicationConfigurationService;
|
||||
|
||||
@Autowired
|
||||
public ReportingController(final ReportingRestOperations reportingRestOperations,
|
||||
public ReportingController(
|
||||
ApplicationConfigurationService applicationConfigurationService) {
|
||||
this.reportingRestOperations = reportingRestOperations;
|
||||
this.applicationConfigurationService = applicationConfigurationService;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/flexireport", method = RequestMethod.GET)
|
||||
public String viewFlexibleReportingPage(final Model model, final Principal principal) {
|
||||
public String viewFlexibleReportingPage(final Model model) {
|
||||
|
||||
UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) principal;
|
||||
ConsumerUserDetails user = (ConsumerUserDetails) authenticationToken.getPrincipal();
|
||||
if (user.hasNoReportingAuthority()) {
|
||||
throw new AccessDeniedException("");
|
||||
}
|
||||
|
||||
this.reportingRestOperations.hackToForceAuthentication();
|
||||
// UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) principal;
|
||||
// ConsumerUserDetails user = (ConsumerUserDetails) authenticationToken.getPrincipal();
|
||||
// if (user.hasNoReportingAuthority()) {
|
||||
// throw new AccessDeniedException("");
|
||||
// }
|
||||
|
||||
OAuthProviderDetails oauthDetails = this.applicationConfigurationService
|
||||
.retrieveOAuthProviderDetails();
|
||||
|
||||
String resourceId = "protectedMifosNgServices";
|
||||
OAuthSecurityContext context = OAuthSecurityContextHolder.getContext();
|
||||
if (context == null) {
|
||||
throw new IllegalStateException(
|
||||
"No OAuth security context has been established. Unable to access resource '"
|
||||
+ resourceId + "'.");
|
||||
}
|
||||
|
||||
Map<String, OAuthConsumerToken> accessTokens = context
|
||||
.getAccessTokens();
|
||||
OAuthConsumerToken accessToken = accessTokens == null ? null
|
||||
: accessTokens.get(resourceId);
|
||||
|
||||
model.addAttribute("baseUrl", oauthDetails.getProviderBaseUrl());
|
||||
model.addAttribute("consumerKey", oauthDetails.getConsumerkey());
|
||||
model.addAttribute("consumerSecret", oauthDetails.getSharedSecret());
|
||||
if (accessToken != null) {
|
||||
model.addAttribute("accessToken", accessToken.getValue());
|
||||
model.addAttribute("tokenSecret", accessToken.getSecret());
|
||||
}
|
||||
|
||||
// model.addAttribute("consumerKey", oauthDetails.getConsumerkey());
|
||||
// model.addAttribute("consumerSecret", oauthDetails.getSharedSecret());
|
||||
//
|
||||
return "reports/flexireport";
|
||||
}
|
||||
|
||||
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
package org.mifosng.ui.reporting;
|
||||
|
||||
public interface ReportingRestOperations {
|
||||
|
||||
void hackToForceAuthentication();
|
||||
|
||||
}
|
||||
-53
@@ -1,53 +0,0 @@
|
||||
package org.mifosng.ui.reporting;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.mifosng.configuration.ApplicationConfigurationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.oauth.consumer.client.OAuthRestTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.HttpStatusCodeException;
|
||||
|
||||
@Service
|
||||
public class ReportingRestOperationsImpl implements ReportingRestOperations {
|
||||
|
||||
private final OAuthRestTemplate oauthRestServiceTemplate;
|
||||
private final ApplicationConfigurationService applicationConfigurationService;
|
||||
|
||||
@Autowired
|
||||
public ReportingRestOperationsImpl(
|
||||
final OAuthRestTemplate oauthRestServiceTemplate, final ApplicationConfigurationService applicationConfigurationService) {
|
||||
this.oauthRestServiceTemplate = oauthRestServiceTemplate;
|
||||
this.applicationConfigurationService = applicationConfigurationService;
|
||||
}
|
||||
|
||||
private HttpEntity<Object> emptyRequest() {
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.set("Accept", "application/xml");
|
||||
requestHeaders.set("Content-Type", "application/xml");
|
||||
|
||||
HttpEntity<Object> requestEntity = new HttpEntity<Object>(
|
||||
requestHeaders);
|
||||
return requestEntity;
|
||||
}
|
||||
|
||||
private String getBaseServerUrl() {
|
||||
return this.applicationConfigurationService.retrieveOAuthProviderDetails().getProviderBaseUrl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hackToForceAuthentication() {
|
||||
try {
|
||||
URI restUri = URI
|
||||
.create(getBaseServerUrl().concat("api/v1/reports/forceauth"));
|
||||
|
||||
this.oauthRestServiceTemplate.exchange(restUri, HttpMethod.GET, emptyRequest(), null);
|
||||
} catch (HttpStatusCodeException e) {
|
||||
System.err.println(e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user