Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to initialize Auth0Api #147

Closed
reckerswartz opened this issue Nov 29, 2018 · 1 comment
Closed

Unable to initialize Auth0Api #147

reckerswartz opened this issue Nov 29, 2018 · 1 comment
Labels
Milestone

Comments

@reckerswartz
Copy link

Description

Unable to initialize Auth0Api
for obtain_access_token

tried various options include eg. Auth0Api.initialize(options = {})
Auth0Api.new(options = {})

Environment

  • Ruby Auth0 version: 4.5.0
  • Ruby version: 2.4.4
  • Rails version (if applicable): 5.1
@joshcanhelp
Copy link
Contributor

Sorry for the trouble here @reckerswartz. We're working on improving the composition of these modules now but right now, to access that method, you can do one of two things.

You can include the necessary modules in your class and define the necessary client_headers method and class properties in your class like this (using a controller as an example):

class YourController < ApplicationController

  before_action :set_auth0_vars, only: %i[process_access_token]

  include Auth0::Api::AuthenticationEndpoints
  include Auth0::Mixins::HTTPProxy

  def token_page
    render 'token_page'
  end

  def process_access_token
    token_params = params[:token_params]
    response = obtain_access_token(token_params[:access_token], token_params[:connection])
    # ...
  end

  # ...

  private

  def set_auth0_vars
    @domain = ENV['AUTH0_RUBY_DOMAIN']
    @client_id = ENV['AUTH0_RUBY_CLIENT_ID']
    @client_secret = ENV['AUTH0_RUBY_CLIENT_SECRET']
    @base_uri = "https://#{@domain}"
    @headers = client_headers
  end

  def client_headers
    client_info = JSON.dump(name: 'ruby-auth0', version: Auth0::VERSION)
    {
      'Content-Type' => 'application/json',
      'User-Agent' => "Ruby/#{RUBY_VERSION}",
      'Auth0-Client' => Base64.urlsafe_encode64(client_info)
    }
  end
end

Or you can use the complete SDK client but you'll have to pass in a dummy Management token to get it to instantiate. Similar example as the above:

require 'auth0'
class YourController < ApplicationController

  def token_page
    render 'token_page'
  end

  def process_access_token
    token_params = params[:token_params]
    response = auth0_client.obtain_access_token(token_params[:access_token], token_params[:connection])
    # ...
  end

  # ...

  private

  def auth0_client
    @client_id = ENV['AUTH0_RUBY_CLIENT_ID']
    @client_secret = ENV['AUTH0_RUBY_CLIENT_SECRET']
    @domain = ENV['AUTH0_RUBY_DOMAIN']

    @auth0_client ||= Auth0::Client.new(
      token: 'TOKEN',
      client_id: @client_id,
      client_secret: @client_secret,
      domain: @domain
    )
  end
end

Regardless of what method you use, note that obtain_access_token is deprecated in favor of api_token in the master branch (releasing this week).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants