2019-02-28 09:58:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
from common.utils import get_object_or_none
|
2019-08-21 12:27:21 +00:00
|
|
|
from users.models import User
|
2019-11-08 07:48:01 +00:00
|
|
|
from users.serializers import UserProfileSerializer
|
2019-10-31 10:23:43 +00:00
|
|
|
from .models import AccessKey, LoginConfirmSetting
|
2019-02-28 09:58:53 +00:00
|
|
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
__all__ = [
|
|
|
|
'AccessKeySerializer', 'OtpVerifySerializer', 'BearerTokenSerializer',
|
2019-10-31 10:23:43 +00:00
|
|
|
'MFAChallengeSerializer', 'LoginConfirmSettingSerializer',
|
2019-08-21 12:27:21 +00:00
|
|
|
]
|
2019-02-28 09:58:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AccessKeySerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = AccessKey
|
2019-08-21 12:27:21 +00:00
|
|
|
fields = ['id', 'secret', 'is_active', 'date_created']
|
|
|
|
read_only_fields = ['id', 'secret', 'date_created']
|
2019-05-20 04:30:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OtpVerifySerializer(serializers.Serializer):
|
|
|
|
code = serializers.CharField(max_length=6, min_length=6)
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
class BearerTokenSerializer(serializers.Serializer):
|
2019-11-08 07:48:01 +00:00
|
|
|
username = serializers.CharField(allow_null=True, required=False, write_only=True)
|
2019-11-05 10:46:29 +00:00
|
|
|
password = serializers.CharField(write_only=True, allow_null=True,
|
2019-11-08 07:48:01 +00:00
|
|
|
required=False, allow_blank=True)
|
2019-11-05 10:46:29 +00:00
|
|
|
public_key = serializers.CharField(write_only=True, allow_null=True,
|
2019-11-08 07:48:01 +00:00
|
|
|
allow_blank=True, required=False)
|
2019-08-21 12:27:21 +00:00
|
|
|
token = serializers.CharField(read_only=True)
|
|
|
|
keyword = serializers.SerializerMethodField()
|
|
|
|
date_expired = serializers.DateTimeField(read_only=True)
|
2019-11-08 07:48:01 +00:00
|
|
|
user = UserProfileSerializer(read_only=True)
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_keyword(obj):
|
|
|
|
return 'Bearer'
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
request = self.context.get('request')
|
|
|
|
if request.user and not request.user.is_anonymous:
|
|
|
|
user = request.user
|
|
|
|
else:
|
|
|
|
user_id = request.session.get('user_id')
|
|
|
|
user = get_object_or_none(User, pk=user_id)
|
|
|
|
if not user:
|
|
|
|
raise serializers.ValidationError(
|
|
|
|
"user id {} not exist".format(user_id)
|
|
|
|
)
|
2019-08-21 12:27:21 +00:00
|
|
|
token, date_expired = user.create_bearer_token(request)
|
|
|
|
instance = {
|
|
|
|
"token": token,
|
|
|
|
"date_expired": date_expired,
|
2019-11-08 07:48:01 +00:00
|
|
|
"user": user
|
2019-08-21 12:27:21 +00:00
|
|
|
}
|
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2019-11-05 10:46:29 +00:00
|
|
|
class MFAChallengeSerializer(serializers.Serializer):
|
2019-11-06 04:06:30 +00:00
|
|
|
type = serializers.CharField(write_only=True, required=False, allow_blank=True)
|
2019-08-21 12:27:21 +00:00
|
|
|
code = serializers.CharField(write_only=True)
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
2019-11-05 10:46:29 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
pass
|
2019-08-21 12:27:21 +00:00
|
|
|
|
2019-10-31 10:23:43 +00:00
|
|
|
|
|
|
|
class LoginConfirmSettingSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = LoginConfirmSetting
|
|
|
|
fields = ['id', 'user', 'reviewers', 'date_created', 'date_updated']
|
|
|
|
read_only_fields = ['date_created', 'date_updated']
|