-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new endpoint to get ssh keys from other user (#277)
* add endpoint to retrieve other user ssh keys * update ssh_key regex * Remove forgotten unwrap * change to ranked route * Add test * respond with correct content-type matching accept header
- Loading branch information
Showing
6 changed files
with
121 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,6 +436,7 @@ impl User { | |
self.ssh_key = Some(ssh_key); | ||
} | ||
self.subscribed_to_mailing_list = change.subscribed_to_mailing_list; | ||
self.validate()?; | ||
Ok(()) | ||
} | ||
|
||
|
@@ -576,7 +577,16 @@ fn validate_ssh_key_list( | |
) -> std::result::Result<(), ValidationError> { | ||
lazy_static! { | ||
static ref SSH_KEY_REGEX: Regex = Regex::new( | ||
r"ssh-(rsa|dsa|ecdsa|ed25519) [a-zA-Z0-9+/]{1,750}={0,3}( [^ ]+)?" | ||
r"(?x)^ | ||
( | ||
ssh-(rsa|dss|ecdsa|ed25519)| | ||
ecdsa-sha2-nistp(256|384|521)| | ||
sk-( | ||
[email protected]| | ||
[email protected] | ||
) | ||
) | ||
\s[a-zA-Z0-9+/]{1,750}={0,3}( \S+)?" | ||
) | ||
.unwrap(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<pre>{{keys}}</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,20 @@ async fn show_user_as_admin() { | |
#[rocket::async_test] | ||
async fn update_self() { | ||
common::as_user(async move |http_client: HttpClient, db, user: User| { | ||
let response = http_client | ||
.put(format!("/users/{}", user.username)) | ||
.header(ContentType::Form) | ||
.header(Accept::JSON) | ||
.body("ssh_key=ssh-fake%20supersecretkey") | ||
.dispatch() | ||
.await; | ||
|
||
assert_eq!( | ||
response.status(), | ||
Status::UnprocessableEntity, | ||
"user should not be able to update invalid public ssh key" | ||
); | ||
|
||
let response = http_client | ||
.put(format!("/users/{}", user.username)) | ||
.header(ContentType::Form) | ||
|
@@ -1000,3 +1014,44 @@ async fn validate_unique_username() { | |
}) | ||
.await; | ||
} | ||
|
||
#[rocket::async_test] | ||
async fn get_keys() { | ||
common::as_visitor(async move |http_client: HttpClient, db| { | ||
let mut user = User::create( | ||
NewUser { | ||
username: String::from("user"), | ||
password: String::from("password"), | ||
full_name: String::from("name"), | ||
email: String::from("[email protected]"), | ||
ssh_key: None, | ||
not_a_robot: true, | ||
}, | ||
common::BCRYPT_COST, | ||
&db, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
// User::create throws away ssh_key in NewUser. | ||
user.ssh_key = Some(String::from("ssh-rsa rsa \n ssh-ed25519 ed25519")); | ||
let user = user.update(&db).await.unwrap(); | ||
|
||
let response = http_client | ||
.get(format!("/users/{}/keys", user.username)) | ||
.header(Accept::JSON) | ||
.dispatch() | ||
.await; | ||
|
||
assert_eq!(response.status(), Status::Ok); | ||
|
||
let keys = response | ||
.into_json::<Vec<String>>() | ||
.await | ||
.expect("response json"); | ||
assert_eq!(keys.len(), 2); | ||
assert_eq!(keys[0], "ssh-rsa rsa"); | ||
assert_eq!(keys[1], "ssh-ed25519 ed25519"); | ||
}) | ||
.await; | ||
} |