Skip to content

Commit

Permalink
Merge pull request #811 from cjdenio/master
Browse files Browse the repository at this point in the history
Implement app-level tokens + event authorizations
  • Loading branch information
kanata2 authored Oct 14, 2020
2 parents 0ca8e02 + 4cdb81a commit 84ef9d6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 14 deletions.
43 changes: 43 additions & 0 deletions apps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package slack

import (
"context"
"encoding/json"
)

type listEventAuthorizationsResponse struct {
SlackResponse
Authorizations []EventAuthorization `json:"authorizations"`
}

type EventAuthorization struct {
EnterpriseID string `json:"enterprise_id"`
TeamID string `json:"team_id"`
UserID string `json:"user_id"`
IsBot bool `json:"is_bot"`
IsEnterpriseInstall bool `json:"is_enterprise_install"`
}

func (api *Client) ListEventAuthorizations(eventContext string) ([]EventAuthorization, error) {
return api.ListEventAuthorizationsContext(context.Background(), eventContext)
}

// ListEventAuthorizationsContext lists authed users and teams for the given event_context. You must provide an app-level token to the client using OptionAppLevelToken. More info: https://api.slack.com/methods/apps.event.authorizations.list
func (api *Client) ListEventAuthorizationsContext(ctx context.Context, eventContext string) ([]EventAuthorization, error) {
resp := &listEventAuthorizationsResponse{}

request, _ := json.Marshal(map[string]string{
"event_context": eventContext,
})

err := postJSON(ctx, api.httpclient, api.endpoint+"apps.event.authorizations.list", api.appLevelToken, request, &resp, api)

if err != nil {
return nil, err
}
if !resp.Ok {
return nil, resp.Err()
}

return resp.Authorizations, nil
}
38 changes: 38 additions & 0 deletions apps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package slack

import (
"encoding/json"
"net/http"
"testing"
)

func TestListEventAuthorizations(t *testing.T) {
http.HandleFunc("/apps.event.authorizations.list", testListEventAuthorizationsHandler)
once.Do(startServer)

api := New("", OptionAppLevelToken("test-token"), OptionAPIURL("http://"+serverAddr+"/"))

authorizations, err := api.ListEventAuthorizations("1-message-T012345678-DR12345678")

if err != nil {
t.Errorf("Failed, but should have succeeded")
} else if len(authorizations) != 1 {
t.Errorf("Didn't get 1 authorization")
} else if authorizations[0].UserID != "U123456789" {
t.Errorf("User ID is wrong")
}
}

func testListEventAuthorizationsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(listEventAuthorizationsResponse{
SlackResponse: SlackResponse{Ok: true},
Authorizations: []EventAuthorization{
{
UserID: "U123456789",
TeamID: "T012345678",
},
},
})
w.Write(response)
}
16 changes: 11 additions & 5 deletions slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ type authTestResponseFull struct {
type ParamOption func(*url.Values)

type Client struct {
token string
endpoint string
debug bool
log ilogger
httpclient httpClient
token string
appLevelToken string
endpoint string
debug bool
log ilogger
httpclient httpClient
}

// Option defines an option for a Client
Expand Down Expand Up @@ -93,6 +94,11 @@ func OptionAPIURL(u string) func(*Client) {
return func(c *Client) { c.endpoint = u }
}

// OptionAppLevelToken sets an app-level token for the client.
func OptionAppLevelToken(token string) func(*Client) {
return func(c *Client) { c.appLevelToken = token }
}

// New builds a slack client from the provided token and options.
func New(token string, options ...Option) *Client {
s := &Client{
Expand Down
19 changes: 10 additions & 9 deletions slackevents/outer_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ type ChallengeResponse struct {

// EventsAPICallbackEvent is the main (outer) EventsAPI event.
type EventsAPICallbackEvent struct {
Type string `json:"type"`
Token string `json:"token"`
TeamID string `json:"team_id"`
APIAppID string `json:"api_app_id"`
InnerEvent *json.RawMessage `json:"event"`
AuthedUsers []string `json:"authed_users"`
AuthedTeams []string `json:"authed_teams"`
EventID string `json:"event_id"`
EventTime int `json:"event_time"`
Type string `json:"type"`
Token string `json:"token"`
TeamID string `json:"team_id"`
APIAppID string `json:"api_app_id"`
InnerEvent *json.RawMessage `json:"event"`
AuthedUsers []string `json:"authed_users"`
AuthedTeams []string `json:"authed_teams"`
EventID string `json:"event_id"`
EventTime int `json:"event_time"`
EventContext string `json:"event_context"`
}

// EventsAPIAppRateLimited indicates your app's event subscriptions are being rate limited
Expand Down

0 comments on commit 84ef9d6

Please sign in to comment.