oauth and oauth2 apps manager

This commit is contained in:
Mario Vavti 2018-09-26 16:22:34 +02:00
parent 91502b4104
commit aab97adb23
8 changed files with 96 additions and 53 deletions

View File

@ -1,27 +1,33 @@
<?php <?php
namespace Zotlabs\Module\Settings; namespace Zotlabs\Module;
use App;
use Zotlabs\Lib\Apps;
use Zotlabs\Web\Controller;
class Oauth { class Oauth extends Controller {
function post() { function post() {
if(! Apps::system_app_installed(local_channel(), 'OAuth'))
return;
if(x($_POST,'remove')){ if(x($_POST,'remove')){
check_form_security_token_redirectOnErr('/settings/oauth', 'settings_oauth'); check_form_security_token_redirectOnErr('/oauth', 'oauth');
$key = $_POST['remove']; $key = $_POST['remove'];
q("DELETE FROM tokens WHERE id='%s' AND uid=%d", q("DELETE FROM tokens WHERE id='%s' AND uid=%d",
dbesc($key), dbesc($key),
local_channel()); local_channel());
goaway(z_root()."/settings/oauth/"); goaway(z_root()."/oauth");
return; return;
} }
if((argc() > 2) && (argv(2) === 'edit' || argv(2) === 'add') && x($_POST,'submit')) { if((argc() > 1) && (argv(1) === 'edit' || argv(1) === 'add') && x($_POST,'submit')) {
check_form_security_token_redirectOnErr('/settings/oauth', 'settings_oauth'); check_form_security_token_redirectOnErr('oauth', 'oauth');
$name = ((x($_POST,'name')) ? escape_tags($_POST['name']) : ''); $name = ((x($_POST,'name')) ? escape_tags($_POST['name']) : '');
$key = ((x($_POST,'key')) ? escape_tags($_POST['key']) : ''); $key = ((x($_POST,'key')) ? escape_tags($_POST['key']) : '');
@ -73,17 +79,27 @@ class Oauth {
); );
} }
} }
goaway(z_root()."/settings/oauth/"); goaway(z_root()."/oauth");
return; return;
} }
} }
function get() { function get() {
if(! Apps::system_app_installed(local_channel(), 'OAuth Apps Manager')) {
//Do not display any associated widgets at this point
App::$pdl = '';
$o = '<b>OAuth App (Not Installed):</b><br>';
$o .= t('An OAuth apps manager');
return $o;
}
if((argc() > 2) && (argv(2) === 'add')) { if((argc() > 1) && (argv(1) === 'add')) {
$tpl = get_markup_template("settings_oauth_edit.tpl"); $tpl = get_markup_template("oauth_edit.tpl");
$o .= replace_macros($tpl, array( $o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth"), '$form_security_token' => get_form_security_token("oauth"),
'$title' => t('Add application'), '$title' => t('Add application'),
'$submit' => t('Submit'), '$submit' => t('Submit'),
'$cancel' => t('Cancel'), '$cancel' => t('Cancel'),
@ -96,9 +112,9 @@ class Oauth {
return $o; return $o;
} }
if((argc() > 3) && (argv(2) === 'edit')) { if((argc() > 2) && (argv(1) === 'edit')) {
$r = q("SELECT * FROM clients WHERE client_id='%s' AND uid=%d", $r = q("SELECT * FROM clients WHERE client_id='%s' AND uid=%d",
dbesc(argv(3)), dbesc(argv(2)),
local_channel()); local_channel());
if (!count($r)){ if (!count($r)){
@ -107,9 +123,9 @@ class Oauth {
} }
$app = $r[0]; $app = $r[0];
$tpl = get_markup_template("settings_oauth_edit.tpl"); $tpl = get_markup_template("oauth_edit.tpl");
$o .= replace_macros($tpl, array( $o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth"), '$form_security_token' => get_form_security_token("oauth"),
'$title' => t('Add application'), '$title' => t('Add application'),
'$submit' => t('Update'), '$submit' => t('Update'),
'$cancel' => t('Cancel'), '$cancel' => t('Cancel'),
@ -122,13 +138,13 @@ class Oauth {
return $o; return $o;
} }
if((argc() > 3) && (argv(2) === 'delete')) { if((argc() > 2) && (argv(1) === 'delete')) {
check_form_security_token_redirectOnErr('/settings/oauth', 'settings_oauth', 't'); check_form_security_token_redirectOnErr('/oauth', 'oauth', 't');
$r = q("DELETE FROM clients WHERE client_id='%s' AND uid=%d", $r = q("DELETE FROM clients WHERE client_id='%s' AND uid=%d",
dbesc(argv(3)), dbesc(argv(2)),
local_channel()); local_channel());
goaway(z_root()."/settings/oauth/"); goaway(z_root()."/oauth");
return; return;
} }
@ -141,11 +157,11 @@ class Oauth {
local_channel()); local_channel());
$tpl = get_markup_template("settings_oauth.tpl"); $tpl = get_markup_template("oauth.tpl");
$o .= replace_macros($tpl, array( $o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth"), '$form_security_token' => get_form_security_token("oauth"),
'$baseurl' => z_root(), '$baseurl' => z_root(),
'$title' => t('Connected Apps'), '$title' => t('Connected OAuth Apps'),
'$add' => t('Add application'), '$add' => t('Add application'),
'$edit' => t('Edit'), '$edit' => t('Edit'),
'$delete' => t('Delete'), '$delete' => t('Delete'),
@ -158,4 +174,4 @@ class Oauth {
} }
} }

View File

@ -1,15 +1,21 @@
<?php <?php
namespace Zotlabs\Module\Settings; namespace Zotlabs\Module;
use App;
use Zotlabs\Lib\Apps;
use Zotlabs\Web\Controller;
class Oauth2 { class Oauth2 extends Controller {
function post() { function post() {
if(! Apps::system_app_installed(local_channel(), 'OAuth2 Apps Manager'))
return;
if(x($_POST,'remove')){ if(x($_POST,'remove')){
check_form_security_token_redirectOnErr('/settings/oauth2', 'settings_oauth2'); check_form_security_token_redirectOnErr('oauth2', 'oauth2');
$name = ((x($_POST,'name')) ? escape_tags(trim($_POST['name'])) : ''); $name = ((x($_POST,'name')) ? escape_tags(trim($_POST['name'])) : '');
logger("REMOVE! ".$name." uid: ".local_channel()); logger("REMOVE! ".$name." uid: ".local_channel());
$key = $_POST['remove']; $key = $_POST['remove'];
@ -25,13 +31,13 @@ class Oauth2 {
dbesc($name), dbesc($name),
intval(local_channel()) intval(local_channel())
); );
goaway(z_root()."/settings/oauth2/"); goaway(z_root()."/oauth2");
return; return;
} }
if((argc() > 2) && (argv(2) === 'edit' || argv(2) === 'add') && x($_POST,'submit')) { if((argc() > 1) && (argv(1) === 'edit' || argv(1) === 'add') && x($_POST,'submit')) {
check_form_security_token_redirectOnErr('/settings/oauth2', 'settings_oauth2'); check_form_security_token_redirectOnErr('oauth2', 'oauth2');
$name = ((x($_POST,'name')) ? escape_tags(trim($_POST['name'])) : ''); $name = ((x($_POST,'name')) ? escape_tags(trim($_POST['name'])) : '');
$secret = ((x($_POST,'secret')) ? escape_tags(trim($_POST['secret'])) : ''); $secret = ((x($_POST,'secret')) ? escape_tags(trim($_POST['secret'])) : '');
@ -80,17 +86,26 @@ class Oauth2 {
); );
} }
} }
goaway(z_root()."/settings/oauth2/"); goaway(z_root()."/oauth2");
return; return;
} }
} }
function get() { function get() {
if(! Apps::system_app_installed(local_channel(), 'OAuth2 Apps Manager')) {
//Do not display any associated widgets at this point
App::$pdl = '';
$o = '<b>OAuth2 App (Not Installed):</b><br>';
$o .= t('An OAuth2 apps manager');
return $o;
}
if((argc() > 2) && (argv(2) === 'add')) { if((argc() > 1) && (argv(1) === 'add')) {
$tpl = get_markup_template("settings_oauth2_edit.tpl"); $tpl = get_markup_template("oauth2_edit.tpl");
$o .= replace_macros($tpl, array( $o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth2"), '$form_security_token' => get_form_security_token("oauth2"),
'$title' => t('Add OAuth2 application'), '$title' => t('Add OAuth2 application'),
'$submit' => t('Submit'), '$submit' => t('Submit'),
'$cancel' => t('Cancel'), '$cancel' => t('Cancel'),
@ -103,9 +118,9 @@ class Oauth2 {
return $o; return $o;
} }
if((argc() > 3) && (argv(2) === 'edit')) { if((argc() > 2) && (argv(1) === 'edit')) {
$r = q("SELECT * FROM oauth_clients WHERE client_id='%s' AND user_id= %d", $r = q("SELECT * FROM oauth_clients WHERE client_id='%s' AND user_id= %d",
dbesc(argv(3)), dbesc(argv(2)),
intval(local_channel()) intval(local_channel())
); );
@ -116,9 +131,9 @@ class Oauth2 {
$app = $r[0]; $app = $r[0];
$tpl = get_markup_template("settings_oauth2_edit.tpl"); $tpl = get_markup_template("oauth2_edit.tpl");
$o .= replace_macros($tpl, array( $o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth2"), '$form_security_token' => get_form_security_token("oauth2"),
'$title' => t('Add application'), '$title' => t('Add application'),
'$submit' => t('Update'), '$submit' => t('Update'),
'$cancel' => t('Cancel'), '$cancel' => t('Cancel'),
@ -131,26 +146,26 @@ class Oauth2 {
return $o; return $o;
} }
if((argc() > 3) && (argv(2) === 'delete')) { if((argc() > 2) && (argv(1) === 'delete')) {
check_form_security_token_redirectOnErr('/settings/oauth2', 'settings_oauth2', 't'); check_form_security_token_redirectOnErr('oauth2', 'oauth2', 't');
$r = q("DELETE FROM oauth_clients WHERE client_id = '%s' AND user_id = %d", $r = q("DELETE FROM oauth_clients WHERE client_id = '%s' AND user_id = %d",
dbesc(argv(3)), dbesc(argv(2)),
intval(local_channel()) intval(local_channel())
); );
$r = q("DELETE FROM oauth_access_tokens WHERE client_id = '%s' AND user_id = %d", $r = q("DELETE FROM oauth_access_tokens WHERE client_id = '%s' AND user_id = %d",
dbesc(argv(3)), dbesc(argv(2)),
intval(local_channel()) intval(local_channel())
); );
$r = q("DELETE FROM oauth_authorization_codes WHERE client_id = '%s' AND user_id = %d", $r = q("DELETE FROM oauth_authorization_codes WHERE client_id = '%s' AND user_id = %d",
dbesc(argv(3)), dbesc(argv(2)),
intval(local_channel()) intval(local_channel())
); );
$r = q("DELETE FROM oauth_refresh_tokens WHERE client_id = '%s' AND user_id = %d", $r = q("DELETE FROM oauth_refresh_tokens WHERE client_id = '%s' AND user_id = %d",
dbesc(argv(3)), dbesc(argv(2)),
intval(local_channel()) intval(local_channel())
); );
goaway(z_root()."/settings/oauth2/"); goaway(z_root()."/oauth2");
return; return;
} }
@ -164,9 +179,9 @@ class Oauth2 {
intval(local_channel()) intval(local_channel())
); );
$tpl = get_markup_template("settings_oauth2.tpl"); $tpl = get_markup_template("oauth2.tpl");
$o .= replace_macros($tpl, array( $o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_oauth2"), '$form_security_token' => get_form_security_token("oauth2"),
'$baseurl' => z_root(), '$baseurl' => z_root(),
'$title' => t('Connected OAuth2 Apps'), '$title' => t('Connected OAuth2 Apps'),
'$add' => t('Add application'), '$add' => t('Add application'),

6
app/oauth.apd Normal file
View File

@ -0,0 +1,6 @@
version: 1
url: $baseurl/oauth
requires: local_channel
name: OAuth Apps Manager
photo: icon:chevron-circle-up
categories: Access Control

6
app/oauth2.apd Normal file
View File

@ -0,0 +1,6 @@
version: 1
url: $baseurl/oauth2
requires: local_channel
name: OAuth2 Apps Manager
photo: icon:chevron-circle-up
categories: Access Control

View File

@ -4,13 +4,13 @@
</div> </div>
<div class="section-content-tools-wrapper"> <div class="section-content-tools-wrapper">
<form action="settings/oauth" method="post" autocomplete="off"> <form action="oauth" method="post" autocomplete="off">
<input type='hidden' name='form_security_token' value='{{$form_security_token}}'> <input type='hidden' name='form_security_token' value='{{$form_security_token}}'>
<div id="profile-edit-links"> <div id="profile-edit-links">
<ul> <ul>
<li> <li>
<a id="profile-edit-view-link" href="{{$baseurl}}/settings/oauth/add">{{$add}}</a> <a id="profile-edit-view-link" href="{{$baseurl}}/oauth/add">{{$add}}</a>
</li> </li>
</ul> </ul>
</div> </div>
@ -25,8 +25,8 @@
{{/if}} {{/if}}
{{/if}} {{/if}}
{{if $app.my}} {{if $app.my}}
<a href="{{$baseurl}}/settings/oauth/edit/{{$app.client_id}}" title="{{$edit}}"><i class="fa fa-pencil btn btn-outline-secondary"></i></a> <a href="{{$baseurl}}/oauth/edit/{{$app.client_id}}" title="{{$edit}}"><i class="fa fa-pencil btn btn-outline-secondary"></i></a>
<a href="{{$baseurl}}/settings/oauth/delete/{{$app.client_id}}?t={{$form_security_token}}" title="{{$delete}}"><i class="fa fa-trash-o btn btn-outline-secondary"></i></a> <a href="{{$baseurl}}/oauth/delete/{{$app.client_id}}?t={{$form_security_token}}" title="{{$delete}}"><i class="fa fa-trash-o btn btn-outline-secondary"></i></a>
{{/if}} {{/if}}
</div> </div>
{{/foreach}} {{/foreach}}

View File

@ -8,13 +8,13 @@
<div id="profile-edit-links"> <div id="profile-edit-links">
<ul> <ul>
<li> <li>
<a id="profile-edit-view-link" href="{{$baseurl}}/settings/oauth2/add">{{$add}}</a> <a id="profile-edit-view-link" href="{{$baseurl}}/oauth2/add">{{$add}}</a>
</li> </li>
</ul> </ul>
</div> </div>
{{foreach $apps as $app}} {{foreach $apps as $app}}
<form action="settings/oauth2" method="post" autocomplete="off"> <form action="oauth2" method="post" autocomplete="off">
<input type='hidden' name='form_security_token' value='{{$form_security_token}}'> <input type='hidden' name='form_security_token' value='{{$form_security_token}}'>
<input type='hidden' name='name' value='{{$app.client_id}}'> <input type='hidden' name='name' value='{{$app.client_id}}'>
<div class='oauthapp'> <div class='oauthapp'>
@ -25,8 +25,8 @@
{{/if}} {{/if}}
{{/if}} {{/if}}
{{if $app.my}} {{if $app.my}}
<a href="{{$baseurl}}/settings/oauth2/edit/{{$app.client_id}}" title="{{$edit}}"><i class="fa fa-pencil btn btn-outline-secondary"></i></a> <a href="{{$baseurl}}/oauth2/edit/{{$app.client_id}}" title="{{$edit}}"><i class="fa fa-pencil btn btn-outline-secondary"></i></a>
<a href="{{$baseurl}}/settings/oauth2/delete/{{$app.client_id}}?t={{$form_security_token}}" title="{{$delete}}"><i class="fa fa-trash-o btn btn-outline-secondary"></i></a> <a href="{{$baseurl}}/oauth2/delete/{{$app.client_id}}?t={{$form_security_token}}" title="{{$delete}}"><i class="fa fa-trash-o btn btn-outline-secondary"></i></a>
{{/if}} {{/if}}
</div> </div>
</form> </form>