use of org.springframework.web.bind.support.SimpleSessionStatus in project ORCID-Source by ORCID.
the class OauthRegistrationController method registerAndAuthorize.
@RequestMapping(value = "/oauth/custom/registerConfirm.json", method = RequestMethod.POST)
@ResponseBody
public RequestInfoForm registerAndAuthorize(HttpServletRequest request, HttpServletResponse response, @RequestBody OauthRegistrationForm form) {
RequestInfoForm requestInfoForm = (RequestInfoForm) request.getSession().getAttribute(REQUEST_INFO_FORM);
if (form.getApproved()) {
boolean usedCaptcha = false;
// block google.
if (form.getGrecaptchaWidgetId().getValue() != null) {
// to the login page
if (request.getSession().getAttribute(RegistrationController.GRECAPTCHA_SESSION_ATTRIBUTE_NAME) == null || PojoUtil.isEmpty(form.getGrecaptcha()) || !form.getGrecaptcha().getValue().equals(request.getSession().getAttribute(RegistrationController.GRECAPTCHA_SESSION_ATTRIBUTE_NAME))) {
String redirectUri = this.getBaseUri() + REDIRECT_URI_ERROR;
// Set the client id
redirectUri = redirectUri.replace("{0}", requestInfoForm.getClientId());
// Set the response type if needed
if (!PojoUtil.isEmpty(requestInfoForm.getResponseType()))
redirectUri += "&response_type=" + requestInfoForm.getResponseType();
// Set the redirect uri
if (!PojoUtil.isEmpty(requestInfoForm.getRedirectUrl()))
redirectUri += "&redirect_uri=" + requestInfoForm.getRedirectUrl();
// remove email access scope if present but not granted
if (requestInfoForm.containsEmailReadPrivateScope() && !form.isEmailAccessAllowed()) {
requestInfoForm.removeEmailReadPrivateScope();
}
// Set the scope param
if (!PojoUtil.isEmpty(requestInfoForm.getScopesAsString()))
redirectUri += "&scope=" + requestInfoForm.getScopesAsString();
// Copy the state param if present
if (!PojoUtil.isEmpty(requestInfoForm.getStateParam()))
redirectUri += "&state=" + requestInfoForm.getStateParam();
requestInfoForm.setRedirectUrl(redirectUri);
SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);
if (savedRequest != null)
LOGGER.info("OauthConfirmAccessController original request: " + savedRequest.getRedirectUrl());
LOGGER.info("OauthConfirmAccessController form.getRedirectUri being sent to client browser: " + requestInfoForm.getRedirectUrl());
return requestInfoForm;
}
usedCaptcha = true;
}
// Remove the session hash if needed
if (request.getSession().getAttribute(RegistrationController.GRECAPTCHA_SESSION_ATTRIBUTE_NAME) != null) {
request.getSession().removeAttribute(RegistrationController.GRECAPTCHA_SESSION_ATTRIBUTE_NAME);
}
// Strip any html code from names before validating them
if (!PojoUtil.isEmpty(form.getFamilyNames())) {
form.getFamilyNames().setValue(OrcidStringUtils.stripHtml(form.getFamilyNames().getValue()));
}
if (!PojoUtil.isEmpty(form.getGivenNames())) {
form.getGivenNames().setValue(OrcidStringUtils.stripHtml(form.getGivenNames().getValue()));
}
// Check there are no errors
registrationController.validateRegistrationFields(request, form);
if (form.getErrors().isEmpty()) {
// Register user
try {
// Locale
Locale locale = RequestContextUtils.getLocale(request);
// Ip
String ip = OrcidRequestUtil.getIpAddress(request);
registrationController.createMinimalRegistration(request, form, usedCaptcha, locale, ip);
} catch (Exception e) {
requestInfoForm.getErrors().add(getMessage("register.error.generalError"));
return requestInfoForm;
}
// Authenticate user
String email = form.getEmail().getValue();
String password = form.getPassword().getValue();
Authentication auth = authenticateUser(request, email, password);
// Create authorization params
SimpleSessionStatus status = new SimpleSessionStatus();
Map<String, Object> model = new HashMap<String, Object>();
Map<String, String> params = new HashMap<String, String>();
Map<String, String> approvalParams = new HashMap<String, String>();
fillOauthParams(requestInfoForm, params, approvalParams, form.getPersistentTokenEnabled(), form.isEmailAccessAllowed());
// Authorize
try {
authorizationEndpoint.authorize(model, params, status, auth);
} catch (RedirectMismatchException rUriError) {
String redirectUri = this.getBaseUri() + REDIRECT_URI_ERROR;
// Set the client id
redirectUri = redirectUri.replace("{0}", requestInfoForm.getClientId());
// Set the response type if needed
if (!PojoUtil.isEmpty(requestInfoForm.getResponseType()))
redirectUri += "&response_type=" + requestInfoForm.getResponseType();
// Set the redirect uri
if (!PojoUtil.isEmpty(requestInfoForm.getRedirectUrl()))
redirectUri += "&redirect_uri=" + requestInfoForm.getRedirectUrl();
// Set the scope param
if (!PojoUtil.isEmpty(requestInfoForm.getScopesAsString()))
redirectUri += "&scope=" + requestInfoForm.getScopesAsString();
// Copy the state param if present
if (!PojoUtil.isEmpty(requestInfoForm.getStateParam()))
redirectUri += "&state=" + requestInfoForm.getStateParam();
requestInfoForm.setRedirectUrl(redirectUri);
LOGGER.info("OauthRegisterController being sent to client browser: " + requestInfoForm.getRedirectUrl());
return requestInfoForm;
}
Boolean isOauth2ScreensRequest = (Boolean) request.getSession().getAttribute(OrcidOauth2Constants.OAUTH_2SCREENS);
if (isOauth2ScreensRequest != null && isOauth2ScreensRequest) {
// Just redirect to the authorization screen
String queryString = (String) request.getSession().getAttribute(OrcidOauth2Constants.OAUTH_QUERY_STRING);
requestInfoForm.setRedirectUrl(orcidUrlManager.getBaseUrl() + "/oauth/authorize?" + queryString);
request.getSession().removeAttribute(OrcidOauth2Constants.OAUTH_2SCREENS);
} else {
// Approve
RedirectView view = (RedirectView) authorizationEndpoint.approveOrDeny(approvalParams, model, status, auth);
requestInfoForm.setRedirectUrl(view.getUrl());
}
}
} else {
requestInfoForm.setRedirectUrl(buildDenyRedirectUri(requestInfoForm.getRedirectUrl(), requestInfoForm.getStateParam()));
}
if (new HttpSessionRequestCache().getRequest(request, response) != null)
new HttpSessionRequestCache().removeRequest(request, response);
LOGGER.info("OauthConfirmAccessController form.getRedirectUri being sent to client browser: " + requestInfoForm.getRedirectUrl());
return requestInfoForm;
}
use of org.springframework.web.bind.support.SimpleSessionStatus in project Gemma by PavlidisLab.
the class PubMedQueryControllerTest method testOnSubmitInvalidValue.
@Test
public final void testOnSubmitInvalidValue() throws Exception {
MockHttpServletRequest request = this.newPost("/pubMedSearch.html");
ModelAndView mv = controller.onSubmit(request, new PubMedSearchCommand("bad idea"), new BeanPropertyBindingResult(new PubMedSearchCommand("bad idea"), "searchCriteria"), new SimpleSessionStatus());
Errors errors = (Errors) mv.getModel().get(BindingResult.MODEL_KEY_PREFIX + "searchCriteria");
assertTrue("Expected an error", errors != null);
assertEquals("bibRefSearch", mv.getViewName());
}
use of org.springframework.web.bind.support.SimpleSessionStatus in project Gemma by PavlidisLab.
the class PubMedQueryControllerTest method testOnSubmitAlreadyExists.
@Test
public final void testOnSubmitAlreadyExists() throws Exception {
// put it in the system.
this.getTestPersistentBibliographicReference("12299");
MockHttpServletRequest request = this.newPost("/pubMedSearch.html");
ModelAndView mv = controller.onSubmit(request, new PubMedSearchCommand("12299"), new BeanPropertyBindingResult(new PubMedSearchCommand("12299"), "searchCriteria"), new SimpleSessionStatus());
Errors errors = (Errors) mv.getModel().get(BindingResult.MODEL_KEY_PREFIX + "searchCriteria");
assertNull("Errors in model: " + errors, errors);
assertNotNull(mv.getModel().get("bibliographicReference"));
assertNotNull(request.getSession().getAttribute("messages"));
assertEquals("bibRefView", mv.getViewName());
}
Aggregations