Skip to content

Commit

Permalink
Add starategy mode for jaas mode jitsi (#9)
Browse files Browse the repository at this point in the history
* Add starategy mode for jaas mode jitsi

* Add variable to TestCase

* Add package_status in testcase

* Correct bug

* Add try catch

* Add try catch

* Add debug

* Add debug

* Add debug

* Repair tests

* Add priority for mode video conference

* Remove secret key from config

* Add ServiceProvider to testCase

* REmove on provider

* Add missing provider totestbench

* Add provider to testcase file

* Remove courses from jitsi composer

Co-authored-by: Hubert Krzysztofiak <[email protected]>
  • Loading branch information
HerbertIV and Hubert Krzysztofiak authored Nov 8, 2022
1 parent c122367 commit ac18943
Show file tree
Hide file tree
Showing 20 changed files with 511 additions and 74 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": ">=5.0",
"escolalms/courses": "^0"
"orchestra/testbench": ">=5.0"
},
"license": "MIT",
"authors": [
Expand Down
10 changes: 10 additions & 0 deletions config/jaas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
return [

'jaas_host' => env('JAAS_HOST', 'https://8x8.vc/'),
'aud' => env('JAAS_AUD', 'jitsi'),
'iss' => env('JAAS_ISS', 'chat'),
'sub' => env('JAAS_SUB', ''),
'kid' => env('JAAS_KEY_ID', ''),
'private_key' => env('JAAS_PRIVATE_KEY', '')
];
4 changes: 2 additions & 2 deletions config/jitsi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

return [

'host' => env('JITSI_HOST', 'meet-stage.escolalms.com'),
'jitsi_host' => env('JITSI_HOST', 'meet-stage.escolalms.com'),
'app_id' => env('JITSI_APP_ID', 'meet-id'),
'secret' => env('JITSI_APP_SECRET', 'ZKGfn5kYsv47avM4'),
'secret' => env('JITSI_APP_SECRET', 'Test'),

'package_status' => PackageStatusEnum::ENABLED,

Expand Down
3 changes: 2 additions & 1 deletion env/mysql/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ providers:
- EscolaLms\Settings\EscolaLmsSettingsServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Categories\EscolaLmsCategoriesServiceProvider
- EscolaLms\Categories\EscolaLmsCategoriesServiceProvider
- EscolaLms\ModelFields\ModelFieldsServiceProvider
3 changes: 2 additions & 1 deletion env/postgres/testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ providers:
- EscolaLms\Settings\EscolaLmsSettingsServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Auth\EscolaLmsAuthServiceProvider
- EscolaLms\Categories\EscolaLmsCategoriesServiceProvider
- EscolaLms\Categories\EscolaLmsCategoriesServiceProvider
- EscolaLms\ModelFields\ModelFieldsServiceProvider
2 changes: 2 additions & 0 deletions src/Enum/JitsiEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ class JitsiEnum extends Enum
'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
'š' => 's', 'ū' => 'u', 'ž' => 'z',
];

public const DEFAULT_CONFIG = 'jitsi';
}
7 changes: 7 additions & 0 deletions src/EscolaLmsJitsiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace EscolaLms\Jitsi;

use EscolaLms\Jitsi\Providers\SettingsServiceProvider;
use EscolaLms\Jitsi\Services\Contracts\JaasServiceContract;
use EscolaLms\Jitsi\Services\JaasService;
use Illuminate\Support\ServiceProvider;
use EscolaLms\Jitsi\Services\Contracts\JitsiServiceContract;
use EscolaLms\Jitsi\Services\JitsiService;
Expand All @@ -15,6 +17,7 @@ class EscolaLmsJitsiServiceProvider extends ServiceProvider
{
public $singletons = [
JitsiServiceContract::class => JitsiService::class,
JaasServiceContract::class => JaasService::class,
];

/**
Expand All @@ -32,6 +35,10 @@ public function register()
__DIR__ . '/../config/jitsi.php',
'jitsi'
);
$this->mergeConfigFrom(
__DIR__ . '/../config/jaas.php',
'jaas'
);

$this->app->register(SettingsServiceProvider::class);
}
Expand Down
51 changes: 51 additions & 0 deletions src/Helpers/StrategyHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace EscolaLms\Jitsi\Helpers;

class StrategyHelper
{
private string $namespace;

public function __construct(string $baseStrategyName)
{
$this->setNamespace($baseStrategyName);
}

/**
* This method used strategy pattern and execute method given in the parameters
* Strategy dir it must contain minimum to file: BaseStrategy contain in pattern {{parentDir}}Strategy
* in localization ?/Strategies/{{parentDir}} and strategy class in the same localization
*
* @param string $className
* @param string $baseStrategyName
* @param string $method
* @param ...$params
* @return mixed|null
*/
public static function useStrategyPattern(
string $className,
string $baseStrategyName,
string $method,
...$params
) {
$strategyHelper = new StrategyHelper($baseStrategyName);
$class = $strategyHelper->namespace . '\\' . $className;
$baseStrategyClass = $strategyHelper->namespace . '\\' . $baseStrategyName;
if (
class_exists($class) &&
class_exists($baseStrategyClass) &&
method_exists($baseStrategyClass, $method)
) {
return (new $baseStrategyClass(
new $class()
))->$method($params);
}
return null;
}

private function setNamespace(string $baseStrategyName): void
{
$this->namespace = 'EscolaLms\Jitsi\Strategies\\' .
preg_replace('/^(.*)Strategy$/', '$1', $baseStrategyName);
}
}
12 changes: 9 additions & 3 deletions src/Providers/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ public function register()
$this->app->register(EscolaLmsSettingsServiceProvider::class);
}

AdministrableConfig::registerConfig(self::CONFIG_KEY . '.package_status', ['required', 'string', 'in:' . implode(',', PackageStatusEnum::getValues())], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.host', ['required', 'string']);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.package_status', ['nullable', 'string', 'in:' . implode(',', PackageStatusEnum::getValues())], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.jitsi_host', ['nullable', 'string']);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.app_id', ['nullable', 'string'], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.secret', ['nullable', 'string'], false);

AdministrableConfig::registerConfig(self::CONFIG_KEY . '.jaas_host', ['nullable', 'string']);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.aud', ['nullable', 'string'], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.iss', ['nullable', 'string'], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.kid', ['nullable', 'string'], false);
AdministrableConfig::registerConfig(self::CONFIG_KEY . '.private_key', ['nullable', 'string'], false);
}
}
}
}
19 changes: 19 additions & 0 deletions src/Services/Contracts/JaasServiceContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace EscolaLms\Jitsi\Services\Contracts;

use EscolaLms\Auth\Models\User;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Support\Facades\Auth;

interface JaasServiceContract
{
public function getChannelData(
User $user,
string $channelDisplayName,
bool $isModerator = false,
array $configOverwrite = [],
$interfaceConfigOverwrite = []
): array;
public function setConfig(array $config): void;
}
9 changes: 8 additions & 1 deletion src/Services/Contracts/JitsiServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@

interface JitsiServiceContract
{
public function getChannelData(User $user, string $channelDisplayName, bool $isModerator = false, array $configOverwrite = [], $interfaceConfigOverwrite = []): array;
public function getChannelData(
User $user,
string $channelDisplayName,
bool $isModerator = false,
array $configOverwrite = [],
$interfaceConfigOverwrite = []
): array;
public function setConfig(array $config): void;
}
39 changes: 39 additions & 0 deletions src/Services/JaasService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace EscolaLms\Jitsi\Services;

use EscolaLms\Auth\Models\User;
use EscolaLms\Jitsi\Services\Contracts\JaasServiceContract;
use Gnello\Mattermost\Driver;
use Firebase\JWT\JWT;

class JaasService extends JitsiService implements JaasServiceContract
{
public Driver $driver;
private array $config;

public function generateJwt(
User $user,
string $room = '*',
bool $isModerator = false,
int $expireInMinutes = 60
): string {
$userData = $this->getUserData($user, $isModerator);
$payload = [
'aud' => $this->config['aud'],
'iss' => $this->config['iss'],
'exp' => now()->addMinutes($expireInMinutes)->timestamp,
'sub' => $this->config['sub'],
'room' => $room,
'context' => [
'user' => $userData,
],
];
return JWT::encode($payload, $this->config['private_key'], 'RS256', $this->config['kid']);
}

public function setConfig(array $config): void
{
$this->config = $config;
}
}
Loading

0 comments on commit ac18943

Please sign in to comment.