def __init__(self, myClient): self.auth = Authentication() self.credentials = self.auth.getCredentials(myClient) self.http = self.credentials.authorize(httplib2.Http()) self.service = discovery.build('calendar', 'v3', http=self.http) self.maxEvents = 1000 self.dt = datetime.datetime
def login(body: LoginUserBodyModel) -> LoginResponseModel: UserActions.is_user_exist(body.username, should_exist=True) hashed_password = UserActions.password_to_hash(body.password) user_in_db: UserDB = session.query(UserDB).filter( UserDB.name == body.username)[0] if user_in_db.password == hashed_password: return LoginResponseModel(Authorization=Authentication.create_jwt( JwtPayloadModel(username=body.username))) raise Forbidden(description="wrong password or username")
def signup(body: SignUpUserBodyModel) -> SignUpResponseModel: hashed_password = UserActions.password_to_hash(body.password) user = UserDB(name=body.username, password=hashed_password) UserActions.is_user_exist(user.name) session.add(user) token = Authentication.create_jwt( JwtPayloadModel(username=body.username)) session.commit() return SignUpResponseModel(Authorization=token)
def decorated_function(*args, **kwargs): token = request.headers.get("Authorization") if not token: raise Unauthorized(description="Authentication failed") try: user_name = Authentication.validate_jwt(jwt_token=token) kwargs.update({"username": user_name}) except: raise Unauthorized(description="Authentication failed") return f(*args, **kwargs)
class GoogleCalendar: def __init__(self, myClient): self.auth = Authentication() self.credentials = self.auth.getCredentials(myClient) self.http = self.credentials.authorize(httplib2.Http()) self.service = discovery.build('calendar', 'v3', http=self.http) self.maxEvents = 1000 self.dt = datetime.datetime def getLastNEvents(self, n): return self.extractNEventsByPeriod(n) def getEventsFromDateToDate(self, dateFrom, dateTo): return self.extractNEventsByPeriod(self.maxEvents, dateFrom, dateTo) def extractNEventsByPeriod(self, n=1000, dateFrom=None, dateTo=None): dateFrom = self.cleanStringDate(dateFrom) dateTo = self.cleanStringDate(dateTo) message = self.getCorrectMessage(dateFrom, dateTo, n) print(message) eventsResult = self.service.events().list( calendarId='primary', timeMin=dateFrom, timeMax=dateTo, maxResults=n, singleEvents=True, orderBy='startTime').execute() return eventsResult.get('items', []) def getCorrectMessage(self, dateFrom, dateTo, n): message = '' if n != 1000: message += f"Getting the upcoming {n} events" else: message += f"Getting the upcoming events" if dateFrom != None and dateTo != None: message += f" between {dateFrom} and {dateTo}" return message def cleanStringDate(self, date): if type(date) is str: date = self.dt.strptime(date, '%Y-%m-%d').isoformat() + 'Z' return date def printEvents(self, events): if not events: print('No upcoming events found.') for event in events: start = event['start'].get('dateTime', event['start'].get('date')) print(start, event['summary'])
def add_message(packet: DirectMessageDataModel): # get sender username from token sender_username = Authentication.validate_jwt(packet.token) # create full package to send and save in database data = MessagePacket(sender_username=sender_username, receiver_username=packet.receiver, content_type=packet.content_type, content=packet.content) message_in_db = ChatDb(sender_username=data.sender_username, receiver_username=data.receiver_username, content=data.content, content_type=data.content_type, is_seen=False, create_at=data.create_at) session.add(message_in_db) session.commit() data.id = message_in_db.id data.create_at = data.create_at.timestamp() return data
def decorated_function(*args, **kwargs): # data = Authentication.validate_jwt(args) # validate access if len(args) > 0: if "token" not in args[0]: raise ConnectionRefusedError("authentication failed") try: print(args[0]) user_name = Authentication.validate_jwt( json.loads(args[0])["token"]) kwargs.update({"username": user_name}) except Exception as ex: raise ConnectionRefusedError("authentication failed") else: raise ConnectionRefusedError("authentication failed") return f(*args, **kwargs)
def on_connect(self): data = Authentication.validate_jwt( request.args.get("token").encode('utf-8')) join_room(data, request.sid) emit("server_response", {"username": data, "sid": request.sid}) print(f'{data} connected')