|
4 | 4 |
|
5 | 5 | use app::auth::oauth::check; |
6 | 6 | use app::http::hosts::HostUrls; |
| 7 | +use app::models::User; |
| 8 | +use laravel::model::create; |
7 | 9 | use laravel::testing::prelude::*; |
8 | | -use oauth_provider_laravel::OAuthConfig; |
| 10 | +use oauth_provider_laravel::{OAuth, OAuthConfig}; |
9 | 11 |
|
10 | 12 | fn hosts() -> Application { |
11 | 13 | app::testing::configure() |
@@ -54,6 +56,100 @@ async fn mcp_clients_find_each_endpoint_on_its_own_host(app: Application) { |
54 | 56 | app.get(&authorize).header("host", "mcp.visualci.test").await.assert_status(404); |
55 | 57 | } |
56 | 58 |
|
| 59 | +const REDIRECT: &str = "http://127.0.0.1:33418/callback"; |
| 60 | +const RESOURCE: &str = "https://mcp.visualci.test/mcp"; |
| 61 | +const VERIFIER: &str = "visualci-test-verifier-0123456789-abcdefghijklmnopqrstuvwxyz"; |
| 62 | +const CHALLENGE: &str = "v8yzKoHOTGkBq15_ikEfQ4vLdOS_IW3B3JIsz5GmKUc"; |
| 63 | + |
| 64 | +async fn person(name: &str, email: &str) -> laravel::Result<User> { |
| 65 | + create!(User { pid: laravel::str::uuid7(), name: name, email: email, staff: false }).await |
| 66 | +} |
| 67 | + |
| 68 | +/// `user` connects the app `client_id` as an MCP client would (consent, then the code for tokens); returns its |
| 69 | +/// access token. |
| 70 | +async fn connect(oauth: &OAuth, client_id: &str, user: &User) -> String { |
| 71 | + let provider = oauth.provider(); |
| 72 | + let params = [ |
| 73 | + ("response_type", "code"), |
| 74 | + ("client_id", client_id), |
| 75 | + ("redirect_uri", REDIRECT), |
| 76 | + ("code_challenge", CHALLENGE), |
| 77 | + ("code_challenge_method", "S256"), |
| 78 | + ("scope", "mcp"), |
| 79 | + ("resource", RESOURCE), |
| 80 | + ("state", "s"), |
| 81 | + ] |
| 82 | + .into_iter() |
| 83 | + .map(|(k, v)| (k.to_string(), v.to_string())) |
| 84 | + .collect(); |
| 85 | + let request = provider.authorization(¶ms).await.expect("a valid authorization request"); |
| 86 | + let back = provider |
| 87 | + .approve(&request, &oauth_provider_laravel::subject(user), &["mcp".to_string()]) |
| 88 | + .await |
| 89 | + .expect("approved"); |
| 90 | + let code = url::Url::parse(&back).unwrap().query_pairs().find(|(k, _)| k == "code").unwrap().1.into_owned(); |
| 91 | + let body = url::form_urlencoded::Serializer::new(String::new()) |
| 92 | + .extend_pairs([ |
| 93 | + ("grant_type", "authorization_code"), |
| 94 | + ("code", code.as_str()), |
| 95 | + ("code_verifier", VERIFIER), |
| 96 | + ("client_id", client_id), |
| 97 | + ("redirect_uri", REDIRECT), |
| 98 | + ("resource", RESOURCE), |
| 99 | + ]) |
| 100 | + .finish(); |
| 101 | + let issued = provider |
| 102 | + .token(body.as_bytes(), Some("application/x-www-form-urlencoded"), None) |
| 103 | + .await |
| 104 | + .expect("tokens for the code"); |
| 105 | + issued.access_token |
| 106 | +} |
| 107 | + |
| 108 | +#[laravel::test(app = hosts)] |
| 109 | +async fn the_account_page_lists_and_disconnects_ones_own_apps(app: Application) -> laravel::Result<()> { |
| 110 | + let oauth = laravel::container::resolve::<OAuth>().await?; |
| 111 | + let registered = app |
| 112 | + .post("/oauth/register") |
| 113 | + .header("host", "mcp.visualci.test") |
| 114 | + .json( |
| 115 | + json!({ "client_name": "Test Client", "redirect_uris": [REDIRECT], "token_endpoint_auth_method": "none" }), |
| 116 | + ) |
| 117 | + .await; |
| 118 | + registered.assert_status(201); |
| 119 | + let client_id = registered.json()["client_id"].as_str().unwrap().to_string(); |
| 120 | + let (ada, bea) = (person("Ada", "ada@example.com").await?, person("Bea", "bea@example.com").await?); |
| 121 | + let ada_token = connect(&oauth, &client_id, &ada).await; |
| 122 | + let bea_token = connect(&oauth, &client_id, &bea).await; |
| 123 | + let host = "app.visualci.test"; |
| 124 | + |
| 125 | + let listed = app.get("/api/ui/me/connections").header("host", host).acting_as(&ada).await; |
| 126 | + listed.assert_ok().assert_json_path("connections.0.client_id", client_id.as_str()); |
| 127 | + listed.assert_json_path("connections.0.name", "Test Client").assert_json_path("connections.0.verified", false); |
| 128 | + assert_eq!(listed.json()["connections"].as_array().map(Vec::len), Some(1), "{}", listed.json()); |
| 129 | + // Signed out: nothing |
| 130 | + app.get("/api/ui/me/connections") |
| 131 | + .header("host", host) |
| 132 | + .header("accept", "application/json") |
| 133 | + .await |
| 134 | + .assert_status(401); |
| 135 | + |
| 136 | + let gone = app |
| 137 | + .delete(format!("/api/ui/me/connections?client_id={client_id}")) |
| 138 | + .header("host", host) |
| 139 | + .header("sec-fetch-site", "same-origin") |
| 140 | + .acting_as(&ada) |
| 141 | + .await; |
| 142 | + gone.assert_ok().assert_json_path("revoked", 1); |
| 143 | + let provider = oauth.provider(); |
| 144 | + assert!(provider.verify(&ada_token, RESOURCE).await.unwrap().is_err(), "Ada's token stops working"); |
| 145 | + assert!(provider.verify(&bea_token, RESOURCE).await.unwrap().is_ok(), "Bea's connection is hers"); |
| 146 | + let listed = app.get("/api/ui/me/connections").header("host", host).acting_as(&ada).await; |
| 147 | + assert_eq!(listed.assert_ok().json()["connections"].as_array().map(Vec::len), Some(0)); |
| 148 | + let bea_listed = app.get("/api/ui/me/connections").header("host", host).acting_as(&bea).await; |
| 149 | + assert_eq!(bea_listed.json()["connections"].as_array().map(Vec::len), Some(1)); |
| 150 | + Ok(()) |
| 151 | +} |
| 152 | + |
57 | 153 | fn config(issuer: &str, endpoint: &str) -> OAuthConfig { |
58 | 154 | OAuthConfig { |
59 | 155 | issuer: Some(issuer.into()), |
|
0 commit comments