0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-10 04:12:53 +01:00
discourse-custom-wizard/lib/api/authorization.rb

140 Zeilen
3,9 KiB
Ruby

2019-05-11 17:53:37 +02:00
require 'excon'
2019-05-31 09:54:11 +02:00
class CustomWizard::Api::Authorization
include ActiveModel::SerializerSupport
attr_accessor :authorized,
:service,
:auth_type,
:auth_url,
:token_url,
:client_id,
:client_secret,
:auth_params,
:access_token,
:refresh_token,
:token_expires_at,
:token_refresh_at,
:code,
:username,
:password
def initialize(service, params)
@service = service
data = params.is_a?(String) ? ::JSON.parse(params) : params
data.each do |k, v|
self.send "#{k}=", v if self.respond_to?(k)
end
end
def authorized
@authorized ||= @access_token && @token_expires_at.to_datetime > Time.now
end
def self.set(service, data)
model = self.get(service) || {}
2019-05-11 17:53:37 +02:00
data.each do |k, v|
model.send "#{k}=", v if model.respond_to?(k)
end
2019-05-11 17:53:37 +02:00
2019-05-31 09:54:11 +02:00
PluginStore.set("custom_wizard_api_#{service}", 'authorization', model.as_json)
2019-05-11 17:53:37 +02:00
self.get(service)
2019-05-11 17:53:37 +02:00
end
def self.get(service)
2019-05-31 09:54:11 +02:00
data = PluginStore.get("custom_wizard_api_#{service}", 'authorization')
self.new(service, data)
2019-05-11 17:53:37 +02:00
end
def self.get_header_authorization_string(service)
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
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
access_token = access_token(service)
raise Discourse::InvalidParameters.new(access_token) unless access_token.present?
"Bearer #{access_token}"
end
end
def self.get_token(service)
2019-05-31 09:54:11 +02:00
authorization = CustomWizard::Api::Authorization.get(service)
2019-05-11 17:53:37 +02:00
body = {
client_id: authorization.client_id,
client_secret: authorization.client_secret,
code: authorization.code,
2019-05-11 17:53:37 +02:00
grant_type: 'authorization_code',
redirect_uri: Discourse.base_url + "/admin/wizards/apis/#{service}/redirect"
2019-05-11 17:53:37 +02:00
}
result = Excon.post(
authorization.token_url,
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
def self.refresh_token(service)
2019-05-31 09:54:11 +02:00
authorization = CustomWizard::Api::Authorization.get(service)
2019-05-11 17:53:37 +02:00
body = {
grant_type: 'refresh_token',
refresh_token: authorization.refresh_token
2019-05-11 17:53:37 +02:00
}
authorization_string = authorization.client_id + ':' + authorization.client_secret
2019-05-11 17:53:37 +02:00
result = Excon.post(
authorization.token_url,
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)
2019-05-11 17:53:37 +02:00
return false if (data['error'])
access_token = data['access_token']
refresh_token = data['refresh_token']
2019-05-11 17:53:37 +02:00
expires_at = Time.now + data['expires_in'].seconds
refresh_at = expires_at.to_time - 2.hours
opts = {
service: service
}
Jobs.enqueue_at(refresh_at, :refresh_api_access_token, opts)
2019-05-11 17:53:37 +02:00
2019-05-31 09:54:11 +02:00
CustomWizard::Api::Authorization.set(service,
access_token: access_token,
refresh_token: refresh_token,
token_expires_at: expires_at,
token_refresh_at: refresh_at
2019-05-11 17:53:37 +02:00
)
end
end