Skip to content

Commit 9e7be6e

Browse files
committed
extract extra_values binding and simplify extract_scopes with extend
Signed-off-by: mrrajan <86094767+mrrajan@users.noreply.github.com.> (cherry picked from commit fc8d690)
1 parent 2236b9c commit 9e7be6e

2 files changed

Lines changed: 23 additions & 25 deletions

File tree

common/auth/schema/auth.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,8 @@
7979
},
8080
"scopeSelector": {
8181
"description": "JSON path extracting scopes from the access token (default: $['scope','scp'])",
82-
"type": [
83-
"string",
84-
"null"
85-
],
86-
"default": null
82+
"type": "string",
83+
"default": "$['scope','scp']"
8784
},
8885
"groupMappings": {
8986
"description": "Mapping table for groups returned found through the `groups_selector` to permissions.",

common/auth/src/authenticator/mod.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,16 @@ pub struct AuthenticatorClient {
225225
impl AuthenticatorClient {
226226
/// Convert from a set of (verified!) access token claims into a [`ValidatedAccessToken`] struct.
227227
pub fn convert_token(&self, access_token: AccessTokenClaims) -> ValidatedAccessToken {
228+
let extra_values = &access_token.extended_claims;
228229
let mut permissions = Self::map_items(
229-
Self::extract_scopes(&access_token.extended_claims, &self.scope_selector),
230+
Self::extract_scopes(extra_values, &self.scope_selector),
230231
&self.scope_mappings,
231232
);
232233
permissions.extend(self.additional_permissions.clone());
233234
let groups = self
234235
.group_selector
235236
.as_ref()
236-
.map(|selector| Self::extract_groups(&access_token.extended_claims, selector))
237+
.map(|selector| Self::extract_groups(extra_values, selector))
237238
.unwrap_or_default();
238239

239240
permissions.extend(Self::map_items(groups, &self.group_mappings));
@@ -246,24 +247,24 @@ impl AuthenticatorClient {
246247

247248
/// Extract scopes from the value/access token
248249
fn extract_scopes(value: &Value, selector: &JpQuery) -> Vec<String> {
249-
js_path_process(selector, value)
250-
.ok()
251-
.into_iter()
252-
.flatten()
253-
.flat_map(|qr| match qr.val() {
254-
Value::String(s) => s
255-
.split_ascii_whitespace()
256-
.map(|s| s.to_string())
257-
.collect::<Vec<_>>(),
258-
Value::Array(arr) => arr
259-
.iter()
260-
.filter_map(|v| v.as_str())
261-
.flat_map(|s| s.split_ascii_whitespace())
262-
.map(|s| s.to_string())
263-
.collect(),
264-
_ => vec![],
265-
})
266-
.collect()
250+
let mut result = Vec::new();
251+
for qr in js_path_process(selector, value).ok().into_iter().flatten() {
252+
match qr.val() {
253+
Value::String(s) => {
254+
result.extend(s.split_ascii_whitespace().map(str::to_string));
255+
}
256+
Value::Array(arr) => {
257+
result.extend(
258+
arr.iter()
259+
.filter_map(|v| v.as_str())
260+
.flat_map(|s| s.split_ascii_whitespace())
261+
.map(str::to_string),
262+
);
263+
}
264+
_ => {}
265+
}
266+
}
267+
result
267268
}
268269

269270
/// Extract the groups from the value/access token

0 commit comments

Comments
 (0)