Skip to content

Commit

Permalink
swap out openstreetmap oauth1 to use new oauth2.0 refs #277
Browse files Browse the repository at this point in the history
  • Loading branch information
timwaters committed May 27, 2024
1 parent 01240d3 commit a68d037
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ gem 'devise', ">= 4.7"
gem 'devise-encryptable'

gem 'oauth', ">= 0.5.8"
gem 'oauth2'
gem 'omniauth-oauth2'

gem 'omniauth-twitter'
gem 'omniauth-osm'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,11 @@ DEPENDENCIES
mocha
nokogiri (>= 1.10.10)
oauth (>= 0.5.8)
oauth2
omniauth-facebook
omniauth-github
omniauth-mediawiki
omniauth-oauth2
omniauth-osm
omniauth-rails_csrf_protection (~> 0.1.2)
omniauth-twitter
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def osm
redirect_to root_path
end
end

def osm_oauth2
request.env["omniauth.auth"]["provider"] = "osm" if request.env["omniauth.auth"]["provider"] == "osm_oauth2"

osm()
end

def mediawiki
@user = User.find_for_mediawiki_oauth(request.env["omniauth.auth"], current_user)
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class User < ActiveRecord::Base
# :encryptable for custom authentication methods
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [ :osm, :facebook, :github]
:omniauthable, :omniauth_providers => [:osm_oauth2, :facebook, :github]

acts_as_token_authenticatable

Expand Down
2 changes: 1 addition & 1 deletion app/views/devise/shared/_links.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<div class="omniauth_links">
<%= t('.social_sign_in', :action => sign_action) %> <br />
<span class="osm">
<%= link_to "#{image_tag("osm_logo.png", width: '25', height: '25')} #{t('.openstreetmap')}".html_safe, omniauth_authorize_path(resource_name, "osm"), :class=>"osm", method: :post %>
<%= link_to "#{image_tag("osm_logo.png", width: '25', height: '25')} #{t('.openstreetmap')}".html_safe, omniauth_authorize_path(resource_name, "osm_oauth2"), :class=>"osm", method: :post %>
</span>
<span class="github">
<%= link_to "#{image_tag("github_logo.png", width: '25', height: '25')} #{t('.github')}".html_safe, omniauth_authorize_path(resource_name, "github"), :class=>"github", method: :post %>
Expand Down
2 changes: 2 additions & 0 deletions config/application.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defaults: &defaults
reply_to: "[email protected]"
omniauth_osm_key: "ABC"
omniauth_osm_secret: "ABC"
omniauth_osm_oauth2_key: "ABC"
omniauth_osm_oauth2_secret: "ABC"
omniauth_twitter_key: "ABC"
omniauth_twitter_secret: "ABC"
omniauth_mediawiki_site: "https://commons.wikimedia.org"
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
config.omniauth :twitter, APP_CONFIG["omniauth_twitter_key"], APP_CONFIG["omniauth_twitter_secret"]

config.omniauth :osm, APP_CONFIG["omniauth_osm_key"], APP_CONFIG["omniauth_osm_secret"], {:client_options => {:site => "https://www.openstreetmap.org"}}
config.omniauth :osm_oauth2, APP_CONFIG["omniauth_osm_oauth2_key"], APP_CONFIG["omniauth_osm_oauth2_secret"], { :scope => "read_prefs", :client_options => {:site => "https://www.openstreetmap.org"}}

config.omniauth :mediawiki, APP_CONFIG["omniauth_mediawiki_key"], APP_CONFIG["omniauth_mediawiki_secret"], {:client_options => {:site => APP_CONFIG["omniauth_mediawiki_site"] }}

config.omniauth :github, APP_CONFIG["omniauth_github_key"], APP_CONFIG["omniauth_github_secret"]

config.omniauth :facebook, APP_CONFIG["omniauth_facebook_key"], APP_CONFIG["omniauth_facebook_secret"]

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
# change the failure app, you can configure them inside the config.warden block.
Expand Down
57 changes: 57 additions & 0 deletions config/initializers/osm_oauth2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'omniauth-oauth2'

module OmniAuth
module Strategies
class OsmOauth2 < OmniAuth::Strategies::OAuth2
# Give your strategy a name.
option :name, "osm_oauth2"

# This is where you pass the options you would pass when
# initializing your consumer from the OAuth gem.
option :client_options, {:site => "https://www.openstreetmap.org",
:authorize_url => "https://www.openstreetmap.org/oauth2/authorize",
:token_url => "https://www.openstreetmap.org/oauth2/token"}

# These are called after authentication has succeeded. If
# possible, you should try to set the UID without making
# additional calls (if the user id is returned with the token
# or as a URI parameter). This may not be possible with all
# providers.

extra do
{
'raw_info' => raw_info
}
end

uid { raw_info['id'] }

info do
raw_info
end

def raw_info
@raw_info ||= parse_info(access_token.get('/api/0.6/user/details').body)
@raw_info
rescue ::Errno::ETIMEDOUT
raise ::Timeout::Error
end


private
def parse_info(xml_data)
# extract event information
doc = REXML::Document.new(xml_data)
user = doc.elements['//user']

basic_attributes = { }
basic_attributes['id'] = user.attribute('id').value if user
basic_attributes['display_name'] = user.attribute('display_name').value if user

basic_attributes
end


end
end
end

0 comments on commit a68d037

Please sign in to comment.