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

Make HMAC signer thread safe #117

Merged
merged 1 commit into from Mar 12, 2020
Merged

Make HMAC signer thread safe #117

merged 1 commit into from Mar 12, 2020

Conversation

tanner0101
Copy link
Member

@tanner0101 tanner0101 commented Mar 12, 2020

Makes HMAC JWT signers (hs256, hs384, hs512) thread safe (#117).

@tanner0101 tanner0101 added bug Something isn't working semver-patch Internal changes only labels Mar 12, 2020
@tanner0101 tanner0101 added this to In Progress in Vapor 3 via automation Mar 12, 2020
@tanner0101 tanner0101 merged commit 03c8c11 into 3 Mar 12, 2020
@tanner0101 tanner0101 deleted the tn-3-threadsafe-hmac branch March 12, 2020 18:49
@MrMage
Copy link
Contributor

MrMage commented Mar 13, 2020

Out of curiosity, how does the autoclosure change the method's thread safety behavior?

@vzsg
Copy link
Member

vzsg commented Mar 13, 2020

Previously, the JWTSigner kept hold of the HMAC instance it was created with. As the HMAC context is not thread-safe, this made JWTSigner also not thread-safe (multiple calls to the same JWTSigner instance lead to EXC_BAD_ACCESS).

This issue is resolved by replacing the HMAC instance with a factory closure, so every signing/verification operation will create and use its own context.

With an @autoclosure annotation, this becomes a transparent change at build time instead of a source-breaking change.

(My two cents.)

@MrMage
Copy link
Contributor

MrMage commented Mar 13, 2020

Thanks for the info. Two more questions:

  1. This method seems to be called with e.g. an argument of HMAC.SHA256. What exactly does that evaluate to? If HMAC.SHA256 is a fixed reference, wrapping it in an @autoclosure would still return the same instance over and over again.
  2. If I evaluate an @autoclosure twice, will the "wrapped" code be executed twice? I haven't tested that myself, but that would need to be true for the factory to be called twice, I guess.

@tanner0101
Copy link
Member Author

1: HMAC.SHA256 is a computed method so each call gives you a new one back.
2: Yes, @autoclosure basically just wraps { return ... } around the argument. So hmac(HMAC.SHA256) is interpreted as `hmac({ return HMAC.SHA256 }). Each time you call that closure it will run the code inside.

@MrMage
Copy link
Contributor

MrMage commented Mar 16, 2020

1: HMAC.SHA256 is a computed method so each call gives you a new one back.
2: Yes, @autoclosure basically just wraps { return ... } around the argument. So hmac(HMAC.SHA256) is interpreted as `hmac({ return HMAC.SHA256 }). Each time you call that closure it will run the code inside.

Sounds great, thank you for the elaboration! I would suggest documenting this use of @autoclosure with a short comment to document the purpose it serves in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working semver-patch Internal changes only
Projects
No open projects
Vapor 3
  
In Progress
Development

Successfully merging this pull request may close these issues.

None yet

3 participants