Class: Warden::GitHub::Config

Inherits:
Object
  • Object
show all
Includes:
Mixins::Common
Defined in:
lib/warden/github/config.rb

Overview

This class encapsulates the configuration of the strategy. A strategy can be configured through Warden::Manager by defining a scope’s default. Thus, it is possible to use the same strategy with different configurations by using multiple scopes.

To configure a scope, use #scope_defaults inside the Warden::Manager config block. The first arg is the name of the scope (the default is :default, so use that to configure the default scope), the second arg is an options hash which should contain:

- :strategies : An array of strategies to use for this scope. Since this
                strategy is called :github, include it in the array.

- :config :     A hash containing the configs that are used for OAuth.
                Valid parameters include :client_id, :client_secret,
                :scope, :redirect_uri. Please refer to the OAuth
                documentation of the GitHub API for the meaning of these
                parameters.

                If :client_id or :client_secret are not specified, they
                will be fetched from ENV['GITHUB_CLIENT_ID'] and
                ENV['GITHUB_CLIENT_SECRET'], respectively.

                :scope defaults to nil.

                If no :redirect_uri is specified, the current path will
                be used. If a path is specified it will be appended to
                the request host, forming a valid URL.

Examples

use Warden::Manager do |config|
  config.failure_app = BadAuthentication

  # The following line doesn't specify any custom configurations, thus
  # the default scope will be using the implict client_id,
  # client_secret, and redirect_uri.
  config.default_strategies :github

  # This configures an additional scope that uses the github strategy
  # with custom configuration.
  config.scope_defaults :admin, :config => { :client_id => 'foobar',
                                             :client_secret => 'barfoo',
                                             :scope => 'user,repo',
                                             :redirect_uri => '/admin/oauth/callback' }
end

Constant Summary collapse

BadConfig =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, warden_scope) ⇒ Config

Returns a new instance of Config.



58
59
60
61
# File 'lib/warden/github/config.rb', line 58

def initialize(env, warden_scope)
  @env = env
  @warden_scope = warden_scope
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



56
57
58
# File 'lib/warden/github/config.rb', line 56

def env
  @env
end

#warden_scopeObject (readonly)

Returns the value of attribute warden_scope.



56
57
58
# File 'lib/warden/github/config.rb', line 56

def warden_scope
  @warden_scope
end

Instance Method Details

#client_idObject



63
64
65
66
67
68
# File 'lib/warden/github/config.rb', line 63

def client_id
  custom_config[:client_id] ||
    deprecated_config(:github_client_id) ||
    ENV['GITHUB_CLIENT_ID'] ||
    fail(BadConfig, 'Missing client_id configuration.')
end

#client_secretObject



70
71
72
73
74
75
# File 'lib/warden/github/config.rb', line 70

def client_secret
  custom_config[:client_secret] ||
    deprecated_config(:github_secret) ||
    ENV['GITHUB_CLIENT_SECRET'] ||
    fail(BadConfig, 'Missing client_secret configuration.')
end

#redirect_uriObject



77
78
79
80
81
82
83
84
# File 'lib/warden/github/config.rb', line 77

def redirect_uri
  uri_or_path =
    custom_config[:redirect_uri] ||
    deprecated_config(:github_callback_url) ||
    request.path

  normalized_uri(uri_or_path).to_s
end

#scopeObject



86
87
88
# File 'lib/warden/github/config.rb', line 86

def scope
  custom_config[:scope] || deprecated_config(:github_scopes)
end

#to_hashObject



90
91
92
93
94
95
# File 'lib/warden/github/config.rb', line 90

def to_hash
  { :client_id     => client_id,
    :client_secret => client_secret,
    :redirect_uri  => redirect_uri,
    :scope         => scope }
end