add running data
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1112,6 +1112,7 @@ dependencies = [
|
||||
name = "nixideserver_cli"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"nixideserver_lib",
|
||||
"nixideserver_podman_lib",
|
||||
"okapi",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
extern crate clap;
|
||||
extern crate shellexpand;
|
||||
extern crate shellexpand;
|
||||
use clap::{
|
||||
Arg,
|
||||
App,
|
||||
|
||||
@@ -11,4 +11,5 @@ rocket = "0.4.5"
|
||||
rocket_okapi = "0.5.1"
|
||||
okapi = "0.4.0"
|
||||
nixideserver_lib = { path = "../lib"}
|
||||
nixideserver_podman_lib = {path = "../podman"}
|
||||
nixideserver_podman_lib = {path = "../podman"}
|
||||
clap = "2.33.1"
|
||||
@@ -5,17 +5,45 @@ extern crate rocket_okapi;
|
||||
extern crate nixideserver_lib;
|
||||
extern crate nixideserver_podman_lib;
|
||||
extern crate okapi;
|
||||
extern crate clap;
|
||||
|
||||
use nixideserver_podman_lib::PodmanEngine;
|
||||
use rocket_okapi::swagger_ui::*;
|
||||
use std::env;
|
||||
|
||||
use clap::{
|
||||
Arg,
|
||||
App
|
||||
};
|
||||
use nixideserver_lib::*;
|
||||
fn main() {
|
||||
|
||||
let matches =
|
||||
App::new("nixidectl")
|
||||
.version("0.1.0")
|
||||
.author("stubbfel")
|
||||
.about("control nixide process")
|
||||
.arg(Arg::with_name("client_id")
|
||||
.short("i")
|
||||
.long("client-id")
|
||||
.value_name("CLIENT_ID")
|
||||
.help("the client id")
|
||||
.required(true)
|
||||
.takes_value(true))
|
||||
.arg(Arg::with_name("secrect")
|
||||
.short("s")
|
||||
.long("secret")
|
||||
.value_name("SECRET")
|
||||
.help("the secret")
|
||||
.takes_value(true)
|
||||
.required(true))
|
||||
.get_matches();
|
||||
|
||||
let client_id = matches.value_of("client_id").unwrap();
|
||||
let secret = matches.value_of("client_id").unwrap();
|
||||
let _eng = DummyEngine {};
|
||||
let _podman_eng = PodmanEngine::new(env::current_dir().unwrap());
|
||||
//let exectuor = NixIdeManageService::new(Box::new(eng));
|
||||
let exectuor = NixIdeManageService::new(Box::new(_podman_eng));
|
||||
let mut exectuor = NixIdeManageService::new(Box::new(_podman_eng), client_id.to_owned(), secret.to_owned());
|
||||
rocket::ignite()
|
||||
.mount(
|
||||
"/api/v1/",
|
||||
|
||||
@@ -24,9 +24,32 @@ use rocket::{
|
||||
use rocket_contrib::json::Json;
|
||||
use rocket_okapi::{gen::OpenApiGenerator, response::OpenApiResponder};
|
||||
use schemars::JsonSchema;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::collections::{
|
||||
HashMap,
|
||||
hash_map::DefaultHasher
|
||||
};
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::cell::RefCell;
|
||||
use std::cell::{
|
||||
Cell,
|
||||
RefCell
|
||||
};
|
||||
|
||||
|
||||
use oauth2::{
|
||||
AuthorizationCode,
|
||||
AuthUrl,
|
||||
ClientId,
|
||||
ClientSecret,
|
||||
CsrfToken,
|
||||
PkceCodeChallenge,
|
||||
RedirectUrl,
|
||||
Scope,
|
||||
TokenResponse,
|
||||
TokenUrl,
|
||||
basic::BasicClient,
|
||||
reqwest::http_client,
|
||||
PkceCodeVerifier
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize, Hash, JsonSchema, Clone)]
|
||||
pub enum IdeState {
|
||||
@@ -58,12 +81,18 @@ pub trait NixIdeManageServiceEngine {
|
||||
fn start_open(&self, ide_id: &str, param: &OpenGitParam) -> Result<IdeState, Status>;
|
||||
}
|
||||
|
||||
pub struct RunningData{
|
||||
tokensMap : HashMap<String, PkceCodeVerifier>
|
||||
}
|
||||
pub struct NixIdeManageService {
|
||||
eng: RefCell<Box<dyn NixIdeManageServiceEngine>>,
|
||||
client_id : String,
|
||||
secret : String,
|
||||
data : RefCell<Box<RunningData>>
|
||||
}
|
||||
impl NixIdeManageService {
|
||||
pub fn new(engine: Box<dyn NixIdeManageServiceEngine>) -> Self {
|
||||
Self { eng: RefCell::new(engine) }
|
||||
pub fn new(engine: Box<dyn NixIdeManageServiceEngine>, client_id : String, secret : String) -> Self {
|
||||
Self { eng: RefCell::new(engine), client_id, secret, data : RefCell::new(Box::new(RunningData{tokensMap : HashMap::new()})) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +170,7 @@ impl<'r> Responder<'r> for SeeOtherResponse {
|
||||
}
|
||||
|
||||
impl<'r> OpenApiResponder<'r> for SeeOtherResponse {
|
||||
fn responses(gen: &mut OpenApiGenerator) -> rocket_okapi::Result<Responses> {
|
||||
fn responses(_gen: &mut OpenApiGenerator) -> rocket_okapi::Result<Responses> {
|
||||
let mut responses = Responses::default();
|
||||
rocket_okapi::util::set_status_code(&mut responses, 303)?;
|
||||
Ok(responses)
|
||||
@@ -199,28 +228,13 @@ pub fn v1_ide_state(
|
||||
.map_err(|_| status::NotFound("Sorry, I couldn't find it!".to_owned()))
|
||||
}
|
||||
|
||||
|
||||
use oauth2::{
|
||||
AuthorizationCode,
|
||||
AuthUrl,
|
||||
ClientId,
|
||||
ClientSecret,
|
||||
CsrfToken,
|
||||
PkceCodeChallenge,
|
||||
RedirectUrl,
|
||||
Scope,
|
||||
TokenResponse,
|
||||
TokenUrl
|
||||
};
|
||||
use oauth2::basic::BasicClient;
|
||||
use oauth2::reqwest::http_client;
|
||||
|
||||
#[openapi]
|
||||
#[get("/open/<inquirer>/gitea?<clone_url>&<ref_name>")]
|
||||
pub fn v1_open_inquirer_gitea(
|
||||
inquirer: String,
|
||||
clone_url: String,
|
||||
ref_name: String,
|
||||
srv: State<NixIdeManageService>
|
||||
) -> Result<SeeOtherResponse, Error> {
|
||||
let param = OpenGitParam {
|
||||
inquirer,
|
||||
@@ -232,8 +246,8 @@ pub fn v1_open_inquirer_gitea(
|
||||
// token URL.
|
||||
let client =
|
||||
BasicClient::new(
|
||||
ClientId::new("xx".to_string()),
|
||||
Some(ClientSecret::new("xxx".to_string())),
|
||||
ClientId::new(srv.client_id.clone()),
|
||||
Some(ClientSecret::new(srv.secret.clone())),
|
||||
AuthUrl::new("https://gitea.stubbe.rocks/login/oauth/authorize".to_string()).unwrap(),
|
||||
Some(TokenUrl::new("https://gitea.stubbe.rocks/login/oauth/access_token".to_string()).unwrap())
|
||||
)
|
||||
@@ -256,8 +270,9 @@ pub fn v1_open_inquirer_gitea(
|
||||
// This is the URL you should redirect the user to, in order to trigger the authorization
|
||||
// process.
|
||||
println!("Browse to: {}", auth_url);
|
||||
Ok(SeeOtherResponse(format!("{}", auth_url)))
|
||||
|
||||
srv.data.borrow_mut().tokensMap.insert(csrf_token.secret().clone(), pkce_verifier);
|
||||
Ok(SeeOtherResponse(format!("{}", auth_url)))
|
||||
// Once the user has been redirected to the redirect URL, you'll have access to the
|
||||
// authorization code. For security reasons, your code should verify that the `state`
|
||||
// parameter returned by the server matches `csrf_state`.
|
||||
|
||||
Reference in New Issue
Block a user