Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 5901d6e

Browse files
author
Dominik František Bučík
authored
Merge pull request #80 from dBucik/api_write
Feat: API write
2 parents 5d6cda4 + 44e507a commit 5901d6e

File tree

8 files changed

+289
-69
lines changed

8 files changed

+289
-69
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,26 @@ Once you have installed SimpleSAMLphp, installing this module is very simple. Fi
7373
```
7474
'instance_name' => 'Instance name',
7575
```
76+
77+
### Writing via API
78+
#### Configuration
79+
Add the following (and adjust the credentials) to enable writing via the API (example request following). Methods supported are `POST,PUT`.
80+
```
81+
'apiWriteEnabled' => true,
82+
'apiWriteUsername' => 'api_writer',
83+
'apiWritePasswordHash' => password_hash('ap1Wr1T3rP@S$'),
84+
```
85+
#### Example request
86+
```
87+
curl --request POST \
88+
--url https://proxy.com/proxy/module.php/proxystatistics/writeLoginApi.php \
89+
--header 'Authorization: Basic encodedCredentials' \
90+
--header 'Content-Type: application/json' \
91+
--data '{
92+
"userId": "user@somewhere.edu",
93+
"serviceIdentifier": "https://service.com/shibboleth",
94+
"serviceName": "TEST_SERVICE",
95+
"idpIdentifier": "https://idp.org/simplesamlphp",
96+
"idpName": "TEST_IDP"
97+
}'
98+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424
},
2525
"require": {
26-
"php": "^7.1 || ^8",
26+
"php": "^7.3 || ^8",
2727
"ext-ctype": "*",
2828
"ext-filter": "*",
2929
"ext-json": "*",

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config-templates/module_proxystatistics.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,19 @@
8787
* For how many days should the detailed statistics be kept. Minimum is 31.
8888
*/
8989
//'keepPerUser' => 62,
90+
91+
/*
92+
* Enables ability to write via an API
93+
*/
94+
//'apiWriteEnabled' => true,
95+
96+
/*
97+
* Username to protect API write endpoint (has no effect if write is disabled)
98+
*/
99+
//'apiWriteUsername' => 'api_writer',
100+
101+
/*
102+
* Password to protect API write endpoint (has no effect if write is disabled)
103+
*/
104+
//'apiWritePasswordHash' => password_hash('ap1Wr1T3rP@S$'),
90105
];

lib/Auth/Process/Statistics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function process(&$request)
2626
$dateTime = new DateTime();
2727
$dbCmd = new DatabaseCommand();
2828
try {
29-
$dbCmd->insertLogin($request, $dateTime);
29+
$dbCmd->insertLoginFromFilter($request, $dateTime);
3030
} catch (Exception $ex) {
3131
Logger::error(
3232
self::DEBUG_PREFIX . 'Caught exception while inserting login into statistics: ' . $ex->getMessage()

lib/Config.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SimpleSAML\Module\proxystatistics;
66

77
use SimpleSAML\Configuration;
8+
use SimpleSAML\Error\Exception;
89

910
class Config
1011
{
@@ -34,6 +35,12 @@ class Config
3435

3536
private const KEEP_PER_USER = 'keepPerUser';
3637

38+
private const API_WRITE_ENABLED = 'apiWriteEnabled';
39+
40+
private const API_WRITE_USERNAME = 'apiWriteUsername';
41+
42+
private const API_WRITE_PASSWORD_HASH = 'apiWritePasswordHash';
43+
3744
private $config;
3845

3946
private $store;
@@ -50,6 +57,12 @@ class Config
5057

5158
private $idAttribute;
5259

60+
private $apiWriteEnabled;
61+
62+
private $apiWriteUsername;
63+
64+
private $apiWritePasswordHash;
65+
5366
private static $instance;
5467

5568
private function __construct()
@@ -62,6 +75,17 @@ private function __construct()
6275
$this->keepPerUser = $this->config->getIntegerRange(self::KEEP_PER_USER, 31, 1827, 31);
6376
$this->requiredAuthSource = $this->config->getString(self::REQUIRE_AUTH_SOURCE, '');
6477
$this->idAttribute = $this->config->getString(self::USER_ID_ATTRIBUTE, 'uid');
78+
$this->apiWriteEnabled = $this->config->getBoolean(self::API_WRITE_ENABLED, false);
79+
if ($this->apiWriteEnabled) {
80+
$this->apiWriteUsername = $this->config->getString(self::API_WRITE_USERNAME);
81+
if (empty(trim($this->apiWriteUsername))) {
82+
throw new Exception('Username for API write cannot be empty');
83+
}
84+
$this->apiWritePasswordHash = $this->config->getString(self::API_WRITE_PASSWORD_HASH);
85+
if (empty(trim($this->apiWritePasswordHash))) {
86+
throw new Exception('Password for API write cannot be empty');
87+
}
88+
}
6589
}
6690

6791
private function __clone()
@@ -123,4 +147,19 @@ public function getKeepPerUser()
123147
{
124148
return $this->keepPerUser;
125149
}
150+
151+
public function isApiWriteEnabled()
152+
{
153+
return $this->apiWriteEnabled;
154+
}
155+
156+
public function getApiWriteUsername()
157+
{
158+
return $this->apiWriteUsername;
159+
}
160+
161+
public function getApiWritePasswordHash()
162+
{
163+
return $this->apiWritePasswordHash;
164+
}
126165
}

0 commit comments

Comments
 (0)