Skip to content

Commit

Permalink
fix: add collaborate request to get token, install firebase/jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurujai committed Dec 12, 2024
1 parent ef68013 commit 21c473b
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 47 deletions.
3 changes: 1 addition & 2 deletions Command/SyncMediaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public function __construct(
CollaborateAPIAuth $collaborateAPIAuth,
CollaborateAPIRecordings $collaborateAPIRecordings,
string $name = null
)
{
) {
$this->learnAPIAuth = $learnAPIAuth;
$this->learnAPICourse = $learnAPICourse;
$this->collaborateAPIAuth = $collaborateAPIAuth;
Expand Down
42 changes: 35 additions & 7 deletions Services/CollaborateAPIAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,71 @@

namespace Pumukit\BlackboardBundle\Services;

use Firebase\JWT\JWT;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class CollaborateAPIAuth
{
private HttpClientInterface $client;
private CollaborateAPIConfiguration $configuration;
private $assertion;
private $grant_type;
private $payload;
private $header;
private $verify_cert = true;

public function __construct(HttpClientInterface $client, CollaborateAPIConfiguration $configuration)
{
$this->client = $client;
$this->configuration = $configuration;
$this->generateJWTPayload();
}

public function getToken(): string
{
try {
$response = $this->client->request('POST', $this->configuration->apiTokenUrl(), [
'auth_basic' => [$this->configuration->key(), $this->configuration->secret()],
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => [
'grant_type' => 'client_credentials',
'Authorization' => 'Bearer '.$this->assertion,
'Content-Type' => 'application/json',
],
'json' => $this->payload,
'verify_peer' => $this->verify_cert,
]);

if($response->getStatusCode() !== Response::HTTP_OK) {
if (Response::HTTP_OK !== $response->getStatusCode()) {
throw new \Exception('Unable to get collaborate token. Response status code: '.$response->getStatusCode());
}

$token = json_decode($response->getContent(), true);

return $token['access_token'];

} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
}

private function generateJWTPayload(): void
{
$exp = (new \DateTimeImmutable('now'))->modify('+30 minutes')->getTimestamp();
$claims = [
'iss' => $this->configuration->key(),
'sub' => $this->configuration->key(),
'exp' => $exp,
];

$this->header = [
'alg' => 'RS256',
'typ' => 'JWT',
];

$this->assertion = JWT::encode($claims, $this->configuration->secret(), 'RS256');
$this->grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer';

$this->payload = [
'grant_type' => $this->grant_type,
'assertion' => $this->assertion,
];
}
}
8 changes: 4 additions & 4 deletions Services/CollaborateAPIConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class CollaborateAPIConfiguration
{
private const API_BASE_PATH = '';
private string $host;
private string $key;
private string $secret;
private CONST API_BASE_PATH = '/collab/api/csa';

public function __construct(string $host, string $key, string $secret)
{
Expand All @@ -33,16 +33,16 @@ public function secret(): string

public function apiUrl(): string
{
return $this->host() . self::API_BASE_PATH;
return $this->host().self::API_BASE_PATH;
}

public function apiTokenUrl(): string
{
return $this->host() . self::API_BASE_PATH . '/webtoken';
return $this->host().self::API_BASE_PATH.'/token';
}

public function apiRecordingUrl(): string
{
return $this->host() . self::API_BASE_PATH . '/recordings';
return $this->host().self::API_BASE_PATH.'/recordings';
}
}
19 changes: 9 additions & 10 deletions Services/CollaborateAPIRecordings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,33 @@ public function __construct(HttpClientInterface $client, CollaborateAPIConfigura
$this->configuration = $configuration;
}

public function getCourseRecordings(string $accessToken, string $courseId): array
{
$recordingsResponse = $this->recordings($accessToken, $courseId);

return json_decode($recordingsResponse, true);
}

private function recordings(string $accessToken, string $courseId): string
{
try {
$response = $this->client->request('GET', $this->configuration->apiRecordingUrl(), [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'Authorization' => 'Bearer '.$accessToken,
'Accept' => 'application/json',
],
'query' => [
'externalCourseId' => $courseId,
],
]);

if($response->getStatusCode() !== Response::HTTP_OK) {
if (Response::HTTP_OK !== $response->getStatusCode()) {
throw new \Exception('Unable to get courses. Response status code: '.$response->getStatusCode());
}

return $response->getContent();

} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
}

public function getCourseRecordings(string $accessToken, string $courseId): array
{
$recordingsResponse = $this->recordings($accessToken, $courseId);

return json_decode($recordingsResponse, true);
}
}
3 changes: 1 addition & 2 deletions Services/LearnAPIAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ public function getToken(): string
],
]);

if($response->getStatusCode() !== Response::HTTP_OK) {
if (Response::HTTP_OK !== $response->getStatusCode()) {
throw new \Exception('Unable to get learn token. Response status code: '.$response->getStatusCode());
}

$token = json_decode($response->getContent(), true);

return $token['access_token'];

} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
Expand Down
9 changes: 4 additions & 5 deletions Services/LearnAPIConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

class LearnAPIConfiguration
{
private const API_BASE_PATH = '/learn/api/public';
private string $host;
private string $key;
private string $secret;
private CONST API_BASE_PATH = '/learn/api/public';

public function __construct(string $host, string $key, string $secret)
{
Expand All @@ -33,17 +33,16 @@ public function secret(): string

public function apiUrl(): string
{
return $this->host() . self::API_BASE_PATH;
return $this->host().self::API_BASE_PATH;
}

public function apiTokenUrl(): string
{
return $this->host() . self::API_BASE_PATH . '/v1/oauth2/token';
return $this->host().self::API_BASE_PATH.'/v1/oauth2/token';
}

public function apiCourseListUrl(): string
{
return $this->host() . self::API_BASE_PATH . '/v3/courses';
return $this->host().self::API_BASE_PATH.'/v3/courses';
}

}
31 changes: 15 additions & 16 deletions Services/LearnAPICourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,36 @@ public function __construct(HttpClientInterface $client, LearnAPIConfiguration $
$this->configuration = $configuration;
}

public function getIdsFromCourses(string $accessToken): array
{
$coursesResponse = $this->listCourses($accessToken);
$courses = json_decode($coursesResponse, true);

$courseIds = [];
foreach ($courses['results'] as $course) {
$courseIds[] = $course['courseId'];
}

return $courseIds;
}

private function listCourses(string $accessToken): string
{
try {
$response = $this->client->request('GET', $this->configuration->apiCourseListUrl(), [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'Authorization' => 'Bearer '.$accessToken,
'Accept' => 'application/json',
],
]);

if($response->getStatusCode() !== Response::HTTP_OK) {
if (Response::HTTP_OK !== $response->getStatusCode()) {
throw new \Exception('Unable to get courses. Response status code: '.$response->getStatusCode());
}

return $response->getContent();

} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
}

public function getIdsFromCourses(string $accessToken): array
{
$coursesResponse = $this->listCourses($accessToken);
$courses = json_decode($coursesResponse, true);

$courseIds = [];
foreach ($courses['results'] as $course) {
$courseIds[] = $course['courseId'];
}

return $courseIds;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": "^8.2",
"pumukit/pumukit": "^5.0",
"symfony/http-client": "^5.0"
"symfony/http-client": "^5.0",
"firebase/php-jwt": "^6.10"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit 21c473b

Please sign in to comment.