|
| 1 | +module Sorcery |
| 2 | + module Providers |
| 3 | + # This class adds support for OAuth with salesforce.com. |
| 4 | + # |
| 5 | + # config.salesforce.key = <key> |
| 6 | + # config.salesforce.secret = <secret> |
| 7 | + # ... |
| 8 | + # |
| 9 | + class Salesforcesandbox < Base |
| 10 | + include Protocols::Oauth2 |
| 11 | + |
| 12 | + attr_accessor :auth_url, :token_url, :scope |
| 13 | + |
| 14 | + def initialize |
| 15 | + super |
| 16 | + |
| 17 | + @site = 'https://test.salesforce.com' |
| 18 | + @auth_url = '/services/oauth2/authorize' |
| 19 | + @token_url = '/services/oauth2/token' |
| 20 | + end |
| 21 | + |
| 22 | + def get_user_hash(access_token) |
| 23 | + user_info_url = access_token.params['id'] |
| 24 | + response = access_token.get(user_info_url) |
| 25 | + |
| 26 | + auth_hash(access_token).tap do |h| |
| 27 | + h[:user_info] = JSON.parse(response.body) |
| 28 | + h[:uid] = h[:user_info]['user_id'] |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + # calculates and returns the url to which the user should be redirected, |
| 33 | + # to get authenticated at the external provider's site. |
| 34 | + def login_url(_params, _session) |
| 35 | + authorize_url(authorize_url: auth_url) |
| 36 | + end |
| 37 | + |
| 38 | + # tries to login the user from access token |
| 39 | + def process_callback(params, _session) |
| 40 | + args = {}.tap do |a| |
| 41 | + a[:code] = params[:code] if params[:code] |
| 42 | + end |
| 43 | + |
| 44 | + get_access_token(args, token_url: token_url, token_method: :post) |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | +end |
0 commit comments