2019-05-11 17:53:37 +02:00
|
|
|
require 'excon'
|
|
|
|
|
|
|
|
class CustomWizard::Authorization
|
2019-05-14 15:54:33 +02:00
|
|
|
|
|
|
|
BASIC_AUTH = 'basic_authentication'
|
|
|
|
OAUTH2_AUTH = 'OAuth2_authentication'
|
|
|
|
|
|
|
|
def self.authentication_protocol(service)
|
|
|
|
PluginStore.get(service, 'authentication_protocol') || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_authentication_protocol(service, protocol)
|
2019-05-15 08:54:32 +02:00
|
|
|
# TODO: make error more informative
|
|
|
|
raise Discourse::InvalidParameters.new(:protocol) unless [BASIC_AUTH, OAUTH2_AUTH].include? protocol
|
2019-05-14 15:54:33 +02:00
|
|
|
PluginStore.set(service, 'authentication_protocol', protocol)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.access_token(service)
|
|
|
|
PluginStore.get(service, 'access_token') || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_access_token(service, data)
|
|
|
|
PluginStore.set(service, 'access_token', data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.refresh_token (service)
|
|
|
|
PluginStore.get(service, 'refresh_token')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_refresh_token(service, token)
|
|
|
|
PluginStore.set(service, 'refresh_token', token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.code(service)
|
|
|
|
PluginStore.get(service,'code')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_code(service, code)
|
|
|
|
PluginStore.set(service, 'code', code)
|
|
|
|
end
|
|
|
|
|
2019-05-15 08:54:32 +02:00
|
|
|
def self.username(service)
|
|
|
|
PluginStore.get(service,'username')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_username(service, username)
|
|
|
|
PluginStore.set(service, 'username', username)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.password(service)
|
|
|
|
PluginStore.get(service,'password')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_password(service, password)
|
|
|
|
PluginStore.set(service, 'password', password)
|
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.client_id(service)
|
|
|
|
PluginStore.get(service,'client_id')
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.set_client_id(service, client_id)
|
|
|
|
PluginStore.set(service, 'client_id', client_id)
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.client_secret(service)
|
|
|
|
PluginStore.get(service,'client_secret')
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.set_client_secret(service, client_secret)
|
|
|
|
PluginStore.set(service, 'client_secret', client_secret)
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.url(service)
|
|
|
|
PluginStore.get(service,'url')
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.set_url(service, url)
|
|
|
|
PluginStore.set(service, 'url', url)
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
|
2019-05-15 08:54:32 +02:00
|
|
|
def self.get_header_authorization_string(service)
|
|
|
|
# TODO: make error more informative, raise error if service not defined
|
|
|
|
protocol = authentication_protocol(service)
|
|
|
|
raise Discourse::InvalidParameters.new(:service) unless protocol.present?
|
|
|
|
raise Discourse::InvalidParameters.new(:protocol) unless [BASIC_AUTH, OAUTH2_AUTH].include? protocol
|
|
|
|
|
|
|
|
if protocol = BASIC_AUTH
|
|
|
|
# TODO: improve error reporting
|
|
|
|
username = username(service)
|
|
|
|
raise Discourse::InvalidParameters.new(:username) unless username.present?
|
|
|
|
password = password(service)
|
|
|
|
raise Discourse::InvalidParameters.new(:password) unless password.present?
|
|
|
|
authorization_string = (username + ":" + password).chomp
|
|
|
|
"Basic #{Base64.strict_encode64(authorization_string)}"
|
|
|
|
else
|
|
|
|
# must be OAUTH2
|
|
|
|
# TODO: make error more informative, raise error if there is no recorded access token
|
|
|
|
raise Discourse::InvalidParameters unless access_token[:token].present?
|
|
|
|
"Bearer #{access_token[:token]}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.get_access_token(service)
|
2019-05-11 17:53:37 +02:00
|
|
|
body = {
|
2019-05-14 15:54:33 +02:00
|
|
|
client_id: CustomWizard::Authorization.client_id(service),
|
|
|
|
client_secret: CustomWizard::Authorization.client_secret(service),
|
|
|
|
code: CustomWizard::Authorization.code(service),
|
2019-05-11 17:53:37 +02:00
|
|
|
grant_type: 'authorization_code',
|
|
|
|
redirect_uri: (Rails.env.development? ? CustomWizard::NGROK_URL : Discourse.base_url) + '/custom_wizard/authorization/callback'
|
|
|
|
}
|
|
|
|
|
|
|
|
result = Excon.post(
|
2019-05-14 15:54:33 +02:00
|
|
|
CustomWizard::Authorization.url(service),
|
2019-05-11 17:53:37 +02:00
|
|
|
:headers => {
|
|
|
|
"Content-Type" => "application/x-www-form-urlencoded"
|
|
|
|
},
|
|
|
|
:body => URI.encode_www_form(body)
|
|
|
|
)
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
self.handle_token_result(service, result)
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.refresh_access_token(service)
|
2019-05-11 17:53:37 +02:00
|
|
|
body = {
|
|
|
|
grant_type: 'refresh_token',
|
|
|
|
refresh_token: CustomWizard::Authorization.refresh_token
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
authorization_string = CustomWizard::Authorization.client_id(service) + ':' + CustomWizard::Authorization.client_secret(service)
|
2019-05-11 17:53:37 +02:00
|
|
|
|
|
|
|
result = Excon.post(
|
2019-05-14 15:54:33 +02:00
|
|
|
CustomWizard::Authorization.url(service),
|
2019-05-11 17:53:37 +02:00
|
|
|
:headers => {
|
|
|
|
"Content-Type" => "application/x-www-form-urlencoded",
|
|
|
|
"Authorization" => "Basic #{Base64.strict_encode64(authorization_string)}"
|
|
|
|
},
|
|
|
|
:body => URI.encode_www_form(body)
|
|
|
|
)
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
self.handle_token_result(service, result)
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.handle_token_result(service, result)
|
2019-05-11 17:53:37 +02:00
|
|
|
data = JSON.parse(result.body)
|
|
|
|
return false if (data['error'])
|
|
|
|
|
|
|
|
token = data['access_token']
|
|
|
|
expires_at = Time.now + data['expires_in'].seconds
|
|
|
|
refresh_at = expires_at.to_time - 2.hours
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
Jobs.enqueue_at(refresh_at, :refresh_api_access_token)
|
2019-05-11 17:53:37 +02:00
|
|
|
|
|
|
|
CustomWizard::Authorization.set_access_token(
|
2019-05-14 15:54:33 +02:00
|
|
|
service: service,
|
2019-05-11 17:53:37 +02:00
|
|
|
token: token,
|
|
|
|
expires_at: expires_at,
|
|
|
|
refresh_at: refresh_at
|
|
|
|
)
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
CustomWizard::Authorization.set_refresh_token(service, data['refresh_token'])
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
|
2019-05-14 15:54:33 +02:00
|
|
|
def self.authorized(service)
|
|
|
|
CustomWizard::Authorization.access_token[service, :token] &&
|
|
|
|
CustomWizard::Authorization.access_token[service, :expires_at].to_datetime > Time.now
|
2019-05-11 17:53:37 +02:00
|
|
|
end
|
|
|
|
end
|