Skip to content

Commit

Permalink
Add tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
stijndcl committed Aug 4, 2023
1 parent 3b8d5b7 commit 7fceb91
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
43 changes: 19 additions & 24 deletions manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ type Manifest struct {
OAuthConfig OAuthConfig `json:"oauth_config,omitempty" yaml:"oauth_config,omitempty"`
}

// CreateManifest creates an app from an app manifest
func (api *Client) CreateManifest(manifest *Manifest, token string) (*ManifestResponse, error) {
return api.CreateManifestContext(context.Background(), manifest, token)
}

// CreateManifestContext creates an app from an app manifest with a custom context
func (api *Client) CreateManifestContext(ctx context.Context, manifest *Manifest, token string) (*ManifestResponse, error) {
if token == "" {
token = api.configToken
}

jsonBytes, err := json.Marshal(manifest)
if err != nil {
return nil, err
Expand All @@ -39,11 +45,17 @@ func (api *Client) CreateManifestContext(ctx context.Context, manifest *Manifest
return response, response.Err()
}

// DeleteManifest permanently deletes an app created through app manifests
func (api *Client) DeleteManifest(token string, appId string) (*SlackResponse, error) {
return api.DeleteManifestContext(context.Background(), token, appId)
}

// DeleteManifestContext permanently deletes an app created through app manifests with a custom context
func (api *Client) DeleteManifestContext(ctx context.Context, token string, appId string) (*SlackResponse, error) {
if token == "" {
token = api.configToken
}

values := url.Values{
"token": {token},
"app_id": {appId},
Expand All @@ -63,8 +75,12 @@ func (api *Client) ValidateManifest(manifest *Manifest, token string, appId stri
return api.ValidateManifestContext(context.Background(), manifest, token, appId)
}

// ValidateManifestContext sends a request to apps.manifest.validate to validate your app manifest with context
// ValidateManifestContext sends a request to apps.manifest.validate to validate your app manifest with a custom context
func (api *Client) ValidateManifestContext(ctx context.Context, manifest *Manifest, token string, appId string) (*ManifestResponse, error) {
if token == "" {
token = api.configToken
}

// Marshal manifest into string
jsonBytes, err := json.Marshal(manifest)
if err != nil {
Expand Down Expand Up @@ -191,35 +207,14 @@ type OAuthScopes struct {
User []string `json:"user,omitempty" yaml:"user,omitempty"`
}

// ManifestResponse is the response returned by the API
// this is a different format than SlackResponse, so we can't use that here
// However, it intentionally has an Err() method for similar usage
// ManifestResponse is the response returned by the API for app.manifest.x endpoints
type ManifestResponse struct {
Ok bool `json:"ok"`
Error string `json:"error,omitempty"`
Errors []ManifestValidationError `json:"errors,omitempty"`
}

func (m ManifestResponse) Err() error {
if m.Ok {
return nil
}

return ManifestErrorResponse{Err: m.Error, Errors: m.Errors}
SlackResponse
}

// ManifestValidationError is an error message returned for invalid manifests
type ManifestValidationError struct {
Message string `json:"message"`
Pointer string `json:"pointer"`
}

// ManifestErrorResponse is a helper struct to contain an error from a ManifestResponse
type ManifestErrorResponse struct {
Err string
Errors []ManifestValidationError
}

func (m ManifestErrorResponse) Error() string {
return m.Err
}
14 changes: 8 additions & 6 deletions slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ type authTestResponseFull struct {
type ParamOption func(*url.Values)

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

// Option defines an option for a Client
Expand Down
40 changes: 40 additions & 0 deletions tokens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package slack

import (
"context"
"net/url"
)

// RotateTokens exchanges a refresh token for a new app configuration token
func (api *Client) RotateTokens(refreshToken string) (*TokenResponse, error) {
return api.RotateTokensContext(context.Background(), refreshToken)
}

// RotateTokensContext exchanges a refresh token for a new app configuration token with a custom context
func (api *Client) RotateTokensContext(ctx context.Context, refreshToken string) (*TokenResponse, error) {
if refreshToken == "" {
refreshToken = api.configRefreshToken
}

values := url.Values{
"refresh_token": {refreshToken},
}

response := &TokenResponse{}
err := api.postMethod(ctx, "tooling.tokens.rotate", values, response)
if err != nil {
return nil, err
}

return response, response.Err()
}

type TokenResponse struct {
Token string `json:"token,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
TeamId string `json:"team_id,omitempty"`
UserId string `json:"user_id,omitempty"`
CreatedAt uint64 `json:"iat,omitempty"`
ExpiresAt uint64 `json:"exp,omitempty"`
SlackResponse
}

0 comments on commit 7fceb91

Please sign in to comment.