How to implement a PKCE code challenge in C#

Today’s fun was implementing OAuth2’s RFC 7636’s PKCE (Proof Key for Code Exchange) in C#. It’s relatively straightforward, but I decided to share my implementation should it be helpful to someone else out there. PKCE is an extension to the Authorization Code flow to prevent CSRF and authorization code injection attacks. [..] It was originally designed to protect the authorization code flow in mobile apps, but its ability to prevent authorization code injection makes it useful for every type of OAuth client, even web apps that use client authentication (source)....

January 17, 2024

How to use bash to recursively search and replace a string in all directory files

Another achievement I unlocked with the recent website update is the newsletter switch from Substack to a fantastic and independent provider, Buttondown. That required updating all the “subscribe to my newsletter” links. We’re talking 5K posts, all saved as individual files in the same directory. The bash command that did that for me is: find content/post/*.md -type f -exec \ sed -i .bak 's|https://nicolaiarocci.substack.com|https://buttondown.email/nicolaiarocci|g' {} + It is pretty straightforward. find looks for all markdown files in the content/post/ directory....

January 6, 2024

How to use XmlWriter along with StringWriter to properly serialize a UTF-8 string

Today, I (re)learned how to serialize an XML to a UTF-8 string. Like all the other times I did this, I got backstabbed by StringWriter, which only supports UTF-16. A simple code snippet like this: await using var sw = new StringWriter(); await using var w = XmlWriter.Create(sw, new() { Async = true }); ... await w.FlushAsync(); return sw.ToString(); Will emit this output: <?xml version="1.0" encoding="utf-16"?><... There’s nothing inherently wrong with UTF-16, but XML is usually UTF-8, so one must do something about it....

November 9, 2023

LINQ DistinctBy on a property for .NET Standard and older .NET versions

Today I learned how to implement a custom Enumerable.DistinctBy extension method that returns distinct elements from a sequence according to a specified key selector function. .NET 6 and its successors have the method built in within LINQ, but I needed it in a .NET Standard 2.0 class library, so I was out of luck. My implementation is simple, not different from others I found online, and should also work fine with old ....

October 25, 2023

rsync with a different user

Today I learned how to rsync with a user different than the one connected to the remote. Why would one want to do such a thing? The data I need to download from that server is owned by ‘backup,’ a different, service-only user. I wanted to avoid going the change-permissions slippery route and allow my user direct access to the data. Looking at the rsync documentation, I learned about the nifty --rsync-path=PROGRAM option:...

August 23, 2023

Homebrew and .NET 8 Preview don't like each other

Today I learned that .NET 8 Preview could play better with Homebrew (or vice-versa). I’m working on a C# 12 presentation for our local developer meetup, and for that, I wanted .NET 8 Preview to run side by side with version 7 on my Mac. As version 7 was initially installed with Homebrew, I also wanted to install version 8 Preview with Homebrew, but that recipe was unavailable. Not perfectly happy with that, I fell back to the stand-alone installer, expecting problems....

June 13, 2023

Python `decimal.getcontext` does not work with bpython

I have been working on a side project for which I’m using bpython, a “fancy interface to the Python interpreter.” If you use the Python REPL often, you should check it out. It offers unique features like in-line syntax highlighting, readline-like autocomplete, a “rewind” function to pop the last line of code from memory, auto-indentation and more. Anyway, today I found a bug in bpython, and that’s that Python’s decimal.getcontext() does not work with it....

June 6, 2023

macOS networkQuality tool

Today I learned about a precious little macOS command line tool, networkQuality. The networkQuality tool is a built-in tool released in macOS Monterey that can help diagnose network issues and measure network performance. Usage: networkQuality -v Example output: ==== SUMMARY ==== Uplink capacity: 44.448 Mbps (Accuracy: High) Downlink capacity: 162.135 Mbps (Accuracy: High) Responsiveness: Low (73 RPM) (Accuracy: High) Idle Latency: 50.125 milliseconds (Accuracy: High) Interface: en0 Uplink bytes transferred: 69....

May 15, 2023

The real cost of interruption

I’m just back from reading Programmer Interrupted: The Real Cost of Interruption and Context Switching, an interesting short piece in which I learned about at least two new things. First, The Parable of the Two Watchmakers, introduced by Nobel Prize winner Herbert Simon, describes the complex relationship between sub-systems and their larger wholes. In the context of the article, it helps explain, even for non-programmers, the cost of an interruption. It also hints at a possible mitigation technique:...

April 7, 2023

Hand dryers

via

February 28, 2023