Skip to content

Commit

Permalink
feat: add OAUTHBEARER support
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod authored May 19, 2024
1 parent fbc194f commit 638924e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions imap-codec/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mod tests {
let tests = [
(AuthMechanism::Plain, b"PLAIN".as_ref()),
(AuthMechanism::Login, b"LOGIN"),
(AuthMechanism::OAuthBearer, b"OAUTHBEARER"),
(AuthMechanism::try_from("PLAINX").unwrap(), b"PLAINX"),
(AuthMechanism::try_from("LOGINX").unwrap(), b"LOGINX"),
(AuthMechanism::try_from("XOAUTH2X").unwrap(), b"XOAUTH2X"),
Expand Down Expand Up @@ -82,6 +83,12 @@ mod tests {
b" ",
AuthMechanism::try_from(b"Xplain".as_ref()).unwrap(),
),
(
b"oauthbearer ".as_ref(),
b" ".as_ref(),
AuthMechanism::OAuthBearer,
),
(b"oAuThbearEr ", b" ", AuthMechanism::OAuthBearer),
(b"xoauth2 ".as_ref(), b" ".as_ref(), AuthMechanism::XOAuth2),
(b"xOauTh2 ", b" ", AuthMechanism::XOAuth2),
];
Expand Down
18 changes: 18 additions & 0 deletions imap-types/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ pub enum AuthMechanism<'a> {
/// + draft-murchison-sasl-login-00: The LOGIN SASL Mechanism
Login,

/// OAuth 2.0 bearer token mechanism.
///
/// ```imap
/// AUTH=OAUTHBEARER
/// ```
///
/// ```text
/// base64(b"n,a=<user>,\x01host=<host>\x01port=<port>\x01auth=Bearer <token>\x01\x01")
/// ```
///
/// # Reference(s):
///
/// * <https://datatracker.ietf.org/doc/html/rfc7628>
OAuthBearer,

/// Google's OAuth 2.0 mechanism.
///
/// ```imap
Expand Down Expand Up @@ -137,6 +152,7 @@ impl<'a> From<Atom<'a>> for AuthMechanism<'a> {
match atom.as_ref().to_ascii_uppercase().as_str() {
"PLAIN" => Self::Plain,
"LOGIN" => Self::Login,
"OAUTHBEARER" => Self::OAuthBearer,
"XOAUTH2" => Self::XOAuth2,
"SCRAM-SHA-1" => Self::ScramSha1,
"SCRAM-SHA-1-PLUS" => Self::ScramSha1Plus,
Expand All @@ -158,6 +174,7 @@ impl<'a> AsRef<str> for AuthMechanism<'a> {
match self {
Self::Plain => "PLAIN",
Self::Login => "LOGIN",
Self::OAuthBearer => "OAUTHBEARER",
Self::XOAuth2 => "XOAUTH2",
Self::ScramSha1 => "SCRAM-SHA-1",
Self::ScramSha1Plus => "SCRAM-SHA-1-PLUS",
Expand Down Expand Up @@ -220,6 +237,7 @@ mod tests {
fn test_conversion() {
assert!(AuthMechanism::try_from("plain").is_ok());
assert!(AuthMechanism::try_from("login").is_ok());
assert!(AuthMechanism::try_from("oauthbearer").is_ok());
assert!(AuthMechanism::try_from("xoauth2").is_ok());
assert!(AuthMechanism::try_from("xxxplain").is_ok());
assert!(AuthMechanism::try_from("xxxlogin").is_ok());
Expand Down

0 comments on commit 638924e

Please sign in to comment.