Skip to content

Commit 6b609e6

Browse files
committed
feat: implement socialite package
1 parent aae89b8 commit 6b609e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3667
-1
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"Hypervel\\Queue\\": "src/queue/src/",
5555
"Hypervel\\Router\\": "src/router/src/",
5656
"Hypervel\\Session\\": "src/session/src/",
57+
"Hypervel\\Socialite\\": "src/socialite/src/",
5758
"Hypervel\\Support\\": "src/support/src/",
5859
"Hypervel\\Telescope\\": "src/telescope/src/",
5960
"Hypervel\\Testbench\\": "src/testbench/src/",
@@ -125,7 +126,9 @@
125126
"symfony/error-handler": "^6.3",
126127
"symfony/mailer": "^6.2",
127128
"symfony/process": "^6.2",
128-
"tijsverkoyen/css-to-inline-styles": "^2.2.5"
129+
"tijsverkoyen/css-to-inline-styles": "^2.2.5",
130+
"league/oauth1-client": "^1.11",
131+
"phpseclib/phpseclib": "^3.0"
129132
},
130133
"replace": {
131134
"hypervel/auth": "self.version",
@@ -157,6 +160,7 @@
157160
"hypervel/queue": "self.version",
158161
"hypervel/router": "self.version",
159162
"hypervel/session": "self.version",
163+
"hypervel/socialite": "self.version",
160164
"hypervel/support": "self.version",
161165
"hypervel/telescope": "self.version",
162166
"hypervel/testbench": "self.version",
@@ -227,6 +231,7 @@
227231
"Hypervel\\Queue\\ConfigProvider",
228232
"Hypervel\\Router\\ConfigProvider",
229233
"Hypervel\\Session\\ConfigProvider",
234+
"Hypervel\\Socialite\\ConfigProvider",
230235
"Hypervel\\Telescope\\ConfigProvider",
231236
"Hypervel\\Validation\\ConfigProvider"
232237
]

src/socialite/LICENSE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Taylor Otwell
4+
5+
Copyright (c) Hypervel
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.

src/socialite/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Socialite for Hypervel
2+
===
3+
4+
[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/hypervel/socialite)

src/socialite/composer.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "hypervel/socialite",
3+
"type": "library",
4+
"description": "Hypervel wrapper around OAuth 1 & OAuth 2 libraries.",
5+
"license": "MIT",
6+
"keywords": [
7+
"php",
8+
"hyperf",
9+
"oauth",
10+
"swoole",
11+
"hypervel"
12+
],
13+
"authors": [
14+
{
15+
"name": "Albert Chen",
16+
"email": "albert@hypervel.org"
17+
}
18+
],
19+
"support": {
20+
"issues": "https://github.com/hypervel/components/issues",
21+
"source": "https://github.com/hypervel/components"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Hypervel\\Socialite\\": "src/"
26+
}
27+
},
28+
"require": {
29+
"php": "^8.2",
30+
"ext-json": "*",
31+
"firebase/php-jwt": "^6.4",
32+
"guzzlehttp/guzzle": "^6.0|^7.0",
33+
"hypervel/http": "^0.2",
34+
"hypervel/support": "^0.2",
35+
"league/oauth1-client": "^1.11",
36+
"phpseclib/phpseclib": "^3.0"
37+
},
38+
"config": {
39+
"sort-packages": true
40+
},
41+
"extra": {
42+
"hyperf": {
43+
"config": "Hypervel\\Socialite\\ConfigProvider"
44+
},
45+
"branch-alias": {
46+
"dev-main": "0.2-dev"
47+
}
48+
}
49+
}

src/socialite/src/AbstractUser.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Socialite;
6+
7+
use ArrayAccess;
8+
use Hypervel\Socialite\Contracts\User;
9+
10+
abstract class AbstractUser implements ArrayAccess, User
11+
{
12+
/**
13+
* The unique identifier for the user.
14+
*/
15+
public mixed $id;
16+
17+
/**
18+
* The user's nickname / username.
19+
*/
20+
public ?string $nickname = null;
21+
22+
/**
23+
* The user's full name.
24+
*/
25+
public ?string $name = null;
26+
27+
/**
28+
* The user's e-mail address.
29+
*/
30+
public ?string $email = null;
31+
32+
/**
33+
* The user's avatar image URL.
34+
*/
35+
public ?string $avatar = null;
36+
37+
/**
38+
* The user's raw attributes.
39+
*/
40+
public array $user = [];
41+
42+
/**
43+
* The user's other attributes.
44+
*/
45+
public array $attributes = [];
46+
47+
/**
48+
* Get the unique identifier for the user.
49+
*/
50+
public function getId(): mixed
51+
{
52+
return $this->id;
53+
}
54+
55+
/**
56+
* Get the nickname / username for the user.
57+
*/
58+
public function getNickname(): ?string
59+
{
60+
return $this->nickname;
61+
}
62+
63+
/**
64+
* Get the full name of the user.
65+
*/
66+
public function getName(): ?string
67+
{
68+
return $this->name;
69+
}
70+
71+
/**
72+
* Get the e-mail address of the user.
73+
*/
74+
public function getEmail(): ?string
75+
{
76+
return $this->email;
77+
}
78+
79+
/**
80+
* Get the avatar / image URL for the user.
81+
*/
82+
public function getAvatar(): ?string
83+
{
84+
return $this->avatar;
85+
}
86+
87+
/**
88+
* Get the raw user array.
89+
*/
90+
public function getRaw(): array
91+
{
92+
return $this->user;
93+
}
94+
95+
/**
96+
* Set the raw user array from the provider.
97+
*/
98+
public function setRaw(array $user): static
99+
{
100+
$this->user = $user;
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* Map the given array onto the user's properties.
107+
*/
108+
public function map(array $attributes): static
109+
{
110+
$this->attributes = $attributes;
111+
112+
foreach ($attributes as $key => $value) {
113+
if (property_exists($this, $key)) {
114+
$this->{$key} = $value;
115+
}
116+
}
117+
118+
return $this;
119+
}
120+
121+
/**
122+
* Determine if the given raw user attribute exists.
123+
*/
124+
public function offsetExists(mixed $offset): bool
125+
{
126+
return array_key_exists($offset, $this->user);
127+
}
128+
129+
/**
130+
* Get the given key from the raw user.
131+
*/
132+
public function offsetGet(mixed $offset): mixed
133+
{
134+
return $this->user[$offset];
135+
}
136+
137+
/**
138+
* Set the given attribute on the raw user array.
139+
*/
140+
public function offsetSet(mixed $offset, mixed $value): void
141+
{
142+
$this->user[$offset] = $value;
143+
}
144+
145+
/**
146+
* Unset the given value from the raw user array.
147+
*/
148+
public function offsetUnset(mixed $offset): void
149+
{
150+
unset($this->user[$offset]);
151+
}
152+
153+
/**
154+
* Get a user attribute value dynamically.
155+
*/
156+
public function __get(string $key): mixed
157+
{
158+
return $this->attributes[$key] ?? null;
159+
}
160+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Socialite;
6+
7+
use Hypervel\Socialite\Contracts\Factory;
8+
9+
class ConfigProvider
10+
{
11+
public function __invoke(): array
12+
{
13+
return [
14+
'dependencies' => [
15+
Factory::class => SocialiteManager::class,
16+
],
17+
];
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Socialite\Contracts;
6+
7+
interface Factory
8+
{
9+
/**
10+
* Get an OAuth provider implementation.
11+
*
12+
* @return Provider $driver
13+
*/
14+
public function driver(?string $driver = null): mixed;
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Socialite\Contracts;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
interface Provider
10+
{
11+
/**
12+
* Redirect the user to the authentication page for the provider.
13+
*/
14+
public function redirect(): ResponseInterface;
15+
16+
/**
17+
* Get the User instance for the authenticated user.
18+
*/
19+
public function user(): User;
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Socialite\Contracts;
6+
7+
interface User
8+
{
9+
/**
10+
* Get the unique identifier for the user.
11+
*/
12+
public function getId(): mixed;
13+
14+
/**
15+
* Get the nickname / username for the user.
16+
*/
17+
public function getNickname(): ?string;
18+
19+
/**
20+
* Get the full name of the user.
21+
*/
22+
public function getName(): ?string;
23+
24+
/**
25+
* Get the e-mail address of the user.
26+
*/
27+
public function getEmail(): ?string;
28+
29+
/**
30+
* Get the avatar / image URL for the user.
31+
*/
32+
public function getAvatar(): ?string;
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypervel\Socialite\Exceptions;
6+
7+
use InvalidArgumentException;
8+
9+
class DriverMissingConfigurationException extends InvalidArgumentException
10+
{
11+
/**
12+
* Create a new exception for a missing configuration.
13+
*
14+
* @param array<int, string> $keys
15+
*/
16+
public static function make(string $provider, array $keys): static
17+
{
18+
/* @phpstan-ignore new.static */
19+
return new static('Missing required configuration keys [' . implode(', ', $keys) . "] for [{$provider}] OAuth provider.");
20+
}
21+
}

0 commit comments

Comments
 (0)