Skip to content

Commit cdd59af

Browse files
committed
remote: ensure configured username matches authenticated session username
Bearer tokens don't require a username to authenticate, so its possible to use the "wrong" token and everything end up seem fine. In that case, config.username would be wrong and though right now that wouldn't affect anything, it could in the future and even if not, still means the user is perhaps in a muddle. Best to notice it early and bail out, since its almost certainly an easy fix in the config.
1 parent 56c3824 commit cdd59af

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/remote.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ pub enum Error {
5353
source: ureq::Error,
5454
},
5555

56+
#[snafu(display("Session username doesn't match configured username: {}", username))]
57+
UsernameMismatch { username: String },
58+
5659
#[snafu(display("Could not complete API request: {}", source))]
5760
Request { source: ureq::Error },
5861

@@ -185,7 +188,8 @@ pub struct Remote {
185188
impl Remote {
186189
pub fn open(config: &Config) -> Result<Self> {
187190
let password = config.password().context(GetPasswordSnafu {})?;
188-
match (&config.fqdn, &config.session_url) {
191+
192+
let remote = match (&config.fqdn, &config.session_url) {
189193
(Some(fqdn), _) => {
190194
Self::open_host(&fqdn, config.username.as_str(), &password, config.timeout)
191195
}
@@ -202,7 +206,16 @@ impl Remote {
202206
.context(NoDomainNameSnafu {})?;
203207
Self::open_host(domain, config.username.as_str(), &password, config.timeout)
204208
}
205-
}
209+
}?;
210+
211+
ensure!(
212+
remote.session.username == config.username,
213+
UsernameMismatchSnafu {
214+
username: remote.session.username
215+
}
216+
);
217+
218+
Ok(remote)
206219
}
207220

208221
fn open_host(fqdn: &str, username: &str, password: &str, timeout: u64) -> Result<Self> {

0 commit comments

Comments
 (0)