-
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.
Merge branch 'main' into feature/page/landing
- Loading branch information
Showing
19 changed files
with
367 additions
and
40 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,33 @@ | ||
name: Build Frontend | ||
on: | ||
push: | ||
paths: | ||
- 'backend/**' | ||
- '.github/workflows/**' | ||
branches: | ||
- main | ||
|
||
pull_request: | ||
paths: | ||
- 'backend/**' | ||
- '.github/workflows/**' | ||
types: | ||
- opened | ||
- synchronize | ||
- reopened | ||
|
||
jobs: | ||
sonarcloud: | ||
name: SonarCloud Backend | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis | ||
- name: SonarCloud Scan | ||
uses: SonarSource/sonarcloud-github-action@master | ||
with: | ||
projectBaseDir: backend | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN_BACKEND }} |
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,33 @@ | ||
name: Build Frontend | ||
on: | ||
push: | ||
paths: | ||
- 'frontend/**' | ||
- '.github/workflows/**' | ||
branches: | ||
- main | ||
|
||
pull_request: | ||
paths: | ||
- 'frontend/**' | ||
- '.github/workflows/**' | ||
types: | ||
- opened | ||
- synchronize | ||
- reopened | ||
|
||
jobs: | ||
sonarcloud: | ||
name: SonarCloud Frontend | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis | ||
- name: SonarCloud Scan | ||
uses: SonarSource/sonarcloud-github-action@master | ||
with: | ||
projectBaseDir: frontend | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN_FRONTEND }} |
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,62 @@ | ||
import os | ||
from hashlib import sha256 | ||
import hmac | ||
import time | ||
import base64 | ||
|
||
|
||
validity = 3600 # seconds (int) | ||
|
||
k = os.urandom(32) | ||
|
||
|
||
def sign(data: str) -> str: | ||
hmac_sha256 = hmac.new(k, digestmod=sha256) | ||
hmac_sha256.update(data.encode()) | ||
t = int(time.time()) | ||
expiration = t + validity | ||
expiration = expiration.to_bytes(4, byteorder='big') | ||
hmac_sha256.update(expiration) | ||
hash = hmac_sha256.digest() | ||
# xor the first 16 bytes with the last 16 bytes | ||
hash = bytes([a ^ b for a, b in zip(hash[:16], hash[16:])]) | ||
# base85 encode the hash | ||
signature = base64.b85encode(expiration + hash) | ||
return signature.decode() | ||
|
||
|
||
def verify(data: str) -> bool: | ||
if len(data) < 25: | ||
return False | ||
|
||
signature = base64.b85decode(data[-25:].encode()) | ||
expiration = int.from_bytes(signature[:4], byteorder='big') | ||
if expiration < time.time(): | ||
return False | ||
|
||
msg = data[:-25] | ||
hmac_sha256 = hmac.new(k, digestmod=sha256) | ||
hmac_sha256.update(msg.encode()) | ||
hmac_sha256.update(signature[:4]) | ||
hash = hmac_sha256.digest() | ||
hash = bytes([a ^ b for a, b in zip(hash[:16], hash[16:])]) | ||
return hash == signature[4:] | ||
|
||
|
||
def encode(data: str) -> str: | ||
return data + sign(data) | ||
|
||
|
||
def decode(data: str) -> str: | ||
if verify(data): | ||
return data[:-25] | ||
return None | ||
|
||
|
||
if __name__ == "__main__": | ||
signed_msg = sign("Hello World") | ||
print(signed_msg) | ||
print(verify(signed_msg)) | ||
signed_msg = signed_msg[:-1] + "A" # tamper with the message | ||
print(verify(signed_msg)) | ||
|
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
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
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
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
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
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,53 @@ | ||
from fastapi import APIRouter, HTTPException | ||
from fastapi.responses import StreamingResponse | ||
|
||
import io | ||
import qrcode | ||
|
||
from app.dependencies import qrvalidation | ||
from app.schemas.qrcode import QRCodeRequest | ||
|
||
router = APIRouter( | ||
prefix="/qrcode", | ||
tags=["qrcode"], | ||
responses={404: {"description": "Not found"}} | ||
) | ||
|
||
@router.post("/encode") | ||
async def qrcode_encode(data: QRCodeRequest): | ||
if not data: | ||
raise HTTPException(status_code=400, detail="No data provided") | ||
|
||
# TODO: Use auth to validate user | ||
|
||
msg = data.msg | ||
|
||
qr = qrcode.QRCode( | ||
version=1, | ||
error_correction=qrcode.constants.ERROR_CORRECT_L, | ||
box_size=10, | ||
border=4, | ||
) | ||
qr.add_data(qrvalidation.encode(msg)) | ||
qr.make(fit=True) | ||
|
||
img = qr.make_image(fill='black', back_color='white') | ||
img_io = io.BytesIO() | ||
img.save(img_io, 'PNG') | ||
img_io.seek(0) | ||
|
||
return StreamingResponse(img_io, media_type="image/png") | ||
|
||
@router.post("/decode") | ||
async def qrcode_decode(data: QRCodeRequest): | ||
if not data: | ||
raise HTTPException(status_code=400, detail="No data provided") | ||
|
||
msg = data.msg | ||
|
||
userId = qrvalidation.decode(msg) | ||
|
||
if userId is None: | ||
raise HTTPException(status_code=400, detail="Invalid QR code") | ||
|
||
return {"msg": userId} |
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
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
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,6 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
|
||
class QRCodeRequest(BaseModel): | ||
msg: str |
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,5 @@ | ||
# Examples | ||
|
||
These examples demonstrate the integration between different components of the system. | ||
|
||
**They only serve as support for project development and do not constitute an integral part of the system.** |
Oops, something went wrong.