slack-ruby-bot-server 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02acf8078e1ec2fb418d45f9fac6823d7af6b473ad00c8992f59ff4a64311609
4
- data.tar.gz: d1da43dc7201cd30a5e49eb6193eb476820edd936b804e714e8952aafaf4d566
3
+ metadata.gz: 86a1479af364899fd951bee2d75f30d3fd32d2ccb60f3804716d574746f1b85e
4
+ data.tar.gz: 88cfbd7ed0bec93439f327f5389f9bff6a8eeeb0eeeb2d89f35fa3b08cefc416
5
5
  SHA512:
6
- metadata.gz: bc2b495018d4d24f36c75eada4e9eb218205611bc2ab94d77b89326e4c23ac0e254302747a6d3bee758dc20fb46f44a4722da93fcac69a262754eb106aca80a6
7
- data.tar.gz: 4e503d3c5f70d464a4c07f429cd81b1aa6bdc3414e95fa9b3e668ad6649f644e6df83a8401bf491147c9e5821a3b865052c3a74532cb4ca0e711a495047d5753
6
+ metadata.gz: 9efdeede9435e4d5bca785b0dbd567a65119892a2521882477e3e5e79480257886375f2170dd54166e6e05fa9be186e4f8da2f878bdae0674c7b76061ac9e66a
7
+ data.tar.gz: 2a7bf1ee2397aaca5a3337503209009e679e0ebf5daaeed374c8a75fb4ffb102d7a4298d87a8c5c092789932f1f259919119c7959ee21f0c3130c8d90195b673
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2019-02-25 14:24:34 -0500 using RuboCop version 0.58.2.
3
+ # on 2019-03-23 14:16:02 -0400 using RuboCop version 0.58.2.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ### Changelog
2
2
 
3
+ #### 0.10.0 (2019/3/23)
4
+
5
+ * [#97](https://github.com/slack-ruby/slack-ruby-bot-server/pull/97): Added `Config#service_class` to override the `SlackRubyBotServer::Service.instance` singleton - [@dblock](https://github.com/dblock).
6
+ * [#96](https://github.com/slack-ruby/slack-ruby-bot-server/pull/96): Added `Team#bot_user_id`, `activated_user_id` and `activated_user_access_token` - [@dblock](https://github.com/dblock).
7
+ * [#95](https://github.com/slack-ruby/slack-ruby-bot-server/pull/95): Expose the optional `state` parameter that is returned from the Add to Slack button - [@aok-solutions](https://github.com/aok-solutions).
8
+
3
9
  #### 0.9.0 (2019/2/25)
4
10
 
5
11
  * [#93](https://github.com/slack-ruby/slack-ruby-bot-server/pull/93): Removed ping worker in favor of slack-ruby-client lower level ping - [@dblock](https://github.com/dblock).
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ when 'mongoid' then
5
5
  gem 'kaminari-mongoid'
6
6
  gem 'mongoid'
7
7
  gem 'mongoid-scroll'
8
+ gem 'mongoid-shell'
8
9
  when 'activerecord' then
9
10
  gem 'activerecord', '~> 5.0.0'
10
11
  gem 'otr-activerecord', '~> 1.2.1'
@@ -26,7 +27,6 @@ group :development, :test do
26
27
  gem 'fabrication'
27
28
  gem 'faker'
28
29
  gem 'hyperclient'
29
- gem 'mongoid-shell'
30
30
  gem 'rack-server-pages'
31
31
  gem 'rack-test'
32
32
  gem 'rake'
data/README.md CHANGED
@@ -13,7 +13,7 @@ A library that contains a [Grape](http://github.com/ruby-grape/grape) API servin
13
13
 
14
14
  ### Stable Release
15
15
 
16
- You're reading the documentation for the **stable** release of slack-ruby-bot-server, v0.9.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
16
+ You're reading the documentation for the **stable** release of slack-ruby-bot-server, v0.10.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
17
17
 
18
18
  ### Try Me
19
19
 
@@ -103,15 +103,15 @@ You can introduce custom behavior into the service lifecycle via callbacks. This
103
103
  ```ruby
104
104
  instance = SlackRubyBotServer::Service.instance
105
105
 
106
- instance.on :created do |team, error|
106
+ instance.on :created do |team, error, options|
107
107
  # a new team has been registered
108
108
  end
109
109
 
110
- instance.on :deactivated do |team, error|
110
+ instance.on :deactivated do |team, error, options|
111
111
  # an existing team has been deactivated in Slack
112
112
  end
113
113
 
114
- instance.on :error do |team, error|
114
+ instance.on :error do |team, error, options|
115
115
  # an error has occurred
116
116
  end
117
117
  ```
@@ -132,6 +132,25 @@ The following callbacks are supported. All callbacks receive a `team`, except `e
132
132
  | deactivating | a team is being deactivated |
133
133
  | deactivated | a team has been deactivated |
134
134
 
135
+
136
+ The [Add to Slack button](https://api.slack.com/docs/slack-button) also allows for an optional `state` parameter that will be returned on completion of the request. The `creating` and `created` callbacks include an options hash where this value can be accessed (to check for forgery attacks for instance).
137
+ ```ruby
138
+ auth = OpenSSL::HMAC.hexdigest("SHA256", "key", "data")
139
+ ```
140
+ ```html
141
+ <a href="https://slack.com/oauth/authorize?scope=bot&client_id=<%= ENV['SLACK_CLIENT_ID'] %>&state=#{auth)"> ... </a>
142
+ ```
143
+ ```ruby
144
+ instance = SlackRubyBotServer::Service.instance
145
+ instance.on :creating do |team, error, options|
146
+ raise "Unauthorized response" unless options[:state] == auth
147
+ end
148
+ ```
149
+
150
+ A number of extensions use service manager callbacks to implement useful functionality.
151
+
152
+ * [slack-ruby-bot-server-mailchimp](https://github.com/slack-ruby/slack-ruby-bot-server-mailchimp): Subscribes new bot users to a Mailchimp mailing list.
153
+
135
154
  #### Server Class
136
155
 
137
156
  You can override the server class to handle additional events, and configure the service to use it.
@@ -152,14 +171,32 @@ SlackRubyBotServer.configure do |config|
152
171
  end
153
172
  ```
154
173
 
174
+ #### Service Class
175
+
176
+ You can override the service class to handle additional methods.
177
+
178
+ ```ruby
179
+ class MyService < SlackRubyBotServer::Service
180
+ def url
181
+ 'https://www.example.com'
182
+ end
183
+ end
184
+
185
+ SlackRubyBotServer.configure do |config|
186
+ config.service_class = MyService
187
+ end
188
+
189
+ SlackRubyBotServer::Service.instance # MyService
190
+ SlackRubyBotServer::Service.instance.url # https://www.example.com
191
+ ```
192
+
155
193
  ### Access Tokens
156
194
 
157
- By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) stores a `bot_access_token` that grants a certain amount of privileges to the bot user as described in [Slack OAuth Docs](https://api.slack.com/docs/oauth). You may not want a bot user at all, or may require different auth scopes, such as `users.profile:read` to access user profile information via `Slack::Web::Client#users_profile_get`. To obtain the non-bot access token make the following changes.
195
+ By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) stores a `bot_access_token` as `token` that grants a certain amount of privileges to the bot user as described in [Slack OAuth Docs](https://api.slack.com/docs/oauth) along with `activated_user_access_token` that represents the token of the installing user. You may not want a bot user at all, or may require different auth scopes, such as `users.profile:read` to access user profile information via `Slack::Web::Client#users_profile_get`. To change required scopes make the following changes.
158
196
 
159
197
  1) Configure your app to require additional scopes in Slack API under _OAuth_, _Permissions_
160
- 2) Add `access_token` and, optionally, `scope` to your `Team` model
161
- 3) Change the _Add to Slack_ buttons to require the additional scope, eg. `https://slack.com/oauth/authorize?scope=bot,users.profile:read&client_id=...`
162
- 4) Store the access token returned from `Slack::Web::Client#oauth_access` and scope when creating a team in your `Teams` API endpoint.
198
+ 2) Change the _Add to Slack_ buttons to require the additional scope, eg. `https://slack.com/oauth/authorize?scope=bot,users.profile:read&client_id=...`
199
+ 3) The access token with the requested scopes will be stored as `activated_user_access_token`.
163
200
 
164
201
  You can see a sample implementation in [slack-sup#3a497b](https://github.com/dblock/slack-sup/commit/3a497b436d25d3a7738562655cda64b180ae0096).
165
202
 
data/UPGRADING.md CHANGED
@@ -1,9 +1,35 @@
1
1
  Upgrading Slack-Ruby-Bot-Server
2
2
  ===============================
3
3
 
4
+ ### Upgrading to >= 0.10.0
5
+
6
+ #### New Team Fields
7
+
8
+ The following fields have been added to `Team`.
9
+
10
+ * `bot_user_id`: the bot `user_id` during installation
11
+ * `activated_user_id`: the installing Slack user `user_id`
12
+ * `activated_user_access_token`: the installing Slack user `access_token`
13
+
14
+ No action is required for Mongoid.
15
+
16
+ If you're using ActiveRecord, create a migration similar to [sample_apps/sample_app_activerecord/db/migrate/20190323181453_add_activated_fields.rb](sample_apps/sample_app_activerecord/db/migrate/20190323181453_add_activated_fields.rb) to add these fields.
17
+
18
+ ```ruby
19
+ class AddActivatedFields < ActiveRecord::Migration[5.0]
20
+ def change
21
+ add_column :teams, :bot_user_id, :string
22
+ add_column :teams, :activated_user_id, :string
23
+ add_column :teams, :activated_user_access_token, :string
24
+ end
25
+ end
26
+ ```
27
+
28
+ See [#96](https://github.com/slack-ruby/slack-ruby-bot-server/pull/96) for more information.
29
+
4
30
  ### Upgrading to >= 0.9.0
5
31
 
6
- ### Removed Ping Worker
32
+ #### Removed Ping Worker
7
33
 
8
34
  The ping worker that was added in 0.7.0 has been removed in favor of a lower level implementation in slack-ruby-client. Remove any references to `ping` options.
9
35
 
@@ -11,7 +37,7 @@ See [slack-ruby-client#226](https://github.com/slack-ruby/slack-ruby-client/pull
11
37
 
12
38
  ### Upgrading to >= 0.8.0
13
39
 
14
- ### Different Asynchronous I/O Library
40
+ #### Different Asynchronous I/O Library
15
41
 
16
42
  The library now uses [async-websocket](https://github.com/socketry/async-websocket) instead of [celluloid-io](https://github.com/celluloid/celluloid-io). If your application is built on Celluloid you may need to make changes and use `Async::Reactor.run` and the likes.
17
43
 
@@ -2,6 +2,7 @@ require 'async/websocket'
2
2
 
3
3
  require 'grape-swagger'
4
4
  require 'slack-ruby-bot'
5
+ require 'slack-ruby-bot-server/service'
5
6
  require 'slack-ruby-bot-server/server'
6
7
  require 'slack-ruby-bot-server/config'
7
8
 
@@ -13,4 +14,3 @@ require "slack-ruby-bot-server/config/database_adapters/#{SlackRubyBotServer::Co
13
14
 
14
15
  require 'slack-ruby-bot-server/api'
15
16
  require 'slack-ruby-bot-server/app'
16
- require 'slack-ruby-bot-server/service'
@@ -33,6 +33,7 @@ module SlackRubyBotServer
33
33
  desc 'Create a team using an OAuth token.'
34
34
  params do
35
35
  requires :code, type: String
36
+ optional :state, type: String
36
37
  end
37
38
  post do
38
39
  client = Slack::Web::Client.new
@@ -46,21 +47,34 @@ module SlackRubyBotServer
46
47
  )
47
48
 
48
49
  token = rc['bot']['bot_access_token']
50
+ bot_user_id = rc['bot']['bot_user_id']
51
+ user_id = rc['user_id']
52
+ access_token = rc['access_token']
49
53
  team = Team.where(token: token).first
50
54
  team ||= Team.where(team_id: rc['team_id']).first
51
- if team && !team.active?
55
+
56
+ if team
57
+ team.update_attributes!(
58
+ activated_user_id: user_id,
59
+ activated_user_access_token: access_token,
60
+ bot_user_id: bot_user_id
61
+ )
62
+ raise "Team #{team.name} is already registered." if team.active?
52
63
  team.activate!(token)
53
- elsif team
54
- raise "Team #{team.name} is already registered."
55
64
  else
56
65
  team = Team.create!(
57
66
  token: token,
58
67
  team_id: rc['team_id'],
59
- name: rc['team_name']
68
+ name: rc['team_name'],
69
+ activated_user_id: user_id,
70
+ activated_user_access_token: access_token,
71
+ bot_user_id: bot_user_id
60
72
  )
61
73
  end
62
74
 
63
- Service.instance.create!(team)
75
+ options = params.slice(:state)
76
+
77
+ Service.instance.create!(team, options)
64
78
  present team, with: Presenters::TeamPresenter
65
79
  end
66
80
  end
@@ -3,10 +3,12 @@ module SlackRubyBotServer
3
3
  extend self
4
4
 
5
5
  attr_accessor :server_class
6
+ attr_accessor :service_class
6
7
  attr_accessor :database_adapter
7
8
 
8
9
  def reset!
9
10
  self.server_class = SlackRubyBotServer::Server
11
+ self.service_class = SlackRubyBotServer::Service
10
12
  self.database_adapter = if defined?(::Mongoid)
11
13
  :mongoid
12
14
  elsif defined?(::ActiveRecord)
@@ -12,11 +12,15 @@ module SlackRubyBotServer
12
12
 
13
13
  def self.init!
14
14
  return if ActiveRecord::Base.connection.tables.include?('teams')
15
+
15
16
  ActiveRecord::Base.connection.create_table :teams do |t|
16
17
  t.string :team_id
17
18
  t.string :name
18
19
  t.string :domain
19
20
  t.string :token
21
+ t.string :bot_user_id
22
+ t.string :activated_user_id
23
+ t.string :activated_user_access_token
20
24
  t.boolean :active, default: true
21
25
  t.timestamps
22
26
  end
@@ -9,6 +9,9 @@ class Team
9
9
  field :domain, type: String
10
10
  field :token, type: String
11
11
  field :active, type: Boolean, default: true
12
+ field :bot_user_id, type: String
13
+ field :activated_user_id, type: String
14
+ field :activated_user_access_token, type: String
12
15
 
13
16
  include Methods
14
17
 
@@ -10,7 +10,7 @@ module SlackRubyBotServer
10
10
  end
11
11
 
12
12
  def self.instance
13
- @instance ||= new
13
+ @instance ||= SlackRubyBotServer::Config.service_class.new
14
14
  end
15
15
 
16
16
  def initialize
@@ -21,10 +21,10 @@ module SlackRubyBotServer
21
21
  @callbacks[type.to_s] << block
22
22
  end
23
23
 
24
- def create!(team)
25
- run_callbacks :creating, team
24
+ def create!(team, options = {})
25
+ run_callbacks :creating, team, nil, options
26
26
  start!(team)
27
- run_callbacks :created, team
27
+ run_callbacks :created, team, nil, options
28
28
  end
29
29
 
30
30
  def start!(team)
@@ -103,11 +103,11 @@ module SlackRubyBotServer
103
103
  end
104
104
  end
105
105
 
106
- def run_callbacks(type, team = nil, error = nil)
106
+ def run_callbacks(type, team = nil, error = nil, options = {})
107
107
  callbacks = @callbacks[type.to_s]
108
108
  return false unless callbacks
109
109
  callbacks.each do |c|
110
- c.call team, error
110
+ c.call team, error, options
111
111
  end
112
112
  true
113
113
  rescue StandardError => e
@@ -1,3 +1,3 @@
1
1
  module SlackRubyBotServer
2
- VERSION = '0.9.0'.freeze
2
+ VERSION = '0.10.0'.freeze
3
3
  end
@@ -32,6 +32,7 @@ $(document).ready(function() {
32
32
 
33
33
  // Slack OAuth
34
34
  var code = $.url('?code')
35
+ var state = $.url('?state')
35
36
  if (code) {
36
37
  SlackRubyBotServer.message('Working, please wait ...');
37
38
  $('#register').hide();
@@ -39,7 +40,8 @@ $(document).ready(function() {
39
40
  type: "POST",
40
41
  url: "/api/teams",
41
42
  data: {
42
- code: code
43
+ code: code,
44
+ state: state
43
45
  },
44
46
  success: function(data) {
45
47
  SlackRubyBotServer.message('Team successfully registered!<br><br>DM <b>@bot</b> or create a <b>#channel</b> and invite <b>@bot</b> to it.');
@@ -6,7 +6,6 @@ class CreateTeamsTable < ActiveRecord::Migration[5.0]
6
6
  t.boolean :active, default: true
7
7
  t.string :domain
8
8
  t.string :token
9
-
10
9
  t.timestamps
11
10
  end
12
11
  end
@@ -0,0 +1,7 @@
1
+ class AddActivatedFields < ActiveRecord::Migration[5.0]
2
+ def change
3
+ add_column :teams, :bot_user_id, :string
4
+ add_column :teams, :activated_user_id, :string
5
+ add_column :teams, :activated_user_access_token, :string
6
+ end
7
+ end
@@ -10,7 +10,7 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(version: 20_170_307_164_946) do
13
+ ActiveRecord::Schema.define(version: 20_190_323_181_453) do
14
14
  # These are extensions that must be enabled in order to support this database
15
15
  enable_extension 'plpgsql'
16
16
 
@@ -20,7 +20,10 @@ ActiveRecord::Schema.define(version: 20_170_307_164_946) do
20
20
  t.boolean 'active', default: true
21
21
  t.string 'domain'
22
22
  t.string 'token'
23
- t.datetime 'created_at', null: false
24
- t.datetime 'updated_at', null: false
23
+ t.datetime 'created_at', null: false
24
+ t.datetime 'updated_at', null: false
25
+ t.string 'bot_user_id'
26
+ t.string 'activated_user_id'
27
+ t.string 'activated_user_access_token'
25
28
  end
26
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-ruby-bot-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Doubrovkine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-25 00:00:00.000000000 Z
11
+ date: 2019-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async-websocket
@@ -221,7 +221,6 @@ files:
221
221
  - lib/slack-ruby-bot-server/ext/slack-ruby-bot.rb
222
222
  - lib/slack-ruby-bot-server/ext/slack-ruby-bot/client.rb
223
223
  - lib/slack-ruby-bot-server/info.rb
224
- - lib/slack-ruby-bot-server/models.rb
225
224
  - lib/slack-ruby-bot-server/models/team/activerecord.rb
226
225
  - lib/slack-ruby-bot-server/models/team/methods.rb
227
226
  - lib/slack-ruby-bot-server/models/team/mongoid.rb
@@ -252,6 +251,7 @@ files:
252
251
  - sample_apps/sample_app_activerecord/config/newrelic.yml
253
252
  - sample_apps/sample_app_activerecord/config/postgresql.yml
254
253
  - sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb
254
+ - sample_apps/sample_app_activerecord/db/migrate/20190323181453_add_activated_fields.rb
255
255
  - sample_apps/sample_app_activerecord/db/schema.rb
256
256
  - sample_apps/sample_app_activerecord/spec/api/root_spec.rb
257
257
  - sample_apps/sample_app_activerecord/spec/commands/help_spec.rb
@@ -1 +0,0 @@
1
- require 'slack-ruby-bot-server/models/team'