matterbridge/vendor/github.com/nlopes/slack/oauth.go

67 lines
2.3 KiB
Go
Raw Normal View History

2016-09-05 07:34:37 -07:00
package slack
import (
2017-07-16 05:29:46 -07:00
"context"
2016-09-05 07:34:37 -07:00
"errors"
"net/url"
)
type OAuthResponseIncomingWebhook struct {
URL string `json:"url"`
Channel string `json:"channel"`
2017-01-27 15:36:22 -08:00
ChannelID string `json:"channel_id,omitempty"`
2016-09-05 07:34:37 -07:00
ConfigurationURL string `json:"configuration_url"`
}
type OAuthResponseBot struct {
BotUserID string `json:"bot_user_id"`
BotAccessToken string `json:"bot_access_token"`
}
type OAuthResponse struct {
AccessToken string `json:"access_token"`
Scope string `json:"scope"`
TeamName string `json:"team_name"`
TeamID string `json:"team_id"`
IncomingWebhook OAuthResponseIncomingWebhook `json:"incoming_webhook"`
Bot OAuthResponseBot `json:"bot"`
2017-01-27 15:36:22 -08:00
UserID string `json:"user_id,omitempty"`
2016-09-05 07:34:37 -07:00
SlackResponse
}
// GetOAuthToken retrieves an AccessToken
func GetOAuthToken(clientID, clientSecret, code, redirectURI string, debug bool) (accessToken string, scope string, err error) {
2017-07-16 05:29:46 -07:00
return GetOAuthTokenContext(context.Background(), clientID, clientSecret, code, redirectURI, debug)
}
// GetOAuthTokenContext retrieves an AccessToken with a custom context
func GetOAuthTokenContext(ctx context.Context, clientID, clientSecret, code, redirectURI string, debug bool) (accessToken string, scope string, err error) {
response, err := GetOAuthResponseContext(ctx, clientID, clientSecret, code, redirectURI, debug)
2016-09-05 07:34:37 -07:00
if err != nil {
return "", "", err
}
return response.AccessToken, response.Scope, nil
}
func GetOAuthResponse(clientID, clientSecret, code, redirectURI string, debug bool) (resp *OAuthResponse, err error) {
2017-07-16 05:29:46 -07:00
return GetOAuthResponseContext(context.Background(), clientID, clientSecret, code, redirectURI, debug)
}
func GetOAuthResponseContext(ctx context.Context, clientID, clientSecret, code, redirectURI string, debug bool) (resp *OAuthResponse, err error) {
2016-09-05 07:34:37 -07:00
values := url.Values{
"client_id": {clientID},
"client_secret": {clientSecret},
"code": {code},
"redirect_uri": {redirectURI},
}
response := &OAuthResponse{}
2018-08-09 15:38:19 -07:00
err = postSlackMethod(ctx, customHTTPClient, "oauth.access", values, response, debug)
2016-09-05 07:34:37 -07:00
if err != nil {
return nil, err
}
if !response.Ok {
return nil, errors.New(response.Error)
}
return response, nil
}