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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use super::{config, AuthFuture};
use crate::app_auth::{app_state, AppState};
use crate::client::AuthClient;
use crate::{app_container, AuthError};
use bincode::deserialize;
use futures::future::Future;
use safe_core::client::{AuthActions, Client};
use safe_core::core_structs::{access_container_enc_key, AccessContainerEntry, AppAccess};
use safe_core::ipc::req::ContainerPermissions;
use safe_core::ipc::{AppExchangeInfo, IpcError};
use safe_core::utils::symmetric_decrypt;
use safe_core::FutureExt;
use safe_nd::{AppPermissions, MDataAddress, XorName};
use std::collections::HashMap;
#[derive(Debug)]
pub struct RegisteredApp {
pub app_info: AppExchangeInfo,
pub containers: HashMap<String, ContainerPermissions>,
pub app_perms: AppPermissions,
}
pub fn remove_revoked_app(client: &AuthClient, app_id: String) -> Box<AuthFuture<()>> {
let client = client.clone();
let c2 = client.clone();
let c3 = client.clone();
let c4 = client.clone();
let app_id2 = app_id.clone();
let app_id3 = app_id.clone();
config::list_apps(&client)
.and_then(move |(apps_version, apps)| {
app_state(&c2, &apps, &app_id).map(move |app_state| (app_state, apps, apps_version))
})
.and_then(move |(app_state, apps, apps_version)| match app_state {
AppState::Revoked => Ok((apps, apps_version)),
AppState::Authenticated => Err(AuthError::from("App is not revoked")),
AppState::NotAuthenticated => Err(AuthError::IpcError(IpcError::UnknownApp)),
})
.and_then(move |(apps, apps_version)| {
config::remove_app(&c3, apps, config::next_version(apps_version), &app_id2)
})
.and_then(move |_| app_container::remove(c4, &app_id3).map(move |_res| ()))
.into_box()
}
pub fn list_revoked(client: &AuthClient) -> Box<AuthFuture<Vec<AppExchangeInfo>>> {
let c2 = client.clone();
let c3 = client.clone();
config::list_apps(client)
.map(move |(_, auth_cfg)| (c2.access_container(), auth_cfg))
.and_then(move |(access_container, auth_cfg)| {
c3.list_seq_mdata_entries(access_container.name(), access_container.type_tag())
.map_err(From::from)
.map(move |entries| (access_container, entries, auth_cfg))
})
.and_then(move |(access_container, entries, auth_cfg)| {
let mut apps = Vec::new();
let nonce = access_container
.nonce()
.ok_or_else(|| AuthError::from("No nonce on access container's MDataInfo"))?;
for app in auth_cfg.values() {
let key = access_container_enc_key(&app.info.id, &app.keys.enc_key, nonce)?;
let revoked = entries
.get(&key)
.map_or(true, |entry| entry.data.is_empty());
if revoked {
apps.push(app.info.clone());
}
}
Ok(apps)
})
.into_box()
}
pub fn list_registered(client: &AuthClient) -> Box<AuthFuture<Vec<RegisteredApp>>> {
let c2 = client.clone();
let c3 = client.clone();
let c4 = client.clone();
config::list_apps(client)
.map(move |(_, auth_cfg)| (c2.access_container(), auth_cfg))
.and_then(move |(access_container, auth_cfg)| {
c3.list_seq_mdata_entries(access_container.name(), access_container.type_tag())
.map_err(From::from)
.map(move |entries| (access_container, entries, auth_cfg))
})
.and_then(move |(access_container, entries, auth_cfg)| {
c4.list_auth_keys_and_version().map_err(From::from).map(
move |(authorised_keys, _version)| {
(authorised_keys, access_container, entries, auth_cfg)
},
)
})
.and_then(
move |(mut authorised_keys, access_container, entries, auth_cfg)| {
let mut apps = Vec::new();
let nonce = access_container
.nonce()
.ok_or_else(|| AuthError::from("No nonce on access container's MDataInfo"))?;
for app in auth_cfg.values() {
let key = access_container_enc_key(&app.info.id, &app.keys.enc_key, nonce)?;
let entry = match entries.get(&key) {
Some(entry) if !entry.data.is_empty() => Some(entry),
_ => None,
};
if let Some(entry) = entry {
let plaintext = symmetric_decrypt(&entry.data, &app.keys.enc_key)?;
let app_access = deserialize::<AccessContainerEntry>(&plaintext)?;
let mut containers = HashMap::new();
for (container_name, (_, permission_set)) in app_access {
let _ = containers.insert(container_name, permission_set);
}
let app_public_key = app.keys.public_key();
let app_perms = authorised_keys.remove(&app_public_key).unwrap_or_default();
let registered_app = RegisteredApp {
app_info: app.info.clone(),
containers,
app_perms,
};
apps.push(registered_app);
}
}
Ok(apps)
},
)
.into_box()
}
pub fn apps_accessing_mutable_data(
client: &AuthClient,
name: XorName,
type_tag: u64,
) -> Box<AuthFuture<Vec<AppAccess>>> {
let c2 = client.clone();
client
.list_mdata_permissions(MDataAddress::Seq {
name,
tag: type_tag,
})
.map_err(AuthError::from)
.join(config::list_apps(&c2).map(|(_, apps)| {
apps.into_iter()
.map(|(_, app_info)| (app_info.keys.public_key(), app_info.info))
.collect::<HashMap<_, _>>()
}))
.and_then(move |(permissions, apps)| {
let mut app_access_vec: Vec<AppAccess> = Vec::new();
for (user, perm_set) in permissions {
let app_access = match apps.get(&user) {
Some(app_info) => AppAccess {
sign_key: user,
permissions: perm_set,
name: Some(app_info.name.clone()),
app_id: Some(app_info.id.clone()),
},
None => {
AppAccess {
sign_key: user,
permissions: perm_set,
name: None,
app_id: None,
}
}
};
app_access_vec.push(app_access);
}
Ok(app_access_vec)
})
.into_box()
}