Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CodeVerifier in OAuthTokenRequestBody for better encapsulation #745

Merged
merged 3 commits into from
Feb 28, 2023

Conversation

mokagio
Copy link
Contributor

@mokagio mokagio commented Feb 24, 2023

See discussion at #740 (comment)


  • I have considered if this change warrants release notes and have added them to the appropriate section in the CHANGELOG.md if necessary.

let codeVerifier: String
let rawCodeVerifier: String
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered keeping this called codeVerifier, but though it be clearer to have "raw" in the name when the type is String.

I also considered adding a computed codeVerifier: PKCE.CodeVerifier property. I decided not to because the init?(value:) method can fail and it was either force unwrap under the assumption that the Google backend will never send back an invalid one (because it wouldn't allow us to send it an invalid one) or return an Optional. Moreover, there is no existing reader of that value outside of the tests... YAGNI.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the type were instead ProofKeyForCodeExchange.CodeVerifier, could you keep codeVerifier and just access the rawValue exclusively in asURLEncodedData?

Not a huge deal – mostly just a nit 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment made me realize I was approaching the type in the context of Decodable (where it's handy to keep String properties to leverage the compiler-generated decoding) but this type is actually used for encoding data. 😅

See 03f377c.

@@ -587,6 +589,8 @@
3FE8072329365FC20088420C /* GoogleSignIn */ = {
isa = PBXGroup;
children = (
3F30A6B9299F12E30004452F /* Character+URLSafeTests.swift */,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addition is because I sorted the group alphabetically. See the matching removal below.

Copy link
Contributor

@jkmassel jkmassel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, left a couple of minor (potential) improvement opportunities in case you agree they are actually improvements and not just overcomplications 🙂

let codeVerifier: String
let rawCodeVerifier: String
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the type were instead ProofKeyForCodeExchange.CodeVerifier, could you keep codeVerifier and just access the rawValue exclusively in asURLEncodedData?

Not a huge deal – mostly just a nit 🙂

/// The reason we care about it being deterministic is because we don't want implicit randomness test.
/// The only place were we want to use random values in the `CodeVerifier` tests which explicitly check the random generation.
static func fixture() -> Self {
.init(value: (0..<minimumLength).map { _ in "a" }.joined())!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another nit, but if we want to avoid ! here (in case SwiftLint or something wants to yell at us for it) we could add a private initializer like init(knownGoodValue: that uses a precondition or similar to crash if this API is misused.

Not sure if we care, but it's an option 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to park this suggestion for the moment. We don't have a rule to flag force unwraps in the test code ATM and there are arguments in favor of leaving this as an option, trading off the possibility of test-runtime crashes in case of developer error for more compact code.

Base automatically changed from mokagio/pkce-code-verifier to trunk February 28, 2023 21:53
mokagio added 3 commits March 1, 2023 08:55
This is instead of `String`.
See conversation at
https://github.com/wordpress-mobile/WordPressAuthenticator-iOS/pull/745/files#r1116507824

It also comes at the cost of losing `Encodable` conformance, because
`CodeVerifier` does not conform to `Encodable` and, given the capability
is not used anywhere, I decided not to write any extra code to satisfy
the compiler.
@mokagio mokagio force-pushed the mokagio/opaque-codeverifier-string branch from 03f377c to 2989720 Compare February 28, 2023 22:01
@mokagio mokagio enabled auto-merge February 28, 2023 22:03
@mokagio mokagio merged commit 8a81963 into trunk Feb 28, 2023
@mokagio mokagio deleted the mokagio/opaque-codeverifier-string branch February 28, 2023 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants