"""
- `File`: password.py

- `Description`: Authentication challenge 3: rainbow table
"""


import webapp2
from google.appengine.ext.webapp import template
import os
import constants


class PasswordFileHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(
            '{user:mary, pw_hash:"990333"}'
        )

class PasswordLogin(webapp2.RequestHandler):
    def post(self):
        name = self.request.get('name')
        pw = self.request.get('pw')

        result = False
        if name == 'mary':
            if pw == 'M@rY12':
                result = True

        isLogin = True
        if result:
            isLogin = False

        self.response.write(
            template.render(
                os.path.join(constants.TPL_DIR, 'rainbowTable.tpl'),
                {
                    'breadcrumb': [{
                        'name': 'Home',
                        'href': '/',
                        'active': False,
                    }, {
                        'name': 'Authentication',
                        'href': '/authentication',
                        'active': False,
                    }, {
                        'name': 'Rainbow Table',
                        'href': '/authentication/rainbowTable',
                        'active': True,
                    }],
                    'isSignUp': False,
                    'isLogin': isLogin,
                    'isSucceeded': result,
                    'isStarted': True

                }
            )
        )


class RainbowTableHandler(webapp2.RequestHandler):

    def getBreadcrumbContext(self):
        return {
            'breadcrumb': [{
                'name': 'Home',
                'href': '/',
                'active': False,
            }, {
                'name': 'Authentication',
                'href': '/authentication',
                'active': False,
            }, {
                'name': 'Rainbow Table',
                'href': '/authentication/rainbowTable',
                'active': True,
            }],
        }
        
    def get(self):
        ctx = self.getBreadcrumbContext()
        ctx['isSucceeded'] = False
        self.response.write(
            template.render(
                os.path.join(constants.TPL_DIR, 'rainbowTable.tpl'),
                ctx
            )
        )

    def post(self):
        name = self.request.get('name')
        pw = self.request.get('pw')

        if name == 'mary' and pw == '900617':
            isSucceeded = True
        else:
            isSucceeded = False

        ctx = self.getBreadcrumbContext()
        ctx['isSucceeded'] = isSucceeded

        self.response.write(
            template.render(
                os.path.join(constants.TPL_DIR, 'rainbowTable.tpl'),
                ctx
            )
        )

@@ -2509,32 +2509,62 @@
 ucceeded = True%0A
+            userError = False%0A
         else:%0A  
@@ -2561,16 +2561,16 @@
   else:%0A
-
         
@@ -2584,32 +2584,61 @@
 ucceeded = False
+%0A            userError = True
 %0A%0A        ctx = 
@@ -2657,32 +2657,32 @@
 dcrumbContext()%0A
-
         ctx%5B'isS
@@ -2705,16 +2705,53 @@
 ucceeded
+%0A        ctx%5B'userError'%5D = userError
 %0A%0A      

Fix error msg in rainbow table task