Skip to content

Commit d3a24dd

Browse files
aok-solutionsdblock
authored andcommitted
Expose optional State parameter returned from Add to Slack button (#95)
1 parent 8d709a8 commit d3a24dd

File tree

9 files changed

+50
-71
lines changed

9 files changed

+50
-71
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* Your contribution here.
66

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+
79
#### 0.9.0 (2019/2/25)
810

911
* [#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).

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ You can introduce custom behavior into the service lifecycle via callbacks. This
103103
```ruby
104104
instance = SlackRubyBotServer::Service.instance
105105

106-
instance.on :created do |team, error|
106+
instance.on :created do |team, error, options|
107107
# a new team has been registered
108108
end
109109

110-
instance.on :deactivated do |team, error|
110+
instance.on :deactivated do |team, error, options|
111111
# an existing team has been deactivated in Slack
112112
end
113113

114-
instance.on :error do |team, error|
114+
instance.on :error do |team, error, options|
115115
# an error has occurred
116116
end
117117
```
@@ -132,6 +132,22 @@ The following callbacks are supported. All callbacks receive a `team`, except `e
132132
| deactivating | a team is being deactivated |
133133
| deactivated | a team has been deactivated |
134134

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+
135151
#### Server Class
136152

137153
You can override the server class to handle additional events, and configure the service to use it.

lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TeamsEndpoint < Grape::API
3333
desc 'Create a team using an OAuth token.'
3434
params do
3535
requires :code, type: String
36+
optional :state, type: String
3637
end
3738
post do
3839
client = Slack::Web::Client.new
@@ -60,7 +61,9 @@ class TeamsEndpoint < Grape::API
6061
)
6162
end
6263

63-
Service.instance.create!(team)
64+
options = params.slice(:state)
65+
66+
Service.instance.create!(team, options)
6467
present team, with: Presenters::TeamPresenter
6568
end
6669
end

lib/slack-ruby-bot-server/service.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def on(type, &block)
2121
@callbacks[type.to_s] << block
2222
end
2323

24-
def create!(team)
25-
run_callbacks :creating, team
24+
def create!(team, options = {})
25+
run_callbacks :creating, team, nil, options
2626
start!(team)
27-
run_callbacks :created, team
27+
run_callbacks :created, team, nil, options
2828
end
2929

3030
def start!(team)
@@ -103,11 +103,11 @@ def start_server!(team, server, wait = 1)
103103
end
104104
end
105105

106-
def run_callbacks(type, team = nil, error = nil)
106+
def run_callbacks(type, team = nil, error = nil, options = {})
107107
callbacks = @callbacks[type.to_s]
108108
return false unless callbacks
109109
callbacks.each do |c|
110-
c.call team, error
110+
c.call team, error, options
111111
end
112112
true
113113
rescue StandardError => e

public/scripts/register.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ $(document).ready(function() {
3232

3333
// Slack OAuth
3434
var code = $.url('?code')
35+
var state = $.url('?state')
3536
if (code) {
3637
SlackRubyBotServer.message('Working, please wait ...');
3738
$('#register').hide();
3839
$.ajax({
3940
type: "POST",
4041
url: "/api/teams",
4142
data: {
42-
code: code
43+
code: code,
44+
state: state
4345
},
4446
success: function(data) {
4547
SlackRubyBotServer.message('Team successfully registered!<br><br>DM <b>@bot</b> or create a <b>#channel</b> and invite <b>@bot</b> to it.');

spec/api/endpoints/teams_endpoint_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
expect(team.token).to eq 'token'
7373
end.to change(Team, :count).by(1)
7474
end
75+
76+
it 'includes optional state parameter' do
77+
expect(SlackRubyBotServer::Service.instance).to receive(:create!).with(instance_of(Team), state: 'property')
78+
client.teams._post(code: 'code', state: 'property')
79+
end
80+
7581
it 'reactivates a deactivated team' do
7682
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
7783
existing_team = Fabricate(:team, token: 'token', active: false)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
db_config = YAML.safe_load(File.read(File.expand_path('../../../sample_apps/sample_app_activerecord/config/postgresql.yml', __dir__)), [], [], true)[ENV['RACK_ENV']]
22
ActiveRecord::Tasks::DatabaseTasks.create(db_config)
33
ActiveRecord::Base.establish_connection(db_config)
4+
ActiveRecord::Base.logger.level = :info

spec/fixtures/slack/auth_test.yml

Lines changed: 0 additions & 57 deletions
This file was deleted.

spec/integration/teams_spec.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@
99
ENV.delete 'SLACK_CLIENT_ID'
1010
ENV.delete 'SLACK_CLIENT_SECRET'
1111
end
12-
context 'oauth', vcr: { cassette_name: 'auth_test' } do
13-
it 'registers a team' do
14-
allow_any_instance_of(Team).to receive(:ping!).and_return(ok: true)
15-
expect(SlackRubyBotServer::Service.instance).to receive(:start!)
12+
context 'oauth' do
13+
before do
1614
oauth_access = { 'bot' => { 'bot_access_token' => 'token' }, 'team_id' => 'team_id', 'team_name' => 'team_name' }
1715
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with(hash_including(code: 'code')).and_return(oauth_access)
16+
end
17+
it 'registers a team' do
18+
allow_any_instance_of(Team).to receive(:ping!).and_return(ok: true)
19+
expect(SlackRubyBotServer::Service.instance).to receive(:start!).with(instance_of(Team))
1820
expect do
1921
visit '/?code=code'
2022
expect(page.find('#messages')).to have_content 'Team successfully registered!'
2123
end.to change(Team, :count).by(1)
2224
end
25+
it 'includes optional parameter' do
26+
expect(SlackRubyBotServer::Service.instance).to receive(:create!).with(instance_of(Team), state: 'property')
27+
visit '/?code=code&state=property'
28+
end
2329
end
2430
context 'homepage' do
2531
before do

0 commit comments

Comments
 (0)