Add authentication #1
					 6 changed files with 54 additions and 58 deletions
				
			
		change architecture
				commit
				
					
					
						b758c87521
					
				
			
		| 
						 | 
				
			
			@ -1,2 +0,0 @@
 | 
			
		|||
pub mod providers;
 | 
			
		||||
pub mod routes;
 | 
			
		||||
| 
						 | 
				
			
			@ -1,12 +0,0 @@
 | 
			
		|||
// enum Providers {
 | 
			
		||||
//     Ldap(LdapProvider),
 | 
			
		||||
//     Local(LocalProvider),
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
// struct LdapProvider {
 | 
			
		||||
//     user_filter: String,
 | 
			
		||||
//     group_filter: String,
 | 
			
		||||
//     // ...
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
// struct LocalProvider;
 | 
			
		||||
							
								
								
									
										49
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										49
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -4,62 +4,23 @@
 | 
			
		|||
#[macro_use] extern crate rocket_contrib;
 | 
			
		||||
#[macro_use] extern crate diesel;
 | 
			
		||||
 | 
			
		||||
use rocket::State;
 | 
			
		||||
use rocket::http::Status;
 | 
			
		||||
 | 
			
		||||
use rocket_contrib::json::Json;
 | 
			
		||||
 | 
			
		||||
use trust_dns_client::client::{Client, SyncClient};
 | 
			
		||||
use trust_dns_client::client::SyncClient;
 | 
			
		||||
use trust_dns_client::tcp::TcpClientConnection;
 | 
			
		||||
use trust_dns_client::op::{DnsResponse, ResponseCode};
 | 
			
		||||
use trust_dns_client::rr::{DNSClass, Name, RecordType};
 | 
			
		||||
 | 
			
		||||
mod models;
 | 
			
		||||
mod config;
 | 
			
		||||
mod auth;
 | 
			
		||||
mod schema;
 | 
			
		||||
mod routes;
 | 
			
		||||
 | 
			
		||||
use models::errors::ErrorResponse;
 | 
			
		||||
use models::users::UserInfo;
 | 
			
		||||
use auth::routes::*;
 | 
			
		||||
use routes::users::*;
 | 
			
		||||
use routes::zones::*;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#[database("db")]
 | 
			
		||||
pub struct DbConn(diesel::SqliteConnection);
 | 
			
		||||
 | 
			
		||||
type DnsClient = SyncClient<TcpClientConnection>;
 | 
			
		||||
 | 
			
		||||
#[get("/zones/<zone>/records")]
 | 
			
		||||
fn get_zone_records(
 | 
			
		||||
    client: State<SyncClient<TcpClientConnection>>,
 | 
			
		||||
    _user_info: UserInfo,
 | 
			
		||||
    zone: String
 | 
			
		||||
) -> Result<Json<Vec<models::dns::Record>>, ErrorResponse<()>> {
 | 
			
		||||
 | 
			
		||||
    // TODO: Implement FromParam for Name
 | 
			
		||||
    let name = Name::from_utf8(&zone).unwrap();
 | 
			
		||||
 | 
			
		||||
    let response: DnsResponse = client.query(&name, DNSClass::IN, RecordType::AXFR).unwrap();
 | 
			
		||||
 | 
			
		||||
    if response.response_code() != ResponseCode::NoError {
 | 
			
		||||
        return ErrorResponse::new(
 | 
			
		||||
            Status::NotFound,
 | 
			
		||||
            format!("zone {} could not be found", name.to_utf8())
 | 
			
		||||
        ).err()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let answers = response.answers();
 | 
			
		||||
    let mut records: Vec<_> = answers.to_vec().into_iter()
 | 
			
		||||
        .map(|record| models::dns::Record::from(record))
 | 
			
		||||
        .filter(|record| match record.rdata {
 | 
			
		||||
            models::dns::RData::NULL { .. } | models::dns::RData::DNSSEC(_) => false,
 | 
			
		||||
            _ => true,
 | 
			
		||||
        }).collect();
 | 
			
		||||
 | 
			
		||||
    // AXFR response ends with SOA, we remove it so it is not doubled in the response.
 | 
			
		||||
    records.pop();
 | 
			
		||||
 | 
			
		||||
    Ok(Json(records))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[launch]
 | 
			
		||||
fn rocket() -> rocket::Rocket {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								src/routes/mod.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								src/routes/mod.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
pub mod users;
 | 
			
		||||
pub mod zones;
 | 
			
		||||
							
								
								
									
										47
									
								
								src/routes/zones.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/routes/zones.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,47 @@
 | 
			
		|||
use rocket::State;
 | 
			
		||||
use rocket::http::Status;
 | 
			
		||||
 | 
			
		||||
use rocket_contrib::json::Json;
 | 
			
		||||
 | 
			
		||||
use trust_dns_client::client::{Client};
 | 
			
		||||
use trust_dns_client::op::{DnsResponse, ResponseCode};
 | 
			
		||||
use trust_dns_client::rr::{DNSClass, Name, RecordType};
 | 
			
		||||
 | 
			
		||||
use crate::models::dns;
 | 
			
		||||
use crate::models::errors::ErrorResponse;
 | 
			
		||||
use crate::models::users::UserInfo;
 | 
			
		||||
use crate::DnsClient;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#[get("/zones/<zone>/records")]
 | 
			
		||||
pub fn get_zone_records(
 | 
			
		||||
    client: State<DnsClient>,
 | 
			
		||||
    _user_info: UserInfo,
 | 
			
		||||
    zone: String
 | 
			
		||||
) -> Result<Json<Vec<dns::Record>>, ErrorResponse<()>> {
 | 
			
		||||
 | 
			
		||||
    // TODO: Implement FromParam for Name
 | 
			
		||||
    let name = Name::from_utf8(&zone).unwrap();
 | 
			
		||||
 | 
			
		||||
    let response: DnsResponse = client.query(&name, DNSClass::IN, RecordType::AXFR).unwrap();
 | 
			
		||||
 | 
			
		||||
    if response.response_code() != ResponseCode::NoError {
 | 
			
		||||
        return ErrorResponse::new(
 | 
			
		||||
            Status::NotFound,
 | 
			
		||||
            format!("zone {} could not be found", name.to_utf8())
 | 
			
		||||
        ).err()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let answers = response.answers();
 | 
			
		||||
    let mut records: Vec<_> = answers.to_vec().into_iter()
 | 
			
		||||
        .map(|record| dns::Record::from(record))
 | 
			
		||||
        .filter(|record| match record.rdata {
 | 
			
		||||
            dns::RData::NULL { .. } | dns::RData::DNSSEC(_) => false,
 | 
			
		||||
            _ => true,
 | 
			
		||||
        }).collect();
 | 
			
		||||
 | 
			
		||||
    // AXFR response ends with SOA, we remove it so it is not doubled in the response.
 | 
			
		||||
    records.pop();
 | 
			
		||||
 | 
			
		||||
    Ok(Json(records))
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue