/** * Send displaysTrainingView. * * @throws Exception the exception */ @Test public void displayUserPaymentsTest() throws Exception { // Charge csrf in pay member PayMember userFeeMember = userFeeMemberService.findByPayMemberIds(user.getId(), feeMember.getId()).get(0); mockMvc.perform(post("/userPayments/payMember/" + userFeeMember.getId()).locale(Locale.ENGLISH).session(defaultSession) .sessionAttr("_csrf", "csrf").param("payer_email", "email").param("payer_id", "id").param("payment_date", "10:10:10 Jun 10, 2015") .param("payment_status", "Completed").param("txn_id", "txn")).andExpect(view().name("redirect:/userPayments")); // Charge csrf in pay program PayProgram payProgram = payProgramService.findByPayProgramIds(program.getId(), feeProgram.getId()); mockMvc.perform(post("/userPayments/payProgram/" + payProgram.getId()).locale(Locale.ENGLISH).session(defaultSession) .sessionAttr("_csrf", "csrf").param("payer_email", "email").param("payer_id", "id").param("payment_date", "10:10:10 Jun 10, 2015") .param("payment_status", "Completed").param("txn_id", "txn")).andExpect(view().name("redirect:/userPayments")); CsrfToken token = new DefaultCsrfToken("headerName", "parameterName", "token"); try { mockMvc.perform(get("/userPayments").locale(Locale.ENGLISH).session(defaultSession).sessionAttr("_csrf", token)) .andExpect(view().name("userpayments/userpayments")).andExpect(content().string(containsString("<title>My payments</title>"))); } catch (Exception e) { // prueba } }
public void test_0() throws Exception { DefaultCsrfToken token = JSON.parseObject("{\"token\":\"xxx\",\"parameterName\":\"222\",\"headerName\":\"hhh\"}", DefaultCsrfToken.class); assertEquals("hhh", token.getHeaderName()); assertEquals("222", token.getParameterName()); assertEquals("xxx", token.getToken()); assertEquals("{\"headerName\":\"hhh\",\"parameterName\":\"222\",\"token\":\"xxx\"}", JSON.toJSONString(token)); }
/** * Gets the CSRF token from login html because the CSRF token endpoint needs * to be authenticated first. * * @param loginHtml The login page HTML which contains the csrf token. It is * assumed that the CSRF token is embedded on the page inside an input field * with name matching * {@link com.box.l10n.mojito.rest.resttemplate.FormLoginAuthenticationCsrfTokenInterceptor#CSRF_PARAM_NAME} * @return * @throws AuthenticationException */ protected CsrfToken getCsrfTokenFromLoginHtml(String loginHtml) throws AuthenticationException { Pattern pattern = Pattern.compile("CSRF_TOKEN = '(.*?)';"); Matcher matcher = pattern.matcher(loginHtml); if (matcher.find()) { String csrfTokenString = matcher.group(1); logger.debug("CSRF token from login html: {}", csrfTokenString); return new DefaultCsrfToken(CSRF_HEADER_NAME, CSRF_PARAM_NAME, csrfTokenString); } else { throw new SessionAuthenticationException("Could not find CSRF_TOKEN variable on login page"); } }
@Override public CsrfToken loadToken(HttpServletRequest request) { if (request.getCookies() != null) { for (Cookie cookie : request.getCookies()) { if (cookie != null && csrfCookieName.equals(cookie.getName())) { return new DefaultCsrfToken(csrfHeaderName, csrfParameterName, cookie.getValue()); } } } return null; }
@Override public CsrfToken loadToken(HttpServletRequest request) { Cookie cookie = WebUtils.getCookie(request, this.cookieName); if (cookie == null) { return null; } String token = cookie.getValue(); if (!StringUtils.hasLength(token)) { return null; } return new DefaultCsrfToken(this.headerName, this.parameterName, token); }
@Override public CsrfToken loadToken(HttpServletRequest request) { if (request.getCookies() != null) { for (Cookie cookie : request.getCookies()) { if (cookie != null && CSRF_COOKIE_AND_PARAMETER_NAME.equals(cookie.getName())) { return new DefaultCsrfToken(CSRF_HEADER_NAME, CSRF_COOKIE_AND_PARAMETER_NAME, cookie.getValue()); } } } return null; }
/** * Send account form. * * @throws Exception the exception */ @Test public void displaysaccountFormTest() throws Exception { CsrfToken token = new DefaultCsrfToken("headerName", "parameterName", "token"); mockMvc.perform(get("/accountList").locale(Locale.ENGLISH).session(defaultSession).sessionAttr("_csrf", token)) .andExpect(view().name("account/accountlist")) .andExpect(content().string(allOf(containsString("<title>Accounts</title>"), containsString("Account List</h1>")))); }
/** * Send displaysReportList. * * @throws Exception the exception */ @Test public void reportCreateTest() throws Exception { CsrfToken token = new DefaultCsrfToken("headerName", "parameterName", "token"); mockMvc.perform(get("/reportList/reportCreate").locale(Locale.ENGLISH) .session(defaultSession) .sessionAttr("parameterName", token) .sessionAttr("_csrf", token) .requestAttr(CsrfToken.class.getName(), token) .param("parameterName", "title")).andExpect(view().name("report/reportcreate")); }
@Test public void redirecionaParaAutenticacaoQuandoTokenCsrfÉInvalido() throws Exception { AccessDeniedException exception = new InvalidCsrfTokenException( new DefaultCsrfToken("header", "param", "token"), "actualToken" ); handler.handle(request, response, exception); assertThat(response.getRedirectedUrl(), is("/editar/autenticar?sessao")); }
public CsrfToken generateToken(HttpServletRequest request) { return new DefaultCsrfToken(Constants.ERRAI_CSRF_TOKEN_HEADER, Constants.ERRAI_CSRF_TOKEN_VAR, SecureHashUtil.nextSecureHash()); }
@Override public CsrfToken generateToken(HttpServletRequest request) { String tokenValue = new BigInteger(130, secureRandom).toString(32); // http://stackoverflow.com/a/41156 return new DefaultCsrfToken(csrfHeaderName, csrfParameterName, tokenValue); }
@Override public CsrfToken generateToken(HttpServletRequest request) { return new DefaultCsrfToken(this.headerName, this.parameterName, createNewToken()); }
@Override public CsrfToken generateToken(HttpServletRequest request) { String tokenValue = new BigInteger(130, secureRandom).toString(32); // http://stackoverflow.com/a/41156 return new DefaultCsrfToken(CSRF_HEADER_NAME, CSRF_COOKIE_AND_PARAMETER_NAME, tokenValue); }
@Override public CsrfToken generateToken(HttpServletRequest request) { return new DefaultCsrfToken(headerName, parameterName, defaultTestCsrfTokenValue); }
/** * Use the CSRF token endpoint to get the CSRF token corresponding to this * session * * @param csrfTokenUrl The full URL to which the CSRF token can be obtained * @return */ protected CsrfToken getCsrfTokenFromEndpoint(String csrfTokenUrl) { ResponseEntity<String> csrfTokenEntity = restTemplateForAuthenticationFlow.getForEntity(csrfTokenUrl, String.class, ""); logger.debug("CSRF token from {} is {}", csrfTokenUrl, csrfTokenEntity.getBody()); return new DefaultCsrfToken(CSRF_HEADER_NAME, CSRF_PARAM_NAME, csrfTokenEntity.getBody()); }
/** * Generates a CSRF token string. * @param request HTTP request * @return a new token */ public CsrfToken generateToken(HttpServletRequest request) { return new DefaultCsrfToken(headerName, parameterName, Utils.generateSecurityToken()); }