etHdZddlZddlZddlZddlmZmZmZmZm Z m Z m Z m Z m Z ddlmZddlmZmZmZmZej*eZ d*dededed e eegefd e eegefd ef d ZGd dZGddeZGddej:Ze ddZGddej@ejBeZ"de#d efdZ$d+dede e%ded e#fdZ&de#d efdZ'd+dede e%ded e#fdZ(d ejRd efd!Z*d"ed ejRfd#Z+d$ejRd efd%Z,d"ed ejRfd&Z-e d'd(Z.Gd)d(e"Z/y),zJSON (de)serialization framework. The framework presented here is somewhat based on `Go's "json" package`_ (especially the ``omitempty`` functionality). .. _`Go's "json" package`: http://golang.org/pkg/encoding/json/ N) AnyCallableDictIterableListMappingOptionalTypeTypeVar)crypto)b64errors interfacesutil json_namedefault omitemptydecoderencoderreturnc"t|||||S)a4Convenient function to declare a :class:`Field` with proper type annotations. This function allows to write the following code: import josepy class JSON(josepy.JSONObjectWithFields): typ: str = josepy.field('type') def other_type(self) -> str: return self.typ rrrrr) _TypedFieldrs 2/usr/lib/python3/dist-packages/josepy/json_util.pyfieldrs& W 7\c c(eZdZdZdZ ddedededee egefdee egefd df d Z e d ed efd Z d ed efd Z ded dfdZde egefd dfdZde egefd dfdZd ed efdZd ed efdZe d ed efdZe d ed efdZy)FieldaJSON object field. :class:`Field` is meant to be used together with :class:`JSONObjectWithFields`. ``encoder`` (``decoder``) is a callable that accepts a single parameter, i.e. a value to be encoded (decoded), and returns the serialized (deserialized) value. In case of errors it should raise :class:`~josepy.errors.SerializationError` (:class:`~josepy.errors.DeserializationError`). Note, that ``decoder`` should perform partial serialization only. :ivar str json_name: Name of the field when encoded to JSON. :ivar default: Default value (used when not present in JSON object). :ivar bool omitempty: If ``True`` and the field value is empty, then it will not be included in the serialized JSON object, and ``default`` will be used for deserialization. Otherwise, if ``False``, field is considered as required, value will always be included in the serialized JSON objected, and it must also be present when deserializing. )rrrfdecfencNrrrrrrc||_||_||_| |jn||_||j |_y||_yN)rrrdefault_decoderrdefault_encoderr )selfrrrrrs r__init__zField.__init__RsC# ",3OD(( ,3OD((  rvaluec.t|t xr| S)zIs the provided value considered "empty" for this field? This is useful for subclasses that might want to override the definition of being empty, e.g. for some more exotic data types. ) isinstanceboolclsr's r_emptyz Field._emptyaseT**85y8rc@|j|xr |jS)zOmit the value in output?)r-rr%r's romitz Field.omitks{{5!4dnn4rkwargsc |j|j|j|j|jd|}t |di|S)Nr)rrrrr type)r%r1currents r_update_paramszField._update_paramsosL||yyyy    tDz$G$$rrc&|j|S)z6Descriptor to change the decoder on JSON object field.)rr6)r%rs rrz Field.decoderz""4"00rr c&|j|S)z6Descriptor to change the encoder on JSON object field.)rr8)r%r s rrz Field.encoder~r9rc$|j|S)z4Decode a value, optionally with context JSON object.)rr/s rdecodez Field.decodeyyrc$|j|S)z4Encode a value, optionally with context JSON object.)r r/s rencodez Field.encoder=rc &t|trtfd|DSt|trUt j |j Dcic]&\}}j|j|(c}}S|Scc}}w)zDefault decoder. Recursively deserialize into immutable types ( :class:`josepy.util.frozendict` instead of :func:`dict`, :func:`tuple` instead of :func:`list`). c3@K|]}j|ywr")r#).0subvaluer,s r z(Field.default_decoder..sM8,,X6Ms)r)listtupledictr frozendictitemsr#)r,r'keys` rr#zField.default_decoders eT "MuMM M t $??',kkm"U'',c.A.A%.HH L s+B c|S)zDefault (passthrough) encoder.r3r+s rr$zField.default_encoders  rNFNN)__name__ __module__ __qualname____doc__ __slots__strrr*r rr& classmethodr-r0r6rrr<r?r#r$r3rrrr7sq0FI 2626 I I I I (C5#:./ I (C5#:./ I  I9394995#5$5 %s %w %1HcUCZ01W11HcUCZ01W1 C C  C C CC*CCrrceZdZdZy)raSpecialized class to mark a JSON object field with typed annotations. This class is kept private because fields are supposed to be declared using the :function:`field` in this situation. In the future the :class:`Field` may be removed in favor of this one.N)rMrNrOrPr3rrrrsMrrcbeZdZUdZiZeeefed<e eed<dede edeee fddfdZ y ) JSONObjectWithFieldsMetaaMetaclass for :class:`JSONObjectWithFields` and its subclasses. It makes sure that, for any class ``cls`` with ``__metaclass__`` set to ``JSONObjectWithFieldsMeta``: 1. All fields (attributes of type :class:`Field`) in the class definition are moved to the ``cls._fields`` dictionary, where keys are field attribute names and values are fields themselves. 2. ``cls.__slots__`` is extended by all field attribute names (i.e. not :attr:`Field.json_name`). Original ``cls.__slots__`` are stored in ``cls._orig_slots``. In a consequence, for a field attribute name ``some_field``, ``cls.some_field`` will be a slot descriptor and not an instance of :class:`Field`. For example:: some_field = Field('someField', default=()) class Foo: __metaclass__ = JSONObjectWithFieldsMeta __slots__ = ('baz',) some_field = some_field assert Foo.__slots__ == ('some_field', 'baz') assert Foo._orig_slots == () assert Foo.some_field is not Field assert Foo._fields.keys() == ['some_field'] assert Foo._fields['some_field'] is some_field As an implementation note, this metaclass inherits from :class:`abc.ABCMeta` (and not the usual :class:`type`) to mitigate the metaclass conflict (:class:`ImmutableMap` and :class:`JSONDeSerializable`, parents of :class:`JSONObjectWithFields`, use :class:`abc.ABCMeta` as its metaclass). _fields _orig_slotsnamebases namespacerc.i}|D]}|jt|di |jjD]`\}}t |t st |t r&||jdivrtd|d|d|j|||<b|jdd|d<tt|dt|jz|d<||d<tjj||||S) NrW__annotations__zField `z` in JSONObject `z` has no type annotation.rQr3rX)updategetattrcopyrIr)rrget ValueErrorpoprFrEkeysabcABCMeta__new__)mcsrYrZr[fieldsbaserJr's rrgz JSONObjectWithFieldsMeta.__new__s$& 8D MM'$ 26 7 8$..*002 1JC%'e[1)--0A2"FF(%cU*;D6AZ[(mmC0s  1$-==b#A - !&tIm,D'EV[[]H['[!\ +% ){{""3eY??rN) rMrNrOrPrWrrRrr]rrrrgr3rrrVrVs]%N!#GT#u* "#@@#Cy@59#s(^@ #@rrVGenericJSONObjectWithFieldsJSONObjectWithFields)boundceZdZdZedeeeffdZdeddffd Z dedefdZ deeeffd Z deeeffd Z ed e eefddfd Zed e eefdefd Zedeed e eefdefdZxZS)rlaJSON object with fields. Example:: class Foo(JSONObjectWithFields): bar = Field('Bar') empty = Field('Empty', omitempty=True) @bar.encoder def bar(value): return value + 'bar' @bar.decoder def bar(value): if not value.endswith('bar'): raise errors.DeserializationError('No bar suffix!') return value[:-3] assert Foo(bar='baz').to_partial_json() == {'Bar': 'bazbar'} assert Foo.from_json({'Bar': 'bazbar'}) == Foo(bar='baz') assert (Foo.from_json({'Bar': 'bazbar', 'Empty': '!'}) == Foo(bar='baz', empty='!')) assert Foo(bar='baz').bar == 'baz' rc~|jjDcic]\}}||jc}}Scc}}w)zGet default fields values.)rWrIr)r,slotrs r _defaultszJSONObjectWithFields._defaultss28;{{7H7H7JK eemm#KKKs9r1Nc Ht|dii|j|yNr3)superr&rq)r%r1 __class__s rr&zJSONObjectWithFields.__init__s% :9dnn.9&9:rrYc |j|}|j t ||S#t$r%tjdj |wxYw)zEncode a single field. :param str name: Name of the field to be encoded. :raises errors.SerializationError: if field cannot be serialized :raises errors.Error: if field could not be found zField not found: {0})rWKeyErrorrErrorformatr?r_)r%rYrs rr?zJSONObjectWithFields.encodes\ DLL&E||GD$/00 D,,5<|r3t jdjdj|y)Nz&The following fields are required: {0},) r{rWrIrrr|rDeserializationErrorryjoin)r,r~missing_rs r_check_requiredz$JSONObjectWithFields._check_requiredDs% ))+ -HAu??ud'B EOO, - --8??@QR  rc |j|i}|jjD]S\}}|j|vr|jr|j ||<0||j} |j |||<U|S#tj$r+}tjdj|||d}~wwxYw)zDeserialize fields from JSON.z#Could not decode {0!r} ({1!r}): {2}N) rrWrIrrrr<rrry)r,r~rirprr'rs rfields_from_jsonz%JSONObjectWithFields.fields_from_jsonPs D!;;,,. KD%d*u$}}t U__-#(<<#6F4L  22 55=DDT5RWXs.BC&B??Cr,c0|di|j|Srs)r)r,r~s r from_jsonzJSONObjectWithFields.from_jsonbs0S))$/00r)rMrNrOrPrSrrRrrqr&r?rrrrrr rkr __classcell__)rus@rrlrls 4L$sCx.LL;;;13131 S#X$-c3h- 738#4   GCH$5#"1 - .16=c3h6G1 $11r) metaclassdatacJtj|jdS)zJEncode JOSE Base-64 field. :param bytes data: :rtype: `str` ascii)r b64encoder<)rs rencode_b64joseris ==  % %g ..rsizeminimumc: tj|j}|D|st||k7s|r2t||kr$t j dj||S#tj$r}t j |d}~wwxYw)aDecode JOSE Base-64 field. :param unicode data: :param int size: Required length (after decoding). :param bool minimum: If ``True``, then `size` will be treated as minimum required length, as opposed to exact equality. :rtype: bytes Nz&Expected at least or exactly {0} bytes) r b64decoder?binasciirxrrlenry)rrrdecodedrs rdecode_b64joserts1-- .  W-7s7|d?R))*R*Y*YZ^*_`` N >>1))%001s#A--BBBr'cHtj|jS)z;Hexlify. :param bytes value: :rtype: unicode )rhexlifyr<)r's r encode_hex16rs   E " ) ) ++rc&|j}|:|st||dzk7s|r%t||dzkrtj t j |S#tj $r}tj|d}~wwxYw)aDecode hexlified field. :param unicode value: :param int size: Required length (after decoding). :param bool minimum: If ``True``, then `size` will be treated as minimum required length, as opposed to exact equality. :rtype: bytes N)r?rrrr unhexlifyrx)r'rrvalue_brs r decode_hex16rsllnG  W1w3w>1))%001sA##B6B  Bcertct|jtjr t dt tj tj|jS)zEncode certificate as JOSE Base-64 DER. :type cert: `OpenSSL.crypto.X509` wrapped in `.ComparableX509` :rtype: unicode z.Error input is actually a certificate request.)r)wrappedr X509Reqrbrdump_certificate FILETYPE_ASN1)rs r encode_certrsD$,,/IJJ &11&2F2F U VVrb64derc tjtjtjt |S#tj $r}tj|d}~wwxYw)zDecode JOSE Base-64 DER-encoded certificate. :param unicode b64der: :rtype: `OpenSSL.crypto.X509` wrapped in `.ComparableX509` N) rComparableX509r load_certificaterrrxrrrrs r decode_certrs\1""  # #F$8$8.:P Q   <<1))%001?AA/A**A/csrct|jtjr t dt tj tj|jS)zEncode CSR as JOSE Base-64 DER. :type csr: `OpenSSL.crypto.X509Req` wrapped in `.ComparableX509` :rtype: unicode z&Error input is actually a certificate.)r)rr X509rbrdump_certificate_requestr)rs r encode_csrrsG#++v{{+ABB &99&:N:NPSP[P[\ ]]rc tjtjtjt |S#tj $r}tj|d}~wwxYw)zDecode JOSE Base-64 DER-encoded CSR. :param unicode b64der: :rtype: `OpenSSL.crypto.X509Req` wrapped in `.ComparableX509` N) rrr load_certificate_requestrrrxrrrs r decode_csrrs]1""  + +F,@,@.QWBX Y   <<1))%001r GenericTypedJSONObjectWithFieldsTypedJSONObjectWithFieldsc eZdZUdZeZeed< dZeed< eZ e ee fed< e dde e deede e fd Ze d eeefde dfd Zde eeffd Ze d eeefddfd Zy)rzJSON object with type.typr4type_field_nameTYPESNtype_clsrcD| |jn|}||j|<|S)z(Register class for JSON deserialization.)rr)r,rrs rregisterz"TypedJSONObjectWithFields.registers% "khlls! #rr~c||jjvr>|j|vr.tjdj |j|St |ts$tjdj | ||j} |j|S#t$rtjdwxYw#t$rtj||wxYw)z&Get the registered class for ``jobj``.zMissing type field ({0})z{0} is not a dictionary objectzmissing type field) rvaluesrrrryr)rGrwUnrecognizedTypeError)r,r~rs r get_type_clsz&TypedJSONObjectWithFields.get_type_clss #))""$ $""$.11.55c6I6IJ J$%--.N.U.UVZ.[\ \ Ds**+C :99S> !  D--.BC C D  :..sD9 9 :sB/ C/C C1cX|j}|j||j<|S)aGet JSON serializable object. :returns: Serializable JSON object representing ACME typed object. :meth:`validate` will almost certainly not work, due to reasons explained in :class:`josepy.interfaces.IJSONSerializable`. :rtype: dict )rrr)r%r~s rrz)TypedJSONObjectWithFields.to_partial_jsons+**,%)XXT ! !" rcR|j|}|di|j|S)zDeserialize ACME object from valid JSON object. :raises josepy.errors.UnrecognizedTypeError: if type of the ACME object has not been registered. r3)rr)r,r~rs rrz#TypedJSONObjectWithFields.from_json(s-##D):(33D9::rr")rMrNrOrPNotImplementedrrRr]rrrr rSrr rrrrrrr3rrrrs C7!OS! ,E4T ?+3TX<=DLSM . /:S 1:d;V6W::0 c3h  ;WS#X. ;3N ; ;rrL)NF)0rPrerloggingtypingrrrrrrr r r OpenSSLr josepyr rrr getLoggerrMloggerrRr*rrrrfrVrk ImmutableMapJSONDeSerializablerlbytesrintrrrrrrrrrrr3rrrs     00   8 $ .2.2  hucz* +  hucz* +   0nnbM%MC@s{{C@L&&CKabl1z44@Xl1^//3/HSM4TY0,,#,118C=1$1SX1, Wd)) Wc W 1 1 3 3 1 ^D'' ^C ^ 1s 1t22 1$+&.I$ I; 4I;r