Skip to content

Commit

Permalink
oauth2: Delete Token
Browse files Browse the repository at this point in the history
It's about damn time.. that we add ability to delete the OAuth2 Token.
This adds a new button to the Token tab that will allow you to delete
the current Token.
  • Loading branch information
JediKev committed Feb 16, 2023
1 parent b7718be commit ca913ba
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
17 changes: 17 additions & 0 deletions include/ajax.email.php
Expand Up @@ -39,4 +39,21 @@ function configureAuth($id, $type, $auth) {
$template = sprintf('email-%sauth.tmpl.php', $authtype);
include INCLUDE_DIR . "staff/templates/$template";
}

/*
* Delete the OAuth2 token
*/
function deleteToken($id, $type) {
// Check to make sure the email exists
if (!($email=Email::lookup($id)))
Http::response(404, 'Unknown Email');
// Get the authentication account
if (!($account=$email->getAuthAccount($type)))
Http::response(404, 'Unknown Authentication Type');
// Destory the account config which will delete the token
if ($account->destroyConfig())
Http::response(201, __('Token Deleted Successfully.'));
// Couldn't delete the Token
Http::response(404, 'Unable to delete token.');
}
}
9 changes: 8 additions & 1 deletion include/class.email.php
Expand Up @@ -983,6 +983,13 @@ public function updateCredentials($auth, $vars, &$errors) {
return $this->save();
}

/*
* Destory the account config
*/
function destroyConfig() {
return $this->getConfig()->destroy();
}

function update($vars, &$errors) {
return false;
}
Expand Down Expand Up @@ -1010,7 +1017,7 @@ function save($refetch=false) {

function delete() {
// Destroy the Email config
$this->getConfig()->destroy();
$this->destroyConfig();
// Delete the Plugin instance
if ($this->isOAuthAuth() && ($i=$this->getOAuth2Instance()))
$i->delete();
Expand Down
27 changes: 26 additions & 1 deletion include/staff/templates/email-oauth2auth.tmpl.php
Expand Up @@ -123,7 +123,14 @@
__('Expired Access Token gets auto-refreshed on use'));
}
?>
<table class="form_table" width="940" border="0" cellspacing="0" cellpadding="2">
<a class="red button action-button pull-right" style="margin-bottom:8px;"
id="token-delete" data-toggle="tooltip"
href="<?php echo sprintf('#email/%d/auth/config/%s/delete', $email->getId(), $type); ?>"
title="Delete" data-original-title="Delete">
<i class="icon-trash"></i>&nbsp;<?php echo __('Delete Token'); ?>
</a>
<div class="clear"></div>
<table class="form_table" width="100%" border="0" cellspacing="0" cellpadding="2">
<thead>
<tr>
<th colspan="2">
Expand Down Expand Up @@ -173,3 +180,21 @@
</span>
</p>
</form>
<script type="text/javascript">
$(function() {
$('#oauth-tabs_container').on('click', 'a#token-delete', function(e) {
e.preventDefault();
if (confirm(__('Are you sure?'))) {
$.ajax({
url: 'ajax.php/' + $(this).attr('href').substr(1),
type: 'POST',
success: function(json) {
$.toggleOverlay(false);
$('#popup').hide();
}
});
}
return false;
});
});
</script>
1 change: 1 addition & 0 deletions scp/ajax.php
Expand Up @@ -316,6 +316,7 @@ function staffLoginPage($msg='Unauthorized') {
)),
url('^/email', patterns('ajax.email.php:EmailAjaxAPI',
url_post('^/(?P<id>\d+)/stash$', 'stashFormData'),
url_post('^/(?P<id>\d+)/auth/config/(?P<type>\w+)/delete$', 'deleteToken'),
url('^/(?P<id>\d+)/auth/config/(?P<type>\w+)/(?P<auth>.+)$', 'configureAuth'),
))
);
Expand Down

0 comments on commit ca913ba

Please sign in to comment.