Skip to content

Commit 1c32c4b

Browse files
committed
3.7.5
1 parent f03f488 commit 1c32c4b

File tree

34 files changed

+605
-366
lines changed

34 files changed

+605
-366
lines changed

docker/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ RUN usermod -a -G repomanager www-data
7575
RUN chown -R www-data:repomanager $WWW_DIR $DATA_DIR $REPOS_DIR
7676

7777
# Copy entrypoint script
78-
RUN cp /tmp/repomanager/docker/entrypoint.sh /tmp/entrypoint.sh
79-
RUN chmod 700 /tmp/entrypoint.sh
78+
RUN cp /tmp/repomanager/docker/entrypoint.sh /entrypoint.sh
79+
RUN chmod 700 /entrypoint.sh
8080

8181
# Clean
8282
RUN rm -rf /tmp/repomanager
@@ -87,4 +87,4 @@ EXPOSE 8080
8787
# Set working dir
8888
WORKDIR ${DATA_DIR}
8989

90-
ENTRYPOINT ["/tmp/entrypoint.sh"]
90+
ENTRYPOINT ["/entrypoint.sh"]

docker/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if [ ! -z "$FQDN" ];then
1616
echo $FQDN > /etc/mailname
1717

1818
# Repomanager configuration
19-
echo $FQDN > /var/www/repomanager/.fqdn
19+
echo $FQDN > "$WWW_DIR/.fqdn"
2020
fi
2121
if [ ! -z "$MAX_UPLOAD_SIZE" ];then
2222
# Nginx configuration

www/controllers/Api/Api.php

Lines changed: 184 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,69 @@ class Api
99
private $method;
1010
private $uri;
1111
private $route;
12+
private $authHeader;
1213
private $data;
13-
private $authenticationController;
14+
private $loginController;
15+
private $hostController;
1416
private $apiKeyAuthentication = false;
1517
private $hostAuthentication = false;
18+
private $hostId;
19+
private $hostToken;
1620

