Skip to content

Commit 6f26459

Browse files
committed
[GT-184] Add support for renewing API credentials
1 parent a8d8285 commit 6f26459

File tree

13 files changed

+457
-115
lines changed

13 files changed

+457
-115
lines changed

htdocs/web_portal/controllers/site/edit_api_auth.php

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,41 @@
2121
* See the License for the specific language governing permissions and
2222
* limitations under the License.
2323
/*======================================================*/
24-
require_once __DIR__.'/../../../web_portal/components/Get_User_Principle.php';
25-
require_once __DIR__.'/../utils.php';
26-
require_once __DIR__.'/../../../../lib/Gocdb_Services/Factory.php';
24+
require_once __DIR__ . '/../../../web_portal/components/Get_User_Principle.php';
25+
require_once __DIR__ . '/../utils.php';
26+
require_once __DIR__ . '/../../../../lib/Gocdb_Services/Factory.php';
27+
28+
use Exception;
2729

2830
/**
29-
* Controller to edit authentication entity request
31+
* Controller to either edit authentication entity request or renewal request.
32+
*
3033
* @global array $_POST only set if the browser has POSTed data
3134
* @return null
3235
*/
33-
function edit_entity() {
34-
$dn = Get_User_Principle();
35-
$user = \Factory::getUserService()->getUserByPrinciple($dn);
36+
function edit_entity()
37+
{
38+
list($user, $authEnt, $site, $serv) = initialize();
39+
40+
if ($_POST) {
41+
submit($user, $authEnt, $site, $serv);
42+
} else {
43+
draw($user, $authEnt, $site);
44+
}
45+
}
3646

37-
//Check the portal is not in read only mode, returns exception if it is and user is not an admin
47+
function initialize()
48+
{
49+
$identifier = Get_User_Principle();
50+
$user = \Factory::getUserService()->getUserByPrinciple($identifier);
51+
52+
/**
53+
* Check the portal is not in read only mode,
54+
* returns exception if it is and user is not an admin.
55+
*/
3856
checkPortalIsNotReadOnlyOrUserIsAdmin($user);
3957

40-
if (!isset($_REQUEST['authentityid']) || !is_numeric($_REQUEST['authentityid']) ){
58+
if (!isset($_REQUEST['authentityid']) || !is_numeric($_REQUEST['authentityid'])) {
4159
throw new Exception("A authentication entity id must be specified in the url");
4260
}
4361

@@ -47,48 +65,80 @@ function edit_entity() {
4765

4866
// Validate the user has permission to edit properties
4967
if (!$serv->userCanEditSite($user, $site)) {
50-
throw new \Exception("Permission denied: a site role is required to edit authentication entities at " . $site->getShortName());
68+
throw new \Exception(
69+
"Permission denied: a site role is required to edit authentication entities at " .
70+
$site->getShortName()
71+
);
5172
}
5273

53-
if($_POST) { // If we receive a POST request it's to edit an authentication entity
54-
submit($user, $authEnt, $site, $serv);
55-
} else { // If there is no post data, draw the edit authentication entity form
56-
draw($user, $authEnt, $site);
57-
}
74+
return [$user, $authEnt, $site, $serv];
5875
}
5976

60-
function draw(\User $user = null, \APIAuthentication $authEnt = null, \Site $site = null) {
61-
if(is_null($user)){
77+
/**
78+
* Helper to draw either the edit or renewal authentication entity form.
79+
*
80+
* @param \User|null $user
81+
* @param \APIAuthentication|null $authEntity
82+
* @param \Site|null $site
83+
* @throws \Exception
84+
*/
85+
function draw(
86+
\User $user = null,
87+
\APIAuthentication $authEnt = null,
88+
\Site $site = null
89+
) {
90+
if (is_null($user)) {
6291
throw new Exception("Unregistered users can't edit authentication credentials");
6392
}
6493

6594
$params = array();
6695
$params['site'] = $site;
6796
$params['authEnt'] = $authEnt;
6897
$params['authTypes'] = array();
69-
$params['authTypes'][]='X.509';
70-
$params['authTypes'][]='OIDC Subject';
98+
$params['authTypes'][] = 'X.509';
99+
$params['authTypes'][] = 'OIDC Subject';
71100
$params['user'] = $user;
72101

102+
if ($_REQUEST['isRenewalRequest']) {
103+
$params['isRenewalRequest'] = true;
104+
}
73105
show_view("site/edit_api_auth.php", $params);
74106
die();
75107
}
76108

77-
function submit(\User $user, \APIAuthentication $authEnt, \Site $site, org\gocdb\services\Site $serv) {
78-
$newValues = getAPIAuthenticationFromWeb();
109+
/**
110+
* If this receives a POST request, it can be either to edit an API authentication entity
111+
* or to update the `$lastRenewTime` in `APIAuthentication`.
112+
*
113+
* @param \User $user
114+
* @param \APIAuthentication $authEntity
115+
* @param \Site $site
116+
* @param org\gocdb\services\Site $service
117+
*/
118+
function submit(
119+
\User $user,
120+
\APIAuthentication $authEnt,
121+
\Site $site,
122+
org\gocdb\services\Site $serv
123+
) {
124+
$params = array();
125+
126+
if ($_REQUEST['isRenewalRequest']) {
127+
$newValues['isRenewalRequest'] = $params['isRenewalRequest'] = true;
128+
} else {
129+
$newValues = getAPIAuthenticationFromWeb();
130+
}
79131

80132
try {
81133
$authEnt = $serv->editAPIAuthEntity($authEnt, $user, $newValues);
82-
} catch(Exception $e) {
134+
} catch (Exception $e) {
83135
show_view('error.php', $e->getMessage());
84136
die();
85137
}
86138

87-
$params = array();
88139
$params['apiAuthenticationEntity'] = $authEnt;
89140
$params['site'] = $site;
141+
90142
show_view("site/edited_api_auth.php", $params);
91143
die();
92-
93-
94144
}

htdocs/web_portal/img/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
|trash.png|[Link](http://www.softicons.com/business-icons/ecommerce-and-business-icons-by-designcontest.com/trash-icon)|CC Attribution 3.0 Unported|DesignContest.com|
2424
|user.png|[Link](http://www.iconarchive.com/show/ravenna-3d-icons-by-double-j-design/Users-icon.html)|CC Attribution 4.0|Double-J Design|
2525
|virtualsite.png|[Link](http://www.softicons.com/android-icons/mobile-icon-set-by-webiconset.com/maps-icon)|Free Use of icon| WebIconSet.com|
26+
|refresh_key.png|[Link](https://www.softicons.com/toolbar-icons/free-mobile-icon-kit-by-happy-icon-studio/refresh-key-silver-icon)|Free Use of icon| WebIconSet.com|
513 Bytes
Loading

htdocs/web_portal/views/site/edit_api_auth.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
$user = $params['user'];
55
$entUser = $params['authEnt']->getUser();
66

7-
echo('<h1>Edit API credential for ');
7+
echo('<h1>');
8+
if ($params['isRenewalRequest']) {
9+
echo('Renew API credential for ');
10+
} else {
11+
echo('Edit API credential for ');
12+
}
813
xecho($params['site']->getName());
914
echo('</h1>');
1015

1116
if (!is_null($entUser)) {
12-
1317
echo('<h4>This credential is linked to GOCDB user ');
1418
echo('<a href="');
1519
xecho(\GocContextPath::getPath());
@@ -22,14 +26,22 @@
2226
// entities created prior to GOCDB5.8 have a null owning user
2327
if ($entUser->getId() != $user->getId()) {
2428
echo('<div class="input_warning">');
25-
echo("WARNING: editing will change the linked user from '");
29+
if ($params['isRenewalRequest']) {
30+
echo("WARNING: Renewing this will change the linked user from '");
31+
} else {
32+
echo("WARNING: Editing will change the linked user from '");
33+
}
2634
xecho($entUser->getFullname());
2735
echo("' to '");
2836
xecho($user->getFullname());
29-
echo("'. Click the browser Back button to cancel the edit.</div>");
37+
echo("'. Click the browser Back button to cancel the");
38+
if ($params['isRenewalRequest']) {
39+
echo(' renewal.</div>');
40+
} else {
41+
echo(' edit.</div>');
3042
}
31-
32-
} else {
43+
}
44+
} else {
3345
// This clause should be deleted or replaced with exception after all
3446
// authentication entities are assigned a user.
3547
echo('<div class="input_warning">');
@@ -41,23 +53,36 @@
4153
<form class="inputForm" method="post" action="index.php?Page_Type=Edit_API_Authentication_Entity&parentid=<?php echo($params['site']->getId())?>&authentityid=<?php xecho($params['authEnt']->getId())?>" name="addAPIAuthenticationEntity">
4254
<div style="margin-bottom: 0.5em;">
4355
<span class="input_name">Identifier (e.g. Certificate DN or OIDC Subject)*</span>
44-
<input type="text" value="<?php xecho($params['authEnt']->getIdentifier()) ?>" name="IDENTIFIER" class="input_input_text">
56+
<input
57+
type="text"
58+
value="<?php xecho($params['authEnt']->getIdentifier()) ?>"
59+
name="IDENTIFIER"
60+
class="input_input_text"
61+
<?php echo $params['isRenewalRequest'] ? 'disabled' : ''; ?>
62+
>
4563
</div>
64+
4665
<div style="margin-bottom: 0.5em;">
4766
<span class="input_name">Credential type*</span>
48-
<select name="TYPE" class="input_input_text">
67+
<select
68+
name="TYPE"
69+
class="input_input_text"
70+
<?php echo $params['isRenewalRequest'] ? 'disabled' : ''; ?>
71+
>
4972
<?php foreach($params['authTypes'] as $authType) {?>
5073
<option value="<?php xecho($authType) ?>"<?php if ($params['authEnt']->getType() == $authType) {echo " selected=\"selected\"";} ?>>
5174
<?php xecho($authType) ?>
5275
</option>
5376
<?php } ?>
5477
</select>
5578
</div>
79+
<?php if (!($params['isRenewalRequest'])) {?>
5680
<div style="margin-bottom: 1em">
5781
<div class="input_warning">
58-
WARNING: it is possible to delete information using the write functionality of the API. Leave Allow API write unchecked if
59-
you do not need to write data.
82+
WARNING: It is possible to delete information using the write functionality of the API.
83+
Leave Allow API write unchecked if you do not need to write data.
6084
</div>
85+
6186
<div class="input_checkbox">
6287
<input type="checkbox" name="ALLOW_WRITE" id="ALLOW_WRITE" value="checked"
6388
<?php
@@ -67,6 +92,16 @@
6792
<label class="input_label" for="ALLOW_WRITE">Allow API write</label>
6893
</div>
6994
</div>
70-
<input type="submit" value="Edit credential" class="input_button">
95+
<?php } ?>
96+
97+
<?php if ($params['isRenewalRequest']) {
98+
echo '<br><p> Are you sure you want to continue? </P>';
99+
echo '<input class="input_input_hidden" type="hidden" name="isRenewalRequest" value=true />';
100+
} ?>
101+
<input
102+
type="submit"
103+
value="<?php echo $params['isRenewalRequest'] ? 'Renew credential' : 'Edit credential'; ?>"
104+
class="input_button"
105+
>
71106
</form>
72107
</div>

htdocs/web_portal/views/site/edited_api_auth.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
<div class="rightPageContainer">
22
<h1 class="Success">Success</h1><br />
3-
The API authenication credential has now been updated. Type:<?php xecho($params['apiAuthenticationEntity']->getType()) ?>, identifier: <?php xecho($params['apiAuthenticationEntity']->getIdentifier()) ?>.
3+
The API authenication credential has now been
4+
<?php if ($params['isRenewalRequest']) {
5+
echo 'renewed for';
6+
} else {
7+
echo 'updated. Type: ';
8+
xecho($params['apiAuthenticationEntity']->getType());
9+
echo ',';
10+
}
11+
?>
12+
identifier:
13+
<?php xecho($params['apiAuthenticationEntity']->getIdentifier()) ?>.
414
<br />
515
<a href="index.php?Page_Type=Site&amp;id=<?php echo $params['site']->getID(); ?>">
616
View site</a>

htdocs/web_portal/views/site/view_site.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ class="header"
607607
<th>Type</th>
608608
<th>Identifier</th>
609609
<th>User</th>
610+
<th style="text-align:center;white-space: nowrap">Last Renewed</th>
610611
<th style="text-align:center;white-space: nowrap">Last Used</th>
611612
<th style="text-align:center">Write</th>
612613
<th style="text-align:center;">Edit</th>
@@ -649,6 +650,32 @@ class="header"
649650
}
650651
?>
651652
</td>
653+
<td style="text-align:center">
654+
<?php
655+
$useTime = $APIAuthEnt->getLastRenewTime();
656+
$titleStr = 'Last renewed ' . $useTime->format('d-m-Y H:iTP');
657+
echo '<div title="' . $titleStr . '">';
658+
?>
659+
<?php if (!$portalIsReadOnly) :?>
660+
<form action="index.php?Page_Type=Edit_API_Authentication_Entity&amp;
661+
authentityid=<?php echo $APIAuthEnt->getId();?>&amp;
662+
isRenewalRequest=true"
663+
method="post">
664+
<button type="submit">
665+
<?php
666+
echo '<img height="18px" src="'
667+
. \GocContextPath::getPath()
668+
. 'img/refresh_key.png"';
669+
echo 'title="Renew the API credentials"' . '/>';
670+
?>
671+
</button>
672+
<?php
673+
echo $useTime->format('d-m-y');
674+
echo '</div>';
675+
?>
676+
</form>
677+
<?php endif;?>
678+
</td>
652679
<td style="text-align:center">
653680
<?php
654681
$useTime = $APIAuthEnt->getLastUseTime();

0 commit comments

Comments
 (0)