credentials.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # credentials.py -- support for git credential helpers
  2. # Copyright (C) 2022 Daniele Trifirò <daniele@iterative.ai>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Support for git credential helpers.
  21. https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage
  22. """
  23. import sys
  24. from typing import Iterator, Optional
  25. from urllib.parse import ParseResult, urlparse
  26. from .config import ConfigDict, SectionLike
  27. def match_urls(url: ParseResult, url_prefix: ParseResult) -> bool:
  28. base_match = (
  29. url.scheme == url_prefix.scheme
  30. and url.hostname == url_prefix.hostname
  31. and url.port == url_prefix.port
  32. )
  33. user_match = url.username == url_prefix.username if url_prefix.username else True
  34. path_match = url.path.rstrip("/").startswith(url_prefix.path.rstrip())
  35. return base_match and user_match and path_match
  36. def match_partial_url(valid_url: ParseResult, partial_url: str) -> bool:
  37. """Matches a parsed url with a partial url (no scheme/netloc)."""
  38. if "://" not in partial_url:
  39. parsed = urlparse("scheme://" + partial_url)
  40. else:
  41. parsed = urlparse(partial_url)
  42. if valid_url.scheme != parsed.scheme:
  43. return False
  44. if any(
  45. (
  46. (parsed.hostname and valid_url.hostname != parsed.hostname),
  47. (parsed.username and valid_url.username != parsed.username),
  48. (parsed.port and valid_url.port != parsed.port),
  49. (parsed.path and parsed.path.rstrip("/") != valid_url.path.rstrip("/")),
  50. ),
  51. ):
  52. return False
  53. return True
  54. def urlmatch_credential_sections(
  55. config: ConfigDict, url: Optional[str]
  56. ) -> Iterator[SectionLike]:
  57. """Returns credential sections from the config which match the given URL."""
  58. encoding = config.encoding or sys.getdefaultencoding()
  59. parsed_url = urlparse(url or "")
  60. for config_section in config.sections():
  61. if config_section[0] != b"credential":
  62. continue
  63. if len(config_section) < 2:
  64. yield config_section
  65. continue
  66. config_url = config_section[1].decode(encoding)
  67. parsed_config_url = urlparse(config_url)
  68. if parsed_config_url.scheme and parsed_config_url.netloc:
  69. is_match = match_urls(parsed_url, parsed_config_url)
  70. else:
  71. is_match = match_partial_url(parsed_url, config_url)
  72. if is_match:
  73. yield config_section