credentials.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # -*- coding: utf-8 -*-
  2. # MinIO Python Library for Amazon S3 Compatible Cloud Storage,
  3. # (C) 2020 MinIO, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Credential definitions to access S3 service."""
  17. from __future__ import annotations
  18. from dataclasses import dataclass
  19. from datetime import datetime, timedelta, timezone
  20. from typing import Optional
  21. @dataclass(frozen=True)
  22. class Credentials:
  23. """
  24. Represents credentials access key, secret key and session token.
  25. """
  26. access_key: str
  27. secret_key: str
  28. session_token: Optional[str] = None
  29. expiration: Optional[datetime] = None
  30. def __post_init__(self):
  31. if not self.access_key:
  32. raise ValueError("Access key must not be empty")
  33. if not self.secret_key:
  34. raise ValueError("Secret key must not be empty")
  35. if self.expiration and self.expiration.tzinfo:
  36. object.__setattr__(
  37. self, "expiration",
  38. self.expiration.astimezone(timezone.utc).replace(tzinfo=None),
  39. )
  40. def is_expired(self) -> bool:
  41. """Check whether this credentials expired or not."""
  42. return (
  43. self.expiration < (datetime.utcnow() + timedelta(seconds=10))
  44. if self.expiration else False
  45. )