def confirm_email(self, request: Request, view: View, auth: Auth): """Confirm User email and show the correct response. Arguments: request {masonite.request.request} -- The Masonite request class. request {masonite.view.view} -- The Masonite view class. request {masonite.auth.auth} -- The Masonite Auth class. Returns: [type] -- [description] """ sign = Sign() token = sign.unsign(request.param('id')) if token is not None: tokenParts = token.split("::") if len(tokenParts) > 1: user = auth.auth_model.find(tokenParts[0]) if user.verified_at is None: timestamp = datetime.datetime.fromtimestamp(float(tokenParts[1])) now = datetime.datetime.now() timestamp_plus_10 = timestamp + datetime.timedelta(minutes=10) if now < timestamp_plus_10: user.verified_at = datetime.datetime.now() user.save() return view.render('auth/confirm', {'app': request.app().make('Application'), 'Auth': auth}) return view.render('auth/error', {'app': request.app().make('Application'), 'Auth': auth})
def confirm_email(self, request: Request, view: View, auth: Auth, inertia: InertiaResponse): sign = Sign() token = sign.unsign(request.param("id")) if token is not None: tokenParts = token.split("::") if len(tokenParts) > 1: user = auth.auth_model.find(tokenParts[0]) if user.verified_at is None: timestamp = datetime.datetime.fromtimestamp( float(tokenParts[1])) now = datetime.datetime.now() timestamp_plus_10 = timestamp + datetime.timedelta( minutes=10) if now < timestamp_plus_10: user.verified_at = datetime.datetime.now() user.save() request.session.flash( 'success', 'Your email has successfully been verified.') return request.redirect("/") request.session.flash( 'error', 'Something went wrong. We could not verify your email address.') return request.redirect("/")
def verify_email(self, mail_manager, request): mail = mail_manager.helper() sign = Sign() token = sign.sign('{0}::{1}'.format(self.id, time.time())) link = '{0}/email/verify/{1}'.format(request.environ['HTTP_HOST'], token) mail.to(self.email) \ .template('auth/verifymail', {'name': self.name, 'email': self.email, 'link': link}) \ .subject('Please Confirm Your Email').send()
def get_cookie(self, provided_cookie, decrypt=True): """Retrieve a specific cookie from the browser. Arguments: provided_cookie {string} -- Name of the cookie to retrieve Keyword Arguments: decrypt {bool} -- Whether Masonite should try to decrypt the cookie. This should only be True if the cookie was encrypted in the first place. (default: {True}) Returns: string|None -- Returns None if the cookie does not exist. """ if 'HTTP_COOKIE' in self.environ: grab_cookie = cookies.SimpleCookie(self.environ['HTTP_COOKIE']) if provided_cookie in grab_cookie: if decrypt: try: return Sign(self.encryption_key).unsign( grab_cookie[provided_cookie].value) except InvalidToken: self.delete_cookie(provided_cookie) return None return grab_cookie[provided_cookie].value return None
def cookie(self, key, value, encrypt=True, http_only="HttpOnly;", path='/', expires=''): """Set a cookie in the browser. Arguments: key {string} -- Name of the cookie you want set. value {string} -- Value of the cookie you want set. Keyword Arguments: encrypt {bool} -- Whether or not you want to encrypt the cookie (default: {True}) http_only {str} -- If the cookie is HttpOnly or not (default: {"HttpOnly;"}) path {str} -- The path of the cookie to be set to. (default: {'/'}) expires {string} -- When the cookie expires (5 minutes, 1 minute, 10 hours, etc) (default: {''}) Returns: self """ if encrypt: value = Sign(self.encryption_key).sign(value) else: value = value if expires: expires = "Expires={0};".format(cookie_expire_time(expires)) if not http_only: http_only = "" self.cookies.append( ('Set-Cookie', '{0}={1};{2} {3}Path={4}'.format( key, value, expires, http_only, path))) self.append_cookie(key, value) return self
def cookie(self, key, value, encrypt=True, http_only="HttpOnly;", path='/', expires=''): """ Sets a cookie in the browser """ if encrypt: value = Sign(self.encryption_key).sign(value) else: value = value if expires: expires = "Expires={0};".format(cookie_expire_time(expires)) if not http_only: http_only = "" self.cookies.append( ('Set-Cookie', '{0}={1};{2} {3}Path={4}'.format(key, value, expires, http_only, path))) self.append_cookie(key, value) return self
def verify_email(self, mail_manager, request): """Sends email for user verification Arguments: mail_manager {masonite.managers.MailManager} -- Masonite mail manager class. request {masonite.request.Request} -- Masonite request class. """ mail = mail_manager.helper() sign = Sign() token = sign.sign('{0}::{1}'.format(self.id, time.time())) link = '{0}/email/verify/{1}'.format(request.environ['HTTP_HOST'], token) mail.to(self.email) \ .template('auth/verifymail', {'name': self.name, 'email': self.email, 'link': link}) \ .subject('Please Confirm Your Email').send()
def verify_email(self, mail_manager, request): """Sends email for user verification Arguments: mail_manager {masonite.managers.MailManager} -- Masonite mail manager class. request {masonite.request.Request} -- Masonite request class. """ mail = mail_manager.helper() sign = Sign() token = sign.sign("{0}::{1}".format(self.id, time.time())) link = "{0}/users/verify-email/{1}".format( request.environ["HTTP_HOST"], token) mail.to(self.email).template( "users/email-verification-email", { "name": self.name, "email": self.email, "link": link }, ).subject("{0}: Confirm Your Email".format( config("application.name"))).send()
def get_cookie(self, provided_cookie, decrypt=True): """ Retrieves a specific cookie from the browser """ if 'HTTP_COOKIE' in self.environ: grab_cookie = cookies.SimpleCookie(self.environ['HTTP_COOKIE']) if provided_cookie in grab_cookie: if decrypt: try: return Sign(self.encryption_key).unsign( grab_cookie[provided_cookie].value) except InvalidToken: self.delete_cookie(provided_cookie) return None return grab_cookie[provided_cookie].value return None
def test_sign_without_specifying_key(self): s = Sign() assert s.key == 'NCTpkICMlTXie5te9nJniMj9aVbPM6lsjeq5iDZ0dqY='
def test_unsigning_without_value(self): s = Sign(self.secret_key) s.sign('value') assert s.unsign() == 'value'
def test_unsigning_returns_decrypted_value_without_parameter(self): s = Sign(self.secret_key) assert s.unsign(s.sign('value')) == 'value'
def test_sign_incorrect_padding(self): with pytest.raises(InvalidSecretKey): padded_secret_key = "AQAAQDhAAMAAQYS04MjQ2LWRkYzJkMmViYjQ2YQ===" s = Sign(padded_secret_key) assert s.sign('value')
def test_unsigning_returns_decrypted_value_with_parameter(): s = Sign(SECRET_KEY) assert s.unsign(s.sign('value')) == 'value'
def test_unsigning_without_value(): s = Sign(SECRET_KEY) s.sign('value') assert s.unsign() == 'value'