# Slooob OAuth2 Strategy for OmniAuth [![Build Status](https://travis-ci.org/slooob/omniauth-slooob.svg)](https://travis-ci.org/slooob/omniauth-slooob) [![Gem Version](https://badge.fury.io/rb/omniauth-slooob.svg)](https://badge.fury.io/rb/omniauth-slooob) **Built on Slooob Identity API v1** Strategy to authenticate with Slooob via OAuth2 in OmniAuth. Get your API key at: https://developer.slooob.com/dashboard/apps. Note the Client ID and the Client Secret. --- ## Table of Contents * [Installation](#installation) * [Slooob API Setup](#slooob-api-setup) * [Usage](#usage) * [Configuration](#configuration) * [Auth Hash](#auth-hash) * [Devise](#devise) * [Testing](#testing) * [Test Coverage](#test-coverage) * [Contributing](#contributing) * [Contributors](#contributors) * [Semantic Versioning](#semantic-versioning) * [License](#license) --- **Note**: You generate separate keys for development (sandbox) and production (live) with each application you register. Use the [config Gem](https://rubygems.org/gems/config) to organize your keys and keep them safe. For more details we encourage you to read the Slooob Auth API docs: https://developer.slooob.com/docs/api/identity/v1 ## Installation Add to your `Gemfile`: ```ruby gem 'omniauth-slooob' ``` And then execute: $ bundle Or install it yourself as: $ gem install omniauth-slooob ## Slooob API Setup * Go to https://developer.slooob.com/dashboard/apps * Select your app. * Open the Manage APIs tab. * Make sure 'Identity API' is enabled. * Open the Credentials tab. * Set `/users/auth/slooob/callback` as Return URL for each 'sandbox' and 'live'. * Now Save. It may take a few minutes for changes to take effect. ## Usage Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`: ```ruby Rails.application.config.middleware.use OmniAuth::Builder do provider :slooob, ENV['SLOOOB_KEY'], ENV['SLOOOB_SECRET'] end ``` You can now access the OmniAuth Slooob OAuth2 URL: `/auth/slooob` For more examples please take a look at `examples/omniauth.rb` **Note**: While developing your application, if you change the scope in the initializer you will need to restart your app server. Remember that either the 'email' or 'public' scope is required! ### Configuration You can configure several options, which you pass in to the `provider` method via a hash: * `scope`: A comma-separated list of permissions you want to request from the user. See the [Identity API docs](https://developer.slooob.com/docs/api/identity/v1/#permissions) for a full list of available permissions. Caveats: * The `email` and `public` scopes are used by default. By defining your own `scope`, you override these defaults, but Slooob requires at least one of `email` or `public`, so make sure to add at least one of them to your scope! * `redirect_uri`: Override the redirect_uri used by the gem. * `incremental_authorization`: If this is provided with the value true, and the authorization request is granted, the authorization will include any previous authorizations granted to this user/application combination for other scopes. See Slooob's [Incremental Authorization](https://developer.slooob.com/docs/api/identity/v1/#incremental-authorization) for additional details. * `image_size`: The size of the user's profile picture. Possible values: * `raw`: 800x800px * `big`: 350x350px * `medium`: 100x100px * `small`: 75x75px * `tiny`: 50x50px * Defaults to `raw`. ### Auth Hash Here's an example of an authentication hash available in the callback by accessing `request.env['omniauth.auth']`: ```ruby { provider: "slooob", uid: "bathjJwvdhKjgfgh8Jd745J7dh5Qkgflbnczd65dfnw", info: { email: "example@example.com", username: "jony", name: "John Smith", first_name: "John", last_name: "Smith", description: "I love cats.", image: "https://cdn.slooob.com/raw", location: nil, confirmed: true, urls: { website: "https://jonyscats.com", slooob: "https://slooob.com/a/jony" } }, credentials: { token: "token", refresh_token: "refresh_token", expires_at: 1355082790, expires: true }, extra: { raw_info: { username: "jony", name: "John Smith", first_name: "John", last_name: "Smith", description: "I love cats.", avatar: { raw: "https://cdn.slooob.com/raw", big: "https://cdn.slooob.com/big", medium: "https://cdn.slooob.com/medium", small: "https://cdn.slooob.com/small", tiny: "https://cdn.slooob.com/tiny" }, location: nil, latitude: nil, longitude: nil, phone: nil, birthday: nil, website: "https://jonyscats.com", profile: "https://slooob.com/a/jony", plan: "basic", email_confirmed: true, verified: false, lang: "en", status: "online", id: 234345676578969343456, created_at: 1259222400, contacts_count: 2, followers_count: 12, following_count: 36, favorites_count: 169, watching_count: 532 } } } ``` For more details see the Slooob [List Of Attributes](https://developer.slooob.com/docs/api/identity/v1/#attributes). ### Devise First define your application id and secret in `config/initializers/devise.rb`. Do not use the snippet mentioned in the [Usage](#usage) section. ```ruby require 'omniauth-slooob' config.omniauth :slooob, ENV['SLOOOB_CLIENT_ID'], ENV['SLOOOB_CLIENT_SECRET'] ``` Then add the following to 'config/routes.rb' so the callback routes are defined. ```ruby devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' } ``` Make sure your model is `omniauthable`. In general this is defined in `app/models/user.rb` ```ruby devise :omniauthable, omniauth_providers: [:slooob] ``` Then make sure your callbacks controller is setup. ```ruby class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def paypal_oauth2 # 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: 'Slooob' sign_in_and_redirect @user, event: :authentication else session['devise.slooob_data'] = request.env['omniauth.auth'] redirect_to new_user_registration_url end end end ``` And bind to or create the user ```ruby 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: ```erb <%= link_to 'Sign in with Slooob', user_slooob_omniauth_authorize_path %> <%# Devise prior 4.1.0: %> <%= link_to 'Sign in with Slooob', user_omniauth_authorize_path(:slooob) %> ``` An overview is available at https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview --- ## Testing Tests are written with Shoulda. Tests are run using rake. 1. Fork this repository 2. Clone your forked git locally 3. Install dependencies `$ bundle install` 4. Run tests `$ rake test` ### Test Coverage Test coverage can be calculated using SimpleCov. Make sure you have the [simplecov gem](https://github.com/colszowka/simplecov) installed. 1. Uncomment SimpleCov in the Gemfile 2. Uncomment the relevant section in `test/test_helper.rb` 3. Run tests `$ rake test` --- ## Contributing We hope that you will consider contributing to omniauth-slooob. Please read this short overview for some information about how to get started: [Learn more about contributing to this repository](https://github.com/slooob/omniauth-slooob/blob/master/CONTRIBUTING.md), [Code of Conduct](https://github.com/slooob/omniauth-slooob/blob/master/CODE_OF_CONDUCT.md) ### Contributors Give the people some :heart: who are working on this project. See them all at: https://github.com/slooob/omniauth-slooob/graphs/contributors ### Semantic Versioning omniauth-slooob follows Semantic Versioning 2.0 as defined at http://semver.org. ## License MIT License Copyright (c) 2017 Slooob 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.