From 508e8df19e10c9a613ca1a2b43987e89b9c50f8a Mon Sep 17 00:00:00 2001 From: navaneeth Date: Tue, 4 May 2021 12:06:57 +0530 Subject: [PATCH] Slack OAuth backend services --- app/controllers/data_sources_controller.rb | 2 +- app/services/slack_oauth_service.rb | 28 ++++++++++++++++++++++ app/services/slack_query_service.rb | 26 ++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 app/services/slack_oauth_service.rb create mode 100644 app/services/slack_query_service.rb diff --git a/app/controllers/data_sources_controller.rb b/app/controllers/data_sources_controller.rb index 8518034a9a..c4e22f1564 100644 --- a/app/controllers/data_sources_controller.rb +++ b/app/controllers/data_sources_controller.rb @@ -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' } diff --git a/app/services/slack_oauth_service.rb b/app/services/slack_oauth_service.rb new file mode 100644 index 0000000000..dda5de013e --- /dev/null +++ b/app/services/slack_oauth_service.rb @@ -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 diff --git a/app/services/slack_query_service.rb b/app/services/slack_query_service.rb new file mode 100644 index 0000000000..889fc00471 --- /dev/null +++ b/app/services/slack_query_service.rb @@ -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