Skip to content

Unexplained Client authentication failed error #61

Closed
@Kerrialn

Description

@Kerrialn

Stack:

  • php: 8.0.1
  • symfony: 5.4
  • api-platform: 2.6
  • league/oauth2-server-bundle: 0.2.0

installed the package and added to bundle config.

  1. imported the routes inconfig/routes.yaml
  2. I generated a private key like so openssl genrsa -out ./var/oauth/private.key
  3. Then generated a public key like so openssl rsa -in ./var/oauth/private.key -pubout -out ./var/oauth/public.key
  4. created a client like so bin/console league:oauth2-server:create-client FrontEnd --grant-type=password --grant-type=refresh_token

/config/league_oauth2_server.php

<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->extension('league_oauth2_server',
        [
            'authorization_server' => [
                'private_key' => __DIR__ . '/../../var/oauth/private.key',
                'private_key_passphrase' => null,
                'encryption_key' => '%env(OAUTH2_ENCRYPTION_KEY)%',
                'encryption_key_type' => 'plain',
                'access_token_ttl' => 'PT1H',
                'refresh_token_ttl' => 'P1M',
                'auth_code_ttl' => 'PT10M',
                'enable_client_credentials_grant' => true,
                'enable_password_grant' => true,
                'enable_refresh_token_grant' => true,
                'enable_auth_code_grant' => true,
                'require_code_challenge_for_public_clients' => true
            ],
            'resource_server' => ['public_key' => __DIR__ . '/../../var/oauth/public.key'],
            'scopes' => ['available' => ['default_scope'], 'default' => ['default_scope']],
            'persistence' => ['doctrine' => ['entity_manager' => 'default']],
            'role_prefix' => 'ROLE_OAUTH2_',
            'client' => ['classname' => 'League\Bundle\OAuth2ServerBundle\Model\Client']
        ]);
};

/config/packages/security.php

<?php

declare(strict_types=1);

use App\Entity\User;
use App\ValueObject\Role;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->extension(
        'security',
        [
            'enable_authenticator_manager' => true,
            'encoders' => [
                User::class => [
                    'algorithm' => 'auto',
                ],
            ],
            'providers' => [
                'api_user_provider' => [
                    'entity' => [
                        'class' => User::class,
                        'property' => 'email'
                    ],
                ]
            ],
            'firewalls' => [
                'api_token' => [
                    'pattern' => '^/api/token$',
                    'security' => false,
                ],
                'register' => [
                    'pattern' => '^/api/users',
                    'security' => false,
                ],
                'api' => [
                    'pattern' => '^/api',
                    'stateless' => true,
                    'provider' => 'api_user_provider',
                    'security' => true,
                    'oauth2' => true,
                ],
            ],
            'access_control' => [
                [
                    'path' => '^/api',
                    'roles' => [Role::IS_AUTHENTICATED_FULLY],
                ],
            ],
        ]
    );
};

Post request to /token
Data in body:

grant_type: password,
client_id: 876b7...,
client_secret: bd108bd7c8...,
username: [email protected],
password: 12345678

Response:

{
    "error": "invalid_grant",
    "error_description": "The user credentials were incorrect.",
    "message": "The user credentials were incorrect."
}

client in the database all other oauth2 tables are empty:
Screenshot 2021-11-11 at 09 44 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions