Skip to content

Commit 2a89da2

Browse files
authored
Merge pull request #284 from rossmacarthur/ft/basic-auth-from-credentials
Add `BasicAuth::from_credentials`
2 parents 908aaa2 + 858f816 commit 2a89da2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/auth/basic_auth.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ impl BasicAuth {
6363
"Expected basic auth scheme found `{}`",
6464
scheme
6565
);
66+
Self::from_credentials(auth.credentials()).map(Some)
67+
}
6668

67-
let bytes = base64::decode(auth.credentials()).status(400)?;
69+
/// Create a new instance from the base64 encoded credentials.
70+
pub fn from_credentials(credentials: impl AsRef<[u8]>) -> crate::Result<Self> {
71+
let bytes = base64::decode(credentials).status(400)?;
6872
let credentials = String::from_utf8(bytes).status(400)?;
6973

7074
let mut iter = credentials.splitn(2, ':');
@@ -77,7 +81,7 @@ impl BasicAuth {
7781
(None, _) => bail!(400, "Expected basic auth to contain a username"),
7882
};
7983

80-
Ok(Some(Self { username, password }))
84+
Ok(Self { username, password })
8185
}
8286

8387
/// Sets the header.

0 commit comments

Comments
 (0)