Skip to content

Commit

Permalink
GH #2580: Remove use of Auth0 for NDLA audio and image API. Browsers …
Browse files Browse the repository at this point in the history
…now access more of the API directly
  • Loading branch information
chrieinv committed Oct 24, 2023
1 parent ad4c05e commit bf7b01a
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,8 @@ public function overrideAdapterSettings()
'h5p.video.deleteVideoSourceAfterConvertToStream',
'h5p.video.pingDelay',
'h5p.image.authDomain',
'h5p.image.key',
'h5p.image.secret',
'h5p.image.audience',
'h5p.image.url',
'h5p.audio.url',
'h5p.H5P_DragQuestion',
'h5p.H5P_Dialogcards',
'h5p.isHubEnabled',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NDLAAudioBrowser implements H5PAudioInterface, H5PExternalProviderInterfac
{
public const FIND_AUDIOS_URL = '/audio-api/v1/audio';
public const GET_AUDIO_URL = '/audio-api/v1/audio/%s';
public const GET_AUDIO_DETAILS_CLIENT = '/audio-api/v1/audio';

public function __construct(
private readonly Client $client,
Expand Down Expand Up @@ -115,4 +116,14 @@ public function getType(): string
{
return "audio";
}

public static function getClientDetailsUrl(): ?string
{
$url = config('h5p.audio.url') ?: config('h5p.image.url');
if ($url !== null) {
return $url . self::GET_AUDIO_DETAILS_CLIENT;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace App\Libraries\H5P;

use App\Libraries\H5P\Interfaces\H5PAdapterInterface;
use App\Libraries\H5P\Interfaces\H5PAudioInterface;
use App\Libraries\H5P\Interfaces\H5PImageAdapterInterface;

class H5PCreateConfig extends H5PConfigAbstract
{
Expand Down Expand Up @@ -55,5 +57,14 @@ protected function addInheritorConfig(): void
$this->editorConfig['ajaxPath'] = sprintf("/ajax?redirectToken=%s&h5p_id=&action=", $this->redirectToken);

$this->config['editor'] = (object) $this->editorConfig;

$imageBrowser = app(H5PImageAdapterInterface::class);
if ($imageBrowser) {
$this->config['imageBrowserDetailsUrl'] = $imageBrowser::getClientDetailsUrl();
}
$audioBrowser = app(H5PAudioInterface::class);
if ($audioBrowser) {
$this->config['audioBrowserDetailsUrl'] = $audioBrowser::getClientDetailsUrl();
}
}
}
11 changes: 11 additions & 0 deletions sourcecode/apis/contentauthor/app/Libraries/H5P/H5PEditConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace App\Libraries\H5P;

use App\Libraries\H5P\Interfaces\H5PAdapterInterface;
use App\Libraries\H5P\Interfaces\H5PAudioInterface;
use App\Libraries\H5P\Interfaces\H5PImageAdapterInterface;
use App\Traits\H5PBehaviorSettings;

class H5PEditConfig extends H5PConfigAbstract
Expand Down Expand Up @@ -65,5 +67,14 @@ protected function addInheritorConfig(): void
}

$this->config['editor'] = (object) $this->editorConfig;

$imageBrowser = app(H5PImageAdapterInterface::class);
if ($imageBrowser) {
$this->config['imageBrowserDetailsUrl'] = $imageBrowser::getClientDetailsUrl();
}
$audioBrowser = app(H5PAudioInterface::class);
if ($audioBrowser) {
$this->config['audioBrowserDetailsUrl'] = $audioBrowser::getClientDetailsUrl();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NDLAContentBrowser implements H5PImageAdapterInterface, H5PExternalProvide
public const GET_IMAGE_URL = '/image-api/v3/images/%s';
public const GET_IMAGE_ID = '/image-api/raw/id/%s';
public const GET_IMAGE_NAME = '/image-api/raw/%s';
public const GET_IMAGE_DETAILS_CLIENT = '/image-api/v3/images';

public function __construct(
private readonly Client $client,
Expand Down Expand Up @@ -164,4 +165,14 @@ public function alterImageProperties($imageProperties, bool $includeWidthQuery):
}
return $imageProperties;
}

public static function getClientDetailsUrl(): ?string
{
$url = config('h5p.image.url');
if ($url !== null) {
return $url . self::GET_IMAGE_DETAILS_CLIENT;
}

return null;
}
}
34 changes: 6 additions & 28 deletions sourcecode/apis/contentauthor/app/Providers/H5PServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,9 @@ public function register()

$this->app->when(NDLAContentBrowser::class)
->needs(Client::class)
->give(fn () => Auth0Client::getClient(OauthSetup::create([
'key' => config('h5p.image.key'),
'secret' => config('h5p.image.secret'),
'authUrl' => config('h5p.image.authDomain'),
'coreUrl' => config('h5p.image.url'),
'audience' => config('h5p.image.audience'),
])));
->give(fn () => new Client([
'base_uri' => config('h5p.image.url'),
]));

$this->app->bind(H5PImageAdapterInterface::class, function () {
$adapter = $this->app->make(H5PAdapterInterface::class);
Expand All @@ -122,27 +118,9 @@ public function register()

$this->app->when(NDLAAudioBrowser::class)
->needs(Client::class)
->give(function () {
if (!is_null(config('h5p.audio.url'))) {
$authSetup = OauthSetup::create([
'key' => config('h5p.audio.key'),
'secret' => config('h5p.audio.secret'),
'authUrl' => config('h5p.audio.authDomain'),
'coreUrl' => config('h5p.audio.url'),
'audience' => config('h5p.audio.audience'),
]);
} else {
$authSetup = OauthSetup::create([
'key' => config('h5p.image.key'),
'secret' => config('h5p.image.secret'),
'authUrl' => config('h5p.image.authDomain'),
'coreUrl' => config('h5p.image.url'),
'audience' => config('h5p.image.audience'),
]);
}

return Auth0Client::getClient($authSetup);
});
->give(fn () => new Client([
'base_uri' => config('h5p.audio.url') ?: config('h5p.image.url'),
]));

$this->app->bind(H5PAudioInterface::class, function () {
$adapter = $this->app->make(H5PAdapterInterface::class);
Expand Down
8 changes: 0 additions & 8 deletions sourcecode/apis/contentauthor/config/h5p.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,12 @@
'overrideDisableSetting' => env("H5P_OVERRIDE_DISABLE_SETTING", false),
'h5pAdapter' => env('H5P_ADAPTER', 'cerpus'),
'image' => [
'authDomain' => env("H5P_IMAGE_AUTH_DOMAIN"),
'key' => env("H5P_IMAGE_AUTH_KEY"),
'secret' => env("H5P_IMAGE_AUTH_SECRET"),
'audience' => env("H5P_IMAGE_AUDIENCE"),
'url' => env("H5P_IMAGE_URL"),
'properties' => [
'width' => env("H5P_IMAGE_PROPERTIES_WIDTH", 2500),
]
],
'audio' => [
'authDomain' => env("H5P_AUDIO_AUTH_DOMAIN"),
'key' => env("H5P_AUDIO_AUTH_KEY"),
'secret' => env("H5P_AUDIO_AUTH_SECRET"),
'audience' => env("H5P_AUDIO_AUDIENCE"),
'url' => env("H5P_AUDIO_URL"),
],
'saveFrequency' => env('H5P_SAVE_FREQUENCY', 15),
Expand Down
7 changes: 3 additions & 4 deletions sourcecode/apis/contentauthor/config/ndla-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
'overrideDisableSetting' => env("NDLA_H5P_OVERRIDE_DISABLE_SETTING", false),
'h5pAdapter' => 'ndla',
'image' => [
'authDomain' => env("NDLA_H5P_IMAGE_AUTH_DOMAIN"),
'key' => env("NDLA_H5P_IMAGE_AUTH_KEY"),
'secret' => env("NDLA_H5P_IMAGE_AUTH_SECRET"),
'audience' => env("NDLA_H5P_IMAGE_AUDIENCE"),
'url' => env("NDLA_H5P_IMAGE_URL"),
],
'audio' => [
'url' => env("NDLA_H5P_AUDIO_URL"),
],
'video' => [
'enable' => env("NDLA_H5P_VIDEO_STREAM_ENABLED", true),
'url' => env("NDLA_H5P_VIDEO_URL"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ class AudioBrowser extends ContentBrowserBase {
onToggleCallback: cb => this.toggleContentBrowser = cb,
locale: H5PIntegration.locale,
getCurrentLanguage: () => H5PEditor.defaultLanguage,
apiDetailsUrl: H5PIntegration?.audioBrowserDetailsUrl,
});
}
}
Expand Down Expand Up @@ -471,6 +472,7 @@ class ImageBrowser extends ContentBrowserBase {
onToggleCallback: cb => this.toggleContentBrowser = cb,
locale: H5PIntegration.locale,
getCurrentLanguage: () => H5PEditor.defaultLanguage,
apiDetailsUrl: H5PIntegration?.imageBrowserDetailsUrl,
});
this.copyrightHandler.handleDisplayCopyrightButton();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AudioBrowserContainer extends Component {
locale: PropTypes.string,
onToggle: PropTypes.func,
getCurrentLanguage: PropTypes.func,
apiDetailsUrl: PropTypes.string.isRequired,
};

static defaultProps = {
Expand Down Expand Up @@ -45,7 +46,7 @@ class AudioBrowserContainer extends Component {
}

handleFetchAudioDetails(audioId) {
return Axios.get(this.props.searchUrl + '/' + audioId, {
return Axios.get(this.props.apiDetailsUrl + '/' + audioId, {
params: {
language: this.props.getCurrentLanguage(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ImageBrowserContainer extends Component {
searchPlaceholder: PropTypes.string,
onToggle: PropTypes.func,
getCurrentLanguage: PropTypes.func,
apiDetailsUrl: PropTypes.string.isRequired,
};

static defaultProps = {
Expand Down Expand Up @@ -46,7 +47,7 @@ class ImageBrowserContainer extends Component {
}

handleFetchImageDetails(imageId) {
return Axios.get( this.props.searchUrl + '/' + imageId, {
return Axios.get( this.props.apiDetailsUrl + '/' + imageId, {
params: {
language: this.props.getCurrentLanguage(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function initImageBrowser(element, settings) {
locale,
onToggleCallback,
getCurrentLanguage,
apiDetailsUrl,
} = settings;

const i18nData = (locale !== null ? addLanguage(locale) : i18nDefault);
Expand All @@ -23,6 +24,7 @@ function initImageBrowser(element, settings) {
locale={locale}
onToggle={onToggleCallback}
getCurrentLanguage={getCurrentLanguage}
apiDetailsUrl={apiDetailsUrl}
/>
</IntlProvider>,
element
Expand Down Expand Up @@ -56,6 +58,7 @@ function initAudioBrowser(element, settings) {
locale,
onToggleCallback,
getCurrentLanguage,
apiDetailsUrl,
} = settings;

const i18nData = (locale !== null ? addLanguage(locale) : i18nDefault);
Expand All @@ -67,6 +70,7 @@ function initAudioBrowser(element, settings) {
locale={locale}
onToggle={onToggleCallback}
getCurrentLanguage={getCurrentLanguage}
apiDetailsUrl={apiDetailsUrl}
/>
</IntlProvider>,
element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Libraries\H5P\H5PConfigAbstract;
use App\Libraries\H5P\H5PCreateConfig;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
use Tests\TestCase;

Expand All @@ -15,6 +16,9 @@ class H5PCreateConfigTest extends TestCase
/** @dataProvider provider_adapterMode */
public function test_getConfig(string $adapterMode): void
{
Config::set('ndla-mode.h5p.audio.url', 'https://audio.url');
Config::set('ndla-mode.h5p.image.url', 'https://ndla-image.url');

Session::put('adapterMode', $adapterMode);

$config = app(H5PCreateConfig::class);
Expand Down Expand Up @@ -84,10 +88,14 @@ public function test_getConfig(string $adapterMode): void
$this->assertContains('/js/h5p/ndlah5p-youtube.js?ver=' . H5PConfigAbstract::CACHE_BUSTER_STRING, $data->editor->assets->js);

$this->assertSame('https://www.wiris.net/client/plugins/ckeditor/plugin.js', $data->editor->wirisPath);
$this->assertSame('https://audio.url/audio-api/v1/audio', $data->audioBrowserDetailsUrl);
$this->assertSame('https://ndla-image.url/image-api/v3/images', $data->imageBrowserDetailsUrl);
} elseif ($adapterMode === 'cerpus') {
$this->assertContains('/js/videos/streamps.js?ver=' . H5PConfigAbstract::CACHE_BUSTER_STRING, $data->editor->assets->js);
$this->assertContains('/js/videos/brightcove.js?ver=' . H5PConfigAbstract::CACHE_BUSTER_STRING, $data->editor->assets->js);
$this->assertObjectNotHasAttribute('wirisPath', $data->editor);
$this->assertObjectNotHasAttribute('audioBrowserDetailsUrl', $data);
$this->assertObjectNotHasAttribute('imageBrowserDetailsUrl', $data);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Libraries\H5P\H5PConfigAbstract;
use App\Libraries\H5P\H5PEditConfig;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
use Tests\TestCase;

Expand All @@ -18,6 +19,9 @@ class H5PEditConfigTest extends TestCase
/** @dataProvider provider_adapterMode */
public function test_getConfig(string $adapterMode): void
{
Config::set('ndla-mode.h5p.audio.url', 'https://audio.url');
Config::set('ndla-mode.h5p.image.url', 'https://ndla-image.url');

Session::put('adapterMode', $adapterMode);

$config = app(H5PEditConfig::class);
Expand Down Expand Up @@ -86,10 +90,14 @@ public function test_getConfig(string $adapterMode): void
$this->assertContains('/js/h5p/ndlah5p-youtube.js?ver=' . H5PConfigAbstract::CACHE_BUSTER_STRING, $data->editor->assets->js);

$this->assertSame('https://www.wiris.net/client/plugins/ckeditor/plugin.js', $data->editor->wirisPath);
$this->assertSame('https://audio.url/audio-api/v1/audio', $data->audioBrowserDetailsUrl);
$this->assertSame('https://ndla-image.url/image-api/v3/images', $data->imageBrowserDetailsUrl);
} elseif ($adapterMode === 'cerpus') {
$this->assertContains('/js/videos/streamps.js?ver=' . H5PConfigAbstract::CACHE_BUSTER_STRING, $data->editor->assets->js);
$this->assertContains('/js/videos/brightcove.js?ver=' . H5PConfigAbstract::CACHE_BUSTER_STRING, $data->editor->assets->js);
$this->assertObjectNotHasAttribute('wirisPath', $data->editor);
$this->assertObjectNotHasAttribute('audioBrowserDetailsUrl', $data);
$this->assertObjectNotHasAttribute('imageBrowserDetailsUrl', $data);
}
}

Expand Down

0 comments on commit bf7b01a

Please sign in to comment.