2

I'm using Sorcery for Authentication in a Rails app. I have it set up fine, I can add users by username/password or via external auth (only using twitter at the moment). However, one thing I have not been able to figure out is how to add authentications to existing users, ie. a user created by username who might want to later add their twitter account as a means of logging in.

I tried adding a method to the External module to enable this functionality...

module Sorcery
    module Controller
        module Submodules
            module External
                module InstanceMethods
                    protected

                    def add_provider_to_user(provider)
                        provider_name = provider.to_sym
                        provider = Config.send(provider_name)
                        user_hash = provider.get_user_hash
                        config = user_class.sorcery_config

                        user = current_user.send(config.authentications_class.to_s.downcase.pluralize).build(config.provider_uid_attribute_name => user_hash[:uid], config.provider_attribute_name => provider)
                        user.save(:validate => false)

                        return user
                    end
                end
            end
        end
    end
end

... but that didn't work. I don't seem to be able to get the Config class to behave as it does in the internal methods, Config.send('twitter') always returns nil instead of a provider.

There are no public methods for this in Sorcery. Has anyone figured out how to patch this functionality into an app?

1
  • Just a note, I'd be equally interested in a solution that worked through my authentications controller as one that "patched" the sorcery external module. The example above is just my attempt based on some patch requests I saw on github.
    – Andrew
    Apr 30, 2012 at 16:51

2 Answers 2

0

I recently used https://github.com/rcarter/sorcery to adds the ability to link a provider to already authenticated user. Does this do what you're looking for?

I should warn though that it attempts to store access_token in your User model...you'd probably want to remove that. See changes here: https://github.com/rcarter/sorcery/commit/f3984749659bceb7f7438cae8ea95ba5a415d1e2

0

This is a very old question, but I just wanted to add my solution for doing this with Microsoft IDs. I needed to have users login using Microsoft SSO. I wanted to be able to create the user accounts and have them set up to login using their Microsoft credentials. I am using Ruby 2.7, rails 5.1 with Sorcery 0.15. I used the external providers example code in the sorcery wiki to start with https://github.com/Sorcery/sorcery/wiki/External

On the Azure Active Directory side:

  1. Add your web app as a registered Application
  2. Download the .csv of your users and use that information to create your users profiles in your Users table. Their email will be their username.
  3. Create a client secret in Azure under your registered app.
  4. Add a Web Redirect URI for your app like http://localhost:32795/oauth/callback?provider=microsoft

In your app:

  1. Create the users' authentications entries in the Authentications table. Each user will have their user_id from the Users table, their uid which is the "id" in the downloaded .csv file (in Azure this is their object ID), and the provider is microsoft.
  2. Add a route for MSFT callbacks get "/oauth/callback/microsoft" => "oauths#callback" # for microsoft
  3. Set up the external provider in

app/config/initializers/sorcery.rb

config.microsoft.key = <your Application(client)ID>
config.microsoft.secret = <client secret you generated in Azure>
config.microsoft.callback_url = "http://localhost:3000/oauth/callback?provider=microsoft"
config.microsoft.user_info_mapping = {:email => "userPrincipalName", :username => "userPrincipalName"}
config.microsoft.scope = "openid email https://graph.microsoft.com/User.Read"

Your application will now use MSFT ID for login. Note that even if you log out in your app, when you click the link to log in with MSFT it will automatically log you in because your MSFT credentials are saved by the browser. If you go to your MSFT account and log out, you will need to set a logout URI in Azure that will tell your app to also log out. Or you could set up your app to get rid of the office cookies when you logout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.