Dataclass to represent the payload of a JWT token. This is used for type checking and validation of the JWT payload.
| 25 | |
| 26 | @dataclass |
| 27 | class JWTPayload: |
| 28 | """ |
| 29 | Dataclass to represent the payload of a JWT token. |
| 30 | This is used for type checking and validation of the JWT payload. |
| 31 | """ |
| 32 | |
| 33 | exp: int |
| 34 | aud: str |
| 35 | project_id: str |
| 36 | project_prem_status: str |
| 37 | api_key: str |
| 38 | dev: bool = False |
| 39 | |
| 40 | def asdict(self) -> dict: |
| 41 | """Convert the payload to a dictionary format.""" |
| 42 | properties = { |
| 43 | "exp": self.exp, |
| 44 | "aud": self.aud, |
| 45 | "project_id": self.project_id, |
| 46 | "project_prem_status": self.project_prem_status, |
| 47 | "api_key": self.api_key, |
| 48 | } |
| 49 | |
| 50 | if self.dev: |
| 51 | properties["dev"] = self.dev |
| 52 | |
| 53 | return properties |
| 54 | |
| 55 | @classmethod |
| 56 | def from_project(cls, project: ProjectModel, dev: bool = False) -> "JWTPayload": |
| 57 | """ |
| 58 | Create a new instance of JWTPayload with the given project_id and role. |
| 59 | The expiration time is set to 30 days from now. |
| 60 | """ |
| 61 | return cls( |
| 62 | exp=_generate_jwt_timestamp(), |
| 63 | aud="authenticated", |
| 64 | project_id=str(project.id), |
| 65 | project_prem_status=project.org.prem_status.value, |
| 66 | api_key=str(project.api_key), |
| 67 | dev=dev, |
| 68 | ) |
| 69 | |
| 70 | |
| 71 | def generate_jwt(project: ProjectModel) -> str: |
no outgoing calls
searching dependent graphs…