-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
120 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use clap::Arg; | ||
use clap::ArgAction; | ||
use clap::ArgMatches; | ||
use clap::Command; | ||
use dephy_pproxy::auth::proto; | ||
use tonic::transport::Server; | ||
use tonic::Request; | ||
use tonic::Response; | ||
use tonic::Status; | ||
|
||
struct PProxyAuth { | ||
peer_ids: Vec<String>, | ||
} | ||
|
||
#[tonic::async_trait] | ||
impl proto::auth_service_server::AuthService for PProxyAuth { | ||
async fn get_tokens( | ||
&self, | ||
request: Request<proto::GetTokensRequest>, | ||
) -> Result<Response<proto::GetTokensResponse>, Status> { | ||
let request = request.into_inner(); | ||
|
||
let tokens = self | ||
.peer_ids | ||
.iter() | ||
.filter(|peer_id| { | ||
request.peer_id.is_none() || request.peer_id == Some(peer_id.to_owned().to_owned()) | ||
}) | ||
.cloned() | ||
.map(|peer_id| proto::Token { | ||
resource_id: request.resource_id.clone(), | ||
peer_id, | ||
ttl: 3600, | ||
}) | ||
.collect(); | ||
|
||
Ok(Response::new(proto::GetTokensResponse { tokens })) | ||
} | ||
} | ||
|
||
fn parse_args() -> ArgMatches { | ||
Command::new("pproxy-auth-server") | ||
.about("An example pproxy auth server") | ||
.version(dephy_pproxy::VERSION) | ||
.arg( | ||
Arg::new("SERVER_ADDR") | ||
.long("server-addr") | ||
.num_args(1) | ||
.default_value("127.0.0.1:3000") | ||
.action(ArgAction::Set) | ||
.help("Server address"), | ||
) | ||
.arg( | ||
Arg::new("PEER_IDS") | ||
.num_args(0..) | ||
.action(ArgAction::Set) | ||
.help("Will generate tokens for those peers"), | ||
) | ||
.arg_required_else_help(true) | ||
.get_matches() | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let _ = tracing_subscriber::fmt() | ||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) | ||
.try_init(); | ||
|
||
let args = parse_args(); | ||
|
||
let server_addr = args | ||
.get_one::<String>("SERVER_ADDR") | ||
.unwrap() | ||
.parse() | ||
.expect("Invalid server address"); | ||
let peer_ids = args.get_many("PEER_IDS").unwrap().cloned().collect(); | ||
println!("server_addr: {}", server_addr); | ||
println!("peer_ids: {:?}", peer_ids); | ||
|
||
let auth = PProxyAuth { peer_ids }; | ||
|
||
let auth_server = proto::auth_service_server::AuthServiceServer::new(auth); | ||
|
||
Server::builder() | ||
.add_service(tonic_web::enable(auth_server)) | ||
.serve(server_addr) | ||
.await | ||
.expect("Auth server failed"); | ||
} |
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,21 @@ | ||
syntax = "proto3"; | ||
package auth.v1; | ||
|
||
message GetTokensRequest { | ||
string resource_id = 1; | ||
optional string peer_id = 2; | ||
} | ||
|
||
message GetTokensResponse { | ||
repeated Token tokens = 1; | ||
} | ||
|
||
message Token { | ||
string resource_id = 1; | ||
string peer_id = 2; | ||
uint64 ttl = 3; | ||
} | ||
|
||
service AuthService { | ||
rpc GetTokens(GetTokensRequest) returns (GetTokensResponse); | ||
} |
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,3 @@ | ||
pub mod proto { | ||
tonic::include_proto!("auth.v1"); | ||
} |
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