Skip to content

Commit

Permalink
Initial code commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
SAASPASS Developers committed Dec 24, 2018
0 parents commit b943a1b
Show file tree
Hide file tree
Showing 31 changed files with 991 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Api/LoginManagementInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Saaspass\Login\Api;

interface LoginManagementInterface
{
/**
* Updates the specified user with the specified message.
*
* @api
* @param string $session customers session_id
* @return string
*/
public function authenticated($session);

/**
* Receiving post request from Authentication Source.
*
* @api
* @params string
* @return void
*/
public function authenticate();
}
20 changes: 20 additions & 0 deletions Block/SaaspassLoginBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Saaspass\Login\Block;

use Magento\Framework\View\Element\Template as BaseBlock;
use Saaspass\Login\Helper\Data;
use Magento\Framework\View\Element\Template\Context;

class SaaspassLoginBlock extends BaseBlock
{
/**
* @var Data
*/
public $helper;
public function __construct(Context $context, Data $helper, array $data = [])
{
$this->helper = $helper;
parent::__construct($context, $data);
}
}
193 changes: 193 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

namespace Saaspass\Login\Helper;

use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Store\Model\ScopeInterface;
use \Magento\Framework\App\Helper\Context;
use \Magento\Framework\Module\Dir\Reader;
use \Magento\Framework\UrlInterface;
use Magento\Backend\Model\Auth\Session;
use \Magento\Security\Model\AdminSessionsManager;
use \Magento\Framework\App\Config\ScopeConfigInterface;
use Saaspass\Login\Helper\Settings;
use \Magento\Framework\HTTP\Client\Curl;
use Magento\Framework\HTTP\ZendClient;
use Magento\Framework\HTTP\ZendClientFactory;
use Magento\Framework\Session\SessionManager;

class Data extends AbstractHelper
{
/**
* @var \Magento\Framework\HTTP\Client\Curl
*/
private $curl;
/**
* @var UrlInterface
*/
private $urlInterface;
private $adminSessionsManager;
private $settings;
private $session;
private $request;
/**
* @var Reader
*/
private $aToken;
private $cToken;
private $reader;
private $mydomain = "https://www.saaspass.com/sd/rest/applications";
private $mytestdomain = "https://www.saaspass.com/sd/rest/magento";
private $apiKey;
private $apiPassword;
private $sess;
private $httpClientFactory;
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
Curl $curl,
SessionManager $session,
ZendClient $request,
ZendClientFactory $httpClientFactory,
\Saaspass\Login\Helper\Settings $settings,
Context $context,
Reader $reader
) {
$this->settings = $settings;
$this->_scopeConfig = $scopeConfig;
$this->reader = $reader;
$this->httpClientFactory = $httpClientFactory;
parent::__construct($context);
$this->curl = $curl;
$this->session = $session;
$this->request = $request;
$this->apiKey = $this->settings->getKey();
$this->apiPassword = $this->settings->getPass();
$this->aToken = $this->getApplicationToken();
}
public function getApiKey()
{
return $this->settings->getKey();
}
public function getApiPass()
{
return $this->settings->getPass();
}
public function setApiKey()
{
$this->apiKey = $this->settings->getKey();
}
public function setApiPass()
{
$this->apiPassword = $this->settings->getPass();
}
public function getBarcode()
{
$url = $this->mydomain."/". $this->settings->getKey() ."/barcodes?type=IL&session="
. $this->getSession(). "&token=". $this->getApplicationToken();
return $this->requestBarcode($url);
}
public function getSession()
{
return $this->session->getSessionId();
}
public function getApplicationToken()
{
$url = $this->mydomain."/". $this->settings->getKey() . "/tokens?password="
. $this->settings->getPass();
return $this->getToken($url);
}
public function checkOtp($user, $otp)
{
$url = $this->mytestdomain ."/". $this->settings->getKey() . "/otpchecks?username="
. $user . "&otp=" . $otp . "&token=" . $this->aToken;
return $this->getHttpCode($url);
}
public function pushLogin($username, $ses)
{
$url = $this->mytestdomain ."/". $this->settings->getKey()
."/push?username=$username&session=$ses&token=$this->aToken";
return $this->getHttpCode($url);
}
public function isNativeDisabled()
{
return $this->settings->getIsDisabled();
}
public function getToken($url)
{
try {
$client = $this->httpClientFactory->create();
$client->setUri($url);
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
$jsonresponse = $client->request(\Zend_Http_Client::GET)->getBody();
if (!$jsonresponse == null) {
$jdec = json_decode($jsonresponse);
if (isset($jdec->{'token'})) {
return $jdec->{'token'};
} else {
return $jsonresponse;
}
} else {
return null;
}
} catch (\Exception $e) {
return $e->getMessage();
}
}
public function checkTrackerId($tracker, $user)
{
$url = $this->mydomain."/". $this->apiKey."/trackers/$tracker?token=".$this->aToken."&account=$user";
return $this->getHttpCode($url);
}
public function getHttpCode($url)
{
$client = $this->httpClientFactory->create();
$client->setUri($url);
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
$httpcode = $client->request(\Zend_Http_Client::GET)->getStatus();
return $httpcode;
}
public function requestBarcode($url)
{
$client = $this->httpClientFactory->create();
$client->setUri($url);
$data = json_decode($client->request(\Zend_Http_Client::GET)->getBody(), true);
if (isset($data['name'])) {
if ($data['name'] == 'EXPIRED_TOKEN') {
$this->aToken = $this->getApplicationToken();
$this->cToken = $this->getCompanyToken();
requestBarcode($url);
}
}
return $data['barcodeimage'];
}
public function checkResponse($token_url)
{
try {
$client = $this->httpClientFactory->create();
$client->setUri($token_url);
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
$httpcode = $client->request(\Zend_Http_Client::GET)->getStatus();
if ($httpcode == '200') {
return $httpcode;
} else {
return null;
}
} catch (\Exception $e) {
return $e->getMessage();
}
}
public function checkForResponse($url)
{
$client = $this->httpClientFactory->create();
$client->setUri($url);
$data = json_decode($client->request(\Zend_Http_Client::GET)->getBody(), true);
if (isset($data['name'])) {
if ($data['name'] == 'EXPIRED_TOKEN') {
$this->aToken = $this->getApplicationToken();
$this->cToken = $this->getCompanyToken();
requestBarcode($url);
}
}
return $data;
}
}
67 changes: 67 additions & 0 deletions Helper/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Saaspass\Login\Helper;

use \Magento\Framework\App\Config\ScopeConfigInterface;

class Settings
{
private $apiKey;
private $apiPassword;
private $is_disabled;
private $scopeConfig;
private $dummy;
/**
* Settings constructor.
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}
public function setDefault()
{
$this->apiKey = $this->scopeConfig->getValue(
'saaspass/general/saaspass_application_key',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$this->apiPassword = $this->scopeConfig->getValue(
'saaspass/general/saaspass_application_pass',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$this->is_disabled = $this->scopeConfig->getValue(
'saaspass/general/native_disable',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
/**
* @return application key
*/
public function getKey()
{
return $this->apiKey = $this->scopeConfig->getValue(
'saaspass/general/saaspass_application_key',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
/**
* @return application password
*/
public function getPass()
{
return $this->apiPassword = $this->scopeConfig->getValue(
'saaspass/general/saaspass_application_pass',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
/**
* @return if native login is enabled or disabled
*/
public function getIsDisabled()
{
return $this->is_disabled = $this->scopeConfig->getValue(
'saaspass/general/native_disable',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
52 changes: 52 additions & 0 deletions Model/LoginManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Saaspass\Login\Model;

use Saaspass\Login\Api\LoginManagementInterface as ApiInterface;
use Saaspass\Login\Helper\Data;
use Magento\Framework\Filesystem\Driver\File;

class LoginManagement implements ApiInterface
{
private $helper;
private $driver;
/**
* LoginManagement constructor.
* @param File $driver
* @param Data $data
*/
public function __construct(File $driver, Data $data)
{
$this->driver = $driver;
$this->helper = $data;
}
/**
* @param string $session
* @return string
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function authenticated($session)
{
if ($this->driver->isExists($session.'.txt')) {
return "ready";
} else {
return 'not_ready';
}
}
/**
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function authenticate()
{
$headers = apache_request_headers();
if (array_key_exists('username', $headers) && array_key_exists('tracker', $headers)
&& array_key_exists('session', $headers)) {
$username=$headers['username'];
$session=$headers['session'];
$trackerID=$headers['tracker'];
if ($this->helper->checkTrackerId($trackerID, $username) == 200) {
$this->driver->filePutContents($session . '.txt', $username);
}
}
}
}
Loading

0 comments on commit b943a1b

Please sign in to comment.