Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TDDD97-Webprog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Oliver Green
TDDD97-Webprog
Commits
807741fc
Commit
807741fc
authored
2 years ago
by
Gustav Elmqvist
Browse files
Options
Downloads
Patches
Plain Diff
backed signup and signin progress
parent
5dd9af7f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+3
-2
3 additions, 2 deletions
.gitignore
lab2/database_helper.py
+10
-6
10 additions, 6 deletions
lab2/database_helper.py
lab2/schema.sql
+0
-5
0 additions, 5 deletions
lab2/schema.sql
lab2/server.py
+46
-4
46 additions, 4 deletions
lab2/server.py
with
59 additions
and
17 deletions
.gitignore
+
3
−
2
View file @
807741fc
lab2/venv/
database.db
\ No newline at end of file
database.db
__pycache__
venv
This diff is collapsed.
Click to expand it.
lab2/database_helper.py
+
10
−
6
View file @
807741fc
...
...
@@ -4,24 +4,28 @@ con = sqlite3.connect("database.db")
cur
=
con
.
cursor
()
def
get_token_from_email
(
email
):
res
=
cur
.
execute
(
f
"
SELECT token FROM logged_in_users WHERE email=
'
{
email
}
'"
)
def
get_token
(
email
):
res
=
cur
.
execute
(
f
"
SELECT token FROM logged_in_users WHERE email=?
"
,
(
email
,))
return
res
.
fetchone
()
def
get_password
(
email
):
res
=
cur
.
execute
(
f
"
SELECT password_hash FROM user_data WHERE email=
'
{
email
}
'"
)
res
=
cur
.
execute
(
"
SELECT password_hash FROM user_data WHERE email=
?
"
,
(
email
,)
)
return
res
.
fetchone
()
def
update_logged_in_users
(
email
,
token
):
cur
.
execute
(
f
"
Insert INTO logged_in_users
"
)
cur
.
execute
(
"
Insert INTO logged_in_users VALUES (?,?)
"
,
(
email
,
token
))
def
get_user_data
(
email
):
data
=
cur
.
execute
(
f
"
SELECT * FROM user_data WHERE email=
'
{
email
}
'"
)
data
=
cur
.
execute
(
"
SELECT * FROM user_data WHERE email=
?
"
,
(
email
,)
)
return
data
.
fetchall
()
def
create_user
(
email
,
pw_hash
,
fname
,
lname
,
gender
,
city
,
country
):
cur
.
execute
(
f
"
Insert INTO user_data VALUES (?,?,?,?,?,?,?)
"
,
(
email
,
pw_hash
,
fname
,
lname
,
gender
,
city
,
country
))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
lab2/schema.sql
+
0
−
5
View file @
807741fc
...
...
@@ -5,11 +5,6 @@ CREATE TABLE "logged_in_users" (
PRIMARY
KEY
(
"email"
)
);
CREATE
TABLE
"all_users"
(
"email"
TEXT
,
PRIMARY
KEY
(
"email"
)
);
CREATE
TABLE
"user_data"
(
"email"
TEXT
,
"password_hash"
TEXT
,
...
...
This diff is collapsed.
Click to expand it.
lab2/server.py
+
46
−
4
View file @
807741fc
import
random
import
hashlib
import
re
from
flask
import
Flask
from
flask
import
Flask
,
request
import
database_helper
as
dbh
...
...
@@ -14,11 +15,28 @@ def index():
return
'
Hello world!
'
@app.route
(
'
/signin
'
)
@app.route
(
'
/signin
'
,
methods
=
[
'
POST
'
]
)
def
sign_in
(
email
=
'
test@gmail.com
'
,
password
=
'
123123123
'
):
"""
Authenticate the username by the provided password.
"""
args
=
request
.
get_json
()
if
set
(
args
)
!=
{
'
email
'
,
'
password
'
,
'
firstname
'
,
'
familyname
'
,
'
gender
'
,
'
city
'
,
'
country
'
}:
return
{
"
success
"
:
"
false
"
,
"
message
"
:
"
Form data missing or incorrect type.
"
}
if
re
.
fullmatch
(
r
'
\w+@\w+.\w+
'
,
args
[
'
email
'
])
is
None
:
return
False
if
len
(
args
[
'
password
'
])
<
8
:
return
False
email
=
args
[
'
email
'
]
password
=
args
[
'
password
'
]
hashed_password
=
hashlib
.
sha256
((
password
+
email
).
encode
()).
hexdigest
()
database_password
=
dbh
.
get_password
(
email
)
...
...
@@ -34,11 +52,35 @@ def sign_in(email='test@gmail.com', password='123123123'):
return
{
"
success
"
:
"
false
"
,
"
message
"
:
"
Wrong username or password.
"
}
@app.route
(
'
/signup
'
)
def
sign_up
(
jsonObj
):
@app.route
(
'
/signup
'
,
methods
=
[
'
POST
'
]
)
def
sign_up
():
"""
Register a user in the database.
"""
args
=
request
.
get_json
()
if
dbh
.
get_user_data
(
args
[
'
email
'
])
is
not
None
:
return
{
"
success
"
:
"
false
"
,
"
message
"
:
"
User already exists.
"
}
pw_hash
=
hashlib
.
sha256
((
args
[
'
password
'
]
+
args
[
'
email
'
]).
encode
()).
hexdigest
()
dbh
.
create_user
(
args
[
'
email
'
],
pw_hash
,
args
[
'
firstname
'
],
args
[
'
lastname
'
],
args
[
'
gender
'
],
args
[
'
city
'
],
args
[
'
country
'
],
)
return
{
"
success
"
:
"
true
"
,
"
message
"
:
"
Successfully created a new user.
"
};
}
else
{
}
}
else
{
}
pass
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment