Skip to content

Commit dcc26d5

Browse files
committed
Added Team#oauth_version and #scope.
1 parent 47b28ee commit dcc26d5

File tree

8 files changed

+89
-24
lines changed

8 files changed

+89
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
#### 1.2.0 (Next)
44

5+
* [#133](https://github.com/slack-ruby/slack-ruby-bot-server/pull/133): Added `Team#oauth_version` and `#scope` - [@dblock](https://github.com/dblock).
56
* Your contribution here.
67

78
#### 1.1.0 (2020/11/17)
89

9-
* [#132](https://github.com/slack-ruby/slack-ruby-bot-server/pull/132): Add support for OAuth v2 - [@dblock](https://github.com/dblock).
10+
* [#132](https://github.com/slack-ruby/slack-ruby-bot-server/pull/132): Added support for OAuth v2 - [@dblock](https://github.com/dblock).
1011

1112
#### 1.0.0 (2020/11/15)
1213

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ end
272272

273273
### Access Tokens
274274

275-
By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) stores the value of the token with all the requested OAuth scopes in both `token` and `activated_user_access_token` (for backwards compatibility). If a legacy Slack bot integration `bot_access_token` is present, it is stored as `token`, and `activated_user_access_token`is the token that has all the requested OAuth scopes.
275+
By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) stores the value of the token with all the requested OAuth scopes in both `token` and `activated_user_access_token` (for backwards compatibility), along with `oauth_version` and `oauth_scope`. If a legacy Slack bot integration `bot_access_token` is present, it is stored as `token`, and `activated_user_access_token` is the token that has all the requested OAuth scopes.
276276

277277
## Sample Bots Using Slack Ruby Bot Server
278278

UPGRADING.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
Upgrading Slack-Ruby-Bot-Server
22
===============================
33

4+
### Upgrading to >= 1.2.0
5+
6+
#### New Team Fields
7+
8+
The following fields have been added to `Team`.
9+
10+
* `oauth_scope`: Slack OAuth scope
11+
* `oauth_version`: Slack OAuth version used
12+
13+
No action is required for Mongoid.
14+
15+
If you're using ActiveRecord, create a migration to add these fields.
16+
17+
```ruby
18+
class AddOauthFields < ActiveRecord::Migration[5.0]
19+
def change
20+
add_column :teams, :oauth_scope, :string
21+
add_column :teams, :oauth_version, :string, default: 'v1', null: false
22+
end
23+
end
24+
```
25+
426
### Upgrading to >= 1.1.0
527

628
#### Extracted RealTime (Legacy) Support

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,18 @@ class TeamsEndpoint < Grape::API
5454
bot_user_id = nil
5555
team_id = nil
5656
team_name = nil
57+
oauth_scope = nil
58+
oauth_version = SlackRubyBotServer::Config.oauth_version
5759

58-
case SlackRubyBotServer::Config.oauth_version
60+
case oauth_version
5961
when :v2
6062
access_token = rc.access_token
6163
token = rc.access_token
6264
user_id = rc.authed_user&.id
6365
bot_user_id = rc.bot_user_id
6466
team_id = rc.team&.id
6567
team_name = rc.team&.name
68+
oauth_scope = rc.scope
6669
when :v1
6770
access_token = rc.access_token
6871
bot = rc.bot if rc.key?(:bot)
@@ -71,15 +74,19 @@ class TeamsEndpoint < Grape::API
7174
bot_user_id = bot ? bot.bot_user_id : nil
7275
team_id = rc.team_id
7376
team_name = rc.team_name
77+
oauth_scope = rc.scope
7478
end
7579

7680
team = Team.where(token: token).first
81+
team ||= Team.where(team_id: team_id, oauth_version: oauth_version).first
7782
team ||= Team.where(team_id: team_id).first
7883

7984
if team
8085
team.ping_if_active!
8186

8287
team.update_attributes!(
88+
oauth_version: oauth_version,
89+
oauth_scope: oauth_scope,
8390
activated_user_id: user_id,
8491
activated_user_access_token: access_token,
8592
bot_user_id: bot_user_id
@@ -91,6 +98,8 @@ class TeamsEndpoint < Grape::API
9198
else
9299
team = Team.create!(
93100
token: token,
101+
oauth_version: oauth_version,
102+
oauth_scope: oauth_scope,
94103
team_id: team_id,
95104
name: team_name,
96105
activated_user_id: user_id,

lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def self.init!
1818
t.string :name
1919
t.string :domain
2020
t.string :token
21+
t.string :oauth_scope
22+
t.string :oauth_version, default: 'v1', null: false
2123
t.string :bot_user_id
2224
t.string :activated_user_id
2325
t.string :activated_user_access_token

lib/slack-ruby-bot-server/models/team/mongoid.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Team
88
field :name, type: String
99
field :domain, type: String
1010
field :token, type: String
11+
field :oauth_scope, type: String
12+
field :oauth_version, type: String, default: 'v1'
1113
field :active, type: Boolean, default: true
1214
field :bot_user_id, type: String
1315
field :activated_user_id, type: String

spec/api/endpoints/teams_endpoint_spec.rb

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@
4848
context 'register a bot via oauth v2' do
4949
before do
5050
SlackRubyBotServer.config.oauth_version = :v2
51-
oauth_access = Slack::Messages::Message.new({
52-
'access_token' => 'access_token',
53-
'authed_user' => { 'id' => 'user_id' },
54-
'team' => { 'id' => 'team_id', 'name' => 'team_name' }
55-
})
51+
oauth_access = Slack::Messages::Message.new(
52+
'access_token' => 'access_token',
53+
'scope' => 'commands,incoming-webhook',
54+
'authed_user' => { 'id' => 'user_id' },
55+
'team' => { 'id' => 'team_id', 'name' => 'team_name' }
56+
)
5657
ENV['SLACK_CLIENT_ID'] = 'client_id'
5758
ENV['SLACK_CLIENT_SECRET'] = 'client_secret'
5859
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_v2_access).with(
@@ -75,6 +76,8 @@
7576
expect(team.name).to eq 'team_name'
7677
team = Team.find(team.id)
7778
expect(team.token).to eq 'access_token'
79+
expect(team.oauth_version).to eq 'v2'
80+
expect(team.oauth_scope).to eq 'commands,incoming-webhook'
7881
expect(team.activated_user_access_token).to eq 'access_token'
7982
expect(team.activated_user_id).to eq 'user_id'
8083
expect(team.bot_user_id).to be nil
@@ -96,6 +99,8 @@
9699
expect(team.active).to be true
97100
team = Team.find(team.id)
98101
expect(team.token).to eq 'access_token'
102+
expect(team.oauth_version).to eq 'v2'
103+
expect(team.oauth_scope).to eq 'commands,incoming-webhook'
99104
expect(team.active).to be true
100105
expect(team.activated_user_access_token).to eq 'access_token'
101106
expect(team.activated_user_id).to eq 'user_id'
@@ -113,6 +118,8 @@
113118
expect(team.active).to be true
114119
team = Team.find(team.id)
115120
expect(team.token).to eq 'access_token'
121+
expect(team.oauth_version).to eq 'v2'
122+
expect(team.oauth_scope).to eq 'commands,incoming-webhook'
116123
expect(team.active).to be true
117124
expect(team.bot_user_id).to be nil
118125
expect(team.activated_user_id).to eq 'user_id'
@@ -136,6 +143,8 @@
136143
expect(team.active).to be true
137144
team = Team.find(team.id)
138145
expect(team.token).to eq 'access_token'
146+
expect(team.oauth_version).to eq 'v2'
147+
expect(team.oauth_scope).to eq 'commands,incoming-webhook'
139148
expect(team.active).to be true
140149
expect(team.activated_user_access_token).to eq 'access_token'
141150
expect(team.activated_user_id).to eq 'user_id'
@@ -147,12 +156,13 @@
147156
context 'register a bot via oauth v1' do
148157
before do
149158
SlackRubyBotServer.config.oauth_version = :v1
150-
oauth_access = Slack::Messages::Message.new({
151-
'access_token' => 'access_token',
152-
'user_id' => 'user_id',
153-
'team_id' => 'team_id',
154-
'team_name' => 'team_name'
155-
})
159+
oauth_access = Slack::Messages::Message.new(
160+
'access_token' => 'access_token',
161+
'scope' => 'incoming-webhook,commands,bot',
162+
'user_id' => 'user_id',
163+
'team_id' => 'team_id',
164+
'team_name' => 'team_name'
165+
)
156166
ENV['SLACK_CLIENT_ID'] = 'client_id'
157167
ENV['SLACK_CLIENT_SECRET'] = 'client_secret'
158168
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with(
@@ -175,6 +185,8 @@
175185
expect(team.name).to eq 'team_name'
176186
team = Team.find(team.id)
177187
expect(team.token).to eq 'access_token'
188+
expect(team.oauth_version).to eq 'v1'
189+
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
178190
expect(team.activated_user_access_token).to eq 'access_token'
179191
expect(team.activated_user_id).to eq 'user_id'
180192
expect(team.bot_user_id).to be nil
@@ -196,6 +208,8 @@
196208
expect(team.active).to be true
197209
team = Team.find(team.id)
198210
expect(team.token).to eq 'access_token'
211+
expect(team.oauth_version).to eq 'v1'
212+
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
199213
expect(team.active).to be true
200214
expect(team.activated_user_access_token).to eq 'access_token'
201215
expect(team.activated_user_id).to eq 'user_id'
@@ -213,6 +227,8 @@
213227
expect(team.active).to be true
214228
team = Team.find(team.id)
215229
expect(team.token).to eq 'access_token'
230+
expect(team.oauth_version).to eq 'v1'
231+
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
216232
expect(team.active).to be true
217233
expect(team.bot_user_id).to be nil
218234
expect(team.activated_user_id).to eq 'user_id'
@@ -236,6 +252,8 @@
236252
expect(team.active).to be true
237253
team = Team.find(team.id)
238254
expect(team.token).to eq 'access_token'
255+
expect(team.oauth_version).to eq 'v1'
256+
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
239257
expect(team.active).to be true
240258
expect(team.activated_user_access_token).to eq 'access_token'
241259
expect(team.activated_user_id).to eq 'user_id'
@@ -247,16 +265,17 @@
247265
context 'register a legacy bot' do
248266
before do
249267
SlackRubyBotServer.config.oauth_version = :v1
250-
oauth_access = Slack::Messages::Message.new({
251-
'bot' => {
252-
'bot_access_token' => 'token',
253-
'bot_user_id' => 'bot_user_id'
254-
},
255-
'access_token' => 'access_token',
256-
'user_id' => 'user_id',
257-
'team_id' => 'team_id',
258-
'team_name' => 'team_name'
259-
})
268+
oauth_access = Slack::Messages::Message.new(
269+
'bot' => {
270+
'bot_access_token' => 'token',
271+
'bot_user_id' => 'bot_user_id'
272+
},
273+
'access_token' => 'access_token',
274+
'scope' => 'incoming-webhook,commands,bot',
275+
'user_id' => 'user_id',
276+
'team_id' => 'team_id',
277+
'team_name' => 'team_name'
278+
)
260279
ENV['SLACK_CLIENT_ID'] = 'client_id'
261280
ENV['SLACK_CLIENT_SECRET'] = 'client_secret'
262281
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with(
@@ -279,6 +298,8 @@
279298
expect(team.name).to eq 'team_name'
280299
team = Team.find(team.id)
281300
expect(team.token).to eq 'token'
301+
expect(team.oauth_version).to eq 'v1'
302+
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
282303
expect(team.activated_user_access_token).to eq 'access_token'
283304
expect(team.activated_user_id).to eq 'user_id'
284305
expect(team.bot_user_id).to eq 'bot_user_id'
@@ -300,6 +321,8 @@
300321
expect(team.active).to be true
301322
team = Team.find(team.id)
302323
expect(team.token).to eq 'token'
324+
expect(team.oauth_version).to eq 'v1'
325+
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
303326
expect(team.active).to be true
304327
expect(team.activated_user_access_token).to eq 'access_token'
305328
expect(team.activated_user_id).to eq 'user_id'
@@ -317,6 +340,8 @@
317340
expect(team.active).to be true
318341
team = Team.find(team.id)
319342
expect(team.token).to eq 'token'
343+
expect(team.oauth_version).to eq 'v1'
344+
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
320345
expect(team.active).to be true
321346
expect(team.bot_user_id).to eq 'bot_user_id'
322347
expect(team.activated_user_id).to eq 'user_id'
@@ -340,6 +365,8 @@
340365
expect(team.active).to be true
341366
team = Team.find(team.id)
342367
expect(team.token).to eq 'token'
368+
expect(team.oauth_version).to eq 'v1'
369+
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
343370
expect(team.active).to be true
344371
expect(team.activated_user_access_token).to eq 'access_token'
345372
expect(team.activated_user_id).to eq 'user_id'

spec/database_adapters/activerecord/schema.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
t.string :name
99
t.string :domain
1010
t.string :token
11+
t.string :oauth_scope
12+
t.string :oauth_version, default: 'v1', null: false
1113
t.string :bot_user_id
1214
t.string :activated_user_id
1315
t.string :activated_user_access_token

0 commit comments

Comments
 (0)