Dissect the PKCE Authorization Code Grant Flow on iOS | Kodeco

Learn how to use Proof Key for Code Exchange (PKCE) authentication flow to access APIs with your Swift iOS apps.


This is a companion discussion topic for the original entry at https://www.kodeco.com/33091327-dissect-the-pkce-authorization-code-grant-flow-on-ios

Hi @alessandrodn , first let me thank you for your helpful work here!

I have a question on your implementation of the base64URLEncodedString() function in PKCECodeGenerator.swift. — As far as I understand, the Base 64 code (its “charset”) does not include any whitespace characters. So, why do you call .trimmingCharacters(in: .whitespaces) on the Base 64 encoded string (line 61)?

-Malte

1 Like

Hi @malte_again, thanks a lot for your feedback.
That’s a great question!
You’re right in this specific case, where the string is generated internally in the code; it’s not necessary.

I use this small sequence of modifiers for different purposes. When the string comes from a different encoder, it’s always better to ensure that the encoded string has no white spaces :wink:

Consider that even base64EncodedString() in certain cases can include some CR + LF when configured with the options Base64EncodingOptions.lineLengthXCharacters.
In such cases, you might want to use replacingOccurrences(of:, with:, options:) to remove this CR + LF.

Feel free to share your comments in the article so that others may benefit from it.

Best Regards
Alessandro