Repository: ramosbugs/oauth2-rs (language: rust)

Issue:
Implement SecretType::into_secret
It would be helpful to have a method to get the ownership of the underlying string back. What do you think?


Discussion / hints:
Hey @IvanUkhov,

This sounds reasonable. The Rust idiom I often see is an accessor method called `inner()` that returns a reference and another method called `into_inner()` that takes ownership and returns the inner value. Since the secret types currently have an accessor called `secret()`, it would probably make sense to call the new method `into_secret()` instead of implementing the `From` trait. This naming also helps remind callers that they're dealing with a sensitive value they should be careful with, which may help keep these secrets out of logs or other undesirable sinks.

Fix patch:
diff --git a/src/types.rs b/src/types.rs
index 47bc8de..039bfa5 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -159,12 +159,20 @@ macro_rules! new_secret_type {
             pub fn new(s: $type) -> Self {
                 $name(s)
             }
+
             #[doc = $secret_doc]
             ///
             /// # Security Warning
             ///
             /// Leaking this value may compromise the security of the OAuth2 flow.
             pub fn secret(&self) -> &$type { &self.0 }
+
+            #[doc = $secret_doc]
+            ///
+            /// # Security Warning
+            ///
+            /// Leaking this value may compromise the security of the OAuth2 flow.
+            pub fn into_secret(self) -> $type { self.0 }
         }
         impl Debug for $name {
             fn fmt(&self, f: &mut Formatter) -> Result<(), FormatterError> {
@@ -606,7 +614,13 @@ new_secret_type![
 
 #[cfg(test)]
 mod tests {
-    use crate::{ClientSecret, PkceCodeChallenge, PkceCodeVerifier};
+    use crate::{ClientSecret, CsrfToken, PkceCodeChallenge, PkceCodeVerifier};
+
+    #[test]
+    fn test_secret_conversion() {
+        let secret = CsrfToken::new("top_secret".into());
+        assert_eq!(secret.into_secret().into_boxed_str(), "top_secret".into());
+    }
 
     #[test]
     fn test_secret_redaction() {


Test patch: