From 90606fabf9e7471988bba0a911ddf8292de0d495 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 3 Jan 2024 11:26:47 +0100 Subject: [PATCH] set firebase key with checkin Signed-off-by: Jens Langhammer --- .../stages/authenticator_mobile/api/device.py | 44 ++++--------- schema.yml | 61 ++++--------------- schemas/authentik-cloud-gateway.yml | 61 ++++--------------- 3 files changed, 36 insertions(+), 130 deletions(-) diff --git a/authentik/stages/authenticator_mobile/api/device.py b/authentik/stages/authenticator_mobile/api/device.py index b5b088196..d0b5b2006 100644 --- a/authentik/stages/authenticator_mobile/api/device.py +++ b/authentik/stages/authenticator_mobile/api/device.py @@ -47,16 +47,16 @@ class MobileDeviceSerializer(DeviceSerializer): depth = 2 -class MobileDeviceCheckInSerializer(PassiveSerializer): - """Check info into authentik""" - - info = MobileDeviceInfoSerializer() - - class MobileDeviceSetPushKeySerializer(PassiveSerializer): """Set notification key""" - firebase_key = CharField(required=True) + firebase_key = CharField(required=False) + + +class MobileDeviceCheckInSerializer(MobileDeviceSetPushKeySerializer): + """Check info into authentik""" + + info = MobileDeviceInfoSerializer() class MobileDeviceEnrollmentSerializer(MobileDeviceSetPushKeySerializer): @@ -128,6 +128,7 @@ class MobileDeviceViewSet( new_token = MobileDeviceToken.objects.create( device=device, user=device.user, + expiring=False, ) return Response( data={ @@ -163,28 +164,6 @@ class MobileDeviceViewSet( device: MobileDevice = self.get_object() return Response({"status": "success" if device.confirmed else "waiting"}) - @extend_schema( - responses={ - 204: OpenApiResponse(description="Key successfully set"), - }, - request=MobileDeviceSetPushKeySerializer, - ) - @action( - methods=["POST"], - detail=True, - permission_classes=[], - filter_backends=[], - authentication_classes=[MobileDeviceTokenAuthentication], - ) - def set_notification_key(self, request: Request, pk: str) -> Response: - """Called by the phone whenever the firebase key changes and we need to update it""" - device: MobileDevice = self.get_object() - data = MobileDeviceSetPushKeySerializer(data=request.data) - data.is_valid(raise_exception=True) - device.firebase_token = data.validated_data["firebase_key"] - device.save() - return Response(status=204) - @extend_schema( responses={ 204: OpenApiResponse(description="Key successfully set"), @@ -214,7 +193,7 @@ class MobileDeviceViewSet( responses={ 204: OpenApiResponse(description="Checked in"), }, - request=MobileDeviceInfoSerializer, + request=MobileDeviceCheckInSerializer, ) @action( methods=["POST"], @@ -225,11 +204,12 @@ class MobileDeviceViewSet( ) def check_in(self, request: Request, pk: str) -> Response: """Check in data about a device""" - data = MobileDeviceInfoSerializer(data=request.data) + data = MobileDeviceCheckInSerializer(data=request.data) data.is_valid(raise_exception=True) device: MobileDevice = self.get_object() device.last_checkin = now() - device.state = data.validated_data + device.state = data.validated_data["info"] + device.firebase_token = data.validated_data["firebase_key"] device.save() return Response(status=204) diff --git a/schema.yml b/schema.yml index 2d53bd303..e47b46d36 100644 --- a/schema.yml +++ b/schema.yml @@ -2183,7 +2183,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MobileDeviceInfoRequest' + $ref: '#/components/schemas/MobileDeviceCheckInRequest' required: true security: - mobile_device_token: [] @@ -2317,44 +2317,6 @@ paths: schema: $ref: '#/components/schemas/GenericError' description: '' - /authenticators/mobile/{uuid}/set_notification_key/: - post: - operationId: authenticators_mobile_set_notification_key_create - description: Called by the phone whenever the firebase key changes and we need - to update it - parameters: - - in: path - name: uuid - schema: - type: string - format: uuid - description: A UUID string identifying this Mobile Device. - required: true - tags: - - authenticators - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/MobileDeviceSetPushKeyRequest' - required: true - security: - - mobile_device_token: [] - responses: - '204': - description: Key successfully set - '400': - content: - application/json: - schema: - $ref: '#/components/schemas/ValidationError' - description: '' - '403': - content: - application/json: - schema: - $ref: '#/components/schemas/GenericError' - description: '' /authenticators/mobile/{uuid}/used_by/: get: operationId: authenticators_mobile_used_by_list @@ -35369,6 +35331,17 @@ components: - type - verbose_name - verbose_name_plural + MobileDeviceCheckInRequest: + type: object + description: Check info into authentik + properties: + firebase_key: + type: string + minLength: 1 + info: + $ref: '#/components/schemas/MobileDeviceInfoRequest' + required: + - info MobileDeviceEnrollmentCallback: type: object properties: @@ -35390,7 +35363,6 @@ components: $ref: '#/components/schemas/MobileDeviceInfoRequest' required: - device_uid - - firebase_key - info MobileDeviceEnrollmentStatus: type: object @@ -35481,15 +35453,6 @@ components: required: - selected_item - tx_id - MobileDeviceSetPushKeyRequest: - type: object - description: Set notification key - properties: - firebase_key: - type: string - minLength: 1 - required: - - firebase_key ModelEnum: enum: - authentik_crypto.certificatekeypair diff --git a/schemas/authentik-cloud-gateway.yml b/schemas/authentik-cloud-gateway.yml index 89484de1f..059546f01 100644 --- a/schemas/authentik-cloud-gateway.yml +++ b/schemas/authentik-cloud-gateway.yml @@ -227,7 +227,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/MobileDeviceInfoRequest' + $ref: '#/components/schemas/MobileDeviceCheckInRequest' required: true security: - mobile_device_token: [] @@ -361,44 +361,6 @@ paths: schema: $ref: '#/components/schemas/GenericError' description: '' - /authenticators/mobile/{uuid}/set_notification_key/: - post: - operationId: authenticators_mobile_set_notification_key_create - description: Called by the phone whenever the firebase key changes and we need - to update it - parameters: - - in: path - name: uuid - schema: - type: string - format: uuid - description: A UUID string identifying this Mobile Device. - required: true - tags: - - authenticators - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/MobileDeviceSetPushKeyRequest' - required: true - security: - - mobile_device_token: [] - responses: - '204': - description: Key successfully set - '400': - content: - application/json: - schema: - $ref: '#/components/schemas/ValidationError' - description: '' - '403': - content: - application/json: - schema: - $ref: '#/components/schemas/GenericError' - description: '' /authenticators/mobile/{uuid}/used_by/: get: operationId: authenticators_mobile_used_by_list @@ -500,6 +462,17 @@ components: - type - verbose_name - verbose_name_plural + MobileDeviceCheckInRequest: + type: object + description: Check info into authentik + properties: + firebase_key: + type: string + minLength: 1 + info: + $ref: '#/components/schemas/MobileDeviceInfoRequest' + required: + - info MobileDeviceEnrollmentCallback: type: object properties: @@ -521,7 +494,6 @@ components: $ref: '#/components/schemas/MobileDeviceInfoRequest' required: - device_uid - - firebase_key - info MobileDeviceEnrollmentStatus: type: object @@ -604,15 +576,6 @@ components: required: - selected_item - tx_id - MobileDeviceSetPushKeyRequest: - type: object - description: Set notification key - properties: - firebase_key: - type: string - minLength: 1 - required: - - firebase_key PaginatedMobileDeviceList: type: object properties: