Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-25 18:50:27 +01:00
added authentication protocol type
Dieser Commit ist enthalten in:
Ursprung
839f085500
Commit
19a9497d74
1 geänderte Dateien mit 66 neuen und 28 gelöschten Zeilen
|
@ -1,60 +1,97 @@
|
|||
require 'excon'
|
||||
|
||||
class CustomWizard::Authorization
|
||||
def self.access_token
|
||||
PluginStore.get('custom_wizard', 'access_token') || {}
|
||||
|
||||
BASIC_AUTH = 'basic_authentication'
|
||||
OAUTH2_AUTH = 'OAuth2_authentication'
|
||||
|
||||
def self.authentication_protocol(service)
|
||||
PluginStore.get(service, 'authentication_protocol') || {}
|
||||
end
|
||||
|
||||
def self.set_access_token(data)
|
||||
PluginStore.set('custom_wizard', 'access_token', data)
|
||||
def self.set_authentication_protocol(service, protocol)
|
||||
raise Discourse::InvalidParameters unless [BASIC_AUTH, OAUTH2_AUTH].include? protocol
|
||||
PluginStore.set(service, 'authentication_protocol', protocol)
|
||||
end
|
||||
|
||||
def self.refresh_token
|
||||
PluginStore.get('custom_wizard', 'refresh_token')
|
||||
def self.access_token(service)
|
||||
PluginStore.get(service, 'access_token') || {}
|
||||
end
|
||||
|
||||
def self.set_refresh_token(token)
|
||||
PluginStore.set('custom_wizard', 'refresh_token', token)
|
||||
def self.set_access_token(service, data)
|
||||
PluginStore.set(service, 'access_token', data)
|
||||
end
|
||||
|
||||
def self.code
|
||||
PluginStore.get('custom_wizard', 'code')
|
||||
def self.refresh_token (service)
|
||||
PluginStore.get(service, 'refresh_token')
|
||||
end
|
||||
|
||||
def self.set_code(code)
|
||||
PluginStore.set('custom_wizard', 'code', code)
|
||||
def self.set_refresh_token(service, token)
|
||||
PluginStore.set(service, 'refresh_token', token)
|
||||
end
|
||||
|
||||
def self.get_access_token
|
||||
def self.code(service)
|
||||
PluginStore.get(service,'code')
|
||||
end
|
||||
|
||||
def self.set_code(service, code)
|
||||
PluginStore.set(service, 'code', code)
|
||||
end
|
||||
|
||||
def self.client_id(service)
|
||||
PluginStore.get(service,'client_id')
|
||||
end
|
||||
|
||||
def self.set_client_id(service, client_id)
|
||||
PluginStore.set(service, 'client_id', client_id)
|
||||
end
|
||||
|
||||
def self.client_secret(service)
|
||||
PluginStore.get(service,'client_secret')
|
||||
end
|
||||
|
||||
def self.set_client_secret(service, client_secret)
|
||||
PluginStore.set(service, 'client_secret', client_secret)
|
||||
end
|
||||
|
||||
def self.url(service)
|
||||
PluginStore.get(service,'url')
|
||||
end
|
||||
|
||||
def self.set_url(service, url)
|
||||
PluginStore.set(service, 'url', url)
|
||||
end
|
||||
|
||||
def self.get_access_token(service)
|
||||
body = {
|
||||
client_id: SiteSetting.custom_wizard_client_id,
|
||||
client_secret: SiteSetting.custom_wizard_client_secret,
|
||||
code: CustomWizard::Authorization.code,
|
||||
client_id: CustomWizard::Authorization.client_id(service),
|
||||
client_secret: CustomWizard::Authorization.client_secret(service),
|
||||
code: CustomWizard::Authorization.code(service),
|
||||
grant_type: 'authorization_code',
|
||||
redirect_uri: (Rails.env.development? ? CustomWizard::NGROK_URL : Discourse.base_url) + '/custom_wizard/authorization/callback'
|
||||
}
|
||||
|
||||
result = Excon.post(
|
||||
"https://api.custom_wizard.com/token",
|
||||
CustomWizard::Authorization.url(service),
|
||||
:headers => {
|
||||
"Content-Type" => "application/x-www-form-urlencoded"
|
||||
},
|
||||
:body => URI.encode_www_form(body)
|
||||
)
|
||||
|
||||
self.handle_token_result(result)
|
||||
self.handle_token_result(service, result)
|
||||
end
|
||||
|
||||
def self.refresh_access_token
|
||||
def self.refresh_access_token(service)
|
||||
body = {
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: CustomWizard::Authorization.refresh_token
|
||||
}
|
||||
|
||||
authorization_string = SiteSetting.custom_wizard_client_id + ':' + SiteSetting.custom_wizard_client_secret
|
||||
authorization_string = CustomWizard::Authorization.client_id(service) + ':' + CustomWizard::Authorization.client_secret(service)
|
||||
|
||||
result = Excon.post(
|
||||
"https://api.custom_wizard.com/token",
|
||||
CustomWizard::Authorization.url(service),
|
||||
:headers => {
|
||||
"Content-Type" => "application/x-www-form-urlencoded",
|
||||
"Authorization" => "Basic #{Base64.strict_encode64(authorization_string)}"
|
||||
|
@ -62,10 +99,10 @@ class CustomWizard::Authorization
|
|||
:body => URI.encode_www_form(body)
|
||||
)
|
||||
|
||||
self.handle_token_result(result)
|
||||
self.handle_token_result(service, result)
|
||||
end
|
||||
|
||||
def self.handle_token_result(result)
|
||||
def self.handle_token_result(service, result)
|
||||
data = JSON.parse(result.body)
|
||||
return false if (data['error'])
|
||||
|
||||
|
@ -73,19 +110,20 @@ class CustomWizard::Authorization
|
|||
expires_at = Time.now + data['expires_in'].seconds
|
||||
refresh_at = expires_at.to_time - 2.hours
|
||||
|
||||
Jobs.enqueue_at(refresh_at, :refresh_custom_wizard_access_token)
|
||||
Jobs.enqueue_at(refresh_at, :refresh_api_access_token)
|
||||
|
||||
CustomWizard::Authorization.set_access_token(
|
||||
service: service,
|
||||
token: token,
|
||||
expires_at: expires_at,
|
||||
refresh_at: refresh_at
|
||||
)
|
||||
|
||||
CustomWizard::Authorization.set_refresh_token(data['refresh_token'])
|
||||
CustomWizard::Authorization.set_refresh_token(service, data['refresh_token'])
|
||||
end
|
||||
|
||||
def self.authorized
|
||||
CustomWizard::Authorization.access_token[:token] &&
|
||||
CustomWizard::Authorization.access_token[:expires_at].to_datetime > Time.now
|
||||
def self.authorized(service)
|
||||
CustomWizard::Authorization.access_token[service, :token] &&
|
||||
CustomWizard::Authorization.access_token[service, :expires_at].to_datetime > Time.now
|
||||
end
|
||||
end
|
||||
|
|
Laden …
In neuem Issue referenzieren