1721
public function __construct()
1822
{
19-
$this->authenticationController = new \Controllers\Api\Authentication\Authentication();
23+
$this->loginController = new \Controllers\Login();
24+
$this->hostController = new \Controllers\Host();
2025

2126
/**
2227
* Exit if method is not allowed
2328
*/
2429
if ($_SERVER['REQUEST_METHOD'] != 'GET' and $_SERVER['REQUEST_METHOD'] != 'POST' and $_SERVER['REQUEST_METHOD'] != 'PUT' and $_SERVER['REQUEST_METHOD'] != 'DELETE') {
2530
http_response_code(405);
26-
echo json_encode(["return" => "405", "message_error" => array('Method not allowed.')]);
31+
echo json_encode(["return" => "405", "message_error" => array('Method not allowed')]);
2732
exit;
2833
}
2934

3035
/**
31-
* Get method
36+
* Retrieve method
3237
*/
3338
$this->method = $_SERVER['REQUEST_METHOD'];
3439

3540
/**
36-
* Retrieve data
41+
* Retrieve data if any
3742
*/
38-
$this->data = json_decode(file_get_contents("php://input"));
43+
$this->data = file_get_contents("php://input");
44+
45+
if (!empty($this->data)) {
46+
$this->data = json_decode($this->data);
47+
48+
if ($this->data == null) {
49+
self::returnError(400, 'Invalid JSON data');
50+
}
51+
}
3952

4053
/**
41-
* Quit on error if no data was sent
54+
* Retrieve authentication header if any
4255
*/
43-
if (empty($this->data)) {
44-
self::returnError(400, 'Missing data.');
56+
if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
57+
$this->authHeader = $_SERVER['HTTP_AUTHORIZATION'];
4558
}
4659

60+
/**
61+
* Quit on error if no data was sent
62+
*/
63+
// if (empty($this->data)) {
64+
// self::returnError(400, 'Missing data.');
65+
// }
66+
4767
/**
4868
* Retrieve URI
4969
*/
5070
$this->uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
5171
$this->uri = explode('/', $this->uri);
5272

5373
/**
54-
* Get route from URI
74+
* Retrieve route from URI
5575
*/
5676
$this->route = $this->uri[3];
5777

@@ -76,25 +96,147 @@ public function __construct()
7696
/**
7797
* Check if authentication is valid from data sent
7898
*/
79-
if (!$this->authenticationController->valid($this->data)) {
80-
self::returnError(401, 'Bad credentials.');
99+
if (!$this->authenticate($this->authHeader, $this->data)) {
100+
self::returnError(401, 'Bad credentials');
81101
}
82102

83-
/**
84-
* Retrieve valid authentication method
85-
*/
86-
$this->apiKeyAuthentication = $this->authenticationController->getApiKeyAuthenticationStatus();
87-
$this->hostAuthentication = $this->authenticationController->getHostAuthenticationStatus();
88-
89103
/**
90104
* Check if method and URI are specified
91105
*/
92106
if (empty($_SERVER['REQUEST_METHOD'])) {
93-
throw new Exception('No method specified.');
107+
throw new Exception('No method specified');
94108
}
95109
if (empty($_SERVER['REQUEST_URI'])) {
96-
throw new Exception('No route specified.');
110+
throw new Exception('No route specified');
111+
}
112+
}
113+
114+
/**
115+
* Check if authentication is valid
116+
* It can be an API key authentication or a host authId+token authentication
117+
*/
118+
public function authenticate(string $authHeader = null, string|object $data = null)
119+
{
120+
/**
121+
* New authentication method
122+
*/
123+
124+
/**
125+
* If API key or host Id+token is specified through the Authorization header
126+
* e.g.
127+
* "Authorization: Bearer <API_KEY>"
128+
* "Authorization: Host <HOST_ID>:<HOST_TOKEN>"
129+
*/
130+
if (!empty($authHeader)) {
131+
if (strpos($authHeader, 'Bearer ') === 0) {
132+
/**
133+
* Extract the token
134+
* Remove "Bearer " from the header
135+
*/
136+
$apiKey = substr($authHeader, 7);
137+
}
138+
139+
/**
140+
* If host Id+token are specified through the Authorization header
141+
*/
142+
if (strpos($authHeader, 'Host ') === 0) {
143+
/**
144+
* Extract the host Id and token
145+
* Remove "Host " from the header
146+
*/
147+
$hostIdToken = substr($authHeader, 5);
148+
149+
/**
150+
* Split the host Id and token
151+
*/
152+
$hostIdToken = explode(':', $hostIdToken);
153+
154+
/**
155+
* Check if host Id and token are specified
156+
*/
157+
if (count($hostIdToken) != 2) {
158+
return false;
159+
}
160+
161+
/**
162+
* Set host authId and token
163+
*/
164+
$hostId = $hostIdToken[0];
165+
$hostToken = $hostIdToken[1];
166+
}
167+
}
168+
169+
/**
170+
* Old authentication method
171+
*/
172+
173+
/**
174+
* If API key is specified in data
175+
*/
176+
if (!empty($data->apikey)) {
177+
$apiKey = $data->apikey;
178+
}
179+
180+
/**
181+
* If host authId and token are specified in data
182+
*/
183+
if (!empty($data->id)) {
184+
$hostId = $data->id;
185+
}
186+
if (!empty($data->token)) {
187+
$hostToken = $data->token;
188+
}
189+
190+
/**
191+
* If no API key or host authId and token are specified
192+
*/
193+
if (empty($apiKey) and (empty($hostId) or empty($hostToken))) {
194+
return false;
97195
}
196+
197+
/**
198+
* If API key is specified, check that it is valid
199+
*/
200+
if (!empty($apiKey)) {
201+
/**
202+
* Check if API key exists
203+
*/
204+
if (!$this->loginController->apiKeyValid($apiKey)) {
205+
return false;
206+
}
207+
208+
/**
209+
* Set apiKeyAuthentication to true if API key is valid
210+
*/
211+
$this->apiKeyAuthentication = true;
212+
213+
/**
214+
* Check if API key is an Admin API key
215+
*/
216+
if ($this->loginController->apiKeyIsAdmin($apiKey)) {
217+
if (!defined('IS_API_ADMIN')) {
218+
define('IS_API_ADMIN', true);
219+
}
220+
}
221+
}
222+
223+
/**
224+
* If a host authId and token have been specified, check if they are valid
225+
*/
226+
if (!empty($hostId) and !empty($hostToken)) {
227+
if (!$this->hostController->checkIdToken($hostId, $hostToken)) {
228+
return false;
229+
}
230+
231+
/**
232+
* Set hostAuthentication to true if host authId and token are valid
233+
*/
234+
$this->hostAuthentication = true;
235+
$this->hostId = $hostId;
236+
$this->hostToken = $hostToken;
237+
}
238+
239+
return true;
98240
}
99241

100242
/**
@@ -116,17 +258,37 @@ public function run()
116258
* Check if route is valid by checking if corresponding controller exists
117259
*/
118260
if (!file_exists(ROOT . '/controllers/Api/' . ucfirst($this->route) . '/' . ucfirst($this->route) . '.php')) {
119-
throw new Exception('No matching route.');
261+
throw new Exception('No matching route');
120262
}
121263

122264
$apiControllerPath = '\Controllers\Api\\' . ucfirst($this->route) . '\\' . ucfirst($this->route);
123265

124266
/**
125267
* Call API controller
126268
*/
127-
$myapiController = new $apiControllerPath($this->method, $this->uri, $this->data);
269+
$myapiController = new $apiControllerPath($this->method, $this->uri);
270+
271+
/**
272+
* Set authentication method (true or false)
273+
*/
128274
$myapiController->setApiKeyAuthentication($this->apiKeyAuthentication);
129275
$myapiController->setHostAuthentication($this->hostAuthentication);
276+
277+
if ($this->hostAuthentication) {
278+
$myapiController->setHostId($this->hostId);
279+
$myapiController->setHostToken($this->hostToken);
280+
}
281+
282+
/**
283+
* Set JSON data if any
284+
*/
285+
if (!empty($this->data)) {
286+
$myapiController->setJsonData($this->data);
287+
}
288+
289+
/**
290+
* Execute API controller and return results
291+
*/
130292
$resultArray = $myapiController->execute();
131293
self::returnSuccess($resultArray);
132294
} catch (Exception $e) {

0 commit comments

Comments
 (0)