private function checkUsername(Request $request)
    {
        // username must exist and in valid format.
        $username = $request->request->get('username');
        $errors = $this->validator->validate($username, [
            new \Symfony\Component\Validator\Constraints\NotBlank(),
            new \AuthBucket\OAuth2\Symfony\Component\Validator\Constraints\Username(),
        ]);
        if (count($errors) > 0) {
            throw new InvalidRequestException([
                'error_description' => 'The request includes an invalid parameter value.',
            ]);
        }

        // password must exist and in valid format.
        $password = $request->request->get('password');
        $errors = $this->validator->validate($password, [
            new \Symfony\Component\Validator\Constraints\NotBlank(),
            new \AuthBucket\OAuth2\Symfony\Component\Validator\Constraints\Password(),
        ]);
        if (count($errors) > 0) {
            throw new InvalidRequestException([
                'error_description' => 'The request includes an invalid parameter value.',
            ]);
        }

        // Validate credentials with authentication manager.
        try {
            $token = new UsernamePasswordToken($username, $password, 'oauth2');
            $authenticationProvider = new DaoAuthenticationProvider(
                $this->userProvider,
                new UserChecker(),
                'oauth2',
                $this->encoderFactory
            );
            $authenticationProvider->authenticate($token);
        } catch (BadCredentialsException $e) {
            throw new InvalidGrantException([
                'error_description' => 'The provided resource owner credentials is invalid.',
            ]);
        }

        return $username;
    }