Sha256: d249be301820a15d760db385bd8418db915a3636ea1b0637b8e5501f03a70a33

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'omniauth/strategies/oauth2'

module OmniAuth
  module Strategies
    class Wix < OmniAuth::Strategies::OAuth2
      option :name, 'wix'

      option :client_options, {
        :authorize_url => 'https://www.wix.com/installer/install',
        :token_url => 'https://www.wix.com/oauth/access.json'
      }

      option :provider_ignores_state, true

      uid { request.params["instanceId"] }

      credentials do
        hash = {"token" => access_token.token}
        hash["refresh_token"] = access_token.refresh_token if access_token.refresh_token
        hash["expires_at"] = access_token.expires_at if access_token.expires?
        hash["expires"] = access_token.expires?
        hash
      end
      
      def client
        ::OAuth2::Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options)) do |b|
          b.request :json
          b.adapter Faraday.default_adapter
        end
      end

      def authorize_params
        super.tap do |params|
          params["redirectUrl"] = callback_url
          params["appId"] = options[:client_id]
          params["token"] = request.params["token"]
        end
      end
      
      def callback_url
        full_host + script_name + callback_path
      end

      def build_access_token
        verifier = request.params["code"]
        params = { :redirect_uri => callback_url }.merge(token_params.to_hash(:symbolize_keys => true).merge({ headers: { 'Content-Type' => 'application/json' } }))
        client.auth_code.get_token(verifier, params, deep_symbolize(options.auth_token_params))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
omniauth-wix-0.1.3 lib/omniauth/strategies/wix.rb