set firebase key with checkin

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens Langhammer 2024-01-03 11:26:47 +01:00
parent 76498a2d7b
commit 90606fabf9
No known key found for this signature in database
3 changed files with 36 additions and 130 deletions

View File

@ -47,16 +47,16 @@ class MobileDeviceSerializer(DeviceSerializer):
depth = 2 depth = 2
class MobileDeviceCheckInSerializer(PassiveSerializer):
"""Check info into authentik"""
info = MobileDeviceInfoSerializer()
class MobileDeviceSetPushKeySerializer(PassiveSerializer): class MobileDeviceSetPushKeySerializer(PassiveSerializer):
"""Set notification key""" """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): class MobileDeviceEnrollmentSerializer(MobileDeviceSetPushKeySerializer):
@ -128,6 +128,7 @@ class MobileDeviceViewSet(
new_token = MobileDeviceToken.objects.create( new_token = MobileDeviceToken.objects.create(
device=device, device=device,
user=device.user, user=device.user,
expiring=False,
) )
return Response( return Response(
data={ data={
@ -163,28 +164,6 @@ class MobileDeviceViewSet(
device: MobileDevice = self.get_object() device: MobileDevice = self.get_object()
return Response({"status": "success" if device.confirmed else "waiting"}) 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( @extend_schema(
responses={ responses={
204: OpenApiResponse(description="Key successfully set"), 204: OpenApiResponse(description="Key successfully set"),
@ -214,7 +193,7 @@ class MobileDeviceViewSet(
responses={ responses={
204: OpenApiResponse(description="Checked in"), 204: OpenApiResponse(description="Checked in"),
}, },
request=MobileDeviceInfoSerializer, request=MobileDeviceCheckInSerializer,
) )
@action( @action(
methods=["POST"], methods=["POST"],
@ -225,11 +204,12 @@ class MobileDeviceViewSet(
) )
def check_in(self, request: Request, pk: str) -> Response: def check_in(self, request: Request, pk: str) -> Response:
"""Check in data about a device""" """Check in data about a device"""
data = MobileDeviceInfoSerializer(data=request.data) data = MobileDeviceCheckInSerializer(data=request.data)
data.is_valid(raise_exception=True) data.is_valid(raise_exception=True)
device: MobileDevice = self.get_object() device: MobileDevice = self.get_object()
device.last_checkin = now() 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() device.save()
return Response(status=204) return Response(status=204)

View File

@ -2183,7 +2183,7 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/MobileDeviceInfoRequest' $ref: '#/components/schemas/MobileDeviceCheckInRequest'
required: true required: true
security: security:
- mobile_device_token: [] - mobile_device_token: []
@ -2317,44 +2317,6 @@ paths:
schema: schema:
$ref: '#/components/schemas/GenericError' $ref: '#/components/schemas/GenericError'
description: '' 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/: /authenticators/mobile/{uuid}/used_by/:
get: get:
operationId: authenticators_mobile_used_by_list operationId: authenticators_mobile_used_by_list
@ -35369,6 +35331,17 @@ components:
- type - type
- verbose_name - verbose_name
- verbose_name_plural - 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: MobileDeviceEnrollmentCallback:
type: object type: object
properties: properties:
@ -35390,7 +35363,6 @@ components:
$ref: '#/components/schemas/MobileDeviceInfoRequest' $ref: '#/components/schemas/MobileDeviceInfoRequest'
required: required:
- device_uid - device_uid
- firebase_key
- info - info
MobileDeviceEnrollmentStatus: MobileDeviceEnrollmentStatus:
type: object type: object
@ -35481,15 +35453,6 @@ components:
required: required:
- selected_item - selected_item
- tx_id - tx_id
MobileDeviceSetPushKeyRequest:
type: object
description: Set notification key
properties:
firebase_key:
type: string
minLength: 1
required:
- firebase_key
ModelEnum: ModelEnum:
enum: enum:
- authentik_crypto.certificatekeypair - authentik_crypto.certificatekeypair

View File

@ -227,7 +227,7 @@ paths:
content: content:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/MobileDeviceInfoRequest' $ref: '#/components/schemas/MobileDeviceCheckInRequest'
required: true required: true
security: security:
- mobile_device_token: [] - mobile_device_token: []
@ -361,44 +361,6 @@ paths:
schema: schema:
$ref: '#/components/schemas/GenericError' $ref: '#/components/schemas/GenericError'
description: '' 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/: /authenticators/mobile/{uuid}/used_by/:
get: get:
operationId: authenticators_mobile_used_by_list operationId: authenticators_mobile_used_by_list
@ -500,6 +462,17 @@ components:
- type - type
- verbose_name - verbose_name
- verbose_name_plural - 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: MobileDeviceEnrollmentCallback:
type: object type: object
properties: properties:
@ -521,7 +494,6 @@ components:
$ref: '#/components/schemas/MobileDeviceInfoRequest' $ref: '#/components/schemas/MobileDeviceInfoRequest'
required: required:
- device_uid - device_uid
- firebase_key
- info - info
MobileDeviceEnrollmentStatus: MobileDeviceEnrollmentStatus:
type: object type: object
@ -604,15 +576,6 @@ components:
required: required:
- selected_item - selected_item
- tx_id - tx_id
MobileDeviceSetPushKeyRequest:
type: object
description: Set notification key
properties:
firebase_key:
type: string
minLength: 1
required:
- firebase_key
PaginatedMobileDeviceList: PaginatedMobileDeviceList:
type: object type: object
properties: properties: