Skip to content

Commit

Permalink
Added Team#oauth_version and #scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Nov 25, 2020
1 parent 47b28ee commit 836bc0a
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 24 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

#### 1.2.0 (Next)

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ end

### Access Tokens

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.
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.

## Sample Bots Using Slack Ruby Bot Server

Expand Down
22 changes: 22 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
Upgrading Slack-Ruby-Bot-Server
===============================

### Upgrading to >= 1.2.0

#### New Team Fields

The following fields have been added to `Team`.

* `oauth_scope`: Slack OAuth scope
* `oauth_version`: Slack OAuth version used

No action is required for Mongoid.

If you're using ActiveRecord, create a migration to add these fields.

```ruby
class AddActivatedFields < ActiveRecord::Migration[5.0]
def change
add_column :teams, :oauth_scope, :string
add_column :teams, :oauth_version, :string
end
end
```

### Upgrading to >= 1.1.0

#### Extracted RealTime (Legacy) Support
Expand Down
11 changes: 10 additions & 1 deletion lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@ class TeamsEndpoint < Grape::API
bot_user_id = nil
team_id = nil
team_name = nil
oauth_scope = nil
oauth_version = SlackRubyBotServer::Config.oauth_version

case SlackRubyBotServer::Config.oauth_version
case oauth_version
when :v2
access_token = rc.access_token
token = rc.access_token
user_id = rc.authed_user&.id
bot_user_id = rc.bot_user_id
team_id = rc.team&.id
team_name = rc.team&.name
oauth_scope = rc.scope
when :v1
access_token = rc.access_token
bot = rc.bot if rc.key?(:bot)
Expand All @@ -71,15 +74,19 @@ class TeamsEndpoint < Grape::API
bot_user_id = bot ? bot.bot_user_id : nil
team_id = rc.team_id
team_name = rc.team_name
oauth_scope = rc.scope
end

team = Team.where(token: token).first
team ||= Team.where(team_id: team_id, oauth_version: oauth_version).first
team ||= Team.where(team_id: team_id).first

if team
team.ping_if_active!

team.update_attributes!(
oauth_version: oauth_version,
oauth_scope: oauth_scope,
activated_user_id: user_id,
activated_user_access_token: access_token,
bot_user_id: bot_user_id
Expand All @@ -91,6 +98,8 @@ class TeamsEndpoint < Grape::API
else
team = Team.create!(
token: token,
oauth_version: oauth_version,
oauth_scope: oauth_scope,
team_id: team_id,
name: team_name,
activated_user_id: user_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def self.init!
t.string :name
t.string :domain
t.string :token
t.string :oauth_scope
t.string :oauth_version
t.string :bot_user_id
t.string :activated_user_id
t.string :activated_user_access_token
Expand Down
2 changes: 2 additions & 0 deletions lib/slack-ruby-bot-server/models/team/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Team
field :name, type: String
field :domain, type: String
field :token, type: String
field :oauth_scope, type: String
field :oauth_version, type: String
field :active, type: Boolean, default: true
field :bot_user_id, type: String
field :activated_user_id, type: String
Expand Down
69 changes: 48 additions & 21 deletions spec/api/endpoints/teams_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@
context 'register a bot via oauth v2' do
before do
SlackRubyBotServer.config.oauth_version = :v2
oauth_access = Slack::Messages::Message.new({
'access_token' => 'access_token',
'authed_user' => { 'id' => 'user_id' },
'team' => { 'id' => 'team_id', 'name' => 'team_name' }
})
oauth_access = Slack::Messages::Message.new(
'access_token' => 'access_token',
'scope' => 'commands,incoming-webhook',
'authed_user' => { 'id' => 'user_id' },
'team' => { 'id' => 'team_id', 'name' => 'team_name' }
)
ENV['SLACK_CLIENT_ID'] = 'client_id'
ENV['SLACK_CLIENT_SECRET'] = 'client_secret'
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_v2_access).with(
Expand All @@ -75,6 +76,8 @@
expect(team.name).to eq 'team_name'
team = Team.find(team.id)
expect(team.token).to eq 'access_token'
expect(team.oauth_version).to eq 'v2'
expect(team.oauth_scope).to eq 'commands,incoming-webhook'
expect(team.activated_user_access_token).to eq 'access_token'
expect(team.activated_user_id).to eq 'user_id'
expect(team.bot_user_id).to be nil
Expand All @@ -96,6 +99,8 @@
expect(team.active).to be true
team = Team.find(team.id)
expect(team.token).to eq 'access_token'
expect(team.oauth_version).to eq 'v2'
expect(team.oauth_scope).to eq 'commands,incoming-webhook'
expect(team.active).to be true
expect(team.activated_user_access_token).to eq 'access_token'
expect(team.activated_user_id).to eq 'user_id'
Expand All @@ -113,6 +118,8 @@
expect(team.active).to be true
team = Team.find(team.id)
expect(team.token).to eq 'access_token'
expect(team.oauth_version).to eq 'v2'
expect(team.oauth_scope).to eq 'commands,incoming-webhook'
expect(team.active).to be true
expect(team.bot_user_id).to be nil
expect(team.activated_user_id).to eq 'user_id'
Expand All @@ -136,6 +143,8 @@
expect(team.active).to be true
team = Team.find(team.id)
expect(team.token).to eq 'access_token'
expect(team.oauth_version).to eq 'v2'
expect(team.oauth_scope).to eq 'commands,incoming-webhook'
expect(team.active).to be true
expect(team.activated_user_access_token).to eq 'access_token'
expect(team.activated_user_id).to eq 'user_id'
Expand All @@ -147,12 +156,13 @@
context 'register a bot via oauth v1' do
before do
SlackRubyBotServer.config.oauth_version = :v1
oauth_access = Slack::Messages::Message.new({
'access_token' => 'access_token',
'user_id' => 'user_id',
'team_id' => 'team_id',
'team_name' => 'team_name'
})
oauth_access = Slack::Messages::Message.new(
'access_token' => 'access_token',
'scope' => 'incoming-webhook,commands,bot',
'user_id' => 'user_id',
'team_id' => 'team_id',
'team_name' => 'team_name'
)
ENV['SLACK_CLIENT_ID'] = 'client_id'
ENV['SLACK_CLIENT_SECRET'] = 'client_secret'
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with(
Expand All @@ -175,6 +185,8 @@
expect(team.name).to eq 'team_name'
team = Team.find(team.id)
expect(team.token).to eq 'access_token'
expect(team.oauth_version).to eq 'v1'
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
expect(team.activated_user_access_token).to eq 'access_token'
expect(team.activated_user_id).to eq 'user_id'
expect(team.bot_user_id).to be nil
Expand All @@ -196,6 +208,8 @@
expect(team.active).to be true
team = Team.find(team.id)
expect(team.token).to eq 'access_token'
expect(team.oauth_version).to eq 'v1'
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
expect(team.active).to be true
expect(team.activated_user_access_token).to eq 'access_token'
expect(team.activated_user_id).to eq 'user_id'
Expand All @@ -213,6 +227,8 @@
expect(team.active).to be true
team = Team.find(team.id)
expect(team.token).to eq 'access_token'
expect(team.oauth_version).to eq 'v1'
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
expect(team.active).to be true
expect(team.bot_user_id).to be nil
expect(team.activated_user_id).to eq 'user_id'
Expand All @@ -236,6 +252,8 @@
expect(team.active).to be true
team = Team.find(team.id)
expect(team.token).to eq 'access_token'
expect(team.oauth_version).to eq 'v1'
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
expect(team.active).to be true
expect(team.activated_user_access_token).to eq 'access_token'
expect(team.activated_user_id).to eq 'user_id'
Expand All @@ -247,16 +265,17 @@
context 'register a legacy bot' do
before do
SlackRubyBotServer.config.oauth_version = :v1
oauth_access = Slack::Messages::Message.new({
'bot' => {
'bot_access_token' => 'token',
'bot_user_id' => 'bot_user_id'
},
'access_token' => 'access_token',
'user_id' => 'user_id',
'team_id' => 'team_id',
'team_name' => 'team_name'
})
oauth_access = Slack::Messages::Message.new(
'bot' => {
'bot_access_token' => 'token',
'bot_user_id' => 'bot_user_id'
},
'access_token' => 'access_token',
'scope' => 'incoming-webhook,commands,bot',
'user_id' => 'user_id',
'team_id' => 'team_id',
'team_name' => 'team_name'
)
ENV['SLACK_CLIENT_ID'] = 'client_id'
ENV['SLACK_CLIENT_SECRET'] = 'client_secret'
allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with(
Expand All @@ -279,6 +298,8 @@
expect(team.name).to eq 'team_name'
team = Team.find(team.id)
expect(team.token).to eq 'token'
expect(team.oauth_version).to eq 'v1'
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
expect(team.activated_user_access_token).to eq 'access_token'
expect(team.activated_user_id).to eq 'user_id'
expect(team.bot_user_id).to eq 'bot_user_id'
Expand All @@ -300,6 +321,8 @@
expect(team.active).to be true
team = Team.find(team.id)
expect(team.token).to eq 'token'
expect(team.oauth_version).to eq 'v1'
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
expect(team.active).to be true
expect(team.activated_user_access_token).to eq 'access_token'
expect(team.activated_user_id).to eq 'user_id'
Expand All @@ -317,6 +340,8 @@
expect(team.active).to be true
team = Team.find(team.id)
expect(team.token).to eq 'token'
expect(team.oauth_version).to eq 'v1'
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
expect(team.active).to be true
expect(team.bot_user_id).to eq 'bot_user_id'
expect(team.activated_user_id).to eq 'user_id'
Expand All @@ -340,6 +365,8 @@
expect(team.active).to be true
team = Team.find(team.id)
expect(team.token).to eq 'token'
expect(team.oauth_version).to eq 'v1'
expect(team.oauth_scope).to eq 'incoming-webhook,commands,bot'
expect(team.active).to be true
expect(team.activated_user_access_token).to eq 'access_token'
expect(team.activated_user_id).to eq 'user_id'
Expand Down
2 changes: 2 additions & 0 deletions spec/database_adapters/activerecord/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
t.string :name
t.string :domain
t.string :token
t.string :oath_scope
t.string :oath_version
t.string :bot_user_id
t.string :activated_user_id
t.string :activated_user_access_token
Expand Down

0 comments on commit 836bc0a

Please sign in to comment.