mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-22 08:28:35 +00:00
Slack OAuth backend services
This commit is contained in:
parent
35aa8294b7
commit
508e8df19e
3 changed files with 55 additions and 1 deletions
|
|
@ -8,7 +8,7 @@ class DataSourcesController < ApplicationController
|
|||
|
||||
# Fetch necessary access token if OAuth2 based data source
|
||||
if options.find { |option| option['key'] == 'oauth2' }
|
||||
provider = 'google'
|
||||
provider = options.find { |option| option['key'] === 'provider' } ['value']
|
||||
service_class = "#{provider.capitalize}OauthService".constantize
|
||||
access_info = service_class.fetch_access_token(options.find { |option| option['key'] === 'code' } ['value'])
|
||||
options.reject! { |option| option['key'] == 'code' }
|
||||
|
|
|
|||
28
app/services/slack_oauth_service.rb
Normal file
28
app/services/slack_oauth_service.rb
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
class SlackOauthService
|
||||
def self.generate_base_auth_url
|
||||
client_id = ENV.fetch('SLACK_CLIENT_ID')
|
||||
"https://slack.com/oauth/v2/authorize?response_type=code&client_id=#{client_id}&redirect_uri=#{ENV.fetch('TOOLJET_HOST')}/oauth2/authorize"
|
||||
end
|
||||
|
||||
def self.fetch_access_token(code)
|
||||
access_token_url = "https://slack.com/api/oauth.v2.access"
|
||||
client_id = ENV.fetch('SLACK_CLIENT_ID')
|
||||
client_secret = ENV.fetch('SLACK_CLIENT_SECRET')
|
||||
|
||||
data = { code: code,
|
||||
client_id: client_id,
|
||||
client_secret: client_secret,
|
||||
redirect_uri: "#{ENV.fetch('TOOLJET_HOST')}/oauth2/authorize",
|
||||
}
|
||||
body = URI.encode_www_form(data)
|
||||
|
||||
response = HTTParty.post(access_token_url, body: body)
|
||||
|
||||
result = JSON.parse(response.body)
|
||||
|
||||
access_token = result['access_token']
|
||||
refresh_token = result['refresh_token']
|
||||
|
||||
[['access_token', access_token], ['refresh_token', refresh_token]]
|
||||
end
|
||||
end
|
||||
26
app/services/slack_query_service.rb
Normal file
26
app/services/slack_query_service.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
class SlackQueryService
|
||||
attr_accessor :query, :ource, :options, :source_options, :current_user
|
||||
|
||||
def initialize(data_query, options, source_options, current_user)
|
||||
@query = data_query
|
||||
@source = query.data_source
|
||||
@options = options
|
||||
@source_options = source_options
|
||||
@current_user = current_user
|
||||
end
|
||||
|
||||
def process
|
||||
operation = query.options['operation']
|
||||
access_token = source_options['access_token']
|
||||
data = []
|
||||
|
||||
if operation === 'list_users'
|
||||
result = HTTParty.get("https://slack.com/api/users.list",
|
||||
headers: { "Authorization": "Bearer #{access_token}" })
|
||||
|
||||
data = JSON.parse(result.body)
|
||||
end
|
||||
|
||||
{ status: 'success', data: data }
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue