authentik.enterprise.requests.grants
Persistence of grant requests, shared by the browser flow's final stage and the headless agent endpoint.
1"""Persistence of grant requests, shared by the browser flow's final stage and the headless 2agent endpoint.""" 3 4from dataclasses import dataclass 5from datetime import timedelta 6from typing import Any 7 8from django.db import transaction 9from django.db.models import QuerySet 10from django.http import HttpRequest 11from django.urls import reverse 12from django.utils.timezone import now 13 14from authentik.core.models import User 15from authentik.enterprise.requests.models import ( 16 GrantRequest, 17 GrantRequestTarget, 18 RequestRule, 19 RequestStatus, 20) 21from authentik.enterprise.requests.tasks import requests_send_request_notification 22from authentik.events.middleware import audit_ignore 23from authentik.events.models import Event, EventAction 24from authentik.lib.utils.time import timedelta_to_string 25from authentik.policies.models import PolicyBindingModel 26 27 28@dataclass(frozen=True) 29class GrantExpiry: 30 """The two durations a request carries: how long it stays pending before lapsing, and how 31 long the grant lasts once approved.""" 32 33 pending: timedelta 34 granted: timedelta 35 36 37def fulfill_url(request: HttpRequest, req: GrantRequest) -> str: 38 """The page an approver opens to act on `req`. Also what an agent hands back to the human 39 it acts for, since it has no browser of its own to run an approval in.""" 40 return ( 41 request.build_absolute_uri(reverse("authentik_core:if-user")) 42 + f"#/requests/access-request/{req.uuid}/fulfill" 43 ) 44 45 46def assign_request_permissions( 47 rules: QuerySet[RequestRule], req: GrantRequest, agent_owner: User | None = None 48): 49 """Grant the requester and every reviewer eligible for any of `rules` 50 object-level permission on `req`, so they can see and act on it without 51 needing a blanket, org-wide grant of those permissions.""" 52 req.created_by.assign_perms_to_managed_role( 53 ["authentik_requests.view_grantrequest", "authentik_requests.delete_grantrequest"], 54 req, 55 ) 56 reviewers: set[User] = set() 57 for rule in rules: 58 reviewers.update(rule.reviewers_among(User.objects.all())) 59 if agent_owner: 60 # An agent's owner must approve its request, so they need the same object-level access 61 # a reviewer gets -- whether or not any rule makes them an eligible reviewer, and even 62 # when the request carries no rule at all. 63 reviewers.add(agent_owner) 64 for reviewer in reviewers: 65 reviewer.assign_perms_to_managed_role( 66 [ 67 "authentik_requests.view_grantrequest", 68 "authentik_requests.fulfill_grantrequest", 69 "authentik_requests.revoke_grantrequest", 70 ], 71 req, 72 ) 73 74 75def notify_reviewers(rules: QuerySet[RequestRule], event: Event): 76 """Notify reviewers of each rule attached to any of the requested targets, 77 per that rule's own notification_transports/notification_mode. An agent request resolves no 78 rules, so nothing is dispatched for one -- its owner reaches it through the `fulfill_url` 79 handed back to the agent, and through `pending_review`.""" 80 for rule in rules: 81 transports = list(rule.notification_transports.all()) 82 if not transports: 83 continue 84 for recipient in rule.notification_recipients(): 85 for transport in transports: 86 requests_send_request_notification.send_with_options( 87 args=(transport.pk, event.pk, recipient.pk), 88 rel_obj=transport, 89 ) 90 91 92def create_grant_request( 93 request: HttpRequest, 94 *, 95 created_by: User, 96 pbms: list[PolicyBindingModel], 97 requester_data: dict[str, Any], 98 expiry: GrantExpiry, 99 agent_owner: User | None = None, 100) -> GrantRequest: 101 """Persist a grant request against `pbms`, assign the object permissions the requester, 102 reviewers and (for an agent) its owner need to act on it, and notify them.""" 103 with transaction.atomic(), audit_ignore(): 104 req = GrantRequest.objects.create( 105 created_by=created_by, 106 requester_data=requester_data, 107 requested_expiry=timedelta_to_string(expiry.granted), 108 expiring=True, 109 expires=now() + expiry.pending, 110 status=RequestStatus.CREATED, 111 agent_owner=agent_owner, 112 ) 113 for pbm in pbms: 114 GrantRequestTarget.objects.create( 115 request=req, 116 target=pbm, 117 binding=None, 118 ) 119 # An agent request is decided by its owner alone, so RequestRules attached to the target 120 # play no part: their reviewers must not be handed fulfill/revoke on it, and telling 121 # them about a request they have no say in would only leak it. 122 rules = ( 123 RequestRule.objects.none() 124 if agent_owner 125 else RequestRule.objects.filter(targets__in=pbms) 126 .distinct() 127 .prefetch_related("notification_transports") 128 ) 129 assign_request_permissions(rules, req, agent_owner) 130 event = Event.new( 131 EventAction.ACCESS_REQUEST_CREATED, 132 model=req, 133 targets=pbms, 134 requester_data=requester_data, 135 hyperlink=fulfill_url(request, req), 136 hyperlink_label="Fulfill", 137 ).from_http(request, created_by) 138 notify_reviewers(rules, event) 139 return req
29@dataclass(frozen=True) 30class GrantExpiry: 31 """The two durations a request carries: how long it stays pending before lapsing, and how 32 long the grant lasts once approved.""" 33 34 pending: timedelta 35 granted: timedelta
The two durations a request carries: how long it stays pending before lapsing, and how long the grant lasts once approved.
38def fulfill_url(request: HttpRequest, req: GrantRequest) -> str: 39 """The page an approver opens to act on `req`. Also what an agent hands back to the human 40 it acts for, since it has no browser of its own to run an approval in.""" 41 return ( 42 request.build_absolute_uri(reverse("authentik_core:if-user")) 43 + f"#/requests/access-request/{req.uuid}/fulfill" 44 )
The page an approver opens to act on req. Also what an agent hands back to the human
it acts for, since it has no browser of its own to run an approval in.
47def assign_request_permissions( 48 rules: QuerySet[RequestRule], req: GrantRequest, agent_owner: User | None = None 49): 50 """Grant the requester and every reviewer eligible for any of `rules` 51 object-level permission on `req`, so they can see and act on it without 52 needing a blanket, org-wide grant of those permissions.""" 53 req.created_by.assign_perms_to_managed_role( 54 ["authentik_requests.view_grantrequest", "authentik_requests.delete_grantrequest"], 55 req, 56 ) 57 reviewers: set[User] = set() 58 for rule in rules: 59 reviewers.update(rule.reviewers_among(User.objects.all())) 60 if agent_owner: 61 # An agent's owner must approve its request, so they need the same object-level access 62 # a reviewer gets -- whether or not any rule makes them an eligible reviewer, and even 63 # when the request carries no rule at all. 64 reviewers.add(agent_owner) 65 for reviewer in reviewers: 66 reviewer.assign_perms_to_managed_role( 67 [ 68 "authentik_requests.view_grantrequest", 69 "authentik_requests.fulfill_grantrequest", 70 "authentik_requests.revoke_grantrequest", 71 ], 72 req, 73 )
Grant the requester and every reviewer eligible for any of rules
object-level permission on req, so they can see and act on it without
needing a blanket, org-wide grant of those permissions.
76def notify_reviewers(rules: QuerySet[RequestRule], event: Event): 77 """Notify reviewers of each rule attached to any of the requested targets, 78 per that rule's own notification_transports/notification_mode. An agent request resolves no 79 rules, so nothing is dispatched for one -- its owner reaches it through the `fulfill_url` 80 handed back to the agent, and through `pending_review`.""" 81 for rule in rules: 82 transports = list(rule.notification_transports.all()) 83 if not transports: 84 continue 85 for recipient in rule.notification_recipients(): 86 for transport in transports: 87 requests_send_request_notification.send_with_options( 88 args=(transport.pk, event.pk, recipient.pk), 89 rel_obj=transport, 90 )
Notify reviewers of each rule attached to any of the requested targets,
per that rule's own notification_transports/notification_mode. An agent request resolves no
rules, so nothing is dispatched for one -- its owner reaches it through the fulfill_url
handed back to the agent, and through pending_review.
93def create_grant_request( 94 request: HttpRequest, 95 *, 96 created_by: User, 97 pbms: list[PolicyBindingModel], 98 requester_data: dict[str, Any], 99 expiry: GrantExpiry, 100 agent_owner: User | None = None, 101) -> GrantRequest: 102 """Persist a grant request against `pbms`, assign the object permissions the requester, 103 reviewers and (for an agent) its owner need to act on it, and notify them.""" 104 with transaction.atomic(), audit_ignore(): 105 req = GrantRequest.objects.create( 106 created_by=created_by, 107 requester_data=requester_data, 108 requested_expiry=timedelta_to_string(expiry.granted), 109 expiring=True, 110 expires=now() + expiry.pending, 111 status=RequestStatus.CREATED, 112 agent_owner=agent_owner, 113 ) 114 for pbm in pbms: 115 GrantRequestTarget.objects.create( 116 request=req, 117 target=pbm, 118 binding=None, 119 ) 120 # An agent request is decided by its owner alone, so RequestRules attached to the target 121 # play no part: their reviewers must not be handed fulfill/revoke on it, and telling 122 # them about a request they have no say in would only leak it. 123 rules = ( 124 RequestRule.objects.none() 125 if agent_owner 126 else RequestRule.objects.filter(targets__in=pbms) 127 .distinct() 128 .prefetch_related("notification_transports") 129 ) 130 assign_request_permissions(rules, req, agent_owner) 131 event = Event.new( 132 EventAction.ACCESS_REQUEST_CREATED, 133 model=req, 134 targets=pbms, 135 requester_data=requester_data, 136 hyperlink=fulfill_url(request, req), 137 hyperlink_label="Fulfill", 138 ).from_http(request, created_by) 139 notify_reviewers(rules, event) 140 return req
Persist a grant request against pbms, assign the object permissions the requester,
reviewers and (for an agent) its owner need to act on it, and notify them.