Commit 49759f00 authored by Thomas's avatar Thomas
Browse files

Add authorization code

parent 4b7c0718
......@@ -269,7 +269,6 @@ class Mastodon_api {
$parameters['client_name'] = $client_name;
$parameters['scopes'] = implode(' ',$scopes);
if (empty($redirect_uris)) {
$parameters['redirect_uris'] = 'urn:ietf:wg:oauth:2.0:oob';
} else {
......@@ -325,6 +324,67 @@ class Mastodon_api {
return $response;
}
/**
* login
*
* @param string $code Authorization code
* @param array $scopes read / write / follow
* @param string $redirect_uri
*
* @return array $response
* string $response['access_token']
* string $response['token_type'] bearer
* string $response['scope'] read
* int $response['created_at'] time
*/
public function loginAuthorization ($code, $scopes = array(), $redirect_uri = '') {
$parameters = array();
$parameters['client_id'] = $this->client_id;
$parameters['client_secret'] = $this->client_secret;
$parameters['grant_type'] = 'authorization_code';
$parameters['code'] = $code;
if (empty($redirect_uri)) {
$parameters['redirect_uri'] = 'urn:ietf:wg:oauth:2.0:oob';
} else {
$parameters['redirect_uri'] = $redirect_uri;
}
if (count($scopes) == 0) {
if (count($this->scopes) == 0) {
$scopes = array('read','write','follow');
} else {
$scopes = $this->scopes;
}
}
$parameters['scopes'] = implode(' ',$scopes);
$response = $this->_post('/oauth/token',$parameters);
if (isset($response['html']['access_token'])) {
$this->token['access_token'] = $response['html']['access_token'];
$this->token['token_type'] = $response['html']['token_type'];
}
return $response;
}
public function getAuthorizationUrl($redirect_uri)
{
$parameters = array();
$parameters['client_id'] = $this->client_id;
$parameters['redirect_uri'] = $redirect_uri;
$parameters['response_type'] = 'code';
if (count($this->scopes) == 0) {
$parameters['scope'] = implode(' ',array('read','write','follow'));
} else {
$parameters['scope'] = implode(' ',$this->scopes);
}
$response = $this->_get('/oauth/authorize',$parameters);
if (isset($response['html']['access_token'])) {
$this->token['access_token'] = $response['html']['access_token'];
$this->token['token_type'] = $response['html']['token_type'];
}
return $response;
}
/**
......
......@@ -118,6 +118,7 @@ class Mastodon_comments_Admin {
session_start();
}
public function options_update_site() {
register_setting($this->plugin_name.'settings', $this->plugin_name.'settings', array($this, 'set_account'));
register_setting($this->plugin_name.'account', $this->plugin_name.'account', array($this, 'set_manage_account'));
......
......@@ -82,42 +82,53 @@ if( !empty($currentUser) && !empty($currentUser[0]->mastodon_instance) && strlen
<?php
$_SESSION['mc_step'] = 3;
}
$redirectURI = null;
//No valid account, the user need to login in (two steps : Client + Credentials)
if( $_SESSION['mc_step'] == 1 && isset($options['instance_name']) ){
$_SESSION['instance_name'] = $options['instance_name'];
$Mastodon_api = new Mastodon_api();
$Mastodon_api->set_url("https://" . $_SESSION['instance_name']);
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$data = $Mastodon_api->create_app("Mastodon Comments", array('read'), null, "https://mastalab.app");
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$data = $Mastodon_api->create_app("Mastodon Comments", array('read'), null, "https://mastalab.app");
if (! isset($data['html']['id'])) {
$_SESSION['mc_step'] = 1;
unset($_SESSION['instance_name']);
unset($_SESSION['client_secret']);
unset($_SESSION['client_id']);
if(isset($data['html']['error_description']))
$error_message = $data['html']['error_description'];
elseif( !preg_match('/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/', $options['instance_name']))
$error_message = esc_html__('The domain does not seem to be valid!', $this->plugin_name);
else
$error_message = esc_html__('An error occured!', $this->plugin_name);
delete_option( $this->plugin_name);
delete_option( $this->plugin_name."settings");
}else{
$Mastodon_api->set_scopes(array('read'));
$_SESSION['client_id'] = $data['html']['client_id'];
$_SESSION['client_secret'] = $data['html']['client_secret'];
$_SESSION['mc_step'] = 2;
$data = $Mastodon_api->getAuthorizationUrl("urn:ietf:wg:oauth:2.0:oob");
if( isset($data['response']['url'])){
$redirectURI = $data['response']['url'];
}
}
}if ( $_SESSION['mc_step'] == 2 && isset($options['login']) ){
}
if ( isset($options['oauth']) ){
$Mastodon_api = new Mastodon_api();
$Mastodon_api->set_url("https://" . $_SESSION['instance_name']);
$Mastodon_api->set_scopes(array('read'));
$Mastodon_api->set_client($_SESSION['client_id'], $_SESSION['client_secret']);
$data = $Mastodon_api->login($options['login'], $options['pwd']);
$data = $Mastodon_api->loginAuthorization($options['oauth'], array('read'));
if (! isset($data['html']['access_token'])) {
$_SESSION['mc_step'] = 1;
unset($_SESSION['instance_name']);
unset($_SESSION['client_secret']);
unset($_SESSION['client_id']);
delete_option( $this->plugin_name."settings");
if(isset($data['html']['error_description']))
$error_message = $data['html']['error_description'];
else
......@@ -163,6 +174,7 @@ if( $_SESSION['mc_step'] == 1 && isset($options['instance_name']) ){
}
?>
<?php //Forms for connecting an account (two steps : Client + Credentials) ?>
<?php if ( $_SESSION['mc_step'] < 3){?>
<form method="post" name="<?php echo $this->plugin_name.'settings';?>" class="mw_highlight" action="options.php">
<?php settings_fields($this->plugin_name.'settings');?>
......@@ -182,14 +194,14 @@ if( $_SESSION['mc_step'] == 1 && isset($options['instance_name']) ){
<?php }else if( $_SESSION['mc_step'] == 2 ){?>
<fieldset>
<p><?php printf(esc_html__('Authentification on %s', $this->plugin_name), $_SESSION['instance_name']);?></p>
<label for="<?php echo $this->plugin_name;?>-login"><?php _e('Login', $this->plugin_name);?></label>
<input type="text" id="<?php echo $this->plugin_name;?>-login" name="<?php echo $this->plugin_name;?>settings[login]" value="<?php echo $_SESSION['mc_login']; ?>" />
<label for="<?php echo $this->plugin_name;?>-pwd"><?php _e('Password', $this->plugin_name);?></label>
<input type="password" id="<?php echo $this->plugin_name;?>-login" name="<?php echo $this->plugin_name;?>settings[pwd]" value="<?php echo $_SESSION['mc_pwd']; ?>" />
<p>
<a href="<?php echo $redirectURI;?>" target="_blank"><?php _e('Click here to get your authorization code', $this->plugin_name);?></a><br/>
</p>
<label for="<?php echo $this->plugin_name;?>-oauth"><?php _e('Authorization code', $this->plugin_name);?></label>
<input type="text" id="<?php echo $this->plugin_name;?>-oauth" name="<?php echo $this->plugin_name;?>settings[oauth]" />
</fieldset>
<?php }?>
<?php submit_button(__('Login', $this->plugin_name), 'primary','submit', TRUE); ?>
<?php submit_button(__('Connect', $this->plugin_name), 'primary','submit', TRUE); ?>
</form>
<?php }?>
<?php //The form is always displayed, it allows to save targeted DOM element?>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment