-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: uses authentication on main route
- Loading branch information
1 parent
7a8fe11
commit 91b3efd
Showing
3 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from library.server import AUTH_USERNAME, AUTH_PASSWORD | ||
from fastapi import Depends | ||
from fastapi.security import HTTPBasic, HTTPBasicCredentials | ||
import secrets | ||
|
||
security = HTTPBasic() | ||
|
||
|
||
def checkCorrectCredentials(credentials: HTTPBasicCredentials = Depends(security)): | ||
""" | ||
A simple function that compares the passed credentials to the known correct credentials. Documentation and code from https://fastapi.tiangolo.com/advanced/security/http-basic-auth/#check-the-username | ||
Requires: | ||
- credentials: credentials passed from the request. | ||
Returns: | ||
- boolean: the boolean determines if the credentials match. | ||
""" | ||
|
||
|
||
current_username_bytes = credentials.username.encode("utf8") | ||
correct_username_bytes = AUTH_USERNAME.encode("utf-8") | ||
is_correct_username = secrets.compare_digest( | ||
current_username_bytes, correct_username_bytes | ||
) | ||
current_password_bytes = credentials.password.encode("utf8") | ||
correct_password_bytes = AUTH_PASSWORD.encode("utf-8") | ||
is_correct_password = secrets.compare_digest( | ||
current_password_bytes, correct_password_bytes | ||
) | ||
if not (is_correct_username and is_correct_password): | ||
return False | ||
return True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from library.tinfoil import errorMessage | ||
|
||
def checkAllowed(authenticated: bool, switch_uid: str): | ||
""" | ||
Checks if a user should be allowed to finish the request, otherwise returns an error message. | ||
Requires: | ||
- authenticated: a boolean which tells the server if the user is authenticated or not. | ||
- switch_uid: a string which either contains a uid or not. If no UID then a user is not using a switch. Also has the ability to check if the switch UID matches the required UID. | ||
Returns: | ||
- boolean, dict: the boolean determines if the user is allowed past, the dict gives the errorMessage. | ||
""" | ||
|
||
|
||
if not authenticated: | ||
return False, errorMessage("Your given credentials are incorrect. Please try again.", error_code="BAD_TOKEN") | ||
if not switch_uid: | ||
return False, errorMessage("Please use your Nintendo Switch using Tinfoil to access this server.", error_code="INVALID_DEVICE") | ||
|
||
# TODO: allow passing in a switch_uid to compare with | ||
|
||
return True, None | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters