Implementing JWT authentication and authorization in FastAPI

Let’s look at how to implement authentication and authorization in FastAPI. I’m going to assume you use VS Code or an IDE that’s capable of automatically resolving the required imports. There’s already a page in the FastAPIdocumentation about authentication but I’m going to extend that information to show you how to add authorization using JWT where your roles sit in a database. And it’s going to be so easy. When we’re all done we’ll be protecting routes like this @router....

Upload a file to a FastAPI route

Let’s look at how to upload a file to a FastAPI route. It’s going to be easy! For my app I need the Angular front-end to upload a PDF document to the FastAPI route defined at @router.post("/", tags=["Documents"], response_model=schemas.IdResult) @security.authorize(roles='admin,system,user') async def upload_pdf(data:schemas.DocumentForm = Depends(schemas.DocumentForm.as_form), db: Session = Depends(get_db), token_data:TokenData=Depends(security.get_token_data)): #some code removed thistd = uuid.uuid1(0,0).hex file_contents = data.pdf_file.file.read() base_folder = os.path.join(Config.BASE_FILES_DIRECTORY, thistd) os.makedirs(base_folder) file_name = f"{os.path.join(base_folder,thistd)}.pdf" with open(file_name,'wb') as f: f....

Webscraping using Python

So there’s this website where they sell limited amounts of something at a very good price. Someone gave me a hint that something is going to be sold there that’s worth a lot of money on the open market, and since all the items on the website are priced according to class we know what this object will sell for. The only problem is that manually checking the site might take too long and we might miss the boat....

The riddle of the steel

Part 1 The 1983 movie Conan The Barbarian starring Arnold Schwarzenneger as a slave turned swordmaster was heavily influenced by the philosophy of Friedrich Nietzsche, indeed in the first few opening scenes we the see phrase “What does not kill me, makes me stronger” which other than “God is dead” is the phrase which most sums up the Nietzscherian philosophy. Also in the opening scenes we see a dialogue between a young Conan and his father about the teachings of their god whom they call Krom....

2023-11-15 771 words 4 min

Sourdough bread recipe v1

Here are instructions on how to make fail-safe sourdough bread. This will make about a 900g standard loaf Equipment An electronic scale Mixing bowl A hand-pump pressure sprayer for creating a spray of water Baking steel or flat baking tray or dutch oven Environment A kitchen with a temperature of around 23C-27C. The fermentation process is very dependent on ambient temperature, the higher the temperature the faster the fermentation process meaning your dough will rise in a shorter time....

2023-11-07 1677 words 8 min

Using ntfy.sh for notifications

I am hosting this website on a VPS and I have a desire to know when my VPS provider reboots my VPS. I wanted to send a multi-cast notification to any subscribed device when the machine reboots. I had a lot of options: Telegram bot, Email or WhatsApp. I settled for using the free and open-source ntfy.sh software which I self-host. It allows you to send one-way messages via a subscription model and feature great integration with web and mobile clients....

The story of the lamp

I first read this story in a footer of a translation of the Quran I bought on an overseas trip. It’s quite a popular translation. It seems like a rather straight-forward story at first but the more I think about it the stranger I realise it actually is. There was once a man who was so busy one evening he that forgot the time for evening prayers was upon him and only remembered when he heard the call to prayer from a nearby mosque....

The availability heuristic

A heuristic is another way of saying rule-of-thumb. It’s a thing you refer to when making a decision when fully considering the situation you’re in (from first principles) would be too time-consuming, expensive or about which you simply do not have trusted skills available. The availability heuristic is what happens when you rely on an immediate example of something when considering a topic. The flaw here is that the immediate example you’re thinking about may in fact have nothing to do with the topic at all....

What happens when I enter a url into my browser?

(the above image was specified as base64 in a markdown file, inefficient but it works.) Follow up questions Tell me where a CDN fits into this picture? Where are all the places and ways the content might be cached? How would streaming a video work? Where are all the places the content might get compressed? What does load-balancing do?

The one about the floss

There really is a difference when it comes to dental floss. I’m more of the threaded than the tape type. With threaded they usually infuse it with a strong minty flavour because you can do that sort of thing with thread. With tape it’s kind of impossible because I think with the thread the flavour is inside the wax coating but with the tape (which is plastic) the flavour has nothing to bond onto....