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'
|
require 'excon'
|
||||||
|
|
||||||
class CustomWizard::Authorization
|
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
|
end
|
||||||
|
|
||||||
def self.set_access_token(data)
|
def self.set_authentication_protocol(service, protocol)
|
||||||
PluginStore.set('custom_wizard', 'access_token', data)
|
raise Discourse::InvalidParameters unless [BASIC_AUTH, OAUTH2_AUTH].include? protocol
|
||||||
|
PluginStore.set(service, 'authentication_protocol', protocol)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.refresh_token
|
def self.access_token(service)
|
||||||
PluginStore.get('custom_wizard', 'refresh_token')
|
PluginStore.get(service, 'access_token') || {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.set_refresh_token(token)
|
def self.set_access_token(service, data)
|
||||||
PluginStore.set('custom_wizard', 'refresh_token', token)
|
PluginStore.set(service, 'access_token', data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.code
|
def self.refresh_token (service)
|
||||||
PluginStore.get('custom_wizard', 'code')
|
PluginStore.get(service, 'refresh_token')
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.set_code(code)
|
def self.set_refresh_token(service, token)
|
||||||
PluginStore.set('custom_wizard', 'code', code)
|
PluginStore.set(service, 'refresh_token', token)
|
||||||
end
|
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 = {
|
body = {
|
||||||
client_id: SiteSetting.custom_wizard_client_id,
|
client_id: CustomWizard::Authorization.client_id(service),
|
||||||
client_secret: SiteSetting.custom_wizard_client_secret,
|
client_secret: CustomWizard::Authorization.client_secret(service),
|
||||||
code: CustomWizard::Authorization.code,
|
code: CustomWizard::Authorization.code(service),
|
||||||
grant_type: 'authorization_code',
|
grant_type: 'authorization_code',
|
||||||
redirect_uri: (Rails.env.development? ? CustomWizard::NGROK_URL : Discourse.base_url) + '/custom_wizard/authorization/callback'
|
redirect_uri: (Rails.env.development? ? CustomWizard::NGROK_URL : Discourse.base_url) + '/custom_wizard/authorization/callback'
|
||||||
}
|
}
|
||||||
|
|
||||||
result = Excon.post(
|
result = Excon.post(
|
||||||
"https://api.custom_wizard.com/token",
|
CustomWizard::Authorization.url(service),
|
||||||
:headers => {
|
:headers => {
|
||||||
"Content-Type" => "application/x-www-form-urlencoded"
|
"Content-Type" => "application/x-www-form-urlencoded"
|
||||||
},
|
},
|
||||||
:body => URI.encode_www_form(body)
|
:body => URI.encode_www_form(body)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.handle_token_result(result)
|
self.handle_token_result(service, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.refresh_access_token
|
def self.refresh_access_token(service)
|
||||||
body = {
|
body = {
|
||||||
grant_type: 'refresh_token',
|
grant_type: 'refresh_token',
|
||||||
refresh_token: CustomWizard::Authorization.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(
|
result = Excon.post(
|
||||||
"https://api.custom_wizard.com/token",
|
CustomWizard::Authorization.url(service),
|
||||||
:headers => {
|
:headers => {
|
||||||
"Content-Type" => "application/x-www-form-urlencoded",
|
"Content-Type" => "application/x-www-form-urlencoded",
|
||||||
"Authorization" => "Basic #{Base64.strict_encode64(authorization_string)}"
|
"Authorization" => "Basic #{Base64.strict_encode64(authorization_string)}"
|
||||||
|
@ -62,10 +99,10 @@ class CustomWizard::Authorization
|
||||||
:body => URI.encode_www_form(body)
|
:body => URI.encode_www_form(body)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.handle_token_result(result)
|
self.handle_token_result(service, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.handle_token_result(result)
|
def self.handle_token_result(service, result)
|
||||||
data = JSON.parse(result.body)
|
data = JSON.parse(result.body)
|
||||||
return false if (data['error'])
|
return false if (data['error'])
|
||||||
|
|
||||||
|
@ -73,19 +110,20 @@ class CustomWizard::Authorization
|
||||||
expires_at = Time.now + data['expires_in'].seconds
|
expires_at = Time.now + data['expires_in'].seconds
|
||||||
refresh_at = expires_at.to_time - 2.hours
|
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(
|
CustomWizard::Authorization.set_access_token(
|
||||||
|
service: service,
|
||||||
token: token,
|
token: token,
|
||||||
expires_at: expires_at,
|
expires_at: expires_at,
|
||||||
refresh_at: refresh_at
|
refresh_at: refresh_at
|
||||||
)
|
)
|
||||||
|
|
||||||
CustomWizard::Authorization.set_refresh_token(data['refresh_token'])
|
CustomWizard::Authorization.set_refresh_token(service, data['refresh_token'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.authorized
|
def self.authorized(service)
|
||||||
CustomWizard::Authorization.access_token[:token] &&
|
CustomWizard::Authorization.access_token[service, :token] &&
|
||||||
CustomWizard::Authorization.access_token[:expires_at].to_datetime > Time.now
|
CustomWizard::Authorization.access_token[service, :expires_at].to_datetime > Time.now
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Laden …
In neuem Issue referenzieren