Skip to content

Commit

Permalink
Allowed referer to take on multiple urls at once
Browse files Browse the repository at this point in the history
  • Loading branch information
ezra-obiwale committed Oct 26, 2017
1 parent 928f2a7 commit 38a6fa0
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Controllers/Traits/Referer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@

trait Referer {

/**
* Verify that the referer's origin matches the origin of the given url(s)
*
* @param string|array $url If array, it would throw exception if the origin does not match any
* @throws \Exception
* @return void
*/
protected function verifyReferer($url)
{
if (!is_array($url)) $url = [$url];
$referer = request()->headers->get('referer');
$refOrigin = $this->originFromUrl($referer);
$urlOrigin = $this->originFromUrl($url);
if ($refOrigin !== $urlOrigin) throw new \Exception($referer . ' is not a valid domain.');
foreach ($url as $urll) {
$urlOrigin = $this->originFromUrl($url);
if ($refOrigin == $urlOrigin) return;
}
throw new \Exception($referer . ' is not a valid domain.');
}

/**
* Gets the origin from the url string
*
* @param string $url
* @return string
*/
protected function originFromUrl($url)
{
// remove protocol
Expand Down

0 comments on commit 38a6fa0

Please sign in to comment.