1 |
mod substack;
|
2 |
|
3 |
use std::convert::Infallible;
|
4 |
use std::fmt::Debug;
|
5 |
use std::str::FromStr;
|
6 |
use std::{fmt::Display, sync::Arc};
|
7 |
|
8 |
use giterated_stack::models::{
|
9 |
Error, GiteratedObject, GiteratedOperation, Instance, InstanceSignature, User,
|
10 |
UserAuthenticationToken,
|
11 |
};
|
12 |
use giterated_stack::{GiteratedStack, HandlerResolvable, MissingValue};
|
13 |
use rsa::pkcs1::DecodeRsaPrivateKey;
|
14 |
use rsa::pkcs1v15::SigningKey;
|
15 |
use rsa::sha2::Sha256;
|
16 |
use rsa::signature::{RandomizedSigner, SignatureEncoding};
|
17 |
use rsa::RsaPrivateKey;
|
18 |
use serde::{Deserialize, Serialize};
|
19 |
pub use substack::{NetworkedObject, NetworkedOperation, NetworkedSubstack};
|
20 |
|
21 |
#[derive(Clone)]
|
22 |
pub struct NetworkOperationState {
|
23 |
runtime: GiteratedStack<NetworkOperationState>,
|
24 |
authentication: Vec<Arc<dyn AuthenticationSourceProviders + Send + Sync>>,
|
25 |
}
|
26 |
|
27 |
impl NetworkOperationState {
|
28 |
pub fn new(stack: GiteratedStack<NetworkOperationState>) -> Self {
|
29 |
Self {
|
30 |
runtime: stack,
|
31 |
authentication: vec![],
|
32 |
}
|
33 |
}
|
34 |
}
|
35 |
|
36 |
impl NetworkOperationState {
|
37 |
pub fn authenticate(
|
38 |
&mut self,
|
39 |
provider: impl AuthenticationSourceProviders + Send + Sync + 'static,
|
40 |
) {
|
41 |
self.authentication.push(Arc::new(provider))
|
42 |
}
|
43 |
}
|
44 |
|
45 |
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
46 |
pub struct AuthenticatedPayload {
|
47 |
pub source: Vec<AuthenticationSource>,
|
48 |
pub object: String,
|
49 |
pub operation: String,
|
50 |
pub payload: Vec<u8>,
|
51 |
}
|
52 |
|
53 |
impl AuthenticatedPayload {
|
54 |
pub fn into_message(self) -> GiteratedMessage<NetworkedObject, NetworkedOperation> {
|
55 |
GiteratedMessage {
|
56 |
object: NetworkedObject::from_str(&self.object).unwrap(),
|
57 |
operation: self.operation,
|
58 |
payload: serde_json::from_slice(&self.payload).unwrap(),
|
59 |
}
|
60 |
}
|
61 |
}
|
62 |
|
63 |
pub trait AuthenticationSourceProvider: Debug {
|
64 |
fn authenticate(&self, payload: &Vec<u8>) -> AuthenticationSource;
|
65 |
}
|
66 |
|
67 |
pub trait AuthenticationSourceProviders: Debug {
|
68 |
fn authenticate_all(&self, payload: &Vec<u8>) -> Vec<AuthenticationSource>;
|
69 |
}
|
70 |
|
71 |
impl<A> AuthenticationSourceProviders for A
|
72 |
where
|
73 |
A: AuthenticationSourceProvider,
|
74 |
{
|
75 |
fn authenticate_all(&self, payload: &Vec<u8>) -> Vec<AuthenticationSource> {
|
76 |
vec![self.authenticate(payload)]
|
77 |
}
|
78 |
}
|
79 |
|
80 |
impl<A, B> AuthenticationSourceProviders for (A, B)
|
81 |
where
|
82 |
A: AuthenticationSourceProvider,
|
83 |
B: AuthenticationSourceProvider,
|
84 |
{
|
85 |
fn authenticate_all(&self, payload: &Vec<u8>) -> Vec<AuthenticationSource> {
|
86 |
let (first, second) = self;
|
87 |
|
88 |
vec![first.authenticate(payload), second.authenticate(payload)]
|
89 |
}
|
90 |
}
|
91 |
|
92 |
mod verified {}
|
93 |
|
94 |
#[derive(Clone, Debug)]
|
95 |
pub struct UserAuthenticator {
|
96 |
pub user: User,
|
97 |
pub token: UserAuthenticationToken,
|
98 |
}
|
99 |
|
100 |
impl AuthenticationSourceProvider for UserAuthenticator {
|
101 |
fn authenticate(&self, _payload: &Vec<u8>) -> AuthenticationSource {
|
102 |
AuthenticationSource::User {
|
103 |
user: self.user.clone(),
|
104 |
token: self.token.clone(),
|
105 |
}
|
106 |
}
|
107 |
}
|
108 |
|
109 |
#[derive(Debug, Clone)]
|
110 |
pub struct InstanceAuthenticator {
|
111 |
pub instance: Instance,
|
112 |
pub private_key: String,
|
113 |
}
|
114 |
|
115 |
impl AuthenticationSourceProvider for InstanceAuthenticator {
|
116 |
fn authenticate(&self, payload: &Vec<u8>) -> AuthenticationSource {
|
117 |
let mut rng = rand::thread_rng();
|
118 |
|
119 |
let private_key = RsaPrivateKey::from_pkcs1_pem(&self.private_key).unwrap();
|
120 |
let signing_key = SigningKey::<Sha256>::new(private_key);
|
121 |
let signature = signing_key.sign_with_rng(&mut rng, payload);
|
122 |
|
123 |
AuthenticationSource::Instance {
|
124 |
instance: self.instance.clone(),
|
125 |
|
126 |
signature: InstanceSignature(signature.to_bytes().into_vec()),
|
127 |
}
|
128 |
}
|
129 |
}
|
130 |
|
131 |
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
132 |
pub enum AuthenticationSource {
|
133 |
User {
|
134 |
user: User,
|
135 |
token: UserAuthenticationToken,
|
136 |
},
|
137 |
Instance {
|
138 |
instance: Instance,
|
139 |
signature: InstanceSignature,
|
140 |
},
|
141 |
}
|
142 |
|
143 |
#[derive(Serialize)]
|
144 |
#[serde(bound(deserialize = "O: GiteratedObject, V: GiteratedOperation<O>"))]
|
145 |
pub struct GiteratedMessage<O: GiteratedObject, V: GiteratedOperation<O>> {
|
146 |
#[serde(with = "string")]
|
147 |
pub object: O,
|
148 |
pub operation: String,
|
149 |
pub payload: V,
|
150 |
}
|
151 |
|
152 |
#[allow(unused)]
|
153 |
mod string {
|
154 |
use std::fmt::Display;
|
155 |
use std::str::FromStr;
|
156 |
|
157 |
use serde::{de, Deserialize, Deserializer, Serializer};
|
158 |
|
159 |
pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
|
160 |
where
|
161 |
T: Display,
|
162 |
S: Serializer,
|
163 |
{
|
164 |
serializer.collect_str(value)
|
165 |
}
|
166 |
|
167 |
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
|
168 |
where
|
169 |
T: FromStr,
|
170 |
T::Err: Display,
|
171 |
D: Deserializer<'de>,
|
172 |
{
|
173 |
String::deserialize(deserializer)?
|
174 |
.parse()
|
175 |
.map_err(de::Error::custom)
|
176 |
}
|
177 |
}
|
178 |
|
179 |
impl GiteratedMessage<NetworkAnyObject, NetworkAnyOperation> {
|
180 |
pub fn try_into<O: GiteratedObject, V: GiteratedOperation<O>>(
|
181 |
&self,
|
182 |
) -> Result<GiteratedMessage<O, V>, ()> {
|
183 |
let object = O::from_object_str(&self.object.0).map_err(|_| ())?;
|
184 |
let payload = serde_json::from_slice::<V>(&self.payload.0).map_err(|_| ())?;
|
185 |
|
186 |
Ok(GiteratedMessage {
|
187 |
object,
|
188 |
operation: self.operation.clone(),
|
189 |
payload,
|
190 |
})
|
191 |
}
|
192 |
}
|
193 |
|
194 |
impl<V: GiteratedOperation<O> + Debug, O: GiteratedObject + Debug> Debug
|
195 |
for GiteratedMessage<O, V>
|
196 |
{
|
197 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
198 |
f.debug_struct("GiteratedMessage")
|
199 |
.field("object", &self.object)
|
200 |
.field("operation", &self.operation)
|
201 |
.field("payload", &self.payload)
|
202 |
.finish()
|
203 |
}
|
204 |
}
|
205 |
|
206 |
#[derive(Clone, Debug, Serialize, Deserialize)]
|
207 |
#[serde(transparent)]
|
208 |
#[repr(transparent)]
|
209 |
pub struct NetworkAnyObject(pub String);
|
210 |
|
211 |
impl GiteratedObject for NetworkAnyObject {
|
212 |
fn object_name() -> &'static str {
|
213 |
"network_object"
|
214 |
}
|
215 |
|
216 |
fn from_object_str(object_str: &str) -> Result<Self, Error> {
|
217 |
Ok(Self(object_str.to_string()))
|
218 |
}
|
219 |
|
220 |
fn home_uri(&self) -> String {
|
221 |
todo!()
|
222 |
}
|
223 |
}
|
224 |
|
225 |
impl Display for NetworkAnyObject {
|
226 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
227 |
f.write_str(&self.0)
|
228 |
}
|
229 |
}
|
230 |
|
231 |
impl FromStr for NetworkAnyObject {
|
232 |
type Err = Infallible;
|
233 |
|
234 |
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
235 |
Ok(Self(s.to_owned()))
|
236 |
}
|
237 |
}
|
238 |
|
239 |
#[derive(Clone, Debug, Serialize, Deserialize)]
|
240 |
#[serde(transparent)]
|
241 |
#[repr(transparent)]
|
242 |
pub struct NetworkAnyOperation(pub Vec<u8>);
|
243 |
|
244 |
impl<O: GiteratedObject> GiteratedOperation<O> for NetworkAnyOperation {
|
245 |
type Success = Vec<u8>;
|
246 |
|
247 |
type Failure = Vec<u8>;
|
248 |
}
|
249 |
|
250 |
#[async_trait::async_trait(?Send)]
|
251 |
impl<R1> HandlerResolvable<(R1,), NetworkOperationState> for GiteratedStack<NetworkOperationState> {
|
252 |
type Error = MissingValue;
|
253 |
|
254 |
async fn from_handler_state(
|
255 |
_required_parameters: &(R1,),
|
256 |
operation_state: &NetworkOperationState,
|
257 |
) -> Result<Self, Self::Error> {
|
258 |
Ok(operation_state.runtime.clone())
|
259 |
}
|
260 |
}
|
261 |
|
262 |
#[async_trait::async_trait(?Send)]
|
263 |
impl<R1, R2> HandlerResolvable<(R1, R2), NetworkOperationState>
|
264 |
for GiteratedStack<NetworkOperationState>
|
265 |
{
|
266 |
type Error = MissingValue;
|
267 |
|
268 |
async fn from_handler_state(
|
269 |
_required_parameters: &(R1, R2),
|
270 |
operation_state: &NetworkOperationState,
|
271 |
) -> Result<Self, Self::Error> {
|
272 |
Ok(operation_state.runtime.clone())
|
273 |
}
|
274 |
}
|
275 |
|