Add token authentication for controllers
This commit is contained in:
		
							parent
							
								
									faacdc8c83
								
							
						
					
					
						commit
						e2dc5f8857
					
				
					 3 changed files with 9 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,10 @@
 | 
			
		|||
use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
pub const DEFAULT_PORT: u16 = 4419;
 | 
			
		||||
pub const DEFAULT_TOKEN: &str = "emgauwa_token";
 | 
			
		||||
 | 
			
		||||
pub const CONTROLLER_WS_TOKEN_HEADER: &str = "X-Emgauwa-Token";
 | 
			
		||||
 | 
			
		||||
pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
 | 
			
		||||
pub const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(15);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,6 +15,7 @@ use crate::types::EmgauwaUid;
 | 
			
		|||
#[derive(Debug)]
 | 
			
		||||
pub enum EmgauwaError {
 | 
			
		||||
	Api(ApiError),
 | 
			
		||||
	Unauthorized(String),
 | 
			
		||||
	Uid(uuid::Error),
 | 
			
		||||
	Serialization(serde_json::Error),
 | 
			
		||||
	Database(DatabaseError),
 | 
			
		||||
| 
						 | 
				
			
			@ -28,6 +29,7 @@ impl EmgauwaError {
 | 
			
		|||
	fn get_code(&self) -> StatusCode {
 | 
			
		||||
		match self {
 | 
			
		||||
			EmgauwaError::Api(err) => err.get_code(),
 | 
			
		||||
			EmgauwaError::Unauthorized(_) => StatusCode::UNAUTHORIZED,
 | 
			
		||||
			EmgauwaError::Serialization(_) => StatusCode::INTERNAL_SERVER_ERROR,
 | 
			
		||||
			EmgauwaError::Database(err) => err.get_code(),
 | 
			
		||||
			EmgauwaError::Uid(_) => StatusCode::BAD_REQUEST,
 | 
			
		||||
| 
						 | 
				
			
			@ -43,6 +45,7 @@ impl From<&EmgauwaError> for String {
 | 
			
		|||
	fn from(err: &EmgauwaError) -> Self {
 | 
			
		||||
		match err {
 | 
			
		||||
			EmgauwaError::Api(err) => String::from(err),
 | 
			
		||||
			EmgauwaError::Unauthorized(_) => format!("unauthorized request: {}", err),
 | 
			
		||||
			EmgauwaError::Serialization(_) => String::from("error during (de-)serialization"),
 | 
			
		||||
			EmgauwaError::Database(err) => String::from(err),
 | 
			
		||||
			EmgauwaError::Uid(_) => String::from("the uid is in a bad format"),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,6 +9,7 @@ use crate::errors::EmgauwaError;
 | 
			
		|||
pub struct Server {
 | 
			
		||||
	pub host: String,
 | 
			
		||||
	pub port: u16,
 | 
			
		||||
	pub token: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug, Deserialize)]
 | 
			
		||||
| 
						 | 
				
			
			@ -31,6 +32,7 @@ impl Default for Server {
 | 
			
		|||
		Server {
 | 
			
		||||
			host: String::from("127.0.0.1"),
 | 
			
		||||
			port: constants::DEFAULT_PORT,
 | 
			
		||||
			token: String::from(constants::DEFAULT_TOKEN),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue