1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
pub mod builder;
#[cfg(any(feature = "trust-authorization", feature = "challenge-authorization"))]
mod v1_handlers;
use crate::network::auth::{
AuthorizationAcceptingAction, AuthorizationAcceptingState, AuthorizationManagerStateMachine,
AuthorizationMessageSender,
};
use crate::network::dispatch::{
ConnectionId, DispatchError, Dispatcher, Handler, MessageContext, MessageSender,
};
use crate::protocol::authorization::AuthorizationError;
use crate::protos::authorization;
use crate::protos::network::NetworkMessageType;
use crate::protos::prelude::*;
pub use self::builder::AuthorizationDispatchBuilder;
pub struct AuthorizationMessageHandler {
auth_dispatcher: Dispatcher<authorization::AuthorizationMessageType, ConnectionId>,
}
impl AuthorizationMessageHandler {
pub fn new(
auth_dispatcher: Dispatcher<authorization::AuthorizationMessageType, ConnectionId>,
) -> Self {
AuthorizationMessageHandler { auth_dispatcher }
}
}
impl Handler for AuthorizationMessageHandler {
type Source = ConnectionId;
type MessageType = NetworkMessageType;
type Message = authorization::AuthorizationMessage;
fn match_type(&self) -> Self::MessageType {
NetworkMessageType::AUTHORIZATION
}
fn handle(
&self,
mut msg: Self::Message,
context: &MessageContext<Self::Source, Self::MessageType>,
_sender: &dyn MessageSender<Self::Source>,
) -> Result<(), DispatchError> {
let msg_type = msg.get_message_type();
let payload = msg.take_payload();
self.auth_dispatcher
.dispatch(context.source_id().clone(), &msg_type, payload)
}
}
pub struct AuthorizationErrorHandler {
auth_manager: AuthorizationManagerStateMachine,
}
impl AuthorizationErrorHandler {
pub fn new(auth_manager: AuthorizationManagerStateMachine) -> Self {
AuthorizationErrorHandler { auth_manager }
}
}
impl Handler for AuthorizationErrorHandler {
type Source = ConnectionId;
type MessageType = authorization::AuthorizationMessageType;
type Message = authorization::AuthorizationError;
fn match_type(&self) -> Self::MessageType {
authorization::AuthorizationMessageType::AUTHORIZATION_ERROR
}
fn handle(
&self,
msg: Self::Message,
context: &MessageContext<Self::Source, Self::MessageType>,
_: &dyn MessageSender<Self::Source>,
) -> Result<(), DispatchError> {
let auth_error = AuthorizationError::from_proto(msg)?;
match auth_error {
AuthorizationError::AuthorizationRejected(err_msg) => {
match self.auth_manager.next_accepting_state(
context.source_connection_id(),
AuthorizationAcceptingAction::Unauthorizing,
) {
Ok(AuthorizationAcceptingState::Unauthorized) => {
info!(
"Connection unauthorized by connection {}: {}",
context.source_connection_id(),
&err_msg
);
}
Err(err) => {
warn!(
"Unable to handle unauthorizing by connection {}: {}",
context.source_connection_id(),
err
);
}
Ok(next_state) => {
panic!("Should not have been able to transition to {}", next_state)
}
}
}
}
Ok(())
}
}
impl MessageSender<ConnectionId> for AuthorizationMessageSender {
fn send(&self, id: ConnectionId, message: Vec<u8>) -> Result<(), (ConnectionId, Vec<u8>)> {
AuthorizationMessageSender::send(self, message).map_err(|msg| (id, msg))
}
}