-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #811 from cjdenio/master
Implement app-level tokens + event authorizations
- Loading branch information
Showing
4 changed files
with
102 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters