Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added method tooling.tokens.rotate #1204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tooling_tokens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package slack

import (
"context"
"net/url"
)

// ToolingTokensRotate contains our Auth response from the tooling.tokens.rotate endpoint
type ToolingTokensRotate struct {
SlackResponse // Contains the "ok", and "Error", if any
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
TeamID string `json:"team_id"`
UserID string `json:"user_id"`
Iat int64 `json:"iat"`
Exp int64 `json:"exp"`
}

// ToolingTokensRotate will send a refresh for our token
func (api *Client) ToolingTokensRotate(refresh_token string) (*ToolingTokensRotate, error) {
return api.ToolingTokensRotateContext(context.Background(), refresh_token)
}

// ToolingTokensRotateContext will send a refresh request for our token
func (api *Client) ToolingTokensRotateContext(ctx context.Context, refresh_token string) (*ToolingTokensRotate, error) {
response := &ToolingTokensRotate{}
err := api.postMethod(ctx, "tooling.tokens.rotate", url.Values{"refresh_token": {refresh_token}}, response)
if err != nil {
return nil, err
}

return response, response.Err()
}
45 changes: 45 additions & 0 deletions tooling_tokens_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package slack

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func toolingTokensRotate(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
response := []byte(`{
"ok": true,
"token": "xoxe.xoxp-...",
"refresh_token": "xoxe-...",
"team_id": "...",
"user_id": "...",
"iat": 1633095660,
"exp": 1633138860
}`)
rw.Write(response)
}

func TestToolingTokensRotate(t *testing.T) {
http.HandleFunc("/tooling.tokens.rotate", toolingTokensRotate)

once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))

token, err := api.ToolingTokensRotate("xoxe.xoxp-...")
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}

assert.Equal(t, true, token.Ok)
assert.Equal(t, "", token.Error)

assert.Equal(t, "xoxe.xoxp-...", token.Token)
assert.Equal(t, "xoxe-...", token.RefreshToken)
assert.Equal(t, "...", token.TeamID)
assert.Equal(t, "...", token.UserID)
assert.Equal(t, int64(1633095660), token.Iat)
assert.Equal(t, int64(1633138860), token.Exp)
}
Loading