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