|
@ -1,13 +1,14 @@ |
|
|
#Authors: Oulis Evangelos, Oulis Nikolaos, Drosos Katsibras |
|
|
#Authors: Oulis Evangelos, Oulis Nikolaos, Drosos Katsibras |
|
|
#=================================================================== |
|
|
#=================================================================== |
|
|
# using flask restful |
|
|
# using flask restful |
|
|
from flask import Flask, request, jsonify, session |
|
|
from flask import Flask, request, jsonify |
|
|
from flask_restful import Resource, Api |
|
|
from flask_restful import Resource, Api |
|
|
from json import dumps |
|
|
from json import dumps |
|
|
import json |
|
|
import json |
|
|
from flask_cors import CORS |
|
|
from flask_cors import CORS |
|
|
import mysql.connector |
|
|
import mysql.connector |
|
|
import os |
|
|
from base64 import b64encode |
|
|
|
|
|
from os import urandom |
|
|
|
|
|
|
|
|
# ================================================================== |
|
|
# ================================================================== |
|
|
# ================================================================== |
|
|
# ================================================================== |
|
@ -16,8 +17,8 @@ import os |
|
|
app = Flask(__name__) |
|
|
app = Flask(__name__) |
|
|
CORS(app) |
|
|
CORS(app) |
|
|
|
|
|
|
|
|
# create the secret key for session |
|
|
# Session list |
|
|
app.secret_key = os.urandom(24) |
|
|
sessions = [] |
|
|
|
|
|
|
|
|
# creating an API object |
|
|
# creating an API object |
|
|
api = Api(app) |
|
|
api = Api(app) |
|
@ -65,10 +66,13 @@ def isMember(username, password): |
|
|
|
|
|
|
|
|
# Function that return if the requested user is authenticated |
|
|
# Function that return if the requested user is authenticated |
|
|
# or not (True / False). |
|
|
# or not (True / False). |
|
|
def isAuthenticated(): |
|
|
def isAuthenticated(data): |
|
|
if 'device_id' in session: |
|
|
try: |
|
|
return True |
|
|
if data['cookie'] in sessions: |
|
|
else: |
|
|
return True |
|
|
|
|
|
else: |
|
|
|
|
|
return False |
|
|
|
|
|
except KeyError as e: |
|
|
return False |
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -77,16 +81,19 @@ def isAuthenticated(): |
|
|
# the get, post methods correspond to get and post requests |
|
|
# the get, post methods correspond to get and post requests |
|
|
# they are automatically mapped by flask_restful. |
|
|
# they are automatically mapped by flask_restful. |
|
|
# other methods include put, delete, etc. |
|
|
# other methods include put, delete, etc. |
|
|
|
|
|
|
|
|
|
|
|
# Resource that returns th whole parking status in a JSON |
|
|
class Parking(Resource): |
|
|
class Parking(Resource): |
|
|
def get(self): |
|
|
def get(self): |
|
|
parks = None |
|
|
parks = None |
|
|
try: |
|
|
try: |
|
|
parks = getParkings() |
|
|
parks = getParkings() |
|
|
except mysql.connector.errors.DatabaseError as e: |
|
|
except (mysql.connector.errors.DatabaseError, mysql.connector.errors.InterfaceError) as e: |
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
|
|
|
|
|
|
return parks, 200 |
|
|
return parks, 200 |
|
|
|
|
|
|
|
|
|
|
|
# Update parking status resource from authenticated only Node |
|
|
class ParkingStatus(Resource): |
|
|
class ParkingStatus(Resource): |
|
|
def get(self): |
|
|
def get(self): |
|
|
return """<html> |
|
|
return """<html> |
|
@ -94,10 +101,9 @@ class ParkingStatus(Resource): |
|
|
<body><h1>Not get at '/parkingStatus'.</h1></body> |
|
|
<body><h1>Not get at '/parkingStatus'.</h1></body> |
|
|
</html>""" |
|
|
</html>""" |
|
|
def post(self): |
|
|
def post(self): |
|
|
if isAuthenticated(): |
|
|
# Gets the data into as a JSON Object from HTTP request. |
|
|
# Gets the data into as a JSON Object from HTTP request. |
|
|
data = json.loads(request.data) |
|
|
data = json.loads(request.data) |
|
|
if isAuthenticated(data): |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
# SQL get all Parking places status. |
|
|
# SQL get all Parking places status. |
|
|
parks = getParkings() |
|
|
parks = getParkings() |
|
@ -138,13 +144,14 @@ class ParkingStatus(Resource): |
|
|
myCursor.execute("UPDATE PARKING SET PARKING_STATUS=%s WHERE PARKING_CODE=%s", values) |
|
|
myCursor.execute("UPDATE PARKING SET PARKING_STATUS=%s WHERE PARKING_CODE=%s", values) |
|
|
mydb.commit() |
|
|
mydb.commit() |
|
|
parks = getParkings() |
|
|
parks = getParkings() |
|
|
except mysql.connector.errors.DatabaseError as e: |
|
|
except (mysql.connector.errors.DatabaseError, mysql.connector.errors.InterfaceError) as e: |
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
|
|
|
|
|
|
return currentParking, 201 |
|
|
return currentParking, 201 |
|
|
else: |
|
|
else: |
|
|
return "Error! You aren't authenticated. [POST] /authenticate first.", 403 |
|
|
return "Error! You aren't authenticated. [POST] /authenticate first.", 403 |
|
|
|
|
|
|
|
|
|
|
|
# Authentication resource. |
|
|
class Authenticate(Resource): |
|
|
class Authenticate(Resource): |
|
|
def post(self): |
|
|
def post(self): |
|
|
try: |
|
|
try: |
|
@ -155,22 +162,28 @@ class Authenticate(Resource): |
|
|
isValid = isMember(data['username'], data['password']) |
|
|
isValid = isMember(data['username'], data['password']) |
|
|
|
|
|
|
|
|
if isValid: |
|
|
if isValid: |
|
|
session['device_id'] = data['device'] |
|
|
session_key = str(b64encode(urandom(32)).decode('utf-8')) |
|
|
|
|
|
|
|
|
|
|
|
# Send the cookie value back to clinet. |
|
|
|
|
|
session = {"cookie": session_key} |
|
|
|
|
|
sessions.append(session_key) |
|
|
|
|
|
return session, 200 |
|
|
else: |
|
|
else: |
|
|
return "Not Authenticatiove device", 403 |
|
|
return "Not Authenticative device", 403 |
|
|
else: |
|
|
else: |
|
|
return "Error authentication", 403 |
|
|
return "Error authentication", 403 |
|
|
except mysql.connector.errors.DatabaseError as e: |
|
|
except (mysql.connector.errors.DatabaseError, mysql.connector.errors.InterfaceError) as e: |
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
mydb.reconnect(attempts=1, delay=0) |
|
|
|
|
|
|
|
|
# ================================================================== |
|
|
# ================================================================== |
|
|
# adding the defined resources along with their corresponding urls to REST APIs |
|
|
# matches the defined resources to their corresponding urls to REST APIs |
|
|
api.add_resource(Parking, '/') |
|
|
api.add_resource(Parking, '/') |
|
|
api.add_resource(ParkingStatus, '/parkingStatus') |
|
|
api.add_resource(ParkingStatus, '/parkingStatus') |
|
|
api.add_resource(Authenticate, '/authenticate') |
|
|
api.add_resource(Authenticate, '/authenticate') |
|
|
|
|
|
|
|
|
# ================================================================== |
|
|
# ================================================================== |
|
|
# driver function |
|
|
# ===========================MAIN CLASS============================= |
|
|
|
|
|
# driver function "Main Class" |
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
app.run( |
|
|
app.run( |
|
|
debug=True, |
|
|
debug=True, |
|
|