@@ -105,6 +105,39 @@ def check_session():
105
105
return session .get ('auth' ) is True
106
106
107
107
108
+ def validate_parent_access (parent_id ):
109
+ cfg = config .load ()
110
+ if cfg .standalone :
111
+ return True
112
+ auth = _parse_auth_header ()
113
+ if not auth :
114
+ return False
115
+ full_repos_name = auth .get ('repository' , '' ).split ('/' )
116
+ if len (full_repos_name ) != 2 :
117
+ logger .debug ('validate_token: Invalid repository field' )
118
+ return False
119
+ index_endpoint = cfg .index_endpoint
120
+ if index_endpoint is None :
121
+ index_endpoint = 'https://index.docker.io'
122
+ index_endpoint = index_endpoint .strip ('/' )
123
+ url = '{0}/v1/images/{1}/{2}/{3}' .format (
124
+ index_endpoint , full_repos_name [0 ], full_repos_name [1 ], parent_id
125
+ )
126
+ headers = {'Authorization' : flask .request .headers .get ('authorization' )}
127
+ resp = requests .get (url , verify = True , headers = headers )
128
+ if resp .status_code != 200 :
129
+ logger .debug ('validate_parent_access: index returned status {0}' .format (
130
+ resp .status_code
131
+ ))
132
+ return False
133
+ try :
134
+ return json .loads (resp .text ).get ('authorized' , False )
135
+
136
+ except json .JSONDecodeError :
137
+ logger .debug ('validate_parent_access: Wrong response format' )
138
+ return False
139
+
140
+
108
141
def validate_token (auth ):
109
142
full_repos_name = auth .get ('repository' , '' ).split ('/' )
110
143
if len (full_repos_name ) != 2 :
@@ -150,18 +183,23 @@ def is_ssl():
150
183
return False
151
184
152
185
153
- def check_token (args ):
154
- cfg = config .load ()
155
- if cfg .disable_token_auth is True or cfg .standalone is not False :
156
- return True
186
+ def _parse_auth_header ():
157
187
auth = flask .request .headers .get ('authorization' , '' )
158
188
if auth .split (' ' )[0 ].lower () != 'token' :
159
189
logger .debug ('check_token: Invalid token format' )
160
- return False
161
- logger .debug ('args = {0}' .format (args ))
190
+ return None
162
191
logger .debug ('Auth Token = {0}' .format (auth ))
163
192
auth = dict (_re_authorization .findall (auth ))
164
193
logger .debug ('auth = {0}' .format (auth ))
194
+ return auth
195
+
196
+
197
+ def check_token (args ):
198
+ cfg = config .load ()
199
+ if cfg .disable_token_auth is True or cfg .standalone is not False :
200
+ return True
201
+ logger .debug ('args = {0}' .format (args ))
202
+ auth = _parse_auth_header ()
165
203
if not auth :
166
204
return False
167
205
if 'namespace' in args and 'repository' in args :
0 commit comments