-
Notifications
You must be signed in to change notification settings - Fork 3
Create servlet to change password #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SophiaJefferson
wants to merge
5
commits into
milestone_2
Choose a base branch
from
change_password2
base: milestone_2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c2beacb
Interface to change password
SophiaJefferson 9843cf5
Added servlet mappings
SophiaJefferson c49bbfb
Merge branch 'milestone_2' of https://github.com/gauravmishra/CodeU-S…
SophiaJefferson 6af89ce
Servlet to change password
SophiaJefferson ac9a1b6
password persisting changes!
SophiaJefferson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/main/java/codeu/controller/ChangePasswordServlet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| String newPassword = request.getParameter("newpassword"); | ||
| user.setPassword(newPassword); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you've to hash the password before setting it: |
||
| 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); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"