authentik.enterprise.endpoints.connectors.google_chrome.controller
1from json import dumps, loads 2 3from django.http import HttpRequest, HttpResponseRedirect 4from django.urls import reverse 5 6from authentik.endpoints.controller import BaseController, Capabilities 7from authentik.endpoints.facts import DeviceFacts, OSFamily 8from authentik.endpoints.models import Device, DeviceConnection 9from authentik.enterprise.endpoints.connectors.google_chrome.google_schema import ( 10 DeviceSignals, 11 VerifyChallengeResponseResult, 12) 13from authentik.enterprise.endpoints.connectors.google_chrome.models import GoogleChromeConnector 14from authentik.policies.utils import delete_none_values 15 16# Header we get from chrome that initiates verified access 17HEADER_DEVICE_TRUST = "X-Device-Trust" 18# Header we send to the client with the challenge 19HEADER_ACCESS_CHALLENGE = "X-Verified-Access-Challenge" 20# Header we get back from the client that we verify with google 21HEADER_ACCESS_CHALLENGE_RESPONSE = "X-Verified-Access-Challenge-Response" 22# Header value for x-device-trust that initiates the flow 23DEVICE_TRUST_VERIFIED_ACCESS = "VerifiedAccess" 24 25 26class GoogleChromeController(BaseController[GoogleChromeConnector]): 27 28 def __init__(self, connector): 29 super().__init__(connector) 30 from googleapiclient.discovery import build 31 32 self.google_client = build( 33 "verifiedaccess", 34 "v2", 35 cache_discovery=False, 36 **connector.google_credentials(), 37 ) 38 39 @staticmethod 40 def vendor_identifier() -> str: 41 return "chrome.google.com" 42 43 def capabilities(self) -> list[Capabilities]: 44 return [Capabilities.STAGE_ENDPOINTS, Capabilities.ENROLL_AUTOMATIC_USER] 45 46 def generate_challenge(self, request: HttpRequest) -> HttpResponseRedirect: 47 challenge = self.google_client.challenge().generate().execute() 48 res = HttpResponseRedirect( 49 request.build_absolute_uri( 50 reverse("authentik_endpoints_connectors_google_chrome:chrome") 51 ) 52 ) 53 res[HEADER_ACCESS_CHALLENGE] = dumps(challenge) 54 return res 55 56 def validate_challenge(self, response: str) -> Device: 57 response = VerifyChallengeResponseResult( 58 self.google_client.challenge().verify(body=loads(response)).execute() 59 ) 60 # Remove deprecated string representation of deviceSignals 61 response.pop("deviceSignal", None) 62 signals = DeviceSignals(response["deviceSignals"]) 63 device = Device.get_or_create(identifier=signals["serialNumber"], name=signals["hostname"]) 64 conn, _ = DeviceConnection.objects.update_or_create( 65 device=device, 66 connector=self.connector, 67 ) 68 conn.create_snapshot(self.convert_data(signals)) 69 return device 70 71 def convert_os_family(self, family) -> OSFamily: 72 return { 73 "CHROME_OS": OSFamily.linux, 74 "CHROMIUM_OS": OSFamily.linux, 75 "WINDOWS": OSFamily.windows, 76 "MAC_OS_X": OSFamily.macOS, 77 "LINUX": OSFamily.linux, 78 }.get(family, OSFamily.other) 79 80 def convert_data(self, raw_signals: DeviceSignals): 81 data = { 82 "os": delete_none_values( 83 { 84 "family": self.convert_os_family(raw_signals["operatingSystem"]), 85 "version": raw_signals["osVersion"], 86 } 87 ), 88 "disks": [], 89 "network": delete_none_values( 90 { 91 "hostname": raw_signals["hostname"], 92 "interfaces": [], 93 "firewall_enabled": raw_signals["osFirewall"] == "OS_FIREWALL_ENABLED", 94 }, 95 ), 96 "hardware": delete_none_values( 97 { 98 "model": raw_signals["deviceModel"], 99 "manufacturer": raw_signals["deviceManufacturer"], 100 "serial": raw_signals["serialNumber"], 101 } 102 ), 103 "vendor": { 104 self.vendor_identifier(): { 105 "agent_version": raw_signals["browserVersion"], 106 "raw": raw_signals, 107 }, 108 }, 109 } 110 facts = DeviceFacts(data=data) 111 facts.is_valid(raise_exception=True) 112 return facts.validated_data
HEADER_DEVICE_TRUST =
'X-Device-Trust'
HEADER_ACCESS_CHALLENGE =
'X-Verified-Access-Challenge'
HEADER_ACCESS_CHALLENGE_RESPONSE =
'X-Verified-Access-Challenge-Response'
DEVICE_TRUST_VERIFIED_ACCESS =
'VerifiedAccess'
class
GoogleChromeController(authentik.endpoints.controller.BaseController[authentik.enterprise.endpoints.connectors.google_chrome.models.GoogleChromeConnector]):
27class GoogleChromeController(BaseController[GoogleChromeConnector]): 28 29 def __init__(self, connector): 30 super().__init__(connector) 31 from googleapiclient.discovery import build 32 33 self.google_client = build( 34 "verifiedaccess", 35 "v2", 36 cache_discovery=False, 37 **connector.google_credentials(), 38 ) 39 40 @staticmethod 41 def vendor_identifier() -> str: 42 return "chrome.google.com" 43 44 def capabilities(self) -> list[Capabilities]: 45 return [Capabilities.STAGE_ENDPOINTS, Capabilities.ENROLL_AUTOMATIC_USER] 46 47 def generate_challenge(self, request: HttpRequest) -> HttpResponseRedirect: 48 challenge = self.google_client.challenge().generate().execute() 49 res = HttpResponseRedirect( 50 request.build_absolute_uri( 51 reverse("authentik_endpoints_connectors_google_chrome:chrome") 52 ) 53 ) 54 res[HEADER_ACCESS_CHALLENGE] = dumps(challenge) 55 return res 56 57 def validate_challenge(self, response: str) -> Device: 58 response = VerifyChallengeResponseResult( 59 self.google_client.challenge().verify(body=loads(response)).execute() 60 ) 61 # Remove deprecated string representation of deviceSignals 62 response.pop("deviceSignal", None) 63 signals = DeviceSignals(response["deviceSignals"]) 64 device = Device.get_or_create(identifier=signals["serialNumber"], name=signals["hostname"]) 65 conn, _ = DeviceConnection.objects.update_or_create( 66 device=device, 67 connector=self.connector, 68 ) 69 conn.create_snapshot(self.convert_data(signals)) 70 return device 71 72 def convert_os_family(self, family) -> OSFamily: 73 return { 74 "CHROME_OS": OSFamily.linux, 75 "CHROMIUM_OS": OSFamily.linux, 76 "WINDOWS": OSFamily.windows, 77 "MAC_OS_X": OSFamily.macOS, 78 "LINUX": OSFamily.linux, 79 }.get(family, OSFamily.other) 80 81 def convert_data(self, raw_signals: DeviceSignals): 82 data = { 83 "os": delete_none_values( 84 { 85 "family": self.convert_os_family(raw_signals["operatingSystem"]), 86 "version": raw_signals["osVersion"], 87 } 88 ), 89 "disks": [], 90 "network": delete_none_values( 91 { 92 "hostname": raw_signals["hostname"], 93 "interfaces": [], 94 "firewall_enabled": raw_signals["osFirewall"] == "OS_FIREWALL_ENABLED", 95 }, 96 ), 97 "hardware": delete_none_values( 98 { 99 "model": raw_signals["deviceModel"], 100 "manufacturer": raw_signals["deviceManufacturer"], 101 "serial": raw_signals["serialNumber"], 102 } 103 ), 104 "vendor": { 105 self.vendor_identifier(): { 106 "agent_version": raw_signals["browserVersion"], 107 "raw": raw_signals, 108 }, 109 }, 110 } 111 facts = DeviceFacts(data=data) 112 facts.is_valid(raise_exception=True) 113 return facts.validated_data
Abstract base class for generic types.
On Python 3.12 and newer, generic classes implicitly inherit from Generic when they declare a parameter list after the class's name::
class Mapping[KT, VT]:
def __getitem__(self, key: KT) -> VT:
...
# Etc.
On older versions of Python, however, generic classes have to explicitly inherit from Generic.
After a class has been declared to be generic, it can then be used as follows::
def lookup_name[KT, VT](mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
try:
return mapping[key]
except KeyError:
return default
def
generate_challenge( self, request: django.http.request.HttpRequest) -> django.http.response.HttpResponseRedirect:
47 def generate_challenge(self, request: HttpRequest) -> HttpResponseRedirect: 48 challenge = self.google_client.challenge().generate().execute() 49 res = HttpResponseRedirect( 50 request.build_absolute_uri( 51 reverse("authentik_endpoints_connectors_google_chrome:chrome") 52 ) 53 ) 54 res[HEADER_ACCESS_CHALLENGE] = dumps(challenge) 55 return res
57 def validate_challenge(self, response: str) -> Device: 58 response = VerifyChallengeResponseResult( 59 self.google_client.challenge().verify(body=loads(response)).execute() 60 ) 61 # Remove deprecated string representation of deviceSignals 62 response.pop("deviceSignal", None) 63 signals = DeviceSignals(response["deviceSignals"]) 64 device = Device.get_or_create(identifier=signals["serialNumber"], name=signals["hostname"]) 65 conn, _ = DeviceConnection.objects.update_or_create( 66 device=device, 67 connector=self.connector, 68 ) 69 conn.create_snapshot(self.convert_data(signals)) 70 return device
def
convert_data( self, raw_signals: authentik.enterprise.endpoints.connectors.google_chrome.google_schema.DeviceSignals):
81 def convert_data(self, raw_signals: DeviceSignals): 82 data = { 83 "os": delete_none_values( 84 { 85 "family": self.convert_os_family(raw_signals["operatingSystem"]), 86 "version": raw_signals["osVersion"], 87 } 88 ), 89 "disks": [], 90 "network": delete_none_values( 91 { 92 "hostname": raw_signals["hostname"], 93 "interfaces": [], 94 "firewall_enabled": raw_signals["osFirewall"] == "OS_FIREWALL_ENABLED", 95 }, 96 ), 97 "hardware": delete_none_values( 98 { 99 "model": raw_signals["deviceModel"], 100 "manufacturer": raw_signals["deviceManufacturer"], 101 "serial": raw_signals["serialNumber"], 102 } 103 ), 104 "vendor": { 105 self.vendor_identifier(): { 106 "agent_version": raw_signals["browserVersion"], 107 "raw": raw_signals, 108 }, 109 }, 110 } 111 facts = DeviceFacts(data=data) 112 facts.is_valid(raise_exception=True) 113 return facts.validated_data