Update for auth
parent: tbd commit: 3ef0383
1 | use Error; |
2 | use Error; |
3 | use ; |
4 | |
5 | use crate; |
6 | use |
7 | |
8 | AuthenticationTokenRequest, AuthenticationTokenResponse, RegisterAccountRequest, |
9 | TokenExtensionRequest, |
10 | , |
11 | , | InstanceAuthenticator
12 | ; |
13 | |
14 | use ConnectionState; |
15 | |
16 | pub async |
17 | message_type: &str, |
18 | message: &NetworkMessage, |
19 | state: &ConnectionState, |
20 | |
21 | match message_type |
22 | "giterated_models::messages::authentication::RegisterAccountRequest" => |
23 | register_account_request |
24 | .handle_message |
25 | .await?; |
26 | |
27 | Ok |
28 | |
29 | "giterated_models::messages::authentication::AuthenticationTokenRequest" => |
30 | authentication_token_request |
31 | .handle_message |
32 | .await?; |
33 | |
34 | Ok |
35 | |
36 | "giterated_models::messages::authentication::TokenExtensionRequest" => |
37 | token_extension_request |
38 | .handle_message |
39 | .await?; |
40 | |
41 | Ok |
42 | |
43 | _ => Ok, |
44 | |
45 | |
46 | |
47 | async |
48 | State | : ,
49 | Message | : ,
50 | instance: AuthenticatedInstance, |
51 | |
52 | if *instance.inner != connection_state.instance |
53 | return Err; |
54 | |
55 | |
56 | let mut user_backend = connection_state.user_backend.lock .await; |
57 | |
58 | let response = user_backend |
59 | .register |
60 | .await |
61 | .map_err?; |
62 | drop; |
63 | |
64 | connection_state |
65 | .send |
66 | .await |
67 | .map_err?; |
68 | |
69 | Ok |
70 | |
71 | |
72 | async |
73 | State | : ,
74 | Message | : ,
75 | instance: AuthenticatedInstance, |
76 | |
77 | if request.instance != connection_state.instance |
78 | // We need to perform the authentication request on behalf of |
79 | // the user. TODO: Oauth-style flow |
80 | let mut connections = connection_state.instance_connections.lock .await; |
81 | |
82 | let issued_for = instance.inner .clone; |
83 | |
84 | let connection = connections.get_or_open .unwrap; |
85 | |
86 | let private_key = |
87 | let mut file = open |
88 | connection_state.config |
89 | .as_str |
90 | .unwrap, |
91 | |
92 | .await |
93 | .unwrap; |
94 | |
95 | let mut key = String new; |
96 | file.read_to_string .await.unwrap; |
97 | |
98 | key |
99 | ; |
100 | |
101 | info!; |
102 | |
103 | let authenticator = InstanceAuthenticator |
104 | instance: connection_state.instance.clone, |
105 | private_key, |
106 | ; |
107 | |
108 | let request = AuthenticationTokenRequest |
109 | issued_for: Some, |
110 | ..request |
111 | ; |
112 | |
113 | let response = request_local |
114 | .authenticate |
115 | . |
116 | .await |
117 | .unwrap; |
118 | drop; |
119 | |
120 | connection_state |
121 | .send |
122 | .await |
123 | .map_err?; |
124 | |
125 | return Ok; |
126 | |
127 | |
128 | let mut user_backend = connection_state.user_backend.lock .await; |
129 | |
130 | let response = user_backend.login .await.map_err?; |
131 | |
132 | connection_state |
133 | .send |
134 | .await |
135 | .map_err?; |
136 | |
137 | Ok |
138 | |
139 | |
140 | async |
141 | State | : ,
142 | Message | : ,
143 | instance: AuthenticatedInstance, |
144 | |
145 | let issued_for = instance.inner .clone; |
146 | |
147 | let mut token_granter = connection_state.auth_granter.lock .await; |
148 | |
149 | let response = token_granter |
150 | .extension_request |
151 | .await |
152 | .map_err?; |
153 | |
154 | connection_state |
155 | .send |
156 | .await |
157 | .map_err?; |
158 | |
159 | Ok |
160 | |
161 | |
162 | |
163 | |
164 | |
165 | InvalidRequest, |
166 | |
167 | SameInstance, |
168 | |
169 | Registration, |
170 | |
171 | Sending, |
172 | |
173 | TokenIssuance, |
174 | |
175 |