How do I login to Discourse with my own Oauth2 Provider?
I have an app as a Oauth2 provider, and other apps can use omniauth-oauth2 or my custom gem to get the info and sign up the user. Can Discourse do so? or admin provides App ID, App Secret and Provider URL then uses these to log in?
I am developing a first-of-its-kind academic social network for a university and I have my eye on Discourse. I’ll need OAuth to authenticate users against Blackboard, though.
So I’m tossing in my +1 for OAuth. Hopefully it can be supported natively in Discourse.
Discourse has support for custom OAuth2 authentication built in, here’s an example of how you can add a custom auth mechanism using OAuth2 via a plugin:
require 'auth/oauth2_authenticator'
require 'omniauth-oauth2'
class HummingbirdAuthenticator < ::Auth::OAuth2Authenticator
CLIENT_ID = '...'
CLIENT_SECRET = '...'
def register_middleware(omniauth)
omniauth.provider :hummingbird, CLIENT_ID, CLIENT_SECRET
end
end
class OmniAuth::Strategies::Hummingbird < OmniAuth::Strategies::OAuth2
# Give your strategy a name.
option :name, "hummingbird"
# This is where you pass the options you would pass when
# initializing your consumer from the OAuth gem.
option :client_options, site: 'http://hummingbird.me'
# These are called after authentication has succeeded. If
# possible, you should try to set the UID without making
# additional calls (if the user id is returned with the token
# or as a URI parameter). This may not be possible with all
# providers.
uid { raw_info['id'].to_s }
info do
{
:name => raw_info['name'],
:email => raw_info['email']
}
end
extra do
{
'raw_info' => raw_info
}
end
def raw_info
@raw_info ||= access_token.get('/oauth/me.json').parsed
end
end
auth_provider :title => 'Sign in with Hummingbird account',
:message => 'Log in using your Hummingbird account. (Make sure your popup blocker is disabled.)',
:frame_width => 920,
:frame_height => 800,
:authenticator => HummingbirdAuthenticator.new('hummingbird', trusted: true,
auto_create_account: true)
Just wanted to note that the solution does not work for hosted accounts that don’t have access to modify the source at the moment, exposing the Oauth2 configuration through the UI would be a really nice thing!
I think we could try and create a default “generic” oauth plugin. Sam is right that each one I’ve done so far is slightly different but we could take a stab at extracting those differences into settings that people could configure.
I would need some example “plain” oauth sites to test out.
I took a stab at this this week and managed to come up with a Basic OAuth2 plugin that works. The caveat is you need to have a JSON endpoint on your server so that we can obtain other information about the user.
I tested it with SoundCloud as a provider, and it worked great. I’d love other people to give it a whirl and let me know feedback and I’m sure as we try it out with more providers we’ll find changes and configuration options that will be required.
thanks for the OAuth2 Provider. it’s working like a charm!
i have one question, which google couldn’t answer to me.
is it possible to combine the OAuth2 Plugin with the enable_sso plugin?
right now when i click the “login with provider” button it connects to our OAuth Provider and grabs all the information and pre-fills the registration form with the user data (like on meta.discourse.com, when i login with GitHub)
how can i skip the registration and directly create the account? so that the user doesn’t have to register again?
I ended up not getting a contract for this project, so I didn’t pursue it further, but I think it could absolutely be done. Have you tried out the Discourse oAuth 2 plugin?
Is this still the case? I’ve been trying to close the same gap as @beanieboi described for so long to improve the experience and have a seamless experience for my customers but seems like there is no way around it. Any other suggestion to accomplish this?