kanidmd_lib/idm/
applinks.rs1use crate::{idm::server::IdmServerProxyReadTransaction, prelude::*};
2use kanidm_proto::internal::AppLink;
3
4impl IdmServerProxyReadTransaction<'_> {
5 pub fn list_applinks(&mut self, ident: &Identity) -> Result<Vec<AppLink>, OperationError> {
6 let Some(ident_mo) = ident.get_memberof() else {
8 debug!("Ident has no memberof, no applinks are present");
9 return Ok(Vec::with_capacity(0));
10 };
11
12 let f_executed = filter!(f_or(
20 ident_mo
21 .iter()
22 .copied()
23 .map(|uuid| { f_eq(Attribute::OAuth2RsScopeMap, PartialValue::Refer(uuid)) })
24 .collect()
25 ));
26 let f_intent = filter!(f_eq(
27 Attribute::Class,
28 EntryClass::OAuth2ResourceServer.into()
29 ));
30
31 let oauth2_related = self
33 .qs_read
34 .impersonate_search_ext(f_executed, f_intent, ident)?;
35 trace!(?oauth2_related);
36
37 let mut apps = oauth2_related
39 .iter()
40 .filter_map(|entry| {
41 let display_name = entry
42 .get_ava_single_utf8(Attribute::DisplayName)
43 .map(str::to_string)?;
44
45 let redirect_url = entry
46 .get_ava_single_url(Attribute::OAuth2RsOriginLanding)
47 .cloned()?;
48
49 let name = entry
50 .get_ava_single_iname(Attribute::Name)
51 .map(str::to_string)?;
52
53 let has_image = entry.get_ava_single_image(Attribute::Image).is_some();
54
55 Some(AppLink::Oauth2 {
56 name,
57 display_name,
58 redirect_url,
59 has_image,
60 })
61 })
62 .collect::<Vec<_>>();
63
64 apps.sort_unstable_by_key(|a| match a {
66 AppLink::Oauth2 { display_name, .. } => display_name.clone(),
67 });
68
69 debug!("returned {} related apps", apps.len());
70 trace!(?apps);
71
72 Ok(apps)
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use crate::prelude::*;
79 use kanidm_proto::internal::AppLink;
80
81 #[idm_test]
82 async fn test_idm_applinks_list(idms: &IdmServer, _idms_delayed: &mut IdmServerDelayed) {
83 let ct = duration_from_epoch_now();
84 let mut idms_prox_write = idms.proxy_write(ct).await.unwrap();
85
86 let usr_uuid = Uuid::new_v4();
88 let grp_uuid = Uuid::new_v4();
89
90 let e_rs: Entry<EntryInit, EntryNew> = entry_init!(
91 (Attribute::Class, EntryClass::Object.to_value()),
92 (Attribute::Class, EntryClass::Account.to_value()),
93 (
94 Attribute::Class,
95 EntryClass::OAuth2ResourceServer.to_value()
96 ),
97 (
98 Attribute::Class,
99 EntryClass::OAuth2ResourceServerBasic.to_value()
100 ),
101 (Attribute::Name, Value::new_iname("test_resource_server")),
102 (
103 Attribute::DisplayName,
104 Value::new_utf8s("test_resource_server")
105 ),
106 (
107 Attribute::OAuth2RsOrigin,
108 Value::new_urln_s("https://demo.example.com").unwrap()
109 ),
110 (
111 Attribute::OAuth2RsOriginLanding,
112 Value::new_url_s("https://demo.example.com/landing").unwrap()
113 ),
114 (
116 Attribute::OAuth2RsScopeMap,
117 Value::new_oauthscopemap(
118 grp_uuid,
119 btreeset![kanidm_proto::constants::OAUTH2_SCOPE_READ.to_string()]
120 )
121 .expect("invalid oauthscope")
122 )
123 );
124
125 let e_usr = entry_init!(
126 (Attribute::Class, EntryClass::Object.to_value()),
127 (Attribute::Class, EntryClass::Account.to_value()),
128 (Attribute::Class, EntryClass::Person.to_value()),
129 (Attribute::Name, Value::new_iname("testaccount")),
130 (Attribute::Uuid, Value::Uuid(usr_uuid)),
131 (Attribute::Description, Value::new_utf8s("testaccount")),
132 (Attribute::DisplayName, Value::new_utf8s("Test Account"))
133 );
134
135 let e_grp = entry_init!(
136 (Attribute::Class, EntryClass::Object.to_value()),
137 (Attribute::Class, EntryClass::Group.to_value()),
138 (Attribute::Uuid, Value::Uuid(grp_uuid)),
139 (Attribute::Name, Value::new_iname("test_oauth2_group"))
140 );
141
142 let ce = CreateEvent::new_internal(vec![e_rs, e_grp, e_usr]);
143 assert!(idms_prox_write.qs_write.create(&ce).is_ok());
144 assert!(idms_prox_write.commit().is_ok());
145
146 let mut idms_prox_read = idms.proxy_read().await.unwrap();
148
149 let ident = idms_prox_read
150 .qs_read
151 .internal_search_uuid(usr_uuid)
152 .map(Identity::from_impersonate_entry_readonly)
153 .expect("Failed to impersonate identity");
154
155 let apps = idms_prox_read
156 .list_applinks(&ident)
157 .expect("Failed to access related apps");
158
159 assert!(apps.is_empty());
160 drop(idms_prox_read);
161
162 let mut idms_prox_write = idms.proxy_write(ct).await.unwrap();
164 let me_inv_m = ModifyEvent::new_internal_invalid(
165 filter!(f_eq(Attribute::Uuid, PartialValue::Refer(grp_uuid))),
166 ModifyList::new_append(Attribute::Member, Value::ReferN(usr_uuid)),
167 );
168 assert!(idms_prox_write.qs_write.modify(&me_inv_m).is_ok());
169 assert!(idms_prox_write.commit().is_ok());
170
171 let mut idms_prox_read = idms.proxy_read().await.unwrap();
172
173 let ident = idms_prox_read
174 .qs_read
175 .internal_search_uuid(usr_uuid)
176 .map(Identity::from_impersonate_entry_readonly)
177 .expect("Failed to impersonate identity");
178
179 let apps = idms_prox_read
180 .list_applinks(&ident)
181 .expect("Failed to access related apps");
182
183 let app = apps.first().expect("No apps return!");
184
185 assert!(match app {
186 AppLink::Oauth2 {
187 name,
188 display_name,
189 redirect_url,
190 has_image,
191 } => {
192 name == "test_resource_server"
193 && display_name == "test_resource_server"
194 && redirect_url
195 == &Url::parse("https://demo.example.com/landing")
196 .expect("Failed to parse URL")
197 && !has_image
198 } })
200 }
201}