comparison mod_http_oauth2/mod_http_oauth2.lua @ 5418:f2c7bb3af600

mod_http_oauth2: Add role selector to consent page List includes all roles available to the user, if more than one. Defaults to either the first role in the scope string or the users primary role. Earlier draft listed all roles, but having options that can't be selected is bad UX and the entire list of all roles on the server could be long, and perhaps even sensitive. Allows e.g. picking a role with fewer permissions than what might otherwise have been selected. UX wise, doing this with more checkboxes or possibly radio buttons would have been confusion and/or looked messier. Fixes the previous situation where unselecting a role would default to the primary role, which could be more permissions than requested.
author Kim Alvefur <zash@zash.se>
date Fri, 05 May 2023 01:23:13 +0200
parents 3902082c42c4
children a0333176303c
comparison
equal deleted inserted replaced
5417:3902082c42c4 5418:f2c7bb3af600
475 error = user == "token-expired" and "Session expired - try again" or nil; 475 error = user == "token-expired" and "Session expired - try again" or nil;
476 }; 476 };
477 end 477 end
478 478
479 local scope = array():append(form):filter(function(field) 479 local scope = array():append(form):filter(function(field)
480 return field.name == "scope"; 480 return field.name == "scope" or field.name == "role";
481 end):pluck("value"):concat(" "); 481 end):pluck("value"):concat(" ");
482 482
483 user.token = form.user_token; 483 user.token = form.user_token;
484 return { 484 return {
485 user = user; 485 user = user;
652 if not auth_state.user then 652 if not auth_state.user then
653 -- Render login page 653 -- Render login page
654 return render_page(templates.login, { state = auth_state, client = client }); 654 return render_page(templates.login, { state = auth_state, client = client });
655 elseif auth_state.consent == nil then 655 elseif auth_state.consent == nil then
656 -- Render consent page 656 -- Render consent page
657 return render_page(templates.consent, { state = auth_state; client = client; scopes = parse_scopes(params.scope or "") }, true); 657 local scopes, requested_roles = split_scopes(parse_scopes(params.scope or ""));
658 local default_role = select_role(auth_state.user.username, requested_roles);
659 local roles = array(it.values(usermanager.get_all_roles(module.host))):filter(function(role)
660 return can_assume_role(auth_state.user.username, role.name);
661 end):sort(function(a, b)
662 return (a.priority or 0) < (b.priority or 0)
663 end):map(function(role)
664 return { name = role.name; selected = role.name == default_role };
665 end);
666 if not roles[2] then
667 -- Only one role to choose from, might as well skip the selector
668 roles = nil;
669 end
670 return render_page(templates.consent, { state = auth_state; client = client; scopes = scopes; roles = roles }, true);
658 elseif not auth_state.consent then 671 elseif not auth_state.consent then
659 -- Notify client of rejection 672 -- Notify client of rejection
660 return error_response(request, oauth_error("access_denied")); 673 return error_response(request, oauth_error("access_denied"));
661 end 674 end
662 -- else auth_state.consent == true 675 -- else auth_state.consent == true