use of fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAuthorizationCodeDAO in project pyramus by otavanopisto.
the class ClientApplicationsViewController method processSend.
@Override
public void processSend(PageRequestContext requestContext) {
ClientApplicationDAO clientApplicationDAO = DAOFactory.getInstance().getClientApplicationDAO();
ClientApplicationAuthorizationCodeDAO clientApplicationAuthorizationCodeDAO = DAOFactory.getInstance().getClientApplicationAuthorizationCodeDAO();
ClientApplicationAccessTokenDAO clientApplicationAccessTokenDAO = DAOFactory.getInstance().getClientApplicationAccessTokenDAO();
Long clientApplicationsRowCount = requestContext.getLong("clientApplicationsTable.rowCount");
for (int i = 0; i < clientApplicationsRowCount; i++) {
String colPrefix = "clientApplicationsTable." + i;
Long id = requestContext.getLong(colPrefix + ".id");
Boolean remove = "1".equals(requestContext.getString(colPrefix + ".remove"));
Boolean regenerateSecret = "1".equals(requestContext.getString(colPrefix + ".regenerateSecret"));
Boolean skipPrompt = "1".equals(requestContext.getString(colPrefix + ".skipPrompt"));
String clientName = requestContext.getString(colPrefix + ".appName");
String clientId = requestContext.getString(colPrefix + ".appId");
String clientSecret = requestContext.getString(colPrefix + ".appSecret");
if (id == null && !remove) {
clientId = UUID.randomUUID().toString();
clientSecret = new OauthClientSecretGenerator(80).nextString();
clientApplicationDAO.create(clientName, clientId, clientSecret, skipPrompt);
} else if (id != null) {
ClientApplication clientApplication = clientApplicationDAO.findById(id);
if (remove) {
List<ClientApplicationAuthorizationCode> authCodes = clientApplicationAuthorizationCodeDAO.listByClientApplication(clientApplication);
for (ClientApplicationAuthorizationCode clientApplicationAuthorizationCode : authCodes) {
ClientApplicationAccessToken clientApplicationAccessToken = clientApplicationAccessTokenDAO.findByAuthCode(clientApplicationAuthorizationCode);
if (clientApplicationAccessToken != null) {
clientApplicationAccessTokenDAO.delete(clientApplicationAccessToken);
}
clientApplicationAuthorizationCodeDAO.delete(clientApplicationAuthorizationCode);
}
clientApplicationDAO.delete(clientApplication);
} else {
if (regenerateSecret) {
clientSecret = new OauthClientSecretGenerator(80).nextString();
clientApplicationDAO.updateClientSecret(clientApplication, clientSecret);
}
clientApplicationDAO.updateName(clientApplication, clientName);
clientApplicationDAO.updateSkipPrompt(clientApplication, skipPrompt);
}
}
}
processForm(requestContext);
}
use of fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAuthorizationCodeDAO in project pyramus by otavanopisto.
the class AuthorizeClientApplicationViewController method processSend.
@Override
public void processSend(PageRequestContext requestContext) {
if (!requestContext.isLoggedIn()) {
HttpServletRequest request = requestContext.getRequest();
StringBuilder currentUrl = new StringBuilder(request.getRequestURL());
String queryString = request.getQueryString();
if (!StringUtils.isBlank(queryString)) {
currentUrl.append('?');
currentUrl.append(queryString);
}
throw new LoginRequiredException(currentUrl.toString());
}
UserDAO userDAO = DAOFactory.getInstance().getUserDAO();
ClientApplicationDAO clientApplicationDAO = DAOFactory.getInstance().getClientApplicationDAO();
ClientApplicationAuthorizationCodeDAO clientApplicationAuthorizationCodeDAO = DAOFactory.getInstance().getClientApplicationAuthorizationCodeDAO();
HttpServletRequest request = requestContext.getRequest();
HttpSession session = request.getSession();
Boolean authorized = "Authorize".equals(request.getParameter("authorize"));
if (authorized) {
Long userId = (Long) session.getAttribute("loggedUserId");
String authorizationCode = (String) session.getAttribute("pendingAuthCode");
String redirectURI = (String) session.getAttribute("pendingOauthRedirectUrl");
ClientApplication clientApplication = clientApplicationDAO.findByClientId((String) session.getAttribute("clientAppId"));
if (userId != null && authorizationCode != null && redirectURI != null && clientApplication != null) {
try {
OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse.authorizationResponse(request, HttpServletResponse.SC_FOUND);
builder.setCode(authorizationCode);
final OAuthResponse response = builder.location(redirectURI).buildQueryMessage();
User user = userDAO.findById(userId);
clientApplicationAuthorizationCodeDAO.create(user, clientApplication, authorizationCode, redirectURI);
requestContext.setRedirectURL(response.getLocationUri());
} catch (OAuthSystemException e) {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
} else {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_BAD_REQUEST, "Invalid parameters");
}
}
}
use of fi.otavanopisto.pyramus.dao.clientapplications.ClientApplicationAuthorizationCodeDAO in project pyramus by otavanopisto.
the class AuthorizeClientApplicationViewController method processForm.
@Override
public void processForm(PageRequestContext requestContext) {
ClientApplicationDAO clientApplicationDAO = DAOFactory.getInstance().getClientApplicationDAO();
if (!requestContext.isLoggedIn()) {
HttpServletRequest request = requestContext.getRequest();
StringBuilder currentUrl = new StringBuilder(request.getRequestURL());
String queryString = request.getQueryString();
if (!StringUtils.isBlank(queryString)) {
currentUrl.append('?');
currentUrl.append(queryString);
}
String clientId = requestContext.getString("client_id");
if (StringUtils.isNotBlank(clientId)) {
ClientApplication clientApplication = clientApplicationDAO.findByClientId(clientId);
if (clientApplication == null) {
throw new SmvcRuntimeException(HttpServletResponse.SC_FORBIDDEN, "Client application not found");
}
throw new LoginRequiredException(currentUrl.toString(), "OAUTHCLIENT", clientId);
} else {
throw new SmvcRuntimeException(HttpServletResponse.SC_FORBIDDEN, "Client application not defined");
}
}
HttpServletRequest request = requestContext.getRequest();
OAuthAuthzRequest oauthRequest;
OAuthIssuerImpl oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
try {
oauthRequest = new OAuthAuthzRequest(request);
ClientApplication clientApplication = clientApplicationDAO.findByClientId(oauthRequest.getClientId());
if (clientApplication != null) {
request.getSession().setAttribute("clientAppId", oauthRequest.getClientId());
String responseType = oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE);
if (!responseType.equals(ResponseType.CODE.toString())) {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_NOT_IMPLEMENTED, String.format("Response type: %s not supported", responseType));
}
String authorizationCode = oauthIssuerImpl.authorizationCode();
request.getSession().setAttribute("pendingAuthCode", authorizationCode);
String redirectURI = oauthRequest.getParam(OAuth.OAUTH_REDIRECT_URI);
request.getSession().setAttribute("pendingOauthRedirectUrl", redirectURI);
request.setAttribute("clientAppName", clientApplication.getClientName());
if (clientApplication.getSkipPrompt()) {
ClientApplicationAuthorizationCodeDAO clientApplicationAuthorizationCodeDAO = DAOFactory.getInstance().getClientApplicationAuthorizationCodeDAO();
UserDAO userDAO = DAOFactory.getInstance().getUserDAO();
HttpSession session = request.getSession();
Long userId = (Long) session.getAttribute("loggedUserId");
if (userId != null && authorizationCode != null && redirectURI != null && clientApplication != null) {
try {
OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse.authorizationResponse(request, HttpServletResponse.SC_FOUND);
builder.setCode(authorizationCode);
final OAuthResponse response = builder.location(redirectURI).buildQueryMessage();
User user = userDAO.findById(userId);
clientApplicationAuthorizationCodeDAO.create(user, clientApplication, authorizationCode, redirectURI);
requestContext.setRedirectURL(response.getLocationUri());
} catch (OAuthSystemException e) {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
} else {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_BAD_REQUEST, "Invalid parameters");
}
}
} else {
requestContext.setIncludeJSP("/templates/generic/errorpage.jsp");
throw new SmvcRuntimeException(HttpServletResponse.SC_FORBIDDEN, "Client application not found");
}
} catch (OAuthProblemException | OAuthSystemException e) {
throw new SmvcRuntimeException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
// TODO: show auth page only if everything is ok
requestContext.setIncludeJSP("/templates/users/authorizeclientapp.jsp");
}
Aggregations