Skip to content
Snippets Groups Projects
Commit 5ebc0c87 authored by Yannic Nevado Hidalgo's avatar Yannic Nevado Hidalgo
Browse files

add(routes): GET und POST

parent cddfd47a
No related branches found
No related tags found
No related merge requests found
...@@ -3,11 +3,11 @@ CREATE DATABASE "web-sec"; ...@@ -3,11 +3,11 @@ CREATE DATABASE "web-sec";
\c web-sec; \c web-sec;
CREATE TABLE "benutzer" ( CREATE TABLE "benutzer" (
customerID SERIAL NOT NULL, customerid SERIAL NOT NULL,
vorname text NOT NULL, vorname text NOT NULL,
nachname text NOT NULL, nachname text NOT NULL,
email varchar(255) NOT NULL, email varchar(255) NOT NULL,
PRIMARY KEY(customerID) PRIMARY KEY(customerid)
); );
INSERT INTO INSERT INTO
......
...@@ -53,11 +53,13 @@ Die Routen sind Passwort geschützt. ...@@ -53,11 +53,13 @@ Die Routen sind Passwort geschützt.
# passwort: admin # passwort: admin
---- ----
* Dies kann via `curl --user admin:admin -X GET http://localhost:5000/benutzer/1` getestet werden. Ohne Zugangsdaten bekommt man ein `Unauthorized Access` zurück.
=== GET === GET
* `http://localhost:5000/api/user/:customerID` * `http://localhost:5000/api/user/:customerID`
** Gibt die E-Mail Adresse eines Benutzers zurück ** Gibt die E-Mail Adresse eines Benutzers zurück
*** `curl --user admin:admin -X GET http://localhost:5000/benutzer/1`
=== POST === POST
...@@ -67,9 +69,19 @@ Die Routen sind Passwort geschützt. ...@@ -67,9 +69,19 @@ Die Routen sind Passwort geschützt.
[source,bash] [source,bash]
---- ----
{ {
"vorname":"max", # Benutzer1
"nachname":"mustermann", curl http://localhost:5000/benutzer \
"email":"max@mustermann.net" --user admin:admin \
-X POST \
-H "Content-Type: application/json" \
-d '{"vorname":"maxime", "nachname":"musterfrau", "email":"maxime@musterfrau.net"}'
# Benutzer2
curl http://localhost:5000/benutzer \
--user admin:admin \
-X POST \
-H "Content-Type: application/json" \
-d '{"vorname":"hans", "nachname":"meier", "email":"hans@meier.net"}'
} }
---- ----
...@@ -77,4 +89,6 @@ Die Routen sind Passwort geschützt. ...@@ -77,4 +89,6 @@ Die Routen sind Passwort geschützt.
=== DELETE === DELETE
* `http://localhost:5000/api/user/:customerID` * `http://localhost:5000/api/user/:customerID`
** Löscht einen Benutzer anhand seiner `customerID` ** Löscht einen Benutzer anhand seiner `customerID`
\ No newline at end of file *** `curl --user admin:admin -X DELETE http://localhost:5000/benutzer/1`
from flask_migrate import Migrate
from models import db, Benutzer
#from sqlalchemy import create_engine
from flask_httpauth import HTTPBasicAuth
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.security import generate_password_hash, check_password_hash
from flask import Flask, jsonify, request
from sqlalchemy import select
import json import json
from flask import Flask
auth = HTTPBasicAuth()
app = Flask(__name__) app = Flask(__name__)
autorisierte_benutzer = {
"admin": generate_password_hash("admin")
}
#engine = create_engine("postgresql://postgres:postgres@postgres:5432/web-sec",echo = True)
#conn = engine.connect()
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:postgres@postgres:5432/web-sec"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
migrate = Migrate(app, db)
@auth.verify_password
def verify_password(username, password):
if username in autorisierte_benutzer and \
check_password_hash(autorisierte_benutzer.get(username), password):
return username
@app.route('/') @app.route('/')
@auth.login_required
def index(): def index():
return json.dumps({'name': 'alice', return json.dumps({'Aufgabe1': 'Security of web applications'})
'email': 'alice@outlook.com'})
@app.route("/benutzer/<id>", methods=["GET"])
@auth.login_required
def get_email(id):
benutzer_email = db.session.query(Benutzer.email).filter(Benutzer.customerid == id).first()[0]
return jsonify(email=benutzer_email)
@app.route("/benutzer", methods=["POST"])
@auth.login_required
def create_user():
new_user = Benutzer(
vorname=request.json['vorname'],
nachname=request.json['nachname'],
email=request.json['email']
)
db.session.add(new_user)
db.session.commit()
return jsonify(benutzer=new_user)
@app.route("/benutzer/<id>", methods=["DELETE"])
@auth.login_required
def delete_user(id):
benutzer = db.session.query(Benutzer).filter(Benutzer.customerid == id).first()[0]
db.session.delete(benutzer)
db.session.commit()
if __name__ == "__main__": if __name__ == "__main__":
app.run() app.run(debug=True)
\ No newline at end of file \ No newline at end of file
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Benutzer(db.Model):
__tablename__ = 'benutzer'
customerid = db.Column(db.Integer, primary_key = True)
vorname = db.Column(db.String())
nachname = db.Column(db.Integer())
email = db.Column(db.String(20))
def __init__(self, vorname, nachname, email):
self.vorname = vorname
self.nachname = nachname
self.email = email
def __repr__(self):
return f"{self.customerID}:{self.vorname}:{self.nachname}:{self.email}"
\ No newline at end of file
...@@ -31,4 +31,8 @@ services: ...@@ -31,4 +31,8 @@ services:
- database - database
ports: ports:
- 5000:5000 - 5000:5000
environment:
FLASK_DEBUG: 1
volumes:
- "./app:/app"
\ No newline at end of file
Flask==2.0.3 Flask==2.0.3
\ No newline at end of file psycopg2==2.9.3
Flask-SQLAlchemy==2.5.1
Flask-Migrate==3.1.0
Flask-HTTPAuth==4.5.0
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment