Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"azure-oss/storage-blob-flysystem": "^1.2",
"brianium/paratest": "^7.14",
"carthage-software/mago": "1.0.0-beta.28",
"depotwarehouse/oauth2-twitch": "^1.3",
"guzzlehttp/psr7": "^2.6.1",
"league/flysystem-aws-s3-v3": "^3.25.1",
"league/flysystem-ftp": "^3.25.1",
Expand Down Expand Up @@ -88,6 +87,7 @@
"tempest/blade": "dev-main",
"thenetworg/oauth2-azure": "^2.2",
"twig/twig": "^3.16",
"vertisan/oauth2-twitch-helix": "^2.0",
"wohali/oauth2-discord-new": "^1.2"
},
"replace": {
Expand Down
1 change: 1 addition & 0 deletions docs/2-features/17-oauth.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Tempest provides a different configuration object for each OAuth provider. Below
- **Microsoft** authentication using {b`Tempest\Auth\OAuth\Config\MicrosoftOAuthConfig`},
- **Slack** authentication using {b`Tempest\Auth\OAuth\Config\SlackOAuthConfig`},
- **Apple** authentication using {b`Tempest\Auth\OAuth\Config\AppleOAuthConfig`},
- **Twitch** authentication using {b`Tempest\Auth\OAuth\Config\TwitchOAuthConfig`},
- Any other OAuth platform using {b`Tempest\Auth\OAuth\Config\GenericOAuthConfig`}.

## Testing
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"adam-paterson/oauth2-slack": "^1.1",
"wohali/oauth2-discord-new": "^1.2",
"smolblog/oauth2-twitter": "^1.0",
"depotwarehouse/oauth2-twitch": "^1.3"
"vertisan/oauth2-twitch-helix": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 2 additions & 3 deletions packages/auth/src/Installer/OAuthInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ private function publishConfig(SupportedOAuthProvider $provider): void

private function publishController(SupportedOAuthProvider $provider): void
{
$fileName = str($provider->value)
->classBasename()
$fileName = str($provider->getName())
->replace('Provider', '')
->append('Controller.php')
->toString();
Expand All @@ -104,7 +103,7 @@ private function publishController(SupportedOAuthProvider $provider): void
destination: src_path("Authentication/OAuth/{$fileName}"),
callback: function (string $source, string $destination) use ($provider) {
$providerFqcn = $provider::class;
$name = strtolower($provider->name);
$name = strtolower($provider->getName());
$userModelFqcn = to_fqcn(src_path('Authentication/User.php'), root: root_path());

$this->update(
Expand Down
15 changes: 15 additions & 0 deletions packages/auth/src/Installer/oauth/twitch.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Tempest\Auth\OAuth\Config\TwitchOAuthConfig;
use Tempest\Auth\OAuth\SupportedOAuthProvider;

use function Tempest\env;

return new TwitchOAuthConfig(
clientId: env('OAUTH_TWITCH_CLIENT_ID') ?? '',
clientSecret: env('OAUTH_TWITCH_CLIENT_SECRET') ?? '',
redirectTo: [\Tempest\Auth\Installer\oauth\OAuthControllerStub::class, 'callback'],
tag: SupportedOAuthProvider::TWITCH,
);
73 changes: 73 additions & 0 deletions packages/auth/src/OAuth/Config/TwitchOAuthConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Tempest\Auth\OAuth\Config;

use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
use Tempest\Auth\OAuth\OAuthConfig;
use Tempest\Auth\OAuth\OAuthUser;
use Tempest\Mapper\ObjectFactory;
use UnitEnum;
use Vertisan\OAuth2\Client\Provider\TwitchHelix;
use Vertisan\OAuth2\Client\Provider\TwitchHelixResourceOwner;

final class TwitchOAuthConfig implements OAuthConfig
{
public string $provider = TwitchHelix::class;

public function __construct(
/**
* The client ID for the Twitch OAuth application.
*/
public string $clientId,

/**
* The client secret for the Twitch OAuth application.
*/
public string $clientSecret,

/**
* The controller action to redirect to after the user authorizes the application.
*/
public string|array $redirectTo,

/**
* The scopes to request from Twitch.
*
* @var string[]
*/
public array $scopes = ['user:read:email'],

/**
* Identifier for this OAuth configuration.
*/
public null|string|UnitEnum $tag = null,
) {}

public function createProvider(): AbstractProvider
{
return new TwitchHelix([
'clientId' => $this->clientId,
'clientSecret' => $this->clientSecret,
'redirectUri' => $this->redirectTo,
]);
}

/**
* @param TwitchHelixResourceOwner $resourceOwner
*/
public function mapUser(ObjectFactory $factory, ResourceOwnerInterface $resourceOwner): OAuthUser
{
return $factory->withData([
'id' => (string) $resourceOwner->getId(),
'email' => $resourceOwner->getEmail(),
'name' => $resourceOwner->getDisplayName(),
'nickname' => $resourceOwner->getDisplayName(),
'avatar' => $resourceOwner->getProfileImageUrl(),
'provider' => $this->provider,
'raw' => $resourceOwner->toArray(),
])->to(OAuthUser::class);
}
}
30 changes: 30 additions & 0 deletions packages/auth/src/OAuth/SupportedOAuthProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use League\OAuth2\Client\Provider\Instagram;
use League\OAuth2\Client\Provider\LinkedIn;
use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
use Vertisan\OAuth2\Client\Provider\TwitchHelix;
use Wohali\OAuth2\Client\Provider\Discord;

enum SupportedOAuthProvider: string
Expand All @@ -25,7 +26,35 @@ enum SupportedOAuthProvider: string
case LINKEDIN = LinkedIn::class;
case MICROSOFT = Microsoft::class;
case SLACK = Slack::class;
case TWITCH = TwitchHelix::class;

/**
* Returns the canonical name for the given OAuth provider. Required because some of the providers have mixed-case names.
*
* @return string|null The canonical name, or null if the provider is generic.
*/
public function getName(): ?string
{
return match ($this) {
self::APPLE => 'Apple',
self::DISCORD => 'Discord',
self::FACEBOOK => 'Facebook',
self::GENERIC => null,
self::GITHUB => 'Github',
self::GOOGLE => 'Google',
self::INSTAGRAM => 'Instagram',
self::LINKEDIN => 'LinkedIn',
self::MICROSOFT => 'Microsoft',
self::SLACK => 'Slack',
self::TWITCH => 'Twitch',
};
}

/**
* Returns the Composer package name for the given OAuth provider.
*
* @return string|null The Composer package name, or null if the provider is generic.
*/
public function composerPackage(): ?string
{
return match ($this) {
Expand All @@ -39,6 +68,7 @@ public function composerPackage(): ?string
self::LINKEDIN => 'league/oauth2-linkedin',
self::MICROSOFT => 'stevenmaguire/oauth2-microsoft',
self::SLACK => 'adam-paterson/oauth2-slack',
self::TWITCH => 'vertisan/oauth2-twitch-helix',
};
}
}
5 changes: 5 additions & 0 deletions tests/Integration/Auth/Installer/OAuthInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public static function oauthProvider(): array
'expectedConfigPath' => 'App/Authentication/OAuth/slack.config.php',
'expectedControllerPath' => 'App/Authentication/OAuth/SlackController.php',
],
'twitch' => [
'provider' => SupportedOAuthProvider::TWITCH,
'expectedConfigPath' => 'App/Authentication/OAuth/twitch.config.php',
'expectedControllerPath' => 'App/Authentication/OAuth/TwitchController.php',
],
];
}
}
Loading