Skip to content

Commit

Permalink
added method tooling.tokens.rotate
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Halturin <[email protected]>
  • Loading branch information
dhalturin committed Feb 10, 2024
1 parent c4095cb commit 77648e1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
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)
}

0 comments on commit 77648e1

Please sign in to comment.