Problem/Motivation

OAuth2 module has only username/password authentication. It would be great to have authentication with email too.

Proposed resolution

Add a custom OAuth2Grant plugin that handle authentication with mail parameter. We can re-use most of the code from \Drupal\simple_oauth\Plugin\OAuth2Grant\Password class, only validate user should be different.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

a.dmitriiev created an issue. See original summary.

a.dmitriiev’s picture

Attached is the patch for 8.x-1.0-rc5 that allows to authenticate with Simple OAuth module and mail.

In your request don't forget to change the query parameter "grant_type" to "email_registration" and "username" change to "mail".

var data = new FormData();
data.append("grant_type", "email_registration");
data.append("client_id", "your_client_id");
data.append("client_secret", "your_client_secret");
data.append("mail", "test@example.com"); // your user email
data.append("password", "test"); // your user password

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "http://example.com/oauth/token"); // your domain url

xhr.send(data);
a.dmitriiev’s picture

Status: Active » Needs review
andypost’s picture

Version: 8.x-1.0-rc5 » 8.x-1.x-dev
Status: Needs review » Needs work
Issue tags: +Needs tests
Parent issue: » #2856542: [2.x] Allow authentication via mail and password over RPC
Related issues: +#3032949: Add support to email register with GraphQL

Nice feature for 1.1 release, needs tests like

mvantuch’s picture

Even though this looks like a clean way of doing it, it doesn't really work well with Postman, which doesn't support changing the the grant_type outside of the defined list (password, implicit, authorization code, client credentials). Might we this is not compatible with the OAuth2 standard?

My idea was to simply override the `user.auth` implementation to support authorisation by email as well. This would potentially fix other modules too as we'd then simply use the email where the name is expected.

mvantuch’s picture

Sorry, the previous patch was in a wrong format, attaching a new one.

mvantuch’s picture

a.dmitriiev’s picture

There are no strict standards for the grant type. Please read up here https://oauth.net/2/grant-types/ .

The OAuth 2.0 framework specifies several grant types for different use cases, as well as a framework for creating new grant types.

andypost’s picture

andypost’s picture

ptmkenny’s picture

I was able to get OAuth authentication working with the patch and instructions in #2, but the patch in #6 didn't work for me-- I kept getting an error message about an incorrect grant type.

ptmkenny’s picture

Also it would be great if there is a way to either 1) make this work with Postman, as described in #5 or 2) there are instructions provided for how to debug without Postman.

ayalon’s picture

Updated patch #6 for Drupal 9:

andypost’s picture

  1. +++ b/src/EmailRegistrationServiceProvider.php
    @@ -0,0 +1,18 @@
    +    $definition->setClass('Drupal\email_registration\UserAuth');
    

    Should use UserAuth::class

  2. +++ b/src/UserAuth.php
    @@ -0,0 +1,47 @@
    +
    +class UserAuth extends \Drupal\user\UserAuth {
    +
    +  public function authenticate($username, $password) {
    

    needs docblock

fjgarlin’s picture

Addressed the feedback above and made sure the two new files follow coding standards.

keopx’s picture

Patch #2 works properly.

I think the last patch not working because they need to set dependencies.

https://www.drupal.org/docs/drupal-apis/services-and-dependency-injectio...

+++ b/src/EmailRegistrationServiceProvider.php
@@ -0,0 +1,23 @@
+    $definition->setClass(UserAuth::class);

I think they need dependencies:

https://www.drupal.org/docs/drupal-apis/services-and-dependency-injectio...

  user.auth:
    class: Drupal\user\UserAuth
    arguments: ['@entity_type.manager', '@password']
keopx’s picture

Status: Needs review » Needs work
fjgarlin’s picture

The tests are failing because it is expecting protected function setUp(): void in line 38 of tests/src/Functional/Plugin/Commerce/CheckoutPane/EmailRegistrationLoginTest.php.

Not sure if that addition should be included on this patch as the tests are extending "commerce" test base classes.

The dependencies on the new file should be exactly the same ones. As we are extending the core.auth via a plugin I am not sure that we need to set them up again.

fjgarlin’s picture

Status: Needs work » Needs review
FileSize
3.1 KB

Trying to reroll the patch with the previous fix on the test file to see if it runs the tests. I could reproduce the issue of the test runner locally, then applied the patch, and all the tests ran sucessfully.

Note that this patch is the evolution of #5, not #2.

Kojo Unsui’s picture

Patch #2 is working properly in D9.2.7, tested in Next Auth authentication context. Great !

z3cka’s picture

Patch from #19 works like a charm on Drupal 9.3.2 in tandem with email_registration module. Thank you all so much for your work on this!

man-1982’s picture

Category: Feature request » Support request
Status: Needs review » Reviewed & tested by the community

Patch #2 work perfect for simple_oauth Drupal 9.3.3.
Thanks a.dmitriiev

andypost’s picture

NW for remarks and still no test added, the overriden UserAuth needs better explanation (in code comments) why we need to override it

  1. +++ b/src/UserAuth.php
    @@ -0,0 +1,58 @@
    +   * Return whether the given user credentials are valid or not.
    ...
    +   * @param string $username
    ...
    +   * @param string $password
    ...
    +   * @return bool
    ...
    +  public function authenticate($username, $password) {
    

    this method should use @inheritdoc as defined in \Drupal\user\UserAuth::authenticate()

  2. +++ b/tests/src/Functional/Plugin/Commerce/CheckoutPane/EmailRegistrationLoginTest.php
    @@ -35,7 +35,7 @@ class EmailRegistrationLoginTest extends CommerceBrowserTestBase {
    -  protected function setUp() {
    +  protected function setUp(): void {
    

    this change is unrelated and fixed via #3228033: Update the EmailRegistrationLoginTest to match the parent CommerceBrowserTestBase class.

fjgarlin’s picture

Status: Needs work » Needs review

I opened an MR with the changes. I addressed the feedback and added a test that bypasses the login form (that this module alters) via http request to check that login with both username and mail does work. I extended core tests and added comments where appropriate.

Marking as need review again.

z3cka’s picture

I just want to thank @fjgarlin for the latest changes in MR!3, I've tested this MR with Drupal 9.3.21 and it works great. Cheers!

Grevil’s picture

Title: Allow authentication via mail and password over OAuth2 module » [2.x] Allow authentication via mail and password over OAuth2 module
Issue tags: -Needs tests

This would be a great feature for 2.x! Thanks a lot, I'll see if I can squeeze in some time the next couple of weeks! :)

Grevil’s picture

Grevil’s picture

Version: 8.x-1.x-dev » 2.x-dev
Anybody’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Needs a reroll for 2.x, please. Then we can proceed.

Cryt1c made their first commit to this issue’s fork.

I added an entity query access check that is now required with D10.

fjgarlin’s picture

@Cryt1c - no need to create a new MR. The only difference is $query->accessCheck(TRUE);.
You can collaborate in https://git.drupalcode.org/project/email_registration/-/merge_requests/3

A new MR would make sense if the approach is completely different, but not if it's the same.

Cryt1c’s picture

@fjgarlin thanks for the info. I had some issues with pushing before. Now it worked and I have added my commit to your MR.

fjgarlin’s picture

Perfect! Thanks for helping with the issue.

Grevil’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

I guess, this can go back to "Needs review"?

ptmkenny’s picture

Category: Support request » Feature request
andypost’s picture

Status: Needs review » Needs work

needs to add strict types

andypost’s picture

Status: Needs work » Reviewed & tested by the community

Fixed my feedback

  • andypost committed 5d7c80e9 on 2.x authored by fjgarlin
    Issue #2991141 by fjgarlin, andypost, mvantuch, a.dmitriiev, ayalon,...
andypost’s picture

Status: Reviewed & tested by the community » Fixed
Grevil’s picture

Thank you, @andypost! I didn't find the time to review this!