-
Notifications
You must be signed in to change notification settings - Fork 49
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
feat: Removes pyo3 and derives tokens directly in Rust #1513
Conversation
impl Crypto for CryptoImpl { | ||
type Error = TokenserverError; | ||
fn hkdf(&self, secret: &str, salt: Option<&[u8]>, info: &[u8]) -> Result<Vec<u8>, Self::Error> { | ||
let hk = Hkdf::<Sha256>::new(salt, secret.as_bytes()); | ||
let mut okm = [0u8; SHA256_OUTPUT_LEN]; | ||
hk.expand(info, &mut okm) | ||
.map_err(|_| TokenserverError::internal_error())?; | ||
Ok(okm.to_vec()) | ||
} | ||
|
||
fn hmac_sign(&self, key: &[u8], payload: &[u8]) -> Result<Vec<u8>, Self::Error> { | ||
let mut mac: Hmac<Sha256> = | ||
Hmac::new_from_slice(key).map_err(|_| TokenserverError::internal_error())?; | ||
mac.update(payload); | ||
Ok(mac.finalize().into_bytes().to_vec()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will require a careful review since they're trying to do crypto!
e1ed400
to
bc2c3cd
Compare
60d6409
to
f48c0e1
Compare
f48c0e1
to
f0258d9
Compare
af70535
to
927f460
Compare
3e5f129
to
025c2f2
Compare
843ce10
to
cafcb90
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still going through the tests for oauth.rs Done. Nothing looks out of sorts.
Just stopping by to say this is awesome and I'm glad it's happening 🎉 |
This is why I love working in the open - Great to see you @ethowitz!! |
|
||
impl Crypto for CryptoImpl { | ||
type Error = TokenserverError; | ||
fn hkdf(&self, secret: &str, salt: Option<&[u8]>, info: &[u8]) -> Result<Vec<u8>, Self::Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Equivalent function for hawk
crate takes only a single key vec, but specifies the hash algo (e.g. Key::new(vec![99u8; 32], SHA256)
(where the vec!
would contain a real key and not a dummy value like that.) It's worth noting that we are using a salt where the hawk
crate appears not to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the difference is the hawk function we're looking at is generating the Hmac key, which happens (in hmac_sign
for us, and unlike hawk we hard-code the algorithm to be sha256, although we could expand the API if the use case comes!)
We end up using a salt here in the HKDF for the per-token secret, using the salt that is randomly generated and inserted into the token. That all said, a lot of this is out of my comfort zone and I tried to keep it true to what the python was doing and not re-invent anything
Part of me would love to put the python replacement stuff behind a feature flag, maybe break the functions out of Fortunately a LOT of this PR is general cleanup. |
d41ece5
to
36eea7f
Compare
36eea7f
to
9efcaeb
Compare
Thank you @jrconlin!! I like the suggestion of using rust features for this so it's safer (also easier to rollback and canary, etc etc) I went ahead and moved the python implementation behind a rust feature, and removing that feature will have the Rust implementation |
(the ci failures are unrelated and should be fixed in #1518 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, awesome! You were able to break the python bit into it's own feature.
9efcaeb
to
5f6be6c
Compare
Description
We were using pyo3 in two places to call into Python code:
/v1/verify/
call on FxA's oauth server, asking FxA to verify for usmake_token
andget_derived_secret
This PR replaces both usages of pyo3 with Rust code implemented directly in the tokenserver.
Testing
I've tested a local firefox connected to FxA's stage, and my local environment with tokenserver and syncstorage and verified that:
My local firefox syncs successfully with the changes in this PR, however, Out of abundance of caution we should run end-to-end tests and deploy this first on a canary if we plan to take it
TODOs
verify
andget_token_and_derived_secret
Issue(s)
Closes SYNC-3528.