comparison NewTests/Authorization/models.py @ 576:80ba6f1d521c

new tests for authorization plugin (native only)
author Alain Mazy <am@osimis.io>
date Wed, 06 Sep 2023 17:04:36 +0200
parents
children 0649a19df194
comparison
equal deleted inserted replaced
575:28fef24147fa 576:80ba6f1d521c
1 # SPDX-FileCopyrightText: 2022 - 2023 Orthanc Team SRL <info@orthanc.team>
2 #
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from typing import Optional, List
6 from pydantic import BaseModel, Field
7 from pydantic.datetime_parse import parse_datetime
8 from enum import Enum
9 from datetime import datetime
10
11
12 class StringDateTime(datetime):
13 @classmethod
14 def __get_validators__(cls):
15 yield parse_datetime
16 yield cls.validate
17
18 @classmethod
19 def validate(cls, v: datetime):
20 return v.isoformat()
21
22
23 class Levels(str, Enum):
24 PATIENT = 'patient'
25 STUDY = 'study'
26 SERIES = 'series'
27 INSTANCE = 'instance'
28
29 SYSTEM = 'system'
30
31
32 class Methods(str, Enum):
33 GET = 'get'
34 POST = 'post'
35 PUT = 'put'
36 DELETE = 'delete'
37
38
39 class DecoderErrorCodes(str, Enum):
40 EXPIRED = 'expired'
41 INVALID = 'invalid'
42 UNKNOWN = 'unknown'
43
44
45 class TokenType(str, Enum):
46 OSIMIS_VIEWER_PUBLICATION = 'osimis-viewer-publication' # a link to open the Osimis viewer valid for a long period
47 MEDDREAM_VIEWER_PUBLICATION = 'meddream-viewer-publication' # a link to open the MedDream viewer valid for a long period
48 STONE_VIEWER_PUBLICATION = 'stone-viewer-publication' # a link to open the Stone viewer valid for a long period
49 OHIF_VIEWER_PUBLICATION = 'ohif-viewer-publication' # a link to open the OHIF viewer valid for a long period
50
51 MEDDREAM_INSTANT_LINK = 'meddream-instant-link' # a direct link to MedDream viewer that is valid only a few minutes to open the viewer directly
52
53 # OSIMIS_VIEWER_INSTANT_LINK = 'osimis-viewer-instant-link' # a direct link to Osimis viewer that is valid only a few minutes to open the viewer directly
54 # STONE_VIEWER_INSTANT_LINK = 'stone-viewer-instant-link' # a direct link to Stone viewer that is valid only a few minutes to open the viewer directly
55 #
56 # DOWNLOAD_INSTANT_LINK = 'download-instant-link' # a link to download a study/series/instance directly
57 VIEWER_INSTANT_LINK = 'viewer-instant-link' # a link to a resource to be used directly.
58 DOWNLOAD_INSTANT_LINK = 'download-instant-link' # a link to a resource to be used directly.
59
60
61 INVALID = 'invalid'
62
63 class OrthancResource(BaseModel):
64 dicom_uid: Optional[str] = Field(alias="dicom-uid", default=None)
65 orthanc_id: Optional[str] = Field(alias="orthanc-id", default=None)
66 url: Optional[str] = None # e.g. a download link /studies/.../archive
67 level: Levels
68
69 class Config: # allow creating object from dict (used when deserializing the JWT)
70 allow_population_by_field_name = True
71
72
73 class TokenCreationRequest(BaseModel):
74 id: Optional[str] = None
75 resources: List[OrthancResource]
76 type: TokenType = Field(default=TokenType.INVALID)
77 expiration_date: Optional[StringDateTime] = Field(alias="expiration-date", default=None)
78 validity_duration: Optional[int] = Field(alias='validity-duration', default=None) # alternate way to provide an expiration_date, more convenient for instant-links since the duration is relative to the server time, not the client time !
79
80 class Config: # allow creating object from dict (used when deserializing the JWT)
81 allow_population_by_field_name = True
82
83
84 class TokenCreationResponse(BaseModel):
85 request: TokenCreationRequest
86 token: str
87 url: Optional[str] = None
88
89
90 class TokenValidationRequest(BaseModel):
91 dicom_uid: Optional[str] = Field(alias="dicom-uid", default=None)
92 orthanc_id: Optional[str] = Field(alias="orthanc-id", default=None)
93 token_key: Optional[str] = Field(alias="token-key", default=None)
94 token_value: Optional[str] = Field(alias="token-value", default=None)
95 server_id: Optional[str] = Field(alias="server-id", default=None)
96 level: Optional[Levels]
97 method: Methods
98 uri: Optional[str]
99 # labels: Optional[List[str]]
100
101
102 class TokenValidationResponse(BaseModel):
103 granted: bool
104 validity: int
105
106
107 class TokenDecoderRequest(BaseModel):
108 token_key: Optional[str] = Field(alias="token-key", default=None)
109 token_value: Optional[str] = Field(alias="token-value", default=None)
110
111
112 class TokenDecoderResponse(BaseModel):
113 token_type: Optional[TokenType] = Field(alias="token-type", default=None)
114 error_code: Optional[DecoderErrorCodes] = Field(alias="error-code", default=None)
115 redirect_url: Optional[str] = Field(alias="redirect-url", default=None)
116
117
118 class UserProfileRequest(BaseModel):
119 token_key: Optional[str] = Field(alias="token-key", default=None)
120 token_value: Optional[str] = Field(alias="token-value", default=None)
121 server_id: Optional[str] = Field(alias="server-id", default=None)
122
123
124 class UserPermissions(str, Enum):
125 ALL = 'all'
126 VIEW = 'view'
127 DOWNLOAD = 'download'
128 DELETE = 'delete'
129 SEND = 'send'
130 MODIFY = 'modify'
131 ANONYMIZE = 'anonymize'
132 UPLOAD = 'upload'
133 Q_R_REMOTE_MODALITIES = 'q-r-remote-modalities'
134 SETTINGS = 'settings'
135 API_VIEW = 'api-view'
136 EDIT_LABELS = 'edit-labels'
137
138 SHARE = 'share'
139
140
141 class UserProfileResponse(BaseModel):
142 name: str
143 authorized_labels: List[str] = Field(alias="authorized-labels", default_factory=list)
144 # authorized_labels: List[str] = Field(default_factory=list)
145 permissions: List[UserPermissions] = Field(default_factory=list)
146 validity: int
147
148 class Config:
149 use_enum_values = True
150 allow_population_by_field_name = True