Skip to content

Commit

Permalink
Merge pull request #34 from museun/oauth-prefix
Browse files Browse the repository at this point in the history
auto prefix register's password output with `oauth:`
  • Loading branch information
museun committed May 7, 2023
2 parents bef7a05 + 45a5f7c commit 713cc26
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/encode/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use super::Capability;
/// This allows you to initialize the registration handshake with the server
///
/// You must provide a name and an associated chat `OAuth` token to send messages to Twitch
///
/// **NOTE** If your OAuth token is not prefixed with `oauth:` it will be added
///
/// If you just want to read messages, then the [`ANONYMOUS_LOGIN`](crate::ANONYMOUS_LOGIN) tuple is provided.
///
/// # Capabilities
Expand Down Expand Up @@ -41,7 +44,16 @@ impl<'a, const N: usize> Register<'a, N> {
format_args!("CAP REQ {cap}\r\n", cap = cap.as_str()),
)?;
}
apply(writer, format_args!("PASS {pass}\r\n", pass = self.oauth))?;

let is_prefixed = self.oauth.starts_with("oauth:");
apply(
writer,
format_args!(
"PASS {prefix}{pass}\r\n",
pass = self.oauth,
prefix = if is_prefixed { "" } else { "oauth:" }
),
)?;
apply(writer, format_args!("NICK {name}\r\n", name = self.name))
}
}
Expand Down Expand Up @@ -76,16 +88,22 @@ mod tests {
register.format(&mut out).unwrap();
assert_eq!(
out,
"CAP REQ twitch.tv/tags\r\nPASS password\r\nNICK test\r\n"
"CAP REQ twitch.tv/tags\r\nPASS oauth:password\r\nNICK test\r\n"
);
}

#[test]
fn oauth_prefix() {
let register = super::register("test", "password", []);
assert_eq!(register.to_string(), "PASS oauth:password\r\nNICK test\r\n");
}

#[test]
fn register_display() {
let register = super::register("test", "password", [crate::encode::Capability::Tags]);
assert_eq!(
register.to_string(),
"CAP REQ twitch.tv/tags\r\nPASS password\r\nNICK test\r\n"
"CAP REQ twitch.tv/tags\r\nPASS oauth:password\r\nNICK test\r\n"
);
}

Expand All @@ -99,7 +117,7 @@ mod tests {
register.encode(&mut out).unwrap();
assert_eq!(
out,
b"CAP REQ twitch.tv/tags\r\nPASS password\r\nNICK test\r\n"
b"CAP REQ twitch.tv/tags\r\nPASS oauth:password\r\nNICK test\r\n"
);
}
}

0 comments on commit 713cc26

Please sign in to comment.