The Drupal module OAuth2 Login Provider (https://drupal.org/project/oauth2_loginprovider) provides OAuth2 authentication (through the module oauth2_server) and the profile of the authenticated user. This can be used by another site for user authentication and profile, just like the authentication services of Google+, Facebook, Twitter, etc.

There is also an HybridAuth provider that can be used for this purpose: https://github.com/hybridauth/hybridauth/tree/2.2.0/additional-providers...

This patch extends the module 'hybridauth' by adding support for the provider DrupalOAuth2 of the library. It does it by defining a new module called 'hybridauth_DrupalOAuth2'. For this extension to work correctly it was necessary to define a new alter hook for the provider configuration on code of hybridauth.

This module can also be used as an example for defining extension modules for the other providers. In particular, the providers for Google+, Facebook, etc. can be defined as extension modules, instead of being coded inside the module 'hybridauth' itself.

Comments

dashohoxha’s picture

dashohoxha’s picture

A small fix on the previous patch: Used field keys_id for client_id (instead of keys_key).
This patch must be applied after the first one.

dashohoxha’s picture

The patch on HybridAuth library has been applied already (https://github.com/hybridauth/hybridauth/pull/148). I am also a maintainer of that library now.
Can we apply these two patches as well? I can also help with maintaining this module (as much as I can). In particular, I would like to see different providers having their own modules (instead of being hardcoded into the main module).

dashohoxha’s picture

Issue summary: View changes
duozersk’s picture

Status: Patch (to be ported) » Fixed

Dashamir,

Thank you for using the module and for the patch! Sorry for the late reply.

I have committed 2 commits from your patches:
http://drupalcode.org/project/hybridauth.git/commit/eecd805
http://drupalcode.org/project/hybridauth.git/commit/3a475e1

I haven't added the module for the DrupalOAuth2 provider as I would prefer it to be either a separate project or included into https://drupal.org/project/oauth2_loginprovider project. Let me know if you believe it should still be included into HybridAuth module.

Great news on the HybridAuth library maintainer-ship - glad to hear it is moving forward now.

Thanks
AndyB

dashohoxha’s picture

Thanks Andy.

I believe that it is better not to include the DrupalOAuth2 provider inside this module.
I am going to create a new Drupal module for it, called hybridauth_drupaloauth2. Until yesterday I could not promote modules to drupal.org, but now I can. I don't think it should be included inside https://drupal.org/project/oauth2_loginprovider, because this one is a server module, hybridauth_drupaloauth2 is a client module, so it is better to keep them separate.

I also think that some other providers should be separated into their own modules, because this would make the core module cleaner and easier to maintain. However you are the maintainer, so you decide about it.

dashohoxha’s picture

Status: Fixed » Closed (fixed)
duozersk’s picture

Dashamir,

Thank you for your help. I will add links to your modules from the project page.

I would also be glad to see another issue from you where you could describe how creating modules for the providers would make the main module cleaner. I had thoughts on making the providers to be ctools plugins - but then it would require me or another developer to create these plugins - and now there is the auto-discovery mechanism to find new providers implementations.

Thanks
AndyB

dashohoxha’s picture

AndyB,

I intend to create and maintain only one module, namely hybridauth_DrupalOAuth2. You can see it on the patch or from github (it is not yet on drupal.org): https://github.com/dashohoxha/hybridauth_DrupalOAuth2

I have seen in the code of hybridauth that specific configurations are made for Google, for Facebook, etc. This just bloats the code. Instead the hooks can be used to provide specific configurations for specific providers. These hooks are:

  • hook_form_hybridauth_admin_provider_settings_alter(&$form, &$form_state, $form_id)
  • hook_hybridauth_provider_config_alter(&$config, $provider_id)

You can see here how I have used them: https://github.com/dashohoxha/hybridauth_DrupalOAuth2/blob/7.x-1.x/hybri...
So, the idea is to get out of the main code things that are specific to a certain provider.

But there is a problem here. The patch that I submitted includes also icons for the new provider and css code for it (which you have not committed). I totally forgot about them since I have done this patch a long time ago. You have to find a way in your module to make icons and css code provider specific as well. For example, if I store them in the provider module (hybridauth_DrupalOAuth2), the core module (hybridauth) should be able to pick them from there.

duozersk’s picture

Dashamir,

Thank you for your thoughts. I have been thinking about the module architecture since I started working on it - and having a module for every provider looks like much more overkill and bloat than what we have now (provider-specific parts in code). Another note - the module has the auto-discovery mechanism for the providers based on the provider implementation files - and having to code a module for every such provider would also be a step in the wrong direction (imho).

I had an idea to create plugins (ctools) for providers - but again, having to have a plugin for every provider just kills me :)

As for the CSS and icons - I have similar requirements for the Image Editor module (https://drupal.org/project/imageeditor) - and it now uses ctools plugins - you can take a look on how it is done there if you are interested.
In HybridAuth it is done differently - it has icon packs as plugins - and usually "big" sites create their own icon pack plugin (in the module or a theme) according to their design requirements.

Thanks
AndyB

dashohoxha’s picture

AndyB, I understand you.
If you want to keep the configuration code of main providers (FB, Google, etc.) in the core module, that is fine for me. You are the maintainer, so you decide what is better and easier for you.

But I think that you cannot afford to include into the core module the code for special configurations that are needed for all the providers that are out there. An example is the provider DrupalOAuth2, which needs to use special configuration fields for hybridauth. I think you agree with this.

Fortunately, Drupal has a powerful hook system, which allows modules to customize and extend other modules. This is what I have used in the module hybridauth_DrupalOAuth2 to get the job done without needing to ask from you special configurations in the core module (hybridauth) in order to support the provider DrupalOAuth2. Thanks for committing the modifications that make this possible (about config alter etc.)

However the job is not completed because I cannot define a CSS and an icon for this provider, without asking you to add them into the core module. I beleive that this problem can be solved using the hooks and other tools that Drupal provides. Can I try to find a solution for this and then submit a patch about it?

Regards,
Dashamir

duozersk’s picture

Dashamir,

Sure, you can go forward and suggest a way to solve this.
The solution that I see is to use the ctools plugin system (as I did in the Image Editor module). We only need to find a way not to require the plugin to exist, but if it exists - then add the needed css files and call the callbacks if any. Something like this. I will take a shot on it this weekend.

Thanks
AndyB

dashohoxha’s picture

AndyB,

I tried but couldn't find anything that works well.

I think that ctools plugin system makes it easy for themers to create a new set of icons, and if needed, they can include in this set the icon for DrupalOAuth2. Meanwhile I have added some icons to the project: https://github.com/dashohoxha/hybridauth_DrupalOAuth2/tree/7.x-1.x/icons

If you can find a better solution, please let me know.

Thanks,
Dashamir

duozersk’s picture

Status: Closed (fixed) » Fixed

Dashamir,

I have just committed the plugin based functionality - http://drupalcode.org/project/hybridauth.git/commit/dfee54c
I have used your GitHub repository code (the hybridauth_DrupalOAuth2 module) to create plugin for DrupalOAuth2 provider.

The only 2 lines of code that didn't make it into the plugin are:

  $client_id = variable_get('hybridauth_provider_DrupalOAuth2_keys_id', NULL);
  variable_set('hybridauth_provider_DrupalOAuth2_keys_key', $client_id);

as I didn't understand why this is needed.

So now there is no need in the separate module - it is implemented as a plugin.
Please comment if we should change anything else for the DrupalOAuth2 provider to work fine.

Thanks
AndyB

dashohoxha’s picture

AndyB,

Thanks for catching those 2 lines of code. Maybe I have not been sure how the keys_id and keys_key were being used by the library, so I have decided to set both of them. However keys_key is not actually needed. I just tested the latest code of hybridauth (with these 2 lines removed) and it worked well.

So, the good news is that the plugin that you have added for DrupalOAuth2 is working fine (I just tested it).

The other good news is that a separate module also works fine, if you add a css file to it (for the icons) and load the css file on hook_init(), like this:
- https://github.com/dashohoxha/hybridauth_DrupalOAuth2/blob/7.x-1.x/hybri...
- https://github.com/dashohoxha/hybridauth_DrupalOAuth2/blob/7.x-1.x/hybri...
I just tested this solution as well (removed the plugin and installed the module, and it worked, icons were displayed correctly).

However I am not sure how well both of these two solutions work with the plugin icon_pack. I didn't test it, but I think that both of them don't work well. Anyway this should not be a big problem, because a customized theme can always override the default css code of the modules (and the icons as well).

Another drawback of both solutions is that drupal_add_css() is deprecated and is removed in Drupal8 (as far as I know).

In general, I am not familiar with ctool plugins, so I don't see what advantages they have compared to a Drupal module. I think that a Drupal module is designed to be like a plugin unit: you need to add some functionality to an application, you enable a module, and that's it. Why to add extra complexity? (I don't know yet all the rules for building Drupal modules, now I should learn the rules for building ctool plugins as well!). Also, in a Drupal module I can check for the existence of the library provider on which this module depends (the hybridauth provider for DrupalOAuth2), and give instructions for how to get and install it, if needed. (Maybe this can be done on a ctools plugin as well, I don't know.)

So, it is good to have two solutions. I would prefer the one with a module (either as a submodule of hybridauth, or as an independent module), because it seems to me more flexible, more standard solution, and provides better modularity. However the ctools plugin is OK as well. So, keep whichever you prefer.

Regards,
Dashamir

duozersk’s picture

Dashamir,

Thanks for the reply and the time you put into this.
Plugins are better here as you don't need to create as many modules as the number of providers you want to support. You can have one module with as many plugins as you want. Plus, plugins are not loaded on every Drupal bootstrap, they are only loaded when they are actually needed. CSS file - again, with plugins it is added only when it is needed. Plugins are used by the Views module and many others. And plugin system from ctools was reworked and included into Drupal 8 core (as the Views module requires it).

As for the icon_pack compatibility with the CSS file - it is just fine as the CSS file we created contains rules for only the standard icon packs included with the module - any other icon pack won't be affected by these CSS rules.

Thanks
AndyB

dashohoxha’s picture

Status: Fixed » Closed (fixed)