Skip to content

Commit

Permalink
chore: add Firebase JWT (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgautier404 authored Oct 27, 2022
1 parent baa460c commit 574419c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 15 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
},
"require": {
"php": ">=7.4",
"ext-grpc": "*",
"firebase/php-jwt": "^6.3",
"google/protobuf": "3.21.5",
"grpc/grpc": "1.42.0",
"ext-grpc": "*"
"grpc/grpc": "1.42.0"
},
"require-dev": {
"composer/composer" : "^2.4.1",
Expand Down
64 changes: 63 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions src/Auth/AuthUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,36 @@

namespace Momento\Auth;

use Firebase\JWT\JWT;
use Momento\Cache\Errors\InvalidArgumentError;


class AuthUtils
{

private static function throwBadAuthToken() {
private static function throwBadAuthToken()
{
throw new InvalidArgumentError('Invalid Momento auth token.');
}

public static function parseAuthToken(string $authToken) : array {
$exploded = explode (".", $authToken);
public static function parseAuthToken(string $authToken): object
{
$exploded = explode(".", $authToken);
if (count($exploded) != 3) {
self::throwBadAuthToken();
}
list($header, $payload, $signature) = $exploded;
$token = json_decode(base64_decode($payload), true);
if ($token === null) {

try {
list($header, $payload, $signature) = $exploded;
$payload = JWT::jsonDecode(JWT::urlsafeB64Decode($payload));
} catch (\Exception) {
self::throwBadAuthToken();
}

if ($payload === null) {
self::throwBadAuthToken();
}
return $token;
return $payload;
}

}
11 changes: 6 additions & 5 deletions src/Auth/EnvMomentoTokenProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Momento\Auth;

use Momento\Auth\AuthUtils;
Expand All @@ -20,21 +21,21 @@ public function __construct(string $envVariableName)
}
$payload = AuthUtils::parseAuthToken($authToken);
$this->authToken = $authToken;
$this->controlEndpoint = $payload["cp"];
$this->cacheEndpoint = $payload["c"];
$this->controlEndpoint = $payload->cp;
$this->cacheEndpoint = $payload->c;
}

public function getAuthToken() : string
public function getAuthToken(): string
{
return $this->authToken;
}

public function getCacheEndpoint() : string
public function getCacheEndpoint(): string
{
return $this->cacheEndpoint;
}

public function getControlEndpoint() : string
public function getControlEndpoint(): string
{
return $this->controlEndpoint;
}
Expand Down

0 comments on commit 574419c

Please sign in to comment.