Diff

mod_http_oauth2/mod_http_oauth2.lua @ 5448:9d542e86e19a

mod_http_oauth2: Allow requesting a subset of scopes on token refresh This enables clients to request access tokens with fewer permissions than the grant they were given, reducing impact of token leak. Clients could e.g. request access tokens with some privileges and immediately revoke them after use, or other strategies.
author Kim Alvefur <zash@zash.se>
date Thu, 11 May 2023 21:40:09 +0200
parent 5447:aa4828f040c5
child 5449:9c19a6b8e542
line wrap: on
line diff
--- a/mod_http_oauth2/mod_http_oauth2.lua	Thu May 11 19:33:44 2023 +0200
+++ b/mod_http_oauth2/mod_http_oauth2.lua	Thu May 11 21:40:09 2023 +0200
@@ -411,14 +411,22 @@
 	end
 
 	local refresh_scopes = refresh_token_info.grant.data.oauth2_scopes;
+
+	if params.scope then
+		local granted_scopes = set.new(parse_scopes(refresh_scopes));
+		local requested_scopes = parse_scopes(params.scope);
+		refresh_scopes = array.filter(requested_scopes, function(scope)
+			return granted_scopes:contains(scope);
+		end):concat(" ");
+	end
+
+	local username = jid.split(refresh_token_info.jid);
 	local new_scopes, role = filter_scopes(username, refresh_scopes);
 
 	-- new_access_token() requires the actual token
 	refresh_token_info.token = params.refresh_token;
 
-	return json.encode(new_access_token(
-		refresh_token_info.jid, role, new_scopes, client, nil, refresh_token_info
-	));
+	return json.encode(new_access_token(refresh_token_info.jid, role, new_scopes, client, nil, refresh_token_info));
 end
 
 -- RFC 7636 Proof Key for Code Exchange by OAuth Public Clients