Last active
March 25, 2022 09:16
-
-
Save jfqd/747af127c35e954474afd269df304b63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'shiparound_api/rest' | |
require 'rest-client' | |
require 'openssl' | |
require 'base64' | |
require 'json' | |
require 'uri' | |
# auth_code, error = ::ShiparoundApi::AuthCode.get( | |
# password: "password", | |
# api_key: "api_key", | |
# sandbox: false | |
# ) | |
module ShiparoundApi | |
class AuthCode | |
ENCRYPTION_METHOD = 'AES-128-CTR'; | |
ENCRYPTION_KEY = '92~GkkXV44b}*>z}rRf^' | |
def self.get( | |
password: "", | |
api_key: "", | |
sandbox: true | |
) | |
json = create_json(password, api_key) | |
uri = ::ShiparoundApi::Rest.uri_call(sandbox,true) | |
uri << create_uri_parameters(json) | |
response = RestClient.get uri | |
return result_of response | |
end | |
private | |
def self.result_of(response) | |
error = ""; result = nil | |
if response.code == 200 | |
hash = JSON.parse(response.body) | |
if hash["success"] == true | |
result = decrypt(hash["AUTH_CODE_encrypted"]) | |
else | |
error = hash["error"].inspect | |
end | |
else | |
error = "ERROR: #{response.body} (#{response.code})" | |
end | |
[result, error] | |
end | |
def self.create_json(password,api_key) | |
%Q[{"password":#{password.to_json},"SHIPAPI_AUTH_ENCRYPTION_KEY":#{api_key.to_json}}] | |
end | |
def self.create_uri_parameters(json) | |
"?info=#{encrypt(json)}&"+ | |
"encryption_method=#{ENCRYPTION_METHOD}&"+ | |
"encryption_options=1" | |
end | |
def self.encrypt(str) | |
cipher = OpenSSL::Cipher.new(ENCRYPTION_METHOD) | |
cipher.encrypt | |
cipher.key = ENCRYPTION_KEY[0..15] | |
data = cipher.update(str) + cipher.final | |
CGI.escape( Base64.encode64(data).gsub("\n","") ) | |
end | |
def self.decrypt(str) | |
data = Base64.decode64(str) | |
cipher = OpenSSL::Cipher.new(ENCRYPTION_METHOD) | |
cipher.decrypt | |
cipher.key = ENCRYPTION_KEY[0..15] | |
cipher.update(data) + cipher.final | |
end | |
end # class AuthCode | |
end # module ShiparoundApi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// | |
// /usr/bin/php getAuthCode.php 'password' 'api-key' | |
// | |
$baseURL = "https://shiparound.de/shipAPI/getAuthCode.bridge.php"; | |
$ENCRYPTION_METHOD = 'AES-128-CTR'; | |
$ENCRYPTION_KEY = '92~GkkXV44b}*>z}rRf^'; | |
$ENCRYPTION_OPTIONS = OPENSSL_RAW_DATA; | |
$password = $argv[1]; | |
$SHIPAPI_AUTH_ENCRYPTION_KEY = $argv[2]; | |
$info = json_encode(array('password' => $password, 'SHIPAPI_AUTH_ENCRYPTION_KEY' => $SHIPAPI_AUTH_ENCRYPTION_KEY)); | |
$info = base64_encode(@openssl_encrypt($info, $ENCRYPTION_METHOD, $ENCRYPTION_KEY, $ENCRYPTION_OPTIONS)); | |
$url = $baseURL . '?info=' . urlencode($info); | |
$url .= '&encryption_method=' . $ENCRYPTION_METHOD; | |
$url .= '&encryption_options=' . $ENCRYPTION_OPTIONS; | |
$request = file_get_contents($url); | |
$arr = json_decode($request, true); | |
$encrypted_auth = base64_decode($arr["AUTH_CODE_encrypted"]); | |
$decrypted_auth = openssl_decrypt($encrypted_auth, $ENCRYPTION_METHOD, $ENCRYPTION_KEY, $ENCRYPTION_OPTIONS); | |
echo $decrypted_auth; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment