def load_user(user_id): app.logger.debug("Creating user object") m_user_id = appsession.get('users_id') m_users_name = appsession.get('users_name') m_users_roles = appsession.get('roles') m_email = appsession.get('email') user = User(m_user_id, m_users_name, m_email, m_users_roles) return user
def callback(): try: app.logger.debug("Github callback start") code = request.args.get("code") token_url, headers, body = client.prepare_token_request( "https://github.com/login/oauth/access_token", authorization_response=request.url, redirect_url=request.base_url, code=code, ) token_response = requests.post( token_url, headers=headers, data=body, auth=(CLIENT_ID, CLIENT_SECRET), ) # Parse the tokens! params = client.parse_request_body_response(token_response.text) uri, headers, body = client.add_token( "https://api.github.com/user") userinfo_response = requests.get(uri, headers=headers, data=body) app.logger.debug("Github callback succeess") users_name = userinfo_response.json()["login"] users_id = userinfo_response.json()["id"] email = userinfo_response.json()["email"] user = User(users_id, users_name, email, ROLES['reader']) appsession['users_id'] = users_id appsession['users_name'] = users_name appsession['email'] = email if users_name == WRITER_ROLE: user.access = ROLES['writer'] else: user.access = ROLES['reader'] app.logger.info("Logged in user %s", users_name) app.logger.info("Logged in user email %s", email) app.logger.info("Logged in user role %s", user.access) appsession['roles'] = user.access login_user(user) return redirect(url_for('index')) except Exception as e: app.logger.error(str(e)) return "Could not authenticate user."
def get_user_by_username(self, username): print(f"Looking for user with username {username}") user_object = self.database.users.find_one({"_id": username}) if not user_object: print("Didn't find the user") return None print("Found the user, returning user object") return User(user_object["_id"], user_object["username"], user_object["role"])
def add_user(self, username, role): new_user = {"username": username, "role": role} user= self.database.users.insert_one(new_user) print(f"User {user} added successfully with {user.inserted_id} as the ID and {role} as the role") return User(user.inserted_id, username, role)
def get_all_users(self): users = [] print("Getting the users from the mongo db") user_objects = self.database.users.find({}) for user_object in user_objects: user = User(user_object["_id"],user_object["username"], user_object["role"]) users.append(user) return users
def login_callback(): code = request.args.get("code") post_token_request = client.prepare_token_request( "https://github.com/login/oauth/access_token", state="todoapp", client_id=oauth_client_id, client_secret=oauth_client_secret, code=code) (post_token_url, post_token_headers, post_token_body) = post_token_request post_token_headers["Accept"] = "application/json" token_response = requests.post(post_token_url, data=post_token_body, headers=post_token_headers) client.parse_request_body_response(token_response.text) (get_user_url, get_user_headers, _) = client.add_token("https://api.github.com/user") user_response = requests.get(get_user_url, headers=get_user_headers).json() user_id = user_response["id"] user = User(id=user_response["id"], role=user_id_to_role.get(user_id, Role.READER)) login_user(user) return redirect('/')
def load_user(user_id): return User(id=user_id, role=user_id_to_role.get(str(user_id), Role.READER))