diff --git a/.gitignore b/.gitignore index 71bb129..1424e90 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ config.ini tags - +vendor/ diff --git a/composer.json b/composer.json index 00a9faa..7061f8d 100644 --- a/composer.json +++ b/composer.json @@ -1,4 +1,17 @@ { "name": "brunofontes/memsource-api", - "version": "1.0.0" + "version": "1.0.0", + "description": "A personal memsource api to better understand how it works", + "autoload": { + "psr-4": { + "BrunoFontes\\": "src/", + "BrunoFontes\\Memsource\\": "src/" + } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/brunofontes/Memsource-API" + } + ] } diff --git a/src/Memsource.php b/src/Memsource.php new file mode 100644 index 0000000..b7ac081 --- /dev/null +++ b/src/Memsource.php @@ -0,0 +1,79 @@ + + * @link https://github.com/brunofontes + */ +namespace BrunoFontes; + +class Memsource +{ + private $_oauth; + protected $base_url; + protected $token; + + public function __construct(string $memsourceBaseUrl = 'https://cloud.memsource.com/web') + { + $this->base_url = $memsourceBaseUrl; + } + + public function oauth() + { + return $this->_oauth ?? $this->_oauth = new \BrunoFontes\Memsource\oauth(); + } + + public function get(string $url) + { + $response = $this->curl($url); + return json_decode($response, true); + } + + protected function apiDownloadFile(string $url, array $postFields, string $filename) + { + $file = fopen($filename, 'w+'); + $extraSetopt = [ + CURLOPT_FILE => $file, + CURLOPT_FOLLOWLOCATION => true, + ]; + return $this->jsonPost($url, $postFields, $extraSetopt); + } + + protected function jsonPost(string $url, array $postFields, array $extraSetopt = [], array $extraHeader = []) + { + $extraHeader = (['Content-type: application/json'] + $extraHeader); + $postFields = json_encode($postFields); + $response = $this->post($url, $postFields, [], $extraHeader); + return json_decode($response, true); + } + + protected function post(string $url, string $postFields, array $extraSetopt = [], array $extraHeader = []) + { + $extraSetopt = ( + $extraSetopt + + [ + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $postFields + ] + ); + $response = $this->curl($url, $extraSetopt, $extraHeader); + return json_decode($response, true); + } + + protected function curl(string $url, array $curl_extra_setopt=[], array $extraHeader=[]) + { + $header = $this->token ? ["Authorization: Bearer {$this->token}"] : []; + $curl_setopt = [ + CURLOPT_URL => $this->base_url . $url, + CURLOPT_HTTPHEADER => ($header + $extraHeader), + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_TIMEOUT => 500, + CURLOPT_FOLLOWLOCATION => true, + ]; + $curl = curl_init(); + curl_setopt_array($curl, ($curl_setopt + $curl_extra_setopt)); + $response = curl_exec($curl); + curl_close($curl); + return $response; + } +} \ No newline at end of file diff --git a/src/auth.php b/src/auth.php new file mode 100644 index 0000000..cf7ea46 --- /dev/null +++ b/src/auth.php @@ -0,0 +1,34 @@ + + * @link https://github.com/brunofontes + */ +namespace BrunoFontes\Memsource; + +class Auth +{ + protected $base_url; + private $_client_id; + private $_client_secret; + + function __construct(string $base_url) + { + $this->base_url = $base_url; + } + + /** + * Directly login and get a token valid for 24h + * + * @param string $username Memsource username + * @param string $password Memsource password + * + * @return string Authorization code valid for 24h only + */ + function login(string $username, string $password) + { + $authorize_url = $this->base_url . '/oauth/authorize'; + return $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid'; + } +} diff --git a/src/oauth.php b/src/oauth.php new file mode 100644 index 0000000..9be48a6 --- /dev/null +++ b/src/oauth.php @@ -0,0 +1,41 @@ + + * @link https://github.com/brunofontes + */ +namespace BrunoFontes\Memsource; + +class Oauth extends \BrunoFontes\Memsource +{ + private $_url = '/oauth'; + + /** + * Get the URL to generate the Authorization Code from Memsource + * + * @param string $client_id Memsource client ID + * @param string $client_secret Memsource client secret + * @param string $callback_uri URL that Memsource will redirect to + * + * @return string the authorization code + */ + public function getAuthorizationCodeUrl(string $client_id, string $callback_uri) + { + $authorize_url = $this->_url . '/authorize'; + return $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid'; + } + + public function getAccessToken(string $authorization_code, string $client_id, string $client_secret, string $callback_uri) + { + $token_url = $this->_url . '/token'; + $authorization = base64_encode("$client_id:$client_secret"); + $header = ["Authorization: Basic {$authorization}", 'Content-Type: application/x-www-form-urlencoded']; + $content = "grant_type=authorization_code&code=$authorization_code&redirect_uri=$callback_uri"; + $response = $this->post($token_url, $content, [], $header); + if ($respose['error']) { + throw new Exception("Error getting access token", 1); + } + return $response['access_token']; + } +}