class OmniTokenApp

This is the Sinatra application for getting tokens.

Constants

PROVIDER_DIR

configuration folder for various OAuth providers

VERSION

version number

Private Instance Methods

get_provider(name) click to toggle source

Gets the loaded provider by name

# File omnitoken_app.rb, line 60
def get_provider(name)
  provider = nil
  @providers.each do |p|
    provider = p if p[:id].eql?(name.to_sym)
  end
  provider
end
load_providers() click to toggle source

Loads providers from the configuration files

# File omnitoken_app.rb, line 69
def load_providers
  @providers = []
  Dir.glob(File.join(PROVIDER_DIR, '*.yml')) do |file|
    name = File.basename(file, '.yml').to_sym
    # require ruby files for the strategy
    strategy = require_strategy(name)
    @providers << { id: name,
                    display_name: OmniAuth::Utils.camelize(name),
                    path: "#{OmniAuth.config.path_prefix}/#{name}",
                    args: read_strategy_arg(strategy, file),
                    klass: strategy
                  }
  end

  # set the strategies as a middleware
  use_strategy_middleware
end
read_strategy_arg(strategy, file) click to toggle source

Reads the arguments for a strategy from the configuration file.

# File omnitoken_app.rb, line 98
def read_strategy_arg(strategy, file) 
  args = []
  yaml = YAML::load(File.open(file))
  strategy.args.each do |arg|
    if yaml[arg.to_s].nil?
      fail "The #{arg} argument is not provided in #{file}."
    else
      args << yaml[arg.to_s]
    end
  end
  args
end
require_strategy(name) click to toggle source

Requires the ruby file for a strategy.

# File omnitoken_app.rb, line 112
def require_strategy(name)
  begin
    require "omniauth-#{name}"
    OmniAuth::Strategies.const_get(OmniAuth::Utils.camelize(name))
  rescue LoadError => e
    fail e, "Could not find matching strategy for #{name}. You may need to install an additional gem (such as omniauth-#{name})."
  end
end
use_strategy_middleware() click to toggle source

Sets the OmniAuth builder as a middleware

# File omnitoken_app.rb, line 88
def use_strategy_middleware
  providers = @providers
  self.class.use OmniAuth::Builder do
    providers.each do |p|
      provider p[:id], *p[:args]
    end
  end
end