Skip to content

Commit d48c2e5

Browse files
committed
3.7.5
1 parent a981f7c commit d48c2e5

File tree

15 files changed

+269
-130
lines changed

15 files changed

+269
-130
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: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ class Api
99
private $method;
1010
private $uri;
1111
private $route;
12+
private $authHeader;
1213
private $data;
1314
private $authenticationController;
1415
private $apiKeyAuthentication = false;
1516
private $hostAuthentication = false;
1617

1718
public function __construct()
1819
{
19-
$this->authenticationController = new \Controllers\Api\Authentication\Authentication();
20+
$this->authenticationController = new \Controllers\Api\Authentication();
2021

2122
/**
2223
* Exit if method is not allowed
@@ -28,30 +29,37 @@ public function __construct()
2829
}
2930

3031
/**
31-
* Get method
32+
* Retrieve method
3233
*/
3334
$this->method = $_SERVER['REQUEST_METHOD'];
3435

3536
/**
36-
* Retrieve data
37+
* Retrieve JSON data if any
3738
*/
3839
$this->data = json_decode(file_get_contents("php://input"));
3940

4041
/**
41-
* Quit on error if no data was sent
42+
* Retrieve authentication header if any
4243
*/
43-
if (empty($this->data)) {
44-
self::returnError(400, 'Missing data.');
44+
if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
45+
$this->authHeader = $_SERVER['HTTP_AUTHORIZATION'];
4546
}
4647

48+
/**
49+
* Quit on error if no data was sent
50+
*/
51+
// if (empty($this->data)) {
52+
// self::returnError(400, 'Missing data.');
53+
// }
54+
4755
/**
4856
* Retrieve URI
4957
*/
5058
$this->uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
5159
$this->uri = explode('/', $this->uri);
5260

5361
/**
54-
* Get route from URI
62+
* Retrieve route from URI
5563
*/
5664
$this->route = $this->uri[3];
5765

@@ -76,7 +84,7 @@ public function __construct()
7684
/**
7785
* Check if authentication is valid from data sent
7886
*/
79-
if (!$this->authenticationController->valid($this->data)) {
87+
if (!$this->authenticationController->valid($this->authHeader, $this->data)) {
8088
self::returnError(401, 'Bad credentials.');
8189
}
8290

@@ -124,9 +132,24 @@ public function run()
124132
/**
125133
* Call API controller
126134
*/
127-
$myapiController = new $apiControllerPath($this->method, $this->uri, $this->data);
135+
$myapiController = new $apiControllerPath($this->method, $this->uri);
136+
137+
/**
138+
* Set authentication method (true or false)
139+
*/
128140
$myapiController->setApiKeyAuthentication($this->apiKeyAuthentication);
129141
$myapiController->setHostAuthentication($this->hostAuthentication);
142+
143+
/**
144+
* Set JSON data if any
145+
*/
146+
if (!empty($this->data)) {
147+
$myapiController->setJsonData($this->data);
148+
}
149+
150+
/**
151+
* Execute API controller and return results
152+
*/
130153
$resultArray = $myapiController->execute();
131154
self::returnSuccess($resultArray);
132155
} catch (Exception $e) {

www/controllers/Api/Authentication/Authentication.php renamed to www/controllers/Api/Authentication.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Controllers\Api\Authentication;
3+
namespace Controllers\Api;
44

55
use Exception;
66

@@ -29,15 +29,20 @@ public function getHostAuthenticationStatus()
2929

3030
/**
3131
* Check if authentication is valid
32-
* It can be an API key authentication or a host authId and token authentication
32+
* It can be an API key authentication or a host authId+token authentication
3333
*/
34-
public function valid(object $data)
34+
public function valid(string $authHeader = null, object $data = null)
3535
{
3636
/**
37-
* If API key is specified
37+
* If API key is specified through the Authorization header
38+
* e.g. "Authorization: Bearer <API_KEY>"
3839
*/
39-
if (!empty($data->apikey)) {
40-
$apiKey = $data->apikey;
40+
if (!empty($authHeader) && strpos($authHeader, 'Bearer ') === 0) {
41+
/**
42+
* Extract the token
43+
* Remove "Bearer " from the header
44+
*/
45+
$apiKey = substr($authHeader, 7);
4146
}
4247

4348
/**
@@ -72,6 +77,15 @@ public function valid(object $data)
7277
* Set apiKeyAuthentication to true if API key is valid
7378
*/
7479
$this->apiKeyAuthentication = true;
80+
81+
/**
82+
* Check if API key is an Admin API key
83+
*/
84+
if ($this->loginController->apiKeyIsAdmin($apiKey)) {
85+
if (!defined('IS_API_ADMIN')) {
86+
define('IS_API_ADMIN', true);
87+
}
88+
}
7589
}
7690

7791
/**

www/controllers/Api/Controller.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Controllers\Api;
4+
5+
use Exception;
6+
7+
class Controller
8+
{
9+
protected $method;
10+
protected $uri;
11+
protected $data;
12+
protected $apiKeyAuthentication = false;
13+
protected $hostAuthentication = false;
14+
15+
public function __construct(string $method, array $uri)
16+
{
17+
$this->method = $method;
18+
$this->uri = $uri;
19+
}
20+
21+
/**
22+
* Set API key authentication status (true or false)
23+
*/
24+
public function setApiKeyAuthentication(bool $apiKeyAuthentication)
25+
{
26+
$this->apiKeyAuthentication = $apiKeyAuthentication;
27+
}
28+
29+
/**
30+
* Set host authentication status (true or false)
31+
*/
32+
public function setHostAuthentication(bool $hostAuthentication)
33+
{
34+
$this->hostAuthentication = $hostAuthentication;
35+
}
36+
37+
/**
38+
* Set retrieved JSON data from request
39+
*/
40+
public function setJsonData(object $data)
41+
{
42+
$this->data = $data;
43+
}
44+
}

0 commit comments

Comments
 (0)