Skip to content

Commit

Permalink
Merge pull request #19 from amonsour/master
Browse files Browse the repository at this point in the history
Implement OAuth
  • Loading branch information
amonsour authored Nov 22, 2024
2 parents 1fac652 + 0b6787a commit c7c7afe
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 107 deletions.
60 changes: 0 additions & 60 deletions php/generate_jwt.php

This file was deleted.

50 changes: 31 additions & 19 deletions php/generate_token.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
<?php

function generate_token( $apiKey, $secretKey, $jwt )
function generate_token($client_id, $client_secret)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://ims-na1.adobelogin.com/ims/exchange/jwt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$url = 'https://ims-na1.adobelogin.com/ims/token/v3';

curl_setopt($ch, CURLOPT_POSTFIELDS, "client_id=".$apiKey."&client_secret=".$secretKey."&jwt_token=".$jwt);
$postFields = http_build_query([
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'client_credentials',
'scope' => 'openid,AdobeID,additional_info.projectedProductContext',
]);

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postFields,
CURLOPT_HTTPHEADER => [
"Content-Type: application/x-www-form-urlencoded",
],
]);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);

if ($result === false) {
error_log('cURL Error: ' . curl_error($ch));
curl_close($ch);
return false;
}
curl_close($ch);

$token = json_decode($result, true);
$token = $token["access_token"];
$response = json_decode($result, true);

return $token;
}
curl_close($ch);

?>
if (isset($response['access_token'])) {
return $response['access_token'];
}

return false;
}
16 changes: 1 addition & 15 deletions php/getToken.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
<?php

require_once('generate_jwt.php');
require_once('generate_token.php');
require_once('api_get.php');
require_once('api_post.php');
$config = include('config.php');
$randIndex = array_rand($config, 1);

$fp = fopen("../keys/secret.pem", "r");
$priv_key = fread($fp, 8192);
fclose($fp);

$jwt = generate_jwt(
$priv_key,
$config[$randIndex]['ADOBE_ORG_ID'],
$config[$randIndex]['ADOBE_TECH_ID'],
'https://ims-na1.adobelogin.com/c/' .
$config[$randIndex]['ADOBE_API_KEY']
);

$token = generate_token(
$config[$randIndex]['ADOBE_API_KEY'],
$config[$randIndex]['ADOBE_API_SECRET'],
$jwt
$config[$randIndex]['ADOBE_API_SECRET']
);

$_SESSION["token"] = $token;
Expand Down
35 changes: 22 additions & 13 deletions php/process-new.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,32 @@ function strposa($haystack, $needle, $offset = 0)

$bUrl = substr('https://' . $origUrl, 0, 255);

$html = file_get_html('https://' . $origUrl);
try {
$html = file_get_html('https://' . $origUrl);

foreach ($html->find('form') as $e) {
if ($e->action && $e->name == 'cse-search-box') {
$searchURL = $e->action;
break;
}
} catch (Exception $e) {
$html = null;
}

// Find the meta tag with name="dcterms.title" and get its content attribute
foreach ($html->find('meta[name=dcterms.title]') as $e) {
$titlePage = $e->content;
break;
}
$searchURL = null;
$titlePage = null;

$titlePage = trim($titlePage);
$titlePage = html_entity_decode($titlePage);
if ($html) {
foreach ($html->find('form') as $e) {
if ($e->action && $e->name == 'cse-search-box') {
$searchURL = $e->action;
break;
}
}

foreach ($html->find('meta[name=dcterms.title]') as $e) {
$titlePage = $e->content;
break;
}

$titlePage = trim($titlePage);
$titlePage = html_entity_decode($titlePage);
}

$oSearchURL = $searchURL;

Expand Down

0 comments on commit c7c7afe

Please sign in to comment.