Skip to content

Commit 6f1e089

Browse files
committed
✨ Adds Initializers for built-in providers and manager
Adds support for built-in OAuth providers like Github and Google. This change introduces: - A `BuiltInOAuthProvider` enum to manage supported providers. - A dynamic initializer for creating `OAuth2Provider` instances based on the selected provider, using environment variables for configuration. - An `OAuthManagerInitializer` for resolving OAuthManager dependencies. This approach simplifies OAuth integration by providing pre-configured providers and ensuring that necessary environment variables are set correctly, throwing an exception if something is missing.
1 parent 2f01e91 commit 6f1e089

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
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 Tempest\Auth\OAuth;
6+
7+
use Tempest\Support\IsEnumHelper;
8+
9+
enum BuiltInOAuthProvider: string
10+
{
11+
use IsEnumHelper;
12+
13+
case GITHUB = 'github';
14+
case GOOGLE = 'google';
15+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Auth\OAuth\Initializers;
6+
7+
use Tempest\Auth\OAuth\BuiltInOAuthProvider;
8+
use Tempest\Auth\OAuth\Exceptions\OAuthException;
9+
use Tempest\Auth\OAuth\OAuth2Provider;
10+
use Tempest\Auth\OAuth\Providers\GithubOAuthProvider;
11+
use Tempest\Auth\OAuth\Providers\GoogleOAuthProvider;
12+
use Tempest\Container\Container;
13+
use Tempest\Container\DynamicInitializer;
14+
use Tempest\Reflection\ClassReflector;
15+
use TypeError;
16+
use UnitEnum;
17+
use function \Tempest\env;
18+
19+
final class BuiltInOAuth2ProviderInitializer implements DynamicInitializer
20+
{
21+
public function canInitialize(ClassReflector $class, UnitEnum|string|null $tag): bool
22+
{
23+
return ($class->implements(OAuth2Provider::class) || $class->is(OAuth2Provider::class)) && BuiltInOAuthProvider::hasValue((string) $tag);
24+
}
25+
26+
public function initialize(ClassReflector $class, UnitEnum|string|null $tag, Container $container): OAuth2Provider
27+
{
28+
$tag = BuiltInOAuthProvider::from($tag);
29+
30+
try {
31+
return match ($tag) {
32+
BuiltInOAuthProvider::GOOGLE => new GoogleOAuthProvider(
33+
clientId: env('GOOGLE_CLIENT_ID'),
34+
clientSecret: env('GOOGLE_CLIENT_SECRET'),
35+
redirectUri: env('GOOGLE_REDIRECT_URI')
36+
),
37+
BuiltInOAuthProvider::GITHUB => new GithubOAuthProvider(
38+
clientId: env('GITHUB_CLIENT_ID'),
39+
clientSecret: env('GITHUB_CLIENT_SECRET'),
40+
redirectUri: env('GITHUB_REDIRECT_URI')
41+
),
42+
default => throw new OAuthException("Unable to match tag with built-in OAuth2 provider: \"{$tag->value}\""),
43+
};
44+
} catch (TypeError $exception) {
45+
throw new OAuthException(
46+
"Failed to initialize OAuth2 provider for tag: \"{$tag->value}\". Ensure that the environment variables are set correctly.",
47+
previous: $exception
48+
);
49+
}
50+
}
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Auth\OAuth\Initializers;
6+
7+
use Tempest\Auth\OAuth\OAuth2Provider;
8+
use Tempest\Auth\OAuth\OAuthManager;
9+
use Tempest\Container\Container;
10+
use Tempest\Container\DynamicInitializer;
11+
use Tempest\Http\Session\Session;
12+
use Tempest\HttpClient\HttpClient;
13+
use Tempest\Reflection\ClassReflector;
14+
use UnitEnum;
15+
16+
final class OAuthManagerInitializer implements DynamicInitializer
17+
{
18+
public function canInitialize(ClassReflector $class, UnitEnum|string|null $tag): bool
19+
{
20+
// We don't check against built-in providers here, as the OAuthManager should be able to work with non-built-in OAuth2Provider.
21+
return $class->is(OAuthManager::class);
22+
}
23+
24+
public function initialize(ClassReflector $class, UnitEnum|string|null $tag, Container $container): OAuthManager
25+
{
26+
return new OAuthManager(
27+
provider: $container->get(OAuth2Provider::class, $tag),
28+
httpClient: $container->get(HttpClient::class),
29+
session: $container->get(Session::class)
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)