Ejemplo n.º 1
0
 def is_token(self, token: str) -> bool:
     """
     Checks if token exists and is valid
     """
     if token and self.possible_token(token):
         email = self.keys.find_one(token=str(token)).get('email')
         if check_choate_email(email) and self.check_token(email, token):
             return True
     return False
def get_token(request):
    email, firstname, lastname = check_login(request)
    if email and firstname and lastname and check_choate_email(email):
        authentication = Auth()
        authentication.init_db_connection()
        token = authentication.fetch_token(email)
        authentication.end_db_connection()
        return token
    return False
Ejemplo n.º 3
0
 def check_token(self, email: str, token: str) -> bool:
     """
     Checks if token matches expected value
     """
     if check_choate_email(email) and self.possible_token(token):
         if self.keys.find_one(email=str(email)):
             expected_token = self.keys.find_one(
                 email=str(email)).get('token')
             if secrets.compare_digest(str(expected_token), str(token)):
                 return True
     return False
 def is_token(self, token: str) -> bool:
     """
     Checks if token exists and is valid
     """
     if token and self.possible_token(token):
         db_resp = self.db['auth'].find_one(token=str(token))
         if db_resp:
             email = db_resp.get('email')
             if check_choate_email(email):
                 return True
     return False
Ejemplo n.º 5
0
    def create_token(self, email: str) -> str:
        """
        Creates token. If creation was successful, return token. If not, return False
        """
        if check_choate_email(email):
            user = {}
            user['email'] = str(email)

            token = secrets.token_hex(16)
            user['token'] = token
            self.keys.upsert(user, ['email'])

            if self.check_token(email, token):
                return token

        return False
def get_profile(attempt=0):
    """
    Checks and sanitizes email. 
    Returns false if not logged in or not choate email.
    """
    # return "*****@*****.**", "Fan Max"
    # return "*****@*****.**", "Ethan", "Chapman"

    if attempt <= 0:
        try:
            if google.authorized:
                resp = google.get("/oauth2/v1/userinfo")
                if resp.ok and resp.text:
                    response = resp.json()
                    if response.get("verified_email") == True and response.get(
                            "hd") == "choate.edu":
                        email = str(response.get("email"))
                        first_name = str(response.get('given_name'))
                        last_name = str(response.get('family_name'))

                        if check_choate_email(email):
                            log_info("Profile received successfully",
                                     "[" + first_name + " " + last_name + "] ")
                            return email, first_name, last_name
                    else:
                        log_error("Profile retrieval failed with response " +
                                  str(response) + ", attempt" +
                                  str(attempt))  # log next
        except oauthlib.oauth2.rfc6749.errors.InvalidClientIdError:
            session.clear()
            log_info(
                "Not Google authorized and InvalidClientIdError, attempt:" +
                str(attempt))  # log next
            return get_profile(attempt=attempt + 1)
        except oauthlib.oauth2.rfc6749.errors.TokenExpiredError:
            session.clear()
            log_info("Not Google authorized and TokenExpiredError, attempt:" +
                     str(attempt))  # log next
            return get_profile(attempt=attempt + 1)

        log_info("Not Google authorized, attempt: " + str(attempt))  # log next
        return False, False, False
    else:
        log_info("Attempts exhausted: " + str(attempt))  # log next
        return False, False, False