Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback #1

Open
wants to merge 36 commits into
base: feedback
Choose a base branch
from
Open

Feedback #1

wants to merge 36 commits into from

Conversation

github-classroom[bot]
Copy link
Contributor

@github-classroom github-classroom bot commented Feb 19, 2021

👋! GitHub Classroom created this pull request as a place for your teacher to leave feedback on your work. It will update automatically. Don’t close or merge this pull request, unless you’re instructed to do so by your teacher.

In this pull request, your teacher can leave comments and feedback on your code. Click the Subscribe button to be notified if that happens.

Click the Files changed or Commits tab to see all of the changes pushed to master since the assignment started. Your teacher can see this too.

Notes for teachers

Use this PR to leave feedback. Here are some tips:

  • Click the Files changed tab to see all of the changes pushed to master since the assignment started. To leave comments on specific lines of code, put your cursor over a line of code and click the blue + (plus sign). To learn more about comments, read “Commenting on a pull request”.
  • Click the Commits tab to see the commits pushed to master. Click a commit to see specific changes.
  • If you turned on autograding, then click the Checks tab to see the results.
  • This page is an overview. It shows commits, line comments, and general comments. You can leave a general comment below.

For more information about this pull request, read “Leaving assignment feedback in GitHub”.

Subscribed: @mparatcha

Copy link
Member

@gsomoza gsomoza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good progress so far!

}
public function getPublicKeys($url): array
{
$this->curlClient->get($url);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double check the use of the property here.

public function getPublicKeys($url): array
{
$this->curlClient->get($url);
$jwks = json_decode($this->curlClient->getBody(), true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're going to add one improvement here: the Magento framework ships with a json encoder/decoder class that does this same thing, but adds a layer of error protection on top. I'll let you search for it yourself, but if you can't find it after 15 minutes of searching let me know and I'll show you.

The general principle is: we should (almost) never use native PHP function calls in our code, because there's probably a utility from either the Magento Framework or Zend framework that will add important error protection functionality on top.

In the case of \json_decode() for example, if the argument is invalid JSON, the core PHP function will just return false. In order to know WHAT the error actually was, you need to catch that use-case (compare the result with false) and then call the \json_last_error_msg(). So therefore, only calling json_decode is not enough because there's at least one use-case that's not being covered: if things go wrong. By using the Magento Framework implementation, you get that error handling mechanism already implemented for free: your code is suddenly a lot more "defensive", which essentially means it's much harder to "break" your code / cause a bug.

use \Firebase\JWT\JWT;
use \Firebase\JWT\JWK;

class TokenValidator {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work refactoring to this class! We will now start to make the code a lot more "defensive", so I'll add some comments on how to do that.

{
public const TEAM_DOMAIN = 'https://hyva.ancord.io';
public const CERTS_URL = self::TEAM_DOMAIN .'/cdn-cgi/access/certs';
public const ALGORITHM = 'RS256';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move these constants into the TokenValidator service.


public function aroundDispatch(FrontController $subject, callable $proceed)
{
$payload = []; //$this->getMockPayload();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Get $request from FrontController
  2. Get cookies from $request.
  3. Get $token from CF_Authorization cookie (you can rename $payload to $token).

}

try {
$result = $this->serviceToken->getJWT($payload, self::CERTS_URL, self::ALGORITHM);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better name for $result => $validatedToken

public const CERTS_URL = self::TEAM_DOMAIN .'/cdn-cgi/access/certs';
public const ALGORITHM = 'RS256';

protected $serviceToken;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to tokenValidator

return $jwks;
}

public function getJWT($payload, $url, $algorithm) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some improvements:

public function validateToken(string $token, string $url, string $algorithm): object // fijate bien el result que sea igual al de JWT::decode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants