Repository: jupyterhub/nativeauthenticator

Issue:
Add endpoint to change password
Similar to was was done [here](https://github.com/jupyterhub/firstuseauthenticator/pull/8)

Discussion:

Patch:
diff --git a/docs/index.rst b/docs/index.rst
index ce1b415..3d2a5de 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -71,3 +71,8 @@ Authorize new users
 
 To authorize new users to enter the system or to manage those that already have access to the system you can go to `<ip:port>/hub/authorize`. 
 
+
+Change password
+---------------
+
+Users that are logged in the system can easily change their password by going to: `/hub/change-password`.
diff --git a/nativeauthenticator/handlers.py b/nativeauthenticator/handlers.py
index eebdd83..66803e0 100644
--- a/nativeauthenticator/handlers.py
+++ b/nativeauthenticator/handlers.py
@@ -3,6 +3,8 @@ from jinja2 import ChoiceLoader, FileSystemLoader
 from jupyterhub.handlers import BaseHandler
 from jupyterhub.utils import admin_only
 
+from tornado import web
+
 from .orm import UserInfo
 
 TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates')
@@ -66,3 +68,25 @@ class ChangeAuthorizationHandler(LocalBase):
     async def get(self, slug):
         UserInfo.change_authorization(self.db, slug)
         self.redirect('/authorize')
+
+
+class ChangePasswordHandler(LocalBase):
+    """Render the reset password page."""
+
+    @web.authenticated
+    async def get(self):
+        self._register_template_path()
+        html = self.render_template('change-password.html')
+        self.finish(html)
+
+    @web.authenticated
+    async def post(self):
+        user = self.get_current_user()
+        new_password = self.get_body_argument('password', strip=False)
+        self.authenticator.change_password(user.name, new_password)
+
+        html = self.render_template(
+            'change-password.html',
+            result_message='Your password has been changed successfully',
+        )
+        self.finish(html)
diff --git a/nativeauthenticator/nativeauthenticator.py b/nativeauthenticator/nativeauthenticator.py
index d454a33..985b6bb 100644
--- a/nativeauthenticator/nativeauthenticator.py
+++ b/nativeauthenticator/nativeauthenticator.py
@@ -8,7 +8,7 @@ from tornado import gen
 from traitlets import Bool, Integer
 
 from .handlers import (AuthorizationHandler, ChangeAuthorizationHandler,
-                       SignUpHandler)
+                       ChangePasswordHandler, SignUpHandler)
 from .orm import UserInfo
 
 
@@ -139,10 +139,16 @@ class NativeAuthenticator(Authenticator):
         self.db.add(user_info)
         return user_info
 
+    def change_password(self, username, new_password):
+        user = UserInfo.find(self.db, username)
+        user.password = bcrypt.hashpw(new_password.encode(), bcrypt.gensalt())
+        self.db.commit()
+
     def get_handlers(self, app):
         native_handlers = [
             (r'/signup', SignUpHandler),
             (r'/authorize', AuthorizationHandler),
-            (r'/authorize/([^/]*)', ChangeAuthorizationHandler)
+            (r'/authorize/([^/]*)', ChangeAuthorizationHandler),
+            (r'/change-password', ChangePasswordHandler),
         ]
         return super().get_handlers(app) + native_handlers
diff --git a/nativeauthenticator/templates/change-password.html b/nativeauthenticator/templates/change-password.html
new file mode 100644
index 0000000..3444f8b
--- /dev/null
+++ b/nativeauthenticator/templates/change-password.html
@@ -0,0 +1,62 @@
+{% extends "page.html" %}
+{% block main %}
+
+<script type="text/javascript">
+document.addEventListener('DOMContentLoaded', function() {
+    var button = document.getElementById('eye');
+    button.addEventListener("click", function(e){
+        var pwd = document.getElementById("pwd");
+        if(pwd.getAttribute("type")=="password"){
+            pwd.setAttribute("type","text");
+            button.textContent = "🔑";
+        } else {
+            pwd.setAttribute("type","password");
+            button.textContent = "👁";
+        }
+    });
+});
+</script>
+
+  <div class="container">
+    <form action="{{post_url}}" method="post" role="form">
+      <h2>
+      Change Password
+      </h2>
+      <div>
+        <div class="form-group">
+          <label for='password_input'>New password:</label>
+          <div class="input-group">
+            <input
+            type="password"
+            class="form-control"
+            name="password"
+            id="pwd"
+            tabindex="2"
+            />
+            <span class="input-group-addon">
+              <button style="border:0;" type="button" id="eye">
+                👁
+              </button>
+            </span>
+          </div>
+          </div>
+        <div class="form-group">
+          <input
+          type="submit"
+          id="signup_submit"
+          class='btn btn-jupyter'
+          value='Change Password'
+          tabindex="3"
+          />
+        </div>
+      </div>
+    </form>
+
+    {% if result_message %}
+      <div class="alert alert-success" role="alert">{{result_message}}</div>
+    {% endif %}
+    
+  </div>
+
+
+{% endblock %}


Test patch:
diff --git a/nativeauthenticator/tests/test_authenticator.py b/nativeauthenticator/tests/test_authenticator.py
index f3b34e0..c67d3f6 100644
--- a/nativeauthenticator/tests/test_authenticator.py
+++ b/nativeauthenticator/tests/test_authenticator.py
@@ -73,6 +73,7 @@ async def test_handlers(app):
     handlers = auth.get_handlers(app)
     assert handlers[1][0] == '/signup'
     assert handlers[2][0] == '/authorize'
+    assert handlers[4][0] == '/change-password'
 
 
 async def test_add_new_attempt_of_login(tmpcwd, app):
@@ -124,3 +125,12 @@ async def test_authentication_with_exceed_atempts_of_login(tmpcwd, app):
     time.sleep(12)
     response = await auth.authenticate(app, infos)
     assert response
+
+
+async def test_change_password(tmpcwd, app):
+    auth = NativeAuthenticator(db=app.db)
+    user = auth.get_or_create_user('johnsnow', 'password')
+    assert user.is_valid_password('password')
+    auth.change_password('johnsnow', 'newpassword')
+    assert not user.is_valid_password('password')
+    assert user.is_valid_password('newpassword')