Skip to content

Commit 0b11548

Browse files
committed
Revert changes to use typed auth in rust-server. Run sample generation.
1 parent bd7b84b commit 0b11548

File tree

111 files changed

+4412
-2434
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+4412
-2434
lines changed

modules/openapi-generator/src/main/resources/rust-server/auth.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::BTreeSet;
22
use crate::server::Authorization;
33
use serde::{Deserialize, Serialize};
4-
use swagger::{ApiError, auth::AuthData};
4+
use swagger::{ApiError, auth::{Basic, Bearer}};
55

66
#[derive(Debug, Serialize, Deserialize)]
77
pub struct Claims {
@@ -17,13 +17,13 @@ pub struct Claims {
1717
pub trait AuthenticationApi {
1818
1919
/// Method should be implemented (see example-code) to map Bearer-token to an Authorization
20-
fn bearer_authorization(&self, token: &str) -> Result<Authorization, ApiError>;
20+
fn bearer_authorization(&self, token: &Bearer) -> Result<Authorization, ApiError>;
2121
2222
/// Method should be implemented (see example-code) to map ApiKey to an Authorization
2323
fn apikey_authorization(&self, token: &str) -> Result<Authorization, ApiError>;
2424
2525
/// Method should be implemented (see example-code) to map Basic (Username:password) to an Authorization
26-
fn basic_authorization(&self, username: &str, password: &str) -> Result<Authorization, ApiError>;
26+
fn basic_authorization(&self, basic: &Basic) -> Result<Authorization, ApiError>;
2727
}
2828

2929
// Implement it for AllowAllAuthenticator (dummy is needed, but should not used as we have Bearer authorization)
@@ -46,7 +46,7 @@ where
4646
RC::Result: Send + 'static {
4747
4848
/// Get method to map Bearer-token to an Authorization
49-
fn bearer_authorization(&self, _token: &str) -> Result<Authorization, ApiError> {
49+
fn bearer_authorization(&self, _token: &Bearer) -> Result<Authorization, ApiError> {
5050
Ok(dummy_authorization())
5151
}
5252

@@ -56,7 +56,7 @@ where
5656
}
5757

5858
/// Get method to map basic token to an Authorization
59-
fn basic_authorization(&self, _username: &str, _password: &str) -> Result<Authorization, ApiError> {
59+
fn basic_authorization(&self, _basic: &Basic) -> Result<Authorization, ApiError> {
6060
Ok(dummy_authorization())
6161
}
6262
}

modules/openapi-generator/src/main/resources/rust-server/example-server-auth.mustache

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use swagger::{
22
ApiError,
3+
auth::{Basic, Bearer},
34
Has,
45
XSpanIdString};
56
use {{{externCrateName}}}::{AuthenticationApi, Claims};
@@ -94,10 +95,10 @@ fn get_jwt_error_string(error: JwtError::Error) -> String {
9495
impl<C> AuthenticationApi for Server<C> where C: Has<XSpanIdString> + Send + Sync {
9596
9697
/// Implementation of the method to map a Bearer-token to an Authorization
97-
fn bearer_authorization(&self, token: &str) -> Result<Authorization, ApiError> {
98+
fn bearer_authorization(&self, bearer: &Bearer) -> Result<Authorization, ApiError> {
9899
debug!("\tAuthorizationApi: Received Bearer-token, {token:#?}");
99100
100-
match extract_token_data(token, b"secret") {
101+
match extract_token_data(&bearer.token, b"secret") {
101102
Ok(auth_data) => {
102103
debug!("\tUnpack auth_data as: {auth_data:#?}");
103104
let authorization = build_authorization(auth_data.claims);
@@ -123,8 +124,8 @@ impl<C> AuthenticationApi for Server<C> where C: Has<XSpanIdString> + Send + Syn
123124
}
124125

125126
/// Implementation of the method to map a basic authentication (username and password) to an Authorization
126-
fn basic_authorization(&self, username: &str, _password: &str) -> Result<Authorization, ApiError> {
127-
debug!("\tAuthorizationApi: Received Basic-token, {username}");
127+
fn basic_authorization(&self, basic: &Basic) -> Result<Authorization, ApiError> {
128+
debug!("\tAuthorizationApi: Received Basic-token, {basic:#?}");
128129
129130
// TODO: insert the logic to map received apikey to the set of claims
130131
let claims = full_permission_claim();

modules/openapi-generator/src/main/resources/rust-server/server-server_auth.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{Api, AuthenticationApi};
33
use swagger::{
44
ApiError,
55
Authorization,
6+
auth::{Basic, Bearer},
67
Has,
78
XSpanIdString
89
};
@@ -12,17 +13,17 @@ T: Api<C> + Clone + Send + 'static + AuthenticationApi,
1213
C: Has<XSpanIdString> + Has<Option<Authorization>> + Send + Sync + 'static {
1314
1415
/// Passthrough of the task to the api-implementation
15-
fn bearer_authorization(&self, token: &str) -> Result<Authorization, ApiError> {
16+
fn bearer_authorization(&self, token: &Bearer) -> Result<Authorization, ApiError> {
1617
self.api_impl.bearer_authorization(token)
1718
}
1819

1920
/// Passthrough of the task to the api-implementation
20-
fn apikey_authorization(&self, token: &str) -> Result<Authorization, ApiError> {
21+
fn apikey_authorization(&self, token: &Bearer) -> Result<Authorization, ApiError> {
2122
self.api_impl.apikey_authorization(token)
2223
}
2324

2425
/// Passthrough of the task to the api-implementation
25-
fn basic_authorization(&self, username: &str, password: &str) -> Result<Authorization, ApiError> {
26+
fn basic_authorization(&self, basic: &Basic) -> Result<Authorization, ApiError> {
2627
self.api_impl.basic_authorization(username, password)
2728
}
2829
}

samples/server/petstore/rust-server/output/multipart-v3/.cargo/config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ rustflags = [
66

77
"-W", "trivial_numeric_casts", # detects trivial casts of numeric types which could be removed
88

9-
"-W", "unsafe_code", # usage of `unsafe` code
9+
# unsafe is used in `TokioIo` bridging code copied from `hyper`.
10+
# "-W", "unsafe_code", # usage of `unsafe` code
1011

1112
"-W", "unused_qualifications", # detects unnecessarily qualified names
1213

samples/server/petstore/rust-server/output/multipart-v3/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ examples/server-key.pem
1616
examples/server/main.rs
1717
examples/server/server.rs
1818
examples/server/server_auth.rs
19+
examples/server/tokio_io.rs
1920
src/auth.rs
2021
src/client/mod.rs
2122
src/context.rs

samples/server/petstore/rust-server/output/multipart-v3/Cargo.toml

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ client = [
1313
"mime_0_2",
1414
"multipart", "multipart/client", "swagger/multipart_form",
1515
"hyper_0_10", "mime_multipart", "swagger/multipart_related",
16-
"hyper", "hyper-openssl", "hyper-tls", "native-tls", "openssl", "url"
16+
"hyper", "hyper-util/http1", "hyper-util/http2", "hyper-openssl", "hyper-tls", "native-tls", "openssl", "url"
1717
]
1818
server = [
1919
"mime_0_2",
@@ -22,71 +22,76 @@ server = [
2222
"serde_ignored", "hyper", "regex", "percent-encoding", "url", "lazy_static"
2323
]
2424
cli = [
25-
"anyhow", "clap-verbosity-flag", "simple_logger", "structopt", "tokio"
25+
"anyhow", "clap", "clap-verbosity-flag", "simple_logger", "tokio"
2626
]
2727
conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-enum-derive"]
2828

2929
[target.'cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))'.dependencies]
3030
native-tls = { version = "0.2", optional = true }
31-
hyper-tls = { version = "0.5", optional = true }
31+
hyper-tls = { version = "0.6", optional = true }
3232

3333
[target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))'.dependencies]
34-
hyper-openssl = { version = "0.9", optional = true }
35-
openssl = {version = "0.10", optional = true }
34+
hyper-openssl = { version = "0.10", optional = true }
35+
openssl = { version = "0.10", optional = true }
3636

3737
[dependencies]
3838
# Common
39-
async-trait = "0.1.24"
39+
async-trait = "0.1.88"
4040
chrono = { version = "0.4", features = ["serde"] }
4141
futures = "0.3"
42-
swagger = { version = "6.1", features = ["serdejson", "server", "client", "tls", "tcp"] }
43-
log = "0.4.0"
42+
swagger = { version = "7.0.0-rc1", features = ["serdejson", "server", "client", "tls"] }
43+
log = "0.4.27"
4444
mime = "0.3"
4545

4646
serde = { version = "1.0", features = ["derive"] }
4747
serde_json = "1.0"
48-
validator = { version = "0.16", features = ["derive"] }
48+
validator = { version = "0.20", features = ["derive"] }
4949

5050
# Crates included if required by the API definition
5151
mime_0_2 = { package = "mime", version = "0.2.6", optional = true }
5252
multipart = { version = "0.16", default-features = false, optional = true }
5353

5454
# Common between server and client features
55-
hyper = {version = "0.14", features = ["full"], optional = true}
56-
mime_multipart = {version = "0.5", optional = true}
57-
hyper_0_10 = {package = "hyper", version = "0.10", default-features = false, optional=true}
58-
serde_ignored = {version = "0.1.1", optional = true}
59-
url = {version = "2.1", optional = true}
55+
bytes = "1.10.1"
56+
http-body-util = "0.1.3"
57+
hyper = { version = "1.6", features = ["full"], optional = true }
58+
hyper-util = { version = "0.1.12", features = ["service"] }
59+
mime_multipart = { version = "0.5", optional = true }
60+
hyper_0_10 = {package = "hyper", version = "0.10", default-features = false, optional=true }
61+
serde_ignored = { version = "0.1.12", optional = true }
62+
url = { version = "2.5", optional = true }
6063

6164
# Client-specific
65+
tower-service = "0.3.3"
6266

6367
# Server, and client callback-specific
64-
lazy_static = { version = "1.4", optional = true }
65-
percent-encoding = {version = "2.1.0", optional = true}
66-
regex = {version = "1.3", optional = true}
68+
lazy_static = { version = "1.5", optional = true }
69+
percent-encoding = { version = "2.3.1", optional = true }
70+
regex = { version = "1.11", optional = true }
6771

6872
# CLI-specific
6973
anyhow = { version = "1", optional = true }
70-
clap-verbosity-flag = { version = "0.3", optional = true }
71-
simple_logger = { version = "2.0", features = ["stderr"], optional = true }
72-
structopt = { version = "0.3", optional = true }
73-
tokio = { version = "0.2", features = ["rt-threaded", "macros", "stream"], optional = true }
74+
clap-verbosity-flag = { version = "3.0", optional = true }
75+
simple_logger = { version = "5.0", features = ["stderr"], optional = true }
76+
tokio = { version = "1.45", features = ["rt-multi-thread", "macros"], optional = true }
7477

7578
# Conversion
76-
frunk = { version = "0.4.0", optional = true }
77-
frunk_derives = { version = "0.4.0", optional = true }
78-
frunk_core = { version = "0.4.0", optional = true }
79+
frunk = { version = "0.4.3", optional = true }
80+
frunk_derives = { version = "0.4.3", optional = true }
81+
frunk_core = { version = "0.4.3", optional = true }
7982
frunk-enum-derive = { version = "0.3.0", optional = true }
8083
frunk-enum-core = { version = "0.3.0", optional = true }
8184

8285
# Bearer authentication
83-
jsonwebtoken = { version = "9.3.0", optional = false }
86+
jsonwebtoken = { version = "9.3.1", optional = false }
8487

8588
[dev-dependencies]
86-
clap = "2.25"
89+
always_send = "0.1.1"
90+
clap = "4.5"
8791
env_logger = "0.11"
88-
tokio = { version = "1.14", features = ["full"] }
92+
tokio = { version = "1.45", features = ["full"] }
8993
native-tls = "0.2"
94+
pin-project = "1.1.10"
9095

9196
[target.'cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))'.dev-dependencies]
9297
tokio-openssl = "0.6"

samples/server/petstore/rust-server/output/multipart-v3/bin/cli.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! CLI tool driving the API client
22
use anyhow::{anyhow, Context, Result};
3+
use clap::Parser;
34
use log::{debug, info};
45
// models may be unused if all inputs are primitive types
56
#[allow(unused_imports)]
@@ -10,7 +11,6 @@ use multipart_v3::{
1011
MultipleIdenticalMimeTypesPostResponse,
1112
};
1213
use simple_logger::SimpleLogger;
13-
use structopt::StructOpt;
1414
use swagger::{AuthData, ContextBuilder, EmptyContext, Push, XSpanIdString};
1515

1616
type ClientContext = swagger::make_context_ty!(
@@ -20,65 +20,65 @@ type ClientContext = swagger::make_context_ty!(
2020
XSpanIdString
2121
);
2222

23-
#[derive(StructOpt, Debug)]
24-
#[structopt(
23+
#[derive(Parser, Debug)]
24+
#[clap(
2525
name = "Multipart OpenAPI V3 Rust Server Test",
2626
version = "1.0.7",
2727
about = "CLI access to Multipart OpenAPI V3 Rust Server Test"
2828
)]
2929
struct Cli {
30-
#[structopt(subcommand)]
30+
#[clap(subcommand)]
3131
operation: Operation,
3232

3333
/// Address or hostname of the server hosting this API, including optional port
34-
#[structopt(short = "a", long, default_value = "http://localhost")]
34+
#[clap(short = 'a', long, default_value = "http://localhost")]
3535
server_address: String,
3636

3737
/// Path to the client private key if using client-side TLS authentication
3838
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
39-
#[structopt(long, requires_all(&["client-certificate", "server-certificate"]))]
39+
#[clap(long, requires_all(&["client-certificate", "server-certificate"]))]
4040
client_key: Option<String>,
4141

4242
/// Path to the client's public certificate associated with the private key
4343
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
44-
#[structopt(long, requires_all(&["client-key", "server-certificate"]))]
44+
#[clap(long, requires_all(&["client-key", "server-certificate"]))]
4545
client_certificate: Option<String>,
4646

4747
/// Path to CA certificate used to authenticate the server
4848
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
49-
#[structopt(long)]
49+
#[clap(long)]
5050
server_certificate: Option<String>,
5151

5252
/// If set, write output to file instead of stdout
53-
#[structopt(short, long)]
53+
#[clap(short, long)]
5454
output_file: Option<String>,
5555

56-
#[structopt(flatten)]
56+
#[command(flatten)]
5757
verbosity: clap_verbosity_flag::Verbosity,
5858
}
5959

60-
#[derive(StructOpt, Debug)]
60+
#[derive(Parser, Debug)]
6161
enum Operation {
6262
MultipartRelatedRequestPost {
63-
#[structopt(parse(try_from_str = parse_json))]
63+
#[clap(value_parser = parse_json::<swagger::ByteArray>)]
6464
required_binary_field: swagger::ByteArray,
65-
#[structopt(parse(try_from_str = parse_json))]
65+
#[clap(value_parser = parse_json::<models::MultipartRequestObjectField>)]
6666
object_field: Option<models::MultipartRequestObjectField>,
67-
#[structopt(parse(try_from_str = parse_json))]
67+
#[clap(value_parser = parse_json::<swagger::ByteArray>)]
6868
optional_binary_field: Option<swagger::ByteArray>,
6969
},
7070
MultipartRequestPost {
7171
string_field: String,
72-
#[structopt(parse(try_from_str = parse_json))]
72+
#[clap(value_parser = parse_json::<swagger::ByteArray>)]
7373
binary_field: swagger::ByteArray,
7474
optional_string_field: Option<String>,
75-
#[structopt(parse(try_from_str = parse_json))]
75+
#[clap(value_parser = parse_json::<models::MultipartRequestObjectField>)]
7676
object_field: Option<models::MultipartRequestObjectField>,
7777
},
7878
MultipleIdenticalMimeTypesPost {
79-
#[structopt(parse(try_from_str = parse_json))]
79+
#[clap(value_parser = parse_json::<swagger::ByteArray>)]
8080
binary1: Option<swagger::ByteArray>,
81-
#[structopt(parse(try_from_str = parse_json))]
81+
#[clap(value_parser = parse_json::<swagger::ByteArray>)]
8282
binary2: Option<swagger::ByteArray>,
8383
},
8484
}
@@ -118,7 +118,7 @@ fn create_client(args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoCont
118118

119119
#[tokio::main]
120120
async fn main() -> Result<()> {
121-
let args = Cli::from_args();
121+
let args = Cli::parse();
122122
if let Some(log_level) = args.verbosity.log_level() {
123123
SimpleLogger::new().with_level(log_level.to_level_filter()).init()?;
124124
}
@@ -210,6 +210,6 @@ async fn main() -> Result<()> {
210210

211211
// May be unused if all inputs are primitive types
212212
#[allow(dead_code)]
213-
fn parse_json<'a, T: serde::de::Deserialize<'a>>(json_string: &'a str) -> Result<T> {
213+
fn parse_json<T: serde::de::DeserializeOwned>(json_string: &str) -> Result<T> {
214214
serde_json::from_str(json_string).map_err(|err| anyhow!("Error parsing input: {}", err))
215215
}

0 commit comments

Comments
 (0)