OmniAuth Central ID
This gem offers OmniAuth strategy for Central ID service.
Usage
First start by adding this gem to your Gemfile:
gem "omniauth-centralid"Next, let OmniAuth know about provider. In a Rails app, create an initializer for instance config/initializers/omniauth.rb with:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :centralid, "CENTRALID_KEY", "CENTRALID_SECRET"
endYou can override config, like
Rails.application.config.middleware.use OmniAuth::Builder do
provider :centralid, "CENTRALID_KEY", "CENTRALID_SECRET",
client_options: {
site: "http://localhost:3000"
}
endIn your routes file:
get "auth/:provider/callback", to: "oauths#create"
get "auth/failure", to: "oauths#failure" # optional (to override default redirect behaviour)Add a controller (oauths_controller.rb as defined in your routes)
# sends the user on a trip to the provider,
# and after authorizing there back to the callback url.
def create
auth = request.env["omniauth.auth"]
user = User.find_by(provider: auth["provider"], uid: auth["uid"]).try(:user) || User.create_with_omniauth(auth)
# Sign in the user
session[:user_id] = user.id
redirect_to dashboard_path
end
# By default, it is supposed to raise an exception in development mode
# and redirect otherwise. Override if needed.
def failure
redirect_to login_path
endAnd finally add a create_with_omniauth method in your user model.
private
def self.create_with_omniauth(auth)
user = create! do |user|
user.full_name = auth["info"]["name"]
user.email = auth["info"]["email"]
user.password = SecureRandom.hex
user.authentications.new(
provider: auth["provider"],
uid: auth["uid"]
)
end
user
endContributing
Run tests with rake test