AccessToken.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2019 Rigas Papathanasopoulos <rigaspapas@gmail.com> # 4 # # 5 # This file is part of PyGithub. # 6 # http://pygithub.readthedocs.io/ # 7 # # 8 # PyGithub is free software: you can redistribute it and/or modify it under # 9 # the terms of the GNU Lesser General Public License as published by the Free # 10 # Software Foundation, either version 3 of the License, or (at your option) # 11 # any later version. # 12 # # 13 # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # 14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # 15 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # 16 # details. # 17 # # 18 # You should have received a copy of the GNU Lesser General Public License # 19 # along with PyGithub. If not, see <http://www.gnu.org/licenses/>. # 20 # # 21 ################################################################################ 22 from __future__ import annotations 23 24 from datetime import datetime, timedelta, timezone 25 from typing import Any 26 27 from github.GithubObject import Attribute, NonCompletableGithubObject, NotSet 28 29 30 class AccessToken(NonCompletableGithubObject): 31 """ 32 This class represents access tokens. 33 """ 34 35 _created: datetime 36 37 def _initAttributes(self) -> None: 38 self._token: Attribute[str] = NotSet 39 self._type: Attribute[str] = NotSet 40 self._scope: Attribute[str] = NotSet 41 self._expires_in: Attribute[int | None] = NotSet 42 self._refresh_token: Attribute[str] = NotSet 43 self._refresh_expires_in: Attribute[int | None] = NotSet 44 45 def __repr__(self) -> str: 46 return self.get__repr__( 47 { 48 "token": f"{self.token[:5]}...", 49 "scope": self.scope, 50 "type": self.type, 51 "expires_in": self.expires_in, 52 "refresh_token": (f"{self.refresh_token[:5]}..." if self.refresh_token else None), 53 "refresh_token_expires_in": self.refresh_expires_in, 54 } 55 ) 56 57 @property 58 def token(self) -> str: 59 """ 60 :type: string 61 """ 62 return self._token.value 63 64 @property 65 def type(self) -> str: 66 """ 67 :type: string 68 """ 69 return self._type.value 70 71 @property 72 def scope(self) -> str: 73 """ 74 :type: string 75 """ 76 return self._scope.value 77 78 @property 79 def created(self) -> datetime: 80 """ 81 :type: datetime 82 """ 83 return self._created 84 85 @property 86 def expires_in(self) -> int | None: 87 """ 88 :type: Optional[int] 89 """ 90 return self._expires_in.value 91 92 @property 93 def expires_at(self) -> datetime | None: 94 """ 95 :type: Optional[datetime] 96 """ 97 seconds = self.expires_in 98 if seconds is not None: 99 return self._created + timedelta(seconds=seconds) 100 return None 101 102 @property 103 def refresh_token(self) -> str | None: 104 """ 105 :type: Optional[string] 106 """ 107 return self._refresh_token.value 108 109 @property 110 def refresh_expires_in(self) -> int | None: 111 """ 112 :type: Optional[int] 113 """ 114 return self._refresh_expires_in.value 115 116 @property 117 def refresh_expires_at(self) -> datetime | None: 118 """ 119 :type: Optional[datetime] 120 """ 121 seconds = self.refresh_expires_in 122 if seconds is not None: 123 return self._created + timedelta(seconds=seconds) 124 return None 125 126 def _useAttributes(self, attributes: dict[str, Any]) -> None: 127 self._created = datetime.now(timezone.utc) 128 if "access_token" in attributes: # pragma no branch 129 self._token = self._makeStringAttribute(attributes["access_token"]) 130 if "token_type" in attributes: # pragma no branch 131 self._type = self._makeStringAttribute(attributes["token_type"]) 132 if "scope" in attributes: # pragma no branch 133 self._scope = self._makeStringAttribute(attributes["scope"]) 134 if "expires_in" in attributes: # pragma no branch 135 self._expires_in = self._makeIntAttribute(attributes["expires_in"]) 136 if "refresh_token" in attributes: # pragma no branch 137 self._refresh_token = self._makeStringAttribute(attributes["refresh_token"]) 138 if "refresh_token_expires_in" in attributes: # pragma no branch 139 self._refresh_expires_in = self._makeIntAttribute(attributes["refresh_token_expires_in"])