Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/main/java/codeu/controller/ChangePasswordServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package codeu.controller;

import codeu.model.data.User;
import codeu.model.store.basic.MessageStore;
import codeu.model.store.basic.UserStore;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mindrot.jbcrypt.BCrypt;

/** Servlet class responsible for the change password page. */
public class ChangePasswordServlet extends HttpServlet {

/** Store class that gives access to Users and Messages. */
private UserStore userStore;

private MessageStore messageStore;

/**
* Set up state for handling password-changing-related requests. This method is only called when
* running in a server, not when running in a test.
*/
@Override
public void init() throws ServletException {
super.init();
setMessageStore(MessageStore.getInstance());
setUserStore(UserStore.getInstance());
}

/**
* Sets the MessageStore used by this servlet. This function provides a common setup method for
* use by the test framework or the servlet's init() function.
*/
void setMessageStore(MessageStore messageStore) {
this.messageStore = messageStore;
}

/**
* Sets the UserStore used by this servlet. This function provides a common setup method for use
* by the test framework or the servlet's init() function.
*/
void setUserStore(UserStore userStore) {
this.userStore = userStore;
}

/**
* This function fires when a user requests the /changepassword URL. It simply forwards
* the request to changepassword.jsp.
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
request.getRequestDispatcher("/WEB-INF/view/changepassword.jsp").forward(request, response);
}

/**
* This function fires when a user submits the change password form. It gets the
* username and password from the submitted form data, checks that they're valid, and
* either adds the user to the session so we know the user is logged in or shows an error
* to the user.
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String oldpassword = request.getParameter("oldpassword");
String username = (String) request.getSession().getAttribute("user");

if (userStore.isUserRegistered(username)) {
User user = userStore.getUser(username);
if (BCrypt.checkpw(oldpassword, user.getPassword())) {
if (request.getParameter("update") != null) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be "newpassword" instead "update"

String newPassword = request.getParameter("newpassword");
user.setPassword(newPassword);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you've to hash the password before setting it:
String passwordHash = BCrypt.hashpw(newPassword, BCrypt.gensalt());
user.setPassword(passwordHash);

userStore.addUser(user);
response.sendRedirect("/conversations");
}
} else {
request.setAttribute("error", "Invalid password.");
request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);
}
} else {
request.setAttribute("error", "No user logged in.");
request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ public List<Profile> loadProfiles() throws PersistentDataStoreException {

/** Write a User object to the Datastore service. */
public void writeThrough(User user) {
// Delete user in entity if already present
Filter idFilter = new FilterPredicate("uuid", FilterOperator.EQUAL, user.getId().toString());
Query query = new Query("chat-users").setFilter(idFilter);
PreparedQuery results = datastore.prepare(query);
for (Entity entity : results.asIterable()) {
datastore.delete(entity.getKey());
}
Entity userEntity = new Entity("chat-users");
userEntity.setProperty("uuid", user.getId().toString());
userEntity.setProperty("username", user.getName());
Expand Down
44 changes: 44 additions & 0 deletions src/main/webapp/WEB-INF/view/changepassword.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
<link rel="stylesheet" href="/css/main.css">
<style>
label {
display: inline-block;
width: 100px;
}
</style>
</head>
<body>

<nav>
<a id="navTitle" href="/">CodeU Chat App</a>
<a href="/conversations">Conversations</a>
<% if(request.getSession().getAttribute("user") != null){ %>
<a>Hello <%= request.getSession().getAttribute("user") %>!</a>
<a href="/changepassword">Change Password</a>
<% } else{ %>
<a href="/login">Login</a>
<a href="/register">Register</a>
<% } %>
<a href="/about.jsp">About</a>
</nav>

<div id="container">
<h1>Change Password</h1>
<% if(request.getAttribute("error") != null){ %>
<h2 style="color:red"><%= request.getAttribute("error") %></h2>
<% } %>
<form action="/changepassword" method="POST">
<label for="oldpassword">Old Password: </label>
<input type="password" name="oldpassword" id="oldpassword">
<br/>
<label for="newpassword">New Password: </labelpp>
<input type="password" name="newpassword" id="newpassword">
<br/><br/>
<button type="submit">Submit</button>
</form>
</div>
</body>
</html>
10 changes: 10 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
<url-pattern>/login</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>ChangePasswordServlet</servlet-name>
<servlet-class>codeu.controller.ChangePasswordServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ChangePasswordServlet</servlet-name>
<url-pattern>/changepassword</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>ConversationServlet</servlet-name>
<servlet-class>codeu.controller.ConversationServlet</servlet-class>
Expand Down