Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tjarksaul committed May 21, 2020
0 parents commit 633d0b2
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "tjarksaul/oauth2-dlrg",
"license": "MIT",
"authors": [
{
"name": "Tjark Saul"
}
],
"keywords": [
"oauth",
"oauth2",
"client",
"authorization",
"authentication",
"dlrg.net"
],
"require": {
"php": "^5.6 || ^7.0",
"league/oauth2-client": "^2.0"
},
"autoload": {
"psr-4": {
"League\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"League\\OAuth2\\Client\\Test\\": "tests/src/"
}
}
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
19 changes: 19 additions & 0 deletions src/Provider/AppSecretProof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace League\OAuth2\Client\Provider;

class AppSecretProof
{
/**
* The app secret proof to sign requests made to the Graph API
* @see https://developers.facebook.com/docs/graph-api/securing-requests#appsecret_proof
*
* @param string $appSecret
* @param string $accessToken
* @return string
*/
public static function create($appSecret, $accessToken)
{
return hash_hmac('sha256', $accessToken, $appSecret);
}
}
61 changes: 61 additions & 0 deletions src/Provider/Dlrg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace League\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;

class Dlrg extends AbstractProvider {
use BearerAuthorizationTrait;

/**
* @const string
*/
const BASE_DLRG_URL = 'https://dlrg.net/oauth2';

const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'sub';

/**
* @param array $options
* @param array $collaborators
*
* @throws \InvalidArgumentException
*/
public function __construct($options = [], array $collaborators = []) {
parent::__construct($options, $collaborators);
}

public function getBaseAuthorizationUrl() {
return static::BASE_DLRG_URL . '/authorize';
}

public function getBaseAccessTokenUrl(array $params) {
return static::BASE_DLRG_URL . '/token';
}

public function getDefaultScopes() {
return ['profile', 'email'];
}

public function getScopeSeparator() {
return ' ';
}

public function getResourceOwnerDetailsUrl(AccessToken $token) {
return static::BASE_DLRG_URL . '/userinfo';
}

protected function createResourceOwner(array $response, AccessToken $token) {
return new DlrgUser($response);
}

protected function checkResponse(ResponseInterface $response, $data) {
var_dump($data);
if (!empty($data['error'])) {
$message = $data['error'] . ': ' . $data['error_description'];
throw new IdentityProviderException($message, 42, $data);
}
}
}
100 changes: 100 additions & 0 deletions src/Provider/DlrgUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace League\OAuth2\Client\Provider;

class DlrgUser implements ResourceOwnerInterface {
/**
* @var array
*/
protected $data;

/**
* @param array $response
*/
public function __construct(array $response) {
$this->data = $response;
}

/**
* Returns the ID for the user as a string if present.
*
* @return string|null
*/
public function getId() {
return $this->getField('sub');
}

/**
* Returns the name for the user as a string if present.
*
* @return string|null
*/
public function getName() {
return $this->getField('name');
}

/**
* Returns the first name for the user as a string if present.
*
* @return string|null
*/
public function getFirstName() {
return $this->getField('given_name');
}

/**
* Returns the last name for the user as a string if present.
*
* @return string|null
*/
public function getLastName() {
return $this->getField('family_name');
}

/**
* Returns the email for the user as a string if present.
*
* @return string|null
*/
public function getEmail() {
return $this->getField('email');
}

/**
* Returns if the user's email has been verified
*
* @return boolean
*/
public function isEmailVerified() {
return !!$this->getField('email_verified');
}

/**
* Returns the preferred username for the user as a string if present.
*
* @return string|null
*/
public function getPreferredUsername() {
return $this->getField('preferred_username');
}

/**
* Returns all the data obtained about the user.
*
* @return array
*/
public function toArray() {
return $this->data;
}

/**
* Returns a field from the Graph node data.
*
* @param string $key
*
* @return mixed|null
*/
private function getField($key) {
return isset($this->data[$key]) ? $this->data[$key] : null;
}
}

0 comments on commit 633d0b2

Please sign in to comment.