Skip to content

Commit 4989987

Browse files
Support for handling php login using any Oauth service you require.
* Interface for User and Session with it's respective services interface. * Abstract classes for getting user-details from the OauthServiceProvider * Strategy interface for implementing any oauth strategy. * Default implementation for Oauth strategy where you can just pass the required url's and credentials for creating an oauth-strategy
1 parent 319b8ff commit 4989987

18 files changed

+642
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "encryptorcode/php-oauth-login",
3+
"description": "An authentication framework which lets you to configure any type of authentication using OAuth (includes login with Google, Facebook, Github etc.) all with user management in your control.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Abhay Jatin Doshi",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"encryptorcode\\authentication\\": "src/"
15+
}
16+
},
17+
"require": {
18+
"encryptorcode/php-http-client": "^1.0",
19+
"encryptorcode/php-server-utils": "^1.0"
20+
}
21+
}

composer.lock

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
namespace encryptorcode\authentication\implementation;
3+
4+
use encryptorcode\authentication\oauth\OauthStrategy as OauthStrategy;
5+
use encryptorcode\authentication\oauth\OauthToken as OauthToken;
6+
use encryptorcode\authentication\oauth\OauthUser as OauthUser;
7+
use encryptorcode\httpclient\HttpRequest as HttpRequest;
8+
9+
abstract class DefaultOauthStrategy implements OauthStrategy{
10+
11+
private $helper;
12+
private $details;
13+
14+
public function __construct(OauthStrategyHelper $helper){
15+
$this->helper = $helper;
16+
$this->details = $helper->getDetails();
17+
}
18+
19+
public function getLoginUrl(string $state) : string{
20+
$loginUrl = $this->details->loginUrl .
21+
"?client_id=" . $this->details->clientId .
22+
"&redirect_uri=" . $this->details->redirectUri .
23+
"&scope=" . $this->details->scope .
24+
"&access_type=offline" .
25+
"&response_type=code" .
26+
"&prompt=consent" .
27+
"&state=" . $state;
28+
return $loginUrl;
29+
}
30+
31+
public function generateToken(string $grantCode) : OauthToken{
32+
$response = HttpRequest::post($this->details->tokenUrl)
33+
->formParam("code",$grantCode)
34+
->formParam("client_id",$this->details->clientId)
35+
->formParam("client_secret",$this->details->clientSecret)
36+
->formParam("redirect_uri",$this->details->redirectUri)
37+
->formParam("grant_type","authorization_code")
38+
->getResponse();
39+
40+
$token = $this->helper->readToken($response->getBody());
41+
return $token;
42+
}
43+
44+
public function regenerateToken(string $refreshToken) : OauthToken{
45+
$response = HttpRequest::post($this->details->tokenUrl)
46+
->formParam("refresh_token",$refreshToken)
47+
->formParam("client_id",$this->details->clientId)
48+
->formParam("client_secret",$this->details->clientSecret)
49+
->formParam("redirect_uri",$this->details->redirectUri)
50+
->formParam("grant_type","authorization_code")
51+
->getResponse();
52+
53+
$token = $this->helper->readToken($response->getBody());
54+
return $token;
55+
}
56+
57+
public function revokeToken(string $refreshToken) : void{
58+
$reponse = HttpRequest::post($this->details->revokeUrl)
59+
->formParam("token",$refreshToken)
60+
->getResponse();
61+
}
62+
63+
public function getUser(string $accessToken) : OauthUser{
64+
$response = HttpRequest::get($this->details->userUrl)
65+
->header("Authorization","Bearer ".$accessToken)
66+
->getResponse();
67+
68+
return $this->helper->readUser($response->getBody());
69+
}
70+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace encryptorcode\authentication\implementation;
3+
4+
class OauthStrategyDetails{
5+
public $strategyName;
6+
public $loginUrl;
7+
public $clientId;
8+
public $clientSecret;
9+
public $redirectUri;
10+
public $tokenUrl;
11+
public $revokeUrl;
12+
public $userUrl;
13+
public $scope;
14+
15+
public function __construct(string $loginUrl, string $clientId, string $clientSecret, string $redirectUri, string $tokenUrl, string $revokeUrl, string $userUrl, string $scope){
16+
$this->loginUrl = $loginUrl;
17+
$this->clientId = $clientId;
18+
$this->clientSecret = $clientSecret;
19+
$this->redirectUri = $redirectUri;
20+
$this->tokenUrl = $tokenUrl;
21+
$this->revokeUrl = $revokeUrl;
22+
$this->userUrl = $userUrl;
23+
$this->scope = $scope;
24+
}
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace encryptorcode\authentication\implementation;
3+
4+
use encryptorcode\authentication\oauth\OauthUser as OauthUser;
5+
use encryptorcode\authentication\oauth\OauthToken as OauthToken;
6+
7+
abstract class OauthStrategyHelper{
8+
public abstract function getDetails() : OauthStrategyDetails;
9+
public abstract function readUser(string $user) : OauthUser;
10+
public abstract function readToken(string $token) : OauthToken;
11+
}

src/oauth/OauthException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace encryptorcode\authentication\oauth;
3+
4+
class OauthException extends Exception{
5+
6+
}

src/oauth/OauthStrategy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace encryptorcode\authentication\oauth;
3+
4+
interface OauthStrategy{
5+
public function getLoginUrl(string $state) : string;
6+
public function generateToken(string $grantCode) : OauthToken;
7+
public function regenerateToken(string $refreshToken) : OauthToken;
8+
public function revokeToken(string $refreshToken) : void;
9+
public function getUser(string $accessToken) : OauthUser;
10+
}

src/oauth/OauthToken.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace encryptorcode\authentication\oauth;
3+
4+
class OauthToken implements \JsonSerializable{
5+
private $accessToken;
6+
private $refreshToken;
7+
private $expiryTime;
8+
9+
public function __construct(string $accessToken, string $refreshToken, int $expiryTime) {
10+
$this->accessToken = $accessToken;
11+
$this->refreshToken = $refreshToken;
12+
$this->expiryTime = $expiryTime;
13+
}
14+
15+
public function getAccessToken() : string{
16+
return $this->accessToken;
17+
}
18+
19+
public function getRefreshToken() : string {
20+
return $this->refreshToken;
21+
}
22+
23+
public function getExpiryTime() : string{
24+
return $this->expiryTime;
25+
}
26+
27+
public function setAccessToken(string $accessToken) : OauthToken {
28+
$this->accessToken = $accessToken;
29+
return $this;
30+
}
31+
32+
public function setRefreshToken(string $refreshToken) : OauthToken {
33+
$this->refreshToken = $refreshToken;
34+
return $this;
35+
}
36+
37+
public function setExpiryTime(int $expiryTime) : OauthToken {
38+
$this->expiryTime = $expiryTime;
39+
return $this;
40+
}
41+
42+
public function jsonSerialize(){
43+
return [
44+
"accessToken" => $this->accessToken,
45+
"refreshToken" => $this->refreshToken,
46+
"expiryTime" => $this->expiryTime
47+
];
48+
}
49+
50+
}

src/oauth/OauthUser.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace encryptorcode\authentication\oauth;
3+
4+
class OauthUser{
5+
private $oauthId;
6+
private $email;
7+
private $fullName;
8+
private $name;
9+
private $profileImage;
10+
11+
public function __construct(string $oauthId, string $email, string $fullName, string $name, string $profileImage) {
12+
$this->oauthId = $oauthId;
13+
$this->email = $email;
14+
$this->fullName = $fullName;
15+
$this->name = $name;
16+
$this->profileImage = $profileImage;
17+
}
18+
19+
public function getOauthId() : string {
20+
return $this->oauthId;
21+
}
22+
23+
public function getEmail() : string {
24+
return $this->email;
25+
}
26+
27+
public function getFullName() : string {
28+
return $this->fullName;
29+
}
30+
31+
public function getName() : string {
32+
return $this->name;
33+
}
34+
35+
public function getProfileImage() : string {
36+
return $this->profileImage;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)