Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove ability to specify site through GET params

  • Loading branch information...
commit 5a72c343c29d2a8dc8c7c17ecd1b250f7bdf55fa 1 parent 62a3567
@nashby authored
Showing with 14 additions and 27 deletions.
  1. +2 −14 README.md
  2. +12 −13 lib/omniauth/strategies/stackexchange.rb
View
16 README.md
@@ -22,25 +22,13 @@ Once these are in, you need to add the following to your `config/initializers/om
```ruby
Rails.application.config.middleware.use OmniAuth::Builder do
- provider :stackexchange, "client_id", "client_secret", public_key: "key"
+ provider :stackexchange, "client_id", "client_secret", public_key: "key", site: 'stackoverflow'
end
```
You will obviously have to put in your client_id, client_secret and public_key, which you get when you register your app with StackExchange (they call them Client Id, Client Secret and Key).
-You will also need to specify a site api key to uniquely identify the StackExchange site you wish to authenticate against. A list of valid site api keys can be found at https://api.stackexchange.com/docs/sites. You have two options for passing the site api key, the first by passing the key along as a query string parameter on the url you use to call into StackExchange as follows:
-
-```ruby
-/auth/stackexchange?site=stackoverflow
-```
-
-If a value is not passed via the url, the gem uses the one specified in the `omniauth.rb` file as follows where you will have to replace site_api_key with the value you the site you wish to authenticate against (e.g. `stackoverflow` or `superuser`):
-
-```ruby
-Rails.application.config.middleware.use OmniAuth::Builder do
- provider :stackexchange, "client_id", "client_secret", public_key: "key", site: 'site_api_key'
-end
-```
+You will also need to specify a site option to uniquely identify the StackExchange site (e.g. `stackoverflow` or `superuser`) you wish to authenticate against. A list of valid site api keys can be found at https://api.stackexchange.com/docs/sites. It's `stackoverflow` by default.
Now just follow the README at: https://github.com/intridea/omniauth
View
25 lib/omniauth/strategies/stackexchange.rb
@@ -4,6 +4,7 @@ module OmniAuth
module Strategies
class StackExchange < OmniAuth::Strategies::OAuth2
class NotRegisteredForStackExchangeSiteError < StandardError; end
+
option :client_options, {
:site => 'https://api.stackexchange.com/2.0',
:authorize_url => 'https://stackexchange.com/oauth',
@@ -24,7 +25,9 @@ def request_phase
{
'nickname' => raw_info['display_name'],
'image' => raw_info['profile_image'],
- 'url' => raw_info['link']
+ 'urls' => {
+ site => raw_info['link']
+ }
}
end
@@ -33,30 +36,26 @@ def request_phase
end
def raw_info
- #first check to see if a specific site was specified in the request
- site_id = request.env['omniauth.params']['site'] ? request.env['omniauth.params']['site'] : nil
-
- #if no site was specified in the request, pull from omniauth.rb
- site_id = options.site ? options.site : nil if site_id.nil?
+ @raw_info ||= access_token.get('me', :params => params).parsed['items'].first
- #raise an error if no site was provided
- if site_id.nil?
- raise 'At least one site api key must be provided in omniauth.rb or passed as a parameter'
+ unless @raw_info
+ raise NotRegisteredForStackExchangeSiteError, "User is not registered for requested StackExchange site (#{site})"
end
- @raw_info ||= access_token.get('me', :params => {:site => site_id, :access_token => access_token.token, :key => options.public_key}).parsed['items'].first
- if @raw_info.nil?
- raise NotRegisteredForStackExchangeSiteError, 'User is not registered for requested StackExchange site (' + site_id + ')'
- end
@raw_info
end
def params
{
+ :site => site,
:access_token => access_token.token,
:key => options.public_key
}
end
+
+ def site
+ options.site || 'stackoverflow'
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.