Sha256: 5ce2716644e88fa4db4898ac5d4a733b46b4f7acc2d393753e9307ec4d8e235f

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

module Transferwise
  class OAuth
    attr_accessor :access_token

    def initialize(client_id, client_secret)
      @client_id, @client_secret = client_id, client_secret
    end

    # Get the OAuth 2 client
    def client
      @client ||= ::OAuth2::Client.new(
        @client_id,
        @client_secret,
        { site: Transferwise.api_base,
          auth_scheme: :basic_auth
        }
      )
    end
    
    def authorize_client
      @authorize_client ||= ::OAuth2::Client.new(
        @client_id,
        @client_secret,
        { site: Transferwise.authorization_base,
          auth_scheme: :basic_auth
        }
      )
    end

    # Get the url to redirect a user to, pass the redirect_url you want the user
    # to be redirected back to.
    def authorize_url(redirect_url)
      authorize_client.auth_code.authorize_url({redirect_uri: redirect_url})
    end

    # Get the access token. You must pass the exact same redirect_url passed
    # to the authorize_url method
    def get_access_token(code, redirect_url)
      @access_token ||= client.auth_code.get_token(code, redirect_uri: redirect_url)
    end

    # This method is used to refresh the access token before it expires
    def refresh_token(access_token, opts = {})
      OAuth2::AccessToken.new(client, access_token, opts).refresh!
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
transferwise-rb-0.3.0 lib/transferwise/oauth.rb
transferwise-0.2.6 lib/transferwise/oauth.rb
transferwise-0.2.5 lib/transferwise/oauth.rb
transferwise-0.2.4 lib/transferwise/oauth.rb
transferwise-0.2.3 lib/transferwise/oauth.rb