Strategy to authenticate with WildApricot via OAuth2 in OmniAuth.
For more details, read the WildApricot docs: https://gethelp.wildapricot.com/en/articles/200-single-sign-on-service-sso
First start by adding this gem and its dependencies to your Gemfile:
gem 'omniauth', '~> 2.0'
gem "omniauth-rails_csrf_protection"
gem 'omniauth-wild-apricot'
If you need to use the latest HEAD version, you can do so with:
gem 'omniauth-wild-apricot', github: 'rocket-house/omniauth-wild-apricot'
Then bundle install
.
Go to 'https://myWAsite.com/admin/apps/integration/authorized-applications/' then:
- Select 'Authorize Application', then select 'Server Application'
- Name your application
- Select desired permission level: read/write
- Click 'Generate client secret'
- Select 'Authorize users via Wild Apricot single sign-on service'
- Fill in your app's redirect domain
- Take note of the following information:
- Client ID
- Client Secret
Additionally, you'll need your Wild Apricot Account # which can be found on the admin/billing page. You also need your site's url. If you're billing page is 'https://myWAsite.com/admin/billing/' then your site url is 'https://myWAsite.com/'.
The above information can be placed into your project's .env
file (see examples/.env-example
), or set as environment variables in your hosting provider's application settings.
Here's an example for adding the middleware to a Rails app in config/initializers/omniauth.rb
. In this example, we are storing the key variables in the project's .env
file (see examples/.env-example
).
Rails.application.config.middleware.use OmniAuth::Builder do
provider :wild_apricot, ENV['WA_CLIENT_ID'], ENV['WA_CLIENT_SECRET'],
{
account_num: ENV['WA_ACCT_NUM'], # found at: https://myWAsite.com/admin/billing/
site_url: ENV['WA_SITE_URL'], # set to: 'https://myWAsite.com/'
callback_path: '/callback/wild_apricot' # must match your session controller route
}
end
You'll need some routes to handle the callback, and a session controller:
Rails.application.routes.draw do
...
# Routes for OmniAuth
# Wild Apricot uses GET route; use POST for other providers
# post '/auth/:provider/callback', to: 'sessions#create'
get '/callback/:provider', to: 'sessions#create'
get '/logout', to: 'sessions#destroy', as: :logout
end
For your views you can login using:
<%# omniauth-wild-apricot 0.1.x uses OmniAuth 2 and requires using HTTP Post to initiate authentication: %>
<%= link_to "Sign in with Wild Apricot", '/auth/wild_apricot', method: :post %>
<%# when using rails 7.0+ with turbo-links, you'll need to bypass turbo; you *must* use botton_to %>
<%= button_to "Sign in with Wild Apricot", '/auth/wild_apricot', method: :post, 'data-turbo': false %>
Note: the route /auth/wild_apricot
comes included with omniauth (/auth/:provider
)
If you are using Devise then it will look like this:
Devise.setup do |config|
# other stuff...
config.omniauth :wild_apricot, ENV['WA_CLIENT_ID'], ENV['WA_CLIENT_SECRET'],
{
account_num: ENV['WA_ACCT_NUM'], # found at: https://myWAsite.com/admin/billing/
site_url: ENV['WA_SITE_URL'] # set to: 'https://myWAsite.com/'
}
# other stuff...
end
NOTE: If you are using this gem with devise with above snippet in config/initializers/devise.rb
then do not create config/initializers/omniauth.rb
which will conflict with devise configurations.
Then add the following to 'config/routes.rb' so the callback routes are defined.
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
Make sure your model is omniauthable. Generally this is "/app/models/user.rb"
devise :omniauthable, omniauth_providers: [:wild_apricot]
Then make sure your callbacks controller is setup.
# app/controllers/users/omniauth_callbacks_controller.rb:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def wild_apricot
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Wild Apricot'
sign_in_and_redirect @user, event: :authentication
else
session['devise.wild_apricot_data'] = request.env['omniauth.auth'].except('extra') # Removing extra as it can overflow some session stores
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end
end
and bind to or create the user
def self.from_omniauth(access_token)
data = access_token.info
user = User.where(email: data['email']).first
# Uncomment the section below if you want users to be created if they don't exist
# unless user
# user = User.create(name: data['name'],
# email: data['email'],
# password: Devise.friendly_token[0,20]
# )
# end
user
end
For your views you can login using:
<%# omniauth-wild-apricot 0.1.x uses OmniAuth 2 and requires using HTTP Post to initiate authentication: %>
<%= link_to "Sign in with Wild Apricot", '/auth/wild_apricot', method: :post %>
<%# when using rails 7.0+ with turbo-links, you'll need to bypass turbo; you *must* use botton_to %>
<%= button_to "Sign in with Wild Apricot", '/auth/wild_apricot', method: :post, 'data-turbo': false %>
An overview is available at https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
Here's an example of an authentication hash available in the callback by accessing request.env['omniauth.auth']
:
{
"provider":"wild_apricot",
"uid":"50152421",
"info":{
"email":"anita.borg@example.com",
"first_name":"Anita",
"last_name":"Borg",
"name":"Anita Borg"
},
"credentials":{
"token":"thaXd6xjo4qpQnwraebbGA40vzg-",
"refresh_token":"rt_2023-09-02_VguuDxHFznplhQk1YaOtNrxcgyk-",
"expires_at":1693689961,
"expires":true
},
"extra":{
"raw_info":{
"AdministrativeRoleTypes":["AccountAdministrator"],
"FirstName":"Anita",
"LastName":"Anita",
"Email":"anita.borg@example.com",
"DisplayName":"Anita, Anita",
"PasswordExpiration":"2024-08-17T05:42:03+00:00",
"MembershipLevel":{
"Id":1253690,
"Url":"https://api.wildapricot.org/v2/accounts/321456/MembershipLevels/1253690",
"Name":"FREE"
},
"Status":"Active",
"Id":50152421,
"Url":"https://api.wildapricot.org/v2/accounts/321456/Contacts/50152421",
"IsAccountAdministrator":true,
"TermsOfUseAccepted":true
}
}
}
Just set the full_host
in OmniAuth based on the Rails.env.
# config/initializers/omniauth.rb
OmniAuth.config.full_host = Rails.env.production? ? 'https://domain.com' : 'http://localhost:3000'
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Copyright (c) 2023 by Fred Zirdung
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.