serializers.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. import binascii
  2. import copy
  3. import json
  4. import re
  5. from base64 import b64encode
  6. from datetime import timedelta
  7. import django.core.exceptions
  8. from captcha.audio import AudioCaptcha
  9. from captcha.image import ImageCaptcha
  10. from django.contrib.auth.password_validation import validate_password
  11. from django.core.validators import MinValueValidator
  12. from django.db.models import Model, Q
  13. from django.utils import timezone
  14. from netfields import rest_framework as netfields_rf
  15. from rest_framework import fields, serializers
  16. from rest_framework.settings import api_settings
  17. from rest_framework.validators import UniqueTogetherValidator, UniqueValidator, qs_filter
  18. from api import settings
  19. from desecapi import crypto, metrics, models, validators
  20. class CaptchaSerializer(serializers.ModelSerializer):
  21. challenge = serializers.SerializerMethodField()
  22. class Meta:
  23. model = models.Captcha
  24. fields = ('id', 'challenge', 'kind') if not settings.DEBUG else ('id', 'challenge', 'kind', 'content')
  25. def get_challenge(self, obj: models.Captcha):
  26. # TODO Does this need to be stored in the object instance, in case this method gets called twice?
  27. if obj.kind == models.Captcha.Kind.IMAGE:
  28. challenge = ImageCaptcha().generate(obj.content).getvalue()
  29. elif obj.kind == models.Captcha.Kind.AUDIO:
  30. challenge = AudioCaptcha().generate(obj.content)
  31. else:
  32. raise ValueError(f'Unknown captcha type {obj.kind}')
  33. return b64encode(challenge)
  34. class CaptchaSolutionSerializer(serializers.Serializer):
  35. id = serializers.PrimaryKeyRelatedField(
  36. queryset=models.Captcha.objects.all(),
  37. error_messages={'does_not_exist': 'CAPTCHA does not exist.'}
  38. )
  39. solution = serializers.CharField(write_only=True, required=True)
  40. def validate(self, attrs):
  41. captcha = attrs['id'] # Note that this already is the Captcha object
  42. if not captcha.verify(attrs['solution']):
  43. raise serializers.ValidationError('CAPTCHA could not be validated. Please obtain a new one and try again.')
  44. return attrs
  45. class TokenSerializer(serializers.ModelSerializer):
  46. allowed_subnets = serializers.ListField(child=netfields_rf.CidrAddressField(), required=False)
  47. token = serializers.ReadOnlyField(source='plain')
  48. is_valid = serializers.ReadOnlyField()
  49. class Meta:
  50. model = models.Token
  51. fields = ('id', 'created', 'last_used', 'max_age', 'max_unused_period', 'name', 'perm_manage_tokens',
  52. 'allowed_subnets', 'is_valid', 'token',)
  53. read_only_fields = ('id', 'created', 'last_used', 'token')
  54. def __init__(self, *args, include_plain=False, **kwargs):
  55. self.include_plain = include_plain
  56. return super().__init__(*args, **kwargs)
  57. def get_fields(self):
  58. fields = super().get_fields()
  59. if not self.include_plain:
  60. fields.pop('token')
  61. return fields
  62. class DomainSlugRelatedField(serializers.SlugRelatedField):
  63. def get_queryset(self):
  64. return self.context['request'].user.domains
  65. class TokenDomainPolicySerializer(serializers.ModelSerializer):
  66. domain = DomainSlugRelatedField(allow_null=True, slug_field='name')
  67. class Meta:
  68. model = models.TokenDomainPolicy
  69. fields = ('domain', 'perm_dyndns', 'perm_rrsets',)
  70. def to_internal_value(self, data):
  71. return {**super().to_internal_value(data),
  72. 'token': self.context['request'].user.token_set.get(id=self.context['view'].kwargs['token_id'])}
  73. def save(self, **kwargs):
  74. try:
  75. return super().save(**kwargs)
  76. except django.core.exceptions.ValidationError as exc:
  77. raise serializers.ValidationError(exc.message_dict, code='precedence')
  78. class RequiredOnPartialUpdateCharField(serializers.CharField):
  79. """
  80. This field is always required, even for partial updates (e.g. using PATCH).
  81. """
  82. def validate_empty_values(self, data):
  83. if data is serializers.empty:
  84. self.fail('required')
  85. return super().validate_empty_values(data)
  86. class Validator:
  87. message = 'This field did not pass validation.'
  88. def __init__(self, message=None):
  89. self.field_name = None
  90. self.message = message or self.message
  91. self.instance = None
  92. def __call__(self, value):
  93. raise NotImplementedError
  94. def __repr__(self):
  95. return '<%s>' % self.__class__.__name__
  96. class ReadOnlyOnUpdateValidator(Validator):
  97. message = 'Can only be written on create.'
  98. requires_context = True
  99. def __call__(self, value, serializer_field):
  100. field_name = serializer_field.source_attrs[-1]
  101. instance = getattr(serializer_field.parent, 'instance', None)
  102. if isinstance(instance, Model) and value != getattr(instance, field_name):
  103. raise serializers.ValidationError(self.message, code='read-only-on-update')
  104. class ConditionalExistenceModelSerializer(serializers.ModelSerializer):
  105. """
  106. Only considers data with certain condition as existing data.
  107. If the existence condition does not hold, given instances are deleted, and no new instances are created,
  108. respectively. Also, to_representation and data will return None.
  109. Contrary, if the existence condition holds, the behavior is the same as DRF's ModelSerializer.
  110. """
  111. def exists(self, arg):
  112. """
  113. Determine if arg is to be considered existing.
  114. :param arg: Either a model instance or (possibly invalid!) data object.
  115. :return: Whether we treat this as non-existing instance.
  116. """
  117. raise NotImplementedError
  118. def to_representation(self, instance):
  119. return None if not self.exists(instance) else super().to_representation(instance)
  120. @property
  121. def data(self):
  122. try:
  123. return super().data
  124. except TypeError:
  125. return None
  126. def save(self, **kwargs):
  127. validated_data = {}
  128. validated_data.update(self.validated_data)
  129. validated_data.update(kwargs)
  130. known_instance = self.instance is not None
  131. data_exists = self.exists(validated_data)
  132. if known_instance and data_exists:
  133. self.instance = self.update(self.instance, validated_data)
  134. elif known_instance and not data_exists:
  135. self.delete()
  136. elif not known_instance and data_exists:
  137. self.instance = self.create(validated_data)
  138. elif not known_instance and not data_exists:
  139. pass # nothing to do
  140. return self.instance
  141. def delete(self):
  142. self.instance.delete()
  143. class NonBulkOnlyDefault:
  144. """
  145. This class may be used to provide default values that are only used
  146. for non-bulk operations, but that do not return any value for bulk
  147. operations.
  148. Implementation inspired by CreateOnlyDefault.
  149. """
  150. requires_context = True
  151. def __init__(self, default):
  152. self.default = default
  153. def __call__(self, serializer_field):
  154. is_many = getattr(serializer_field.root, 'many', False)
  155. if is_many:
  156. raise serializers.SkipField()
  157. if callable(self.default):
  158. if getattr(self.default, 'requires_context', False):
  159. return self.default(serializer_field)
  160. else:
  161. return self.default()
  162. return self.default
  163. def __repr__(self):
  164. return '%s(%s)' % (self.__class__.__name__, repr(self.default))
  165. class RRSerializer(serializers.ModelSerializer):
  166. class Meta:
  167. model = models.RR
  168. fields = ('content',)
  169. def to_internal_value(self, data):
  170. if not isinstance(data, str):
  171. raise serializers.ValidationError('Must be a string.', code='must-be-a-string')
  172. return super().to_internal_value({'content': data})
  173. def to_representation(self, instance):
  174. return instance.content
  175. class RRsetListSerializer(serializers.ListSerializer):
  176. default_error_messages = {
  177. **serializers.Serializer.default_error_messages,
  178. **serializers.ListSerializer.default_error_messages,
  179. **{'not_a_list': 'Expected a list of items but got {input_type}.'},
  180. }
  181. @staticmethod
  182. def _key(data_item):
  183. return data_item.get('subname'), data_item.get('type')
  184. @staticmethod
  185. def _types_by_position_string(conflicting_indices_by_type):
  186. types_by_position = {}
  187. for type_, conflict_positions in conflicting_indices_by_type.items():
  188. for position in conflict_positions:
  189. types_by_position.setdefault(position, []).append(type_)
  190. # Sort by position, None at the end
  191. types_by_position = dict(sorted(types_by_position.items(), key=lambda x: (x[0] is None, x)))
  192. db_conflicts = types_by_position.pop(None, None)
  193. if db_conflicts: types_by_position['database'] = db_conflicts
  194. for position, types in types_by_position.items():
  195. types_by_position[position] = ', '.join(sorted(types))
  196. types_by_position = [f'{position} ({types})' for position, types in types_by_position.items()]
  197. return ', '.join(types_by_position)
  198. def to_internal_value(self, data):
  199. if not isinstance(data, list):
  200. message = self.error_messages['not_a_list'].format(input_type=type(data).__name__)
  201. raise serializers.ValidationError({api_settings.NON_FIELD_ERRORS_KEY: [message]}, code='not_a_list')
  202. if not self.allow_empty and len(data) == 0:
  203. if self.parent and self.partial:
  204. raise serializers.SkipField()
  205. else:
  206. self.fail('empty')
  207. ret = []
  208. errors = []
  209. partial = self.partial
  210. # build look-up objects for instances and data, so we can look them up with their keys
  211. try:
  212. known_instances = {(x.subname, x.type): x for x in self.instance}
  213. except TypeError: # in case self.instance is None (as during POST)
  214. known_instances = {}
  215. indices = {}
  216. for idx, item in enumerate(data):
  217. # Validate item type before using anything from it
  218. if not isinstance(item, dict):
  219. self.fail('invalid', datatype=type(item).__name__)
  220. s, t = self._key(item) # subname, type
  221. # Construct an index of the RRsets in `data` by `s` and `t`. As (subname, type) may be given multiple times
  222. # (although invalid), we make indices[s][t] a set to properly keep track. We also check and record RRsets
  223. # which are known in the database (once per subname), using index `None` (for checking CNAME exclusivity).
  224. if s not in indices:
  225. types = self.child.domain.rrset_set.filter(subname=s).values_list('type', flat=True)
  226. indices[s] = {type_: {None} for type_ in types}
  227. items = indices[s].setdefault(t, set())
  228. items.add(idx)
  229. collapsed_indices = copy.deepcopy(indices)
  230. for idx, item in enumerate(data):
  231. if item.get('records') == []:
  232. s, t = self._key(item)
  233. collapsed_indices[s][t] -= {idx, None}
  234. # Iterate over all rows in the data given
  235. for idx, item in enumerate(data):
  236. try:
  237. # see if other rows have the same key
  238. s, t = self._key(item)
  239. data_indices = indices[s][t] - {None}
  240. if len(data_indices) > 1:
  241. raise serializers.ValidationError({
  242. 'non_field_errors': [
  243. 'Same subname and type as in position(s) %s, but must be unique.' %
  244. ', '.join(map(str, data_indices - {idx}))
  245. ]
  246. })
  247. # see if other rows violate CNAME exclusivity
  248. if item.get('records') != []:
  249. conflicting_indices_by_type = {k: v for k, v in collapsed_indices[s].items()
  250. if (k == 'CNAME') != (t == 'CNAME')}
  251. if any(conflicting_indices_by_type.values()):
  252. types_by_position = self._types_by_position_string(conflicting_indices_by_type)
  253. raise serializers.ValidationError({
  254. 'non_field_errors': [
  255. f'RRset with conflicting type present: {types_by_position}.'
  256. ' (No other RRsets are allowed alongside CNAME.)'
  257. ]
  258. })
  259. # determine if this is a partial update (i.e. PATCH):
  260. # we allow partial update if a partial update method (i.e. PATCH) is used, as indicated by self.partial,
  261. # and if this is not actually a create request because it is unknown and nonempty
  262. unknown = self._key(item) not in known_instances.keys()
  263. nonempty = item.get('records', None) != []
  264. self.partial = partial and not (unknown and nonempty)
  265. self.child.instance = known_instances.get(self._key(item), None)
  266. # with partial value and instance in place, let the validation begin!
  267. validated = self.child.run_validation(item)
  268. except serializers.ValidationError as exc:
  269. errors.append(exc.detail)
  270. else:
  271. ret.append(validated)
  272. errors.append({})
  273. self.partial = partial
  274. if any(errors):
  275. raise serializers.ValidationError(errors)
  276. return ret
  277. def update(self, instance, validated_data):
  278. """
  279. Creates, updates and deletes RRsets according to the validated_data given. Relevant instances must be passed as
  280. a queryset in the `instance` argument.
  281. RRsets that appear in `instance` are considered "known", other RRsets are considered "unknown". RRsets that
  282. appear in `validated_data` with records == [] are considered empty, otherwise non-empty.
  283. The update proceeds as follows:
  284. 1. All unknown, non-empty RRsets are created.
  285. 2. All known, non-empty RRsets are updated.
  286. 3. All known, empty RRsets are deleted.
  287. 4. Unknown, empty RRsets will not cause any action.
  288. Rationale:
  289. As both "known"/"unknown" and "empty"/"non-empty" are binary partitions on `everything`, the combination of
  290. both partitions `everything` in four disjoint subsets. Hence, every RRset in `everything` is taken care of.
  291. empty | non-empty
  292. ------- | -------- | -----------
  293. known | delete | update
  294. unknown | no-op | create
  295. :param instance: QuerySet of relevant RRset objects, i.e. the Django.Model subclass instances. Relevant are all
  296. instances that are referenced in `validated_data`. If a referenced RRset is missing from instances, it will be
  297. considered unknown and hence be created. This may cause a database integrity error. If an RRset is given, but
  298. not relevant (i.e. not referred to by `validated_data`), a ValueError will be raised.
  299. :param validated_data: List of RRset data objects, i.e. dictionaries.
  300. :return: List of RRset objects (Django.Model subclass) that have been created or updated.
  301. """
  302. def is_empty(data_item):
  303. return data_item.get('records', None) == []
  304. query = Q(pk__in=[]) # start out with an always empty query, see https://stackoverflow.com/q/35893867/6867099
  305. for item in validated_data:
  306. query |= Q(type=item['type'], subname=item['subname']) # validation has ensured these fields exist
  307. instance = instance.filter(query)
  308. instance_index = {(rrset.subname, rrset.type): rrset for rrset in instance}
  309. data_index = {self._key(data): data for data in validated_data}
  310. if data_index.keys() | instance_index.keys() != data_index.keys():
  311. raise ValueError('Given set of known RRsets (`instance`) is not a subset of RRsets referred to in'
  312. ' `validated_data`. While this would produce a correct result, this is illegal due to its'
  313. ' inefficiency.')
  314. everything = instance_index.keys() | data_index.keys()
  315. known = instance_index.keys()
  316. unknown = everything - known
  317. # noinspection PyShadowingNames
  318. empty = {self._key(data) for data in validated_data if is_empty(data)}
  319. nonempty = everything - empty
  320. # noinspection PyUnusedLocal
  321. noop = unknown & empty
  322. created = unknown & nonempty
  323. updated = known & nonempty
  324. deleted = known & empty
  325. ret = []
  326. # The above algorithm makes sure that created, updated, and deleted are disjoint. Thus, no "override cases"
  327. # (such as: an RRset should be updated and delete, what should be applied last?) need to be considered.
  328. # We apply deletion first to get any possible CNAME exclusivity collisions out of the way.
  329. for subname, type_ in deleted:
  330. instance_index[(subname, type_)].delete()
  331. for subname, type_ in created:
  332. ret.append(self.child.create(
  333. validated_data=data_index[(subname, type_)]
  334. ))
  335. for subname, type_ in updated:
  336. ret.append(self.child.update(
  337. instance=instance_index[(subname, type_)],
  338. validated_data=data_index[(subname, type_)]
  339. ))
  340. return ret
  341. def save(self, **kwargs):
  342. kwargs.setdefault('domain', self.child.domain)
  343. return super().save(**kwargs)
  344. class RRsetSerializer(ConditionalExistenceModelSerializer):
  345. domain = serializers.SlugRelatedField(read_only=True, slug_field='name')
  346. records = RRSerializer(many=True)
  347. ttl = serializers.IntegerField(max_value=86400)
  348. class Meta:
  349. model = models.RRset
  350. fields = ('created', 'domain', 'subname', 'name', 'records', 'ttl', 'type', 'touched',)
  351. extra_kwargs = {
  352. 'subname': {'required': False, 'default': NonBulkOnlyDefault('')}
  353. }
  354. list_serializer_class = RRsetListSerializer
  355. def __init__(self, *args, **kwargs):
  356. super().__init__(*args, **kwargs)
  357. try:
  358. self.domain = self.context['domain']
  359. except KeyError:
  360. raise ValueError('RRsetSerializer() must be given a domain object (to validate uniqueness constraints).')
  361. self.minimum_ttl = self.context.get('minimum_ttl', self.domain.minimum_ttl)
  362. def get_fields(self):
  363. fields = super().get_fields()
  364. fields['subname'].validators.append(ReadOnlyOnUpdateValidator())
  365. fields['type'].validators.append(ReadOnlyOnUpdateValidator())
  366. fields['ttl'].validators.append(MinValueValidator(limit_value=self.minimum_ttl))
  367. return fields
  368. def get_validators(self):
  369. return [
  370. UniqueTogetherValidator(
  371. self.domain.rrset_set,
  372. ('subname', 'type'),
  373. message='Another RRset with the same subdomain and type exists for this domain.',
  374. ),
  375. validators.ExclusionConstraintValidator(
  376. self.domain.rrset_set,
  377. ('subname',),
  378. exclusion_condition=('type', 'CNAME',),
  379. message='RRset with conflicting type present: database ({types}).'
  380. ' (No other RRsets are allowed alongside CNAME.)',
  381. ),
  382. ]
  383. @staticmethod
  384. def validate_type(value):
  385. if value not in models.RR_SET_TYPES_MANAGEABLE:
  386. # user cannot manage this type, let's try to tell her the reason
  387. if value in models.RR_SET_TYPES_AUTOMATIC:
  388. raise serializers.ValidationError(f'You cannot tinker with the {value} RR set. It is managed '
  389. f'automatically.')
  390. elif value.startswith('TYPE'):
  391. raise serializers.ValidationError('Generic type format is not supported.')
  392. else:
  393. raise serializers.ValidationError(f'The {value} RR set type is currently unsupported.')
  394. return value
  395. def validate_records(self, value):
  396. # `records` is usually allowed to be empty (for idempotent delete), except for POST requests which are intended
  397. # for RRset creation only. We use the fact that DRF generic views pass the request in the serializer context.
  398. request = self.context.get('request')
  399. if request and request.method == 'POST' and not value:
  400. raise serializers.ValidationError('This field must not be empty when using POST.')
  401. return value
  402. def validate(self, attrs):
  403. if 'records' in attrs:
  404. try:
  405. type_ = attrs['type']
  406. except KeyError: # on the RRsetDetail endpoint, the type is not in attrs
  407. type_ = self.instance.type
  408. try:
  409. attrs['records'] = [{'content': models.RR.canonical_presentation_format(rr['content'], type_)}
  410. for rr in attrs['records']]
  411. except ValueError as ex:
  412. raise serializers.ValidationError(str(ex))
  413. # There is a 12 byte baseline requirement per record, c.f.
  414. # https://lists.isc.org/pipermail/bind-users/2008-April/070137.html
  415. # There also seems to be a 32 byte (?) baseline requirement per RRset, plus the qname length, see
  416. # https://lists.isc.org/pipermail/bind-users/2008-April/070148.html
  417. # The binary length of the record depends actually on the type, but it's never longer than vanilla len()
  418. qname = models.RRset.construct_name(attrs.get('subname', ''), self.domain.name)
  419. conservative_total_length = 32 + len(qname) + sum(12 + len(rr['content']) for rr in attrs['records'])
  420. # Add some leeway for RRSIG record (really ~110 bytes) and other data we have not thought of
  421. conservative_total_length += 256
  422. excess_length = conservative_total_length - 65535 # max response size
  423. if excess_length > 0:
  424. raise serializers.ValidationError(f'Total length of RRset exceeds limit by {excess_length} bytes.',
  425. code='max_length')
  426. return attrs
  427. def exists(self, arg):
  428. if isinstance(arg, models.RRset):
  429. return arg.records.exists()
  430. else:
  431. return bool(arg.get('records')) if 'records' in arg.keys() else True
  432. def create(self, validated_data):
  433. rrs_data = validated_data.pop('records')
  434. rrset = models.RRset.objects.create(**validated_data)
  435. self._set_all_record_contents(rrset, rrs_data)
  436. return rrset
  437. def update(self, instance: models.RRset, validated_data):
  438. rrs_data = validated_data.pop('records', None)
  439. if rrs_data is not None:
  440. self._set_all_record_contents(instance, rrs_data)
  441. ttl = validated_data.pop('ttl', None)
  442. if ttl and instance.ttl != ttl:
  443. instance.ttl = ttl
  444. instance.save() # also updates instance.touched
  445. else:
  446. # Update instance.touched without triggering post-save signal (no pdns action required)
  447. models.RRset.objects.filter(pk=instance.pk).update(touched=timezone.now())
  448. return instance
  449. def save(self, **kwargs):
  450. kwargs.setdefault('domain', self.domain)
  451. return super().save(**kwargs)
  452. @staticmethod
  453. def _set_all_record_contents(rrset: models.RRset, rrs):
  454. """
  455. Updates this RR set's resource records, discarding any old values.
  456. :param rrset: the RRset at which we overwrite all RRs
  457. :param rrs: list of RR representations
  458. """
  459. record_contents = [rr['content'] for rr in rrs]
  460. try:
  461. rrset.save_records(record_contents)
  462. except django.core.exceptions.ValidationError as e:
  463. raise serializers.ValidationError(e.messages, code='record-content')
  464. class DomainSerializer(serializers.ModelSerializer):
  465. default_error_messages = {
  466. **serializers.Serializer.default_error_messages,
  467. 'name_unavailable': 'This domain name conflicts with an existing zone, or is disallowed by policy.',
  468. }
  469. class Meta:
  470. model = models.Domain
  471. fields = ('created', 'published', 'name', 'keys', 'minimum_ttl', 'touched',)
  472. read_only_fields = ('published', 'minimum_ttl',)
  473. extra_kwargs = {
  474. 'name': {'trim_whitespace': False},
  475. }
  476. def __init__(self, *args, include_keys=False, **kwargs):
  477. self.include_keys = include_keys
  478. return super().__init__(*args, **kwargs)
  479. def get_fields(self):
  480. fields = super().get_fields()
  481. if not self.include_keys:
  482. fields.pop('keys')
  483. fields['name'].validators.append(ReadOnlyOnUpdateValidator())
  484. return fields
  485. def validate_name(self, value):
  486. if not models.Domain(name=value, owner=self.context['request'].user).is_registrable():
  487. raise serializers.ValidationError(self.default_error_messages['name_unavailable'], code='name_unavailable')
  488. return value
  489. def create(self, validated_data):
  490. if 'minimum_ttl' not in validated_data and models.Domain(name=validated_data['name']).is_locally_registrable:
  491. validated_data.update(minimum_ttl=60)
  492. return super().create(validated_data)
  493. class DonationSerializer(serializers.ModelSerializer):
  494. class Meta:
  495. model = models.Donation
  496. fields = ('name', 'iban', 'bic', 'amount', 'message', 'email', 'mref', 'interval')
  497. read_only_fields = ('mref',)
  498. extra_kwargs = { # do not return sensitive information
  499. 'iban': {'write_only': True},
  500. 'bic': {'write_only': True},
  501. 'message': {'write_only': True},
  502. }
  503. @staticmethod
  504. def validate_bic(value):
  505. return re.sub(r'[\s]', '', value)
  506. @staticmethod
  507. def validate_iban(value):
  508. return re.sub(r'[\s]', '', value)
  509. def create(self, validated_data):
  510. return self.Meta.model(**validated_data)
  511. class UserSerializer(serializers.ModelSerializer):
  512. class Meta:
  513. model = models.User
  514. fields = ('created', 'email', 'id', 'limit_domains',)
  515. read_only_fields = ('created', 'email', 'id', 'limit_domains',)
  516. def validate_password(self, value):
  517. if value is not None:
  518. validate_password(value)
  519. return value
  520. def create(self, validated_data):
  521. return models.User.objects.create_user(**validated_data)
  522. class RegisterAccountSerializer(UserSerializer):
  523. domain = serializers.CharField(required=False, validators=models.validate_domain_name)
  524. captcha = CaptchaSolutionSerializer(required=False)
  525. class Meta:
  526. model = UserSerializer.Meta.model
  527. fields = ('email', 'password', 'domain', 'captcha',)
  528. extra_kwargs = {
  529. 'password': {
  530. 'write_only': True, # Do not expose password field
  531. 'allow_null': True,
  532. }
  533. }
  534. def validate_domain(self, value):
  535. serializer = DomainSerializer(data=dict(name=value), context=self.context)
  536. try:
  537. serializer.is_valid(raise_exception=True)
  538. except serializers.ValidationError:
  539. raise serializers.ValidationError(serializer.default_error_messages['name_unavailable'],
  540. code='name_unavailable')
  541. return value
  542. def create(self, validated_data):
  543. validated_data.pop('domain', None)
  544. # If validated_data['captcha'] exists, the captcha was also validated, so we can set the user to verified
  545. if 'captcha' in validated_data:
  546. validated_data.pop('captcha')
  547. validated_data['needs_captcha'] = False
  548. return super().create(validated_data)
  549. class EmailSerializer(serializers.Serializer):
  550. email = serializers.EmailField()
  551. class EmailPasswordSerializer(EmailSerializer):
  552. password = serializers.CharField()
  553. class ChangeEmailSerializer(serializers.Serializer):
  554. new_email = serializers.EmailField()
  555. def validate_new_email(self, value):
  556. if value == self.context['request'].user.email:
  557. raise serializers.ValidationError('Email address unchanged.')
  558. return value
  559. class ResetPasswordSerializer(EmailSerializer):
  560. captcha = CaptchaSolutionSerializer(required=True)
  561. class CustomFieldNameUniqueValidator(UniqueValidator):
  562. """
  563. Does exactly what rest_framework's UniqueValidator does, however allows to further customize the
  564. query that is used to determine the uniqueness.
  565. More specifically, we allow that the field name the value is queried against is passed when initializing
  566. this validator. (At the time of writing, UniqueValidator insists that the field's name is used for the
  567. database query field; only how the lookup must match is allowed to be changed.)
  568. """
  569. def __init__(self, queryset, message=None, lookup='exact', lookup_field=None):
  570. self.lookup_field = lookup_field
  571. super().__init__(queryset, message, lookup)
  572. def filter_queryset(self, value, queryset, field_name):
  573. """
  574. Filter the queryset to all instances matching the given value on the specified lookup field.
  575. """
  576. filter_kwargs = {'%s__%s' % (self.lookup_field or field_name, self.lookup): value}
  577. return qs_filter(queryset, **filter_kwargs)
  578. class AuthenticatedActionSerializer(serializers.ModelSerializer):
  579. state = serializers.CharField() # serializer read-write, but model read-only field
  580. validity_period = settings.VALIDITY_PERIOD_VERIFICATION_SIGNATURE
  581. _crypto_context = 'desecapi.serializers.AuthenticatedActionSerializer'
  582. timestamp = None # is set to the code's timestamp during validation
  583. class Meta:
  584. model = models.AuthenticatedAction
  585. fields = ('state',)
  586. @classmethod
  587. def _pack_code(cls, data):
  588. payload = json.dumps(data).encode()
  589. code = crypto.encrypt(payload, context=cls._crypto_context).decode()
  590. return code.rstrip('=')
  591. @classmethod
  592. def _unpack_code(cls, code, *, ttl):
  593. code += -len(code) % 4 * '='
  594. try:
  595. timestamp, payload = crypto.decrypt(code.encode(), context=cls._crypto_context, ttl=ttl)
  596. return timestamp, json.loads(payload.decode())
  597. except (TypeError, UnicodeDecodeError, UnicodeEncodeError, json.JSONDecodeError, binascii.Error):
  598. raise ValueError
  599. def to_representation(self, instance: models.AuthenticatedAction):
  600. # do the regular business
  601. data = super().to_representation(instance)
  602. # encode into single string
  603. return {'code': self._pack_code(data)}
  604. def to_internal_value(self, data):
  605. # Allow injecting validity period from context. This is used, for example, for authentication, where the code's
  606. # integrity and timestamp is checked by AuthenticatedBasicUserActionSerializer with validity injected as needed.
  607. validity_period = self.context.get('validity_period', self.validity_period)
  608. # calculate code TTL
  609. try:
  610. ttl = validity_period.total_seconds()
  611. except AttributeError:
  612. ttl = None # infinite
  613. # decode from single string
  614. try:
  615. self.timestamp, unpacked_data = self._unpack_code(self.context['code'], ttl=ttl)
  616. except KeyError:
  617. raise serializers.ValidationError({'code': ['This field is required.']})
  618. except ValueError:
  619. if ttl is None:
  620. msg = 'This code is invalid.'
  621. else:
  622. msg = f'This code is invalid, possibly because it expired (validity: {validity_period}).'
  623. raise serializers.ValidationError({api_settings.NON_FIELD_ERRORS_KEY: msg})
  624. # add extra fields added by the user, but give precedence to fields unpacked from the code
  625. data = {**data, **unpacked_data}
  626. # do the regular business
  627. return super().to_internal_value(data)
  628. def act(self):
  629. self.instance.act()
  630. return self.instance
  631. def save(self, **kwargs):
  632. raise ValueError
  633. class AuthenticatedBasicUserActionSerializer(AuthenticatedActionSerializer):
  634. user = serializers.PrimaryKeyRelatedField(
  635. queryset=models.User.objects.all(),
  636. error_messages={'does_not_exist': 'This user does not exist.'},
  637. pk_field=serializers.UUIDField()
  638. )
  639. class Meta:
  640. model = models.AuthenticatedBasicUserAction
  641. fields = AuthenticatedActionSerializer.Meta.fields + ('user',)
  642. class AuthenticatedActivateUserActionSerializer(AuthenticatedBasicUserActionSerializer):
  643. captcha = CaptchaSolutionSerializer(required=False)
  644. class Meta(AuthenticatedBasicUserActionSerializer.Meta):
  645. model = models.AuthenticatedActivateUserAction
  646. fields = AuthenticatedBasicUserActionSerializer.Meta.fields + ('captcha', 'domain',)
  647. extra_kwargs = {
  648. 'domain': {'default': None, 'allow_null': True}
  649. }
  650. def validate(self, attrs):
  651. try:
  652. attrs.pop('captcha') # remove captcha from internal value to avoid passing to Meta.model(**kwargs)
  653. except KeyError:
  654. if attrs['user'].needs_captcha:
  655. raise serializers.ValidationError({'captcha': fields.Field.default_error_messages['required']})
  656. return attrs
  657. class AuthenticatedChangeEmailUserActionSerializer(AuthenticatedBasicUserActionSerializer):
  658. new_email = serializers.EmailField(
  659. validators=[
  660. CustomFieldNameUniqueValidator(
  661. queryset=models.User.objects.all(),
  662. lookup_field='email',
  663. message='You already have another account with this email address.',
  664. )
  665. ],
  666. required=True,
  667. )
  668. class Meta(AuthenticatedBasicUserActionSerializer.Meta):
  669. model = models.AuthenticatedChangeEmailUserAction
  670. fields = AuthenticatedBasicUserActionSerializer.Meta.fields + ('new_email',)
  671. class AuthenticatedConfirmAccountUserActionSerializer(AuthenticatedBasicUserActionSerializer):
  672. validity_period = timedelta(days=14)
  673. class Meta(AuthenticatedBasicUserActionSerializer.Meta):
  674. model = models.AuthenticatedNoopUserAction # confirmation happens during authentication, so nothing left to do
  675. class AuthenticatedResetPasswordUserActionSerializer(AuthenticatedBasicUserActionSerializer):
  676. new_password = serializers.CharField(write_only=True)
  677. class Meta(AuthenticatedBasicUserActionSerializer.Meta):
  678. model = models.AuthenticatedResetPasswordUserAction
  679. fields = AuthenticatedBasicUserActionSerializer.Meta.fields + ('new_password',)
  680. class AuthenticatedDeleteUserActionSerializer(AuthenticatedBasicUserActionSerializer):
  681. class Meta(AuthenticatedBasicUserActionSerializer.Meta):
  682. model = models.AuthenticatedDeleteUserAction
  683. class AuthenticatedDomainBasicUserActionSerializer(AuthenticatedBasicUserActionSerializer):
  684. domain = serializers.PrimaryKeyRelatedField(
  685. queryset=models.Domain.objects.all(),
  686. error_messages={'does_not_exist': 'This domain does not exist.'},
  687. )
  688. class Meta:
  689. model = models.AuthenticatedDomainBasicUserAction
  690. fields = AuthenticatedBasicUserActionSerializer.Meta.fields + ('domain',)
  691. class AuthenticatedRenewDomainBasicUserActionSerializer(AuthenticatedDomainBasicUserActionSerializer):
  692. validity_period = None
  693. class Meta(AuthenticatedDomainBasicUserActionSerializer.Meta):
  694. model = models.AuthenticatedRenewDomainBasicUserAction