2016-10-22 01:45:58 +02:00
|
|
|
require 'omniauth-oauth2'
|
|
|
|
|
|
|
|
module OmniAuth
|
|
|
|
module Strategies
|
|
|
|
class Mastodon < OmniAuth::Strategies::OAuth2
|
2016-10-23 12:38:42 +02:00
|
|
|
DEFAULT_SCOPE = 'read'.freeze
|
|
|
|
|
2016-10-22 01:45:58 +02:00
|
|
|
option :name, 'mastodon'
|
|
|
|
|
2016-10-22 14:54:20 +02:00
|
|
|
option :credentials
|
|
|
|
option :identifier
|
2016-10-23 12:38:42 +02:00
|
|
|
option :authorize_options, [:scope]
|
2018-02-28 16:24:25 +01:00
|
|
|
option :domain
|
2016-10-22 01:45:58 +02:00
|
|
|
|
2016-10-22 14:54:20 +02:00
|
|
|
option :client_options, {
|
|
|
|
authorize_url: '/oauth/authorize',
|
|
|
|
token_url: '/oauth/token'
|
|
|
|
}
|
|
|
|
|
2018-10-08 04:27:18 +02:00
|
|
|
uid { raw_info['id'] }
|
2016-10-22 01:45:58 +02:00
|
|
|
|
|
|
|
info do
|
|
|
|
{
|
|
|
|
name: raw_info['username'],
|
|
|
|
nickname: raw_info['username'],
|
|
|
|
image: raw_info['avatar'],
|
|
|
|
urls: { 'Profile' => raw_info['url'] }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
extra do
|
|
|
|
{ raw_info: raw_info }
|
|
|
|
end
|
|
|
|
|
2016-10-22 14:54:20 +02:00
|
|
|
# Before we can redirect the user to authorize access, we must know where the user is from
|
|
|
|
# If the identifier param is not already present, a form will be shown for entering it
|
|
|
|
def request_phase
|
|
|
|
identifier ? start_oauth : get_identifier
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback_phase
|
|
|
|
set_options_from_identifier
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2016-10-22 01:45:58 +02:00
|
|
|
def raw_info
|
|
|
|
@raw_info ||= access_token.get('api/v1/accounts/verify_credentials').parsed
|
|
|
|
end
|
2016-10-22 14:54:20 +02:00
|
|
|
|
|
|
|
def callback_url
|
|
|
|
full_host + script_name + callback_path
|
|
|
|
end
|
|
|
|
|
2016-10-23 12:38:42 +02:00
|
|
|
def authorize_params
|
|
|
|
super.tap do |params|
|
|
|
|
params[:scope] ||= DEFAULT_SCOPE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-22 14:54:20 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def get_identifier
|
2018-10-30 15:40:38 +01:00
|
|
|
I18n.with_locale(locale) do
|
|
|
|
form = OmniAuth::Form.new(title: translate('.omniauth.mastodon.title'))
|
|
|
|
form.text_field translate('.omniauth.mastodon.text'), 'identifier'
|
|
|
|
form.button translate('.omniauth.mastodon.button')
|
|
|
|
form.to_response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def translate(t)
|
|
|
|
I18n.exists?(t) ? I18n.t(t) : I18n.t(t, locale: :en)
|
|
|
|
end
|
|
|
|
|
|
|
|
def locale
|
|
|
|
loc = request.params['locale'] || session[:omniauth_login_locale] || I18n.default_locale
|
|
|
|
loc = :en unless I18n.locale_available?(loc)
|
|
|
|
loc
|
2016-10-22 14:54:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_oauth
|
|
|
|
set_options_from_identifier
|
|
|
|
redirect client.auth_code.authorize_url({:redirect_uri => callback_url}.merge(authorize_params))
|
|
|
|
end
|
|
|
|
|
|
|
|
def identifier
|
2017-04-05 01:45:46 +02:00
|
|
|
i = options.identifier || request.params['identifier'] || (env['omniauth.params'].is_a?(Hash) ? env['omniauth.params']['identifier'] : nil) || session[:identifier]
|
2016-10-22 14:54:20 +02:00
|
|
|
i = i.downcase.strip unless i.nil?
|
|
|
|
i = nil if i == ''
|
2017-04-05 01:45:46 +02:00
|
|
|
session[:identifier] = i unless i.nil?
|
2016-10-22 14:54:20 +02:00
|
|
|
i
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_options_from_identifier
|
|
|
|
username, domain = identifier.split('@')
|
|
|
|
client_id, client_secret = options.credentials.call(domain, callback_url)
|
|
|
|
|
|
|
|
options.identifier = identifier
|
|
|
|
options.client_options[:site] = "https://#{domain}"
|
|
|
|
options.client_id = client_id
|
|
|
|
options.client_secret = client_secret
|
|
|
|
end
|
2016-10-22 01:45:58 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|