3 ]6@sdZddlmZddlmZddlmZGdddeZddZd d Zd d Z d dZ d'ddZ d(ddZ d)ddZ d*ddZd+ddZd,ddZddZe eeedZed d!d"gZd#d$Zd%d&ZdS)-z3Module containing the validation logic for rfc3986.) exceptions)misc) normalizersc@sneZdZdZedddddddgZd d Zd d Zd dZddZ ddZ ddZ ddZ ddZ ddZdS) ValidatoraObject used to configure validation of all objects in rfc3986. .. versionadded:: 1.0 Example usage:: >>> from rfc3986 import api, validators >>> uri = api.uri_reference('https://github.com/') >>> validator = validators.Validator().require_presence_of( ... 'scheme', 'host', 'path', ... ).allow_schemes( ... 'http', 'https', ... ).allow_hosts( ... '127.0.0.1', 'github.com', ... ) >>> validator.validate(uri) >>> invalid_uri = rfc3986.uri_reference('imap://mail.google.com') >>> validator.validate(invalid_uri) Traceback (most recent call last): ... rfc3986.exceptions.MissingComponentError: ('path was required but missing', URIReference(scheme=u'imap', authority=u'mail.google.com', path=None, query=None, fragment=None), ['path']) schemeuserinfohostportpathqueryfragmentcCsDt|_t|_t|_d|_dddddddd|_|jj|_dS)z#Initialize our default validations.TF)rrrr r r r N)setallowed_schemes allowed_hosts allowed_portsallow_passwordrequired_componentscopyvalidated_components)selfrF/tmp/pip-build-el9acr48/urllib3/urllib3/packages/rfc3986/validators.py__init__:s zValidator.__init__cGs$x|D]}|jjtj|qW|S)a Require the scheme to be one of the provided schemes. .. versionadded:: 1.0 :param schemes: Schemes, without ``://`` that are allowed. :returns: The validator instance. :rtype: Validator )raddrZnormalize_scheme)rschemesrrrr allow_schemesKs zValidator.allow_schemescGs$x|D]}|jjtj|qW|S)zRequire the host to be one of the provided hosts. .. versionadded:: 1.0 :param hosts: Hosts that are allowed. :returns: The validator instance. :rtype: Validator )rrrZnormalize_host)rhostsrrrr allow_hosts[s zValidator.allow_hostscGsBx<|D]4}t|dd}d|ko(dknr|jj|qW|S)zRequire the port to be one of the provided ports. .. versionadded:: 1.0 :param ports: Ports that are allowed. :returns: The validator instance. :rtype: Validator )basei)intrr)rZportsr Zport_intrrr allow_portsks  zValidator.allow_portscCs d|_|S)zAllow passwords to be present in the URI. .. versionadded:: 1.0 :returns: The validator instance. :rtype: Validator T)r)rrrrallow_use_of_password}s zValidator.allow_use_of_passwordcCs d|_|S)zPrevent passwords from being included in the URI. .. versionadded:: 1.0 :returns: The validator instance. :rtype: Validator F)r)rrrrforbid_use_of_passwords z Validator.forbid_use_of_passwordcGsNdd|D}x$|D]}||jkrtdj|qW|jjdd|D|S)aACheck the validity of the components provided. This can be specified repeatedly. .. versionadded:: 1.1 :param components: Names of components from :attr:`Validator.COMPONENT_NAMES`. :returns: The validator instance. :rtype: Validator cSsg|] }|jqSr)lower).0crrr sz/Validator.check_validity_of..z"{}" is not a valid componentcSsi|] }d|qS)Tr)r& componentrrr sz/Validator.check_validity_of..)COMPONENT_NAMES ValueErrorformatrupdate)r componentsr)rrrcheck_validity_ofs    zValidator.check_validity_ofcGsNdd|D}x$|D]}||jkrtdj|qW|jjdd|D|S)a3Require the components provided. This can be specified repeatedly. .. versionadded:: 1.0 :param components: Names of components from :attr:`Validator.COMPONENT_NAMES`. :returns: The validator instance. :rtype: Validator cSsg|] }|jqSr)r%)r&r'rrrr(sz1Validator.require_presence_of..z"{}" is not a valid componentcSsi|] }d|qS)Tr)r&r)rrrr*sz1Validator.require_presence_of..)r+r,r-rr.)rr/r)rrrrequire_presence_ofs    zValidator.require_presence_ofcCs|jst|dd|jjD}dd|jjD}|rDt|||rRt||t|j|dt|j |dt|j |ddS)aCheck a URI for conditions specified on this validator. .. versionadded:: 1.0 :param uri: Parsed URI to validate. :type uri: rfc3986.uri.URIReference :raises MissingComponentError: When a required component is missing. :raises UnpermittedComponentError: When a component is not one of those allowed. :raises PasswordForbidden: When a password is present in the userinfo component but is not permitted by configuration. :raises InvalidComponentsError: When a component was found to be invalid. cSsg|]\}}|r|qSrr)r&r)requiredrrrr(sz&Validator.validate..cSsg|]\}}|r|qSrr)r&r)r2rrrr(srrr N) rcheck_passwordritemsr ensure_required_components_existensure_components_are_valid ensure_one_ofrrr)rurirrrrrvalidates  zValidator.validateN)__name__ __module__ __qualname____doc__ frozensetr+rrrr"r#r$r0r1r9rrrrrs$  rcCs8|j}|sdS|jdd}t|dkr*dStj|dS)z4Assert that there is no password present in the uri.N:r)rsplitlenrZPasswordForbidden)r8r credentialsrrrr3s  r3cCs0t||}|dk r,|r,||kr,tj|||dS)z=Assert that the uri's attribute is one of the allowed values.N)getattrrZUnpermittedComponentError)Zallowed_valuesr8 attributevaluerrrr7s r7cs.tfdd|D}|r*tjf|dS)z;Assert that all required components are present in the URI.csg|]}t|dkr|qS)N)rC)r&r))r8rrr( sz4ensure_required_components_exist..N)sortedrZMissingComponentError)r8rZmissing_componentsr)r8rr5s   r5cCs(|r|dk o|j|S|dkp&|j|S)a Determine if a value is valid based on the provided matcher. :param str value: Value to validate. :param matcher: Compiled regular expression to use to validate the value. :param require: Whether or not the value is required. N)match)rEZmatcherrequirerrris_valids  rINFcCs(t|tj|}|r$|dk r$t||S|S)anDetermine if the authority string is valid. :param str authority: The authority to validate. :param str host: (optional) The host portion of the authority to validate. :param bool require: (optional) Specify if authority must not be None. :returns: ``True`` if valid, ``False`` otherwise :rtype: bool N)rIrZSUBAUTHORITY_MATCHER host_is_valid) authorityrrH validatedrrrauthority_is_valid$s  rMcCsZt|tj|}|r.|dk r.tjj|r.t|S|rV|dk rVtjj|rVtjj|dk S|S)aDetermine if the host string is valid. :param str host: The host to validate. :param bool require: (optional) Specify if host must not be None. :returns: ``True`` if valid, ``False`` otherwise :rtype: bool N)rIrZ HOST_MATCHERZ IPv4_MATCHERrGvalid_ipv4_host_addressZ IPv6_MATCHERZIPv6_NO_RFC4007_MATCHER)rrHrLrrrrJ8s rJcCst|tj|S)a+Determine if the scheme is valid. :param str scheme: The scheme string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a scheme. :returns: ``True`` if the scheme is valid. ``False`` otherwise. :rtype: bool )rIrZSCHEME_MATCHER)rrHrrrscheme_is_validLs rOcCst|tj|S)a+Determine if the path component is valid. :param str path: The path string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a path. :returns: ``True`` if the path is valid. ``False`` otherwise. :rtype: bool )rIrZ PATH_MATCHER)r rHrrr path_is_valid[s rPcCst|tj|S)a0Determine if the query component is valid. :param str query: The query string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a query. :returns: ``True`` if the query is valid. ``False`` otherwise. :rtype: bool )rIrZ QUERY_MATCHER)r rHrrrquery_is_validjs rQcCst|tj|S)a?Determine if the fragment component is valid. :param str fragment: The fragment string to validate. :param bool require: (optional) Set to ``True`` to require the presence of a fragment. :returns: ``True`` if the fragment is valid. ``False`` otherwise. :rtype: bool )rIrZFRAGMENT_MATCHER)r rHrrrfragment_is_validys rRcCstdd|jdDS)z4Determine if the given host is a valid IPv4 address.cSs,g|]$}dt|ddko"dknqS)r r)r)r!)r&Zbyterrrr(sz+valid_ipv4_host_address...)allr@)rrrrrNsrN)rr r r rrr cCsy |j}Wntjk r"dSX|dkr8t|dS|dkrDdSyt|d}Wntk rhdSXd|ko|dkSS)z4Determine if the userinfo, host, and port are valid.Frr Tr i)Zauthority_inforZInvalidAuthorityrJr! TypeError)r8r)Zsubauthority_dictr rrrsubauthority_component_is_valids  rWcCsltg}xJ|D]B}|tkr0t||s|j|qt|}|t||s|j|qW|rhtj|f|dS)z0Assert that all components are valid in the URI.N)r _SUBAUTHORITY_VALIDATORSrWr_COMPONENT_VALIDATORSrCrZInvalidComponentsError)r8rZinvalid_componentsr)Z validatorrrrr6s   r6)NF)F)F)F)F)F)r=rrrobjectrr3r7r5rIrMrJrOrPrQrRrNrYr rXrWr6rrrrs.   _