Skip to content

Commit

Permalink
Request /team.info when team:read scope is present (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
panayi authored and doomspork committed Jul 18, 2016
1 parent 09b329e commit d137247
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/ueberauth/strategy/slack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ defmodule Ueberauth.Strategy.Slack do
|> store_token(token)
|> fetch_auth(token)
|> fetch_user(token)
|> fetch_team(token)
end
end

Expand Down Expand Up @@ -157,7 +158,8 @@ defmodule Ueberauth.Strategy.Slack do
raw_info: %{
auth: conn.private.slack_auth,
token: conn.private.slack_token,
user: conn.private.slack_user
user: conn.private.slack_user,
team: conn.private.slack_team
}
}
end
Expand Down Expand Up @@ -200,6 +202,33 @@ defmodule Ueberauth.Strategy.Slack do
end
end

defp fetch_team(%Plug.Conn{ assigns: %{ ueberauth_failure: _fails }} = conn, _), do: conn

defp fetch_team(conn, token) do
auth = conn.private.slack_auth
token = conn.private.slack_token

scopes = (token.other_params["scope"] || "")
|> String.split(",")

case "team:read" in scopes do
false -> conn
true ->
case OAuth2.AccessToken.post(token, "/team.info", [token: token.access_token], [{"Content-Type", "application/x-www-form-urlencoded"}]) do
{ :ok, %OAuth2.Response{status_code: 401, body: _body}} ->
set_errors!(conn, [error("token", "unauthorized")])
{ :ok, %OAuth2.Response{status_code: status_code, body: team} } when status_code in 200..399 ->
if team["ok"] do
put_private(conn, :slack_team, team["team"])
else
set_errors!(conn, [error(team["error"], team["error"])])
end
{ :error, %OAuth2.Error{reason: reason} } ->
set_errors!(conn, [error("OAuth2", reason)])
end
end
end

# Fetch the name to use. We try to start with the most specific name avaialble and
# fallback to the least.
defp name_from_user(user) do
Expand Down

0 comments on commit d137247

Please sign in to comment.