5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-23 10:31:36 +00:00
matterbridge/vendor/github.com/nlopes/slack/oauth.go

65 lines
2.4 KiB
Go
Raw Normal View History

2016-09-05 14:34:37 +00:00
package slack
import (
2017-07-16 12:29:46 +00:00
"context"
2016-09-05 14:34:37 +00:00
"net/url"
)
2018-12-01 18:55:35 +00:00
// OAuthResponseIncomingWebhook ...
2016-09-05 14:34:37 +00:00
type OAuthResponseIncomingWebhook struct {
URL string `json:"url"`
Channel string `json:"channel"`
2017-01-27 23:36:22 +00:00
ChannelID string `json:"channel_id,omitempty"`
2016-09-05 14:34:37 +00:00
ConfigurationURL string `json:"configuration_url"`
}
2018-12-01 18:55:35 +00:00
// OAuthResponseBot ...
2016-09-05 14:34:37 +00:00
type OAuthResponseBot struct {
BotUserID string `json:"bot_user_id"`
BotAccessToken string `json:"bot_access_token"`
}
2018-12-01 18:55:35 +00:00
// OAuthResponse ...
2016-09-05 14:34:37 +00:00
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 23:36:22 +00:00
UserID string `json:"user_id,omitempty"`
2016-09-05 14:34:37 +00:00
SlackResponse
}
// GetOAuthToken retrieves an AccessToken
2018-12-01 18:55:35 +00:00
func GetOAuthToken(client httpClient, clientID, clientSecret, code, redirectURI string) (accessToken string, scope string, err error) {
return GetOAuthTokenContext(context.Background(), client, clientID, clientSecret, code, redirectURI)
2017-07-16 12:29:46 +00:00
}
// GetOAuthTokenContext retrieves an AccessToken with a custom context
2018-12-01 18:55:35 +00:00
func GetOAuthTokenContext(ctx context.Context, client httpClient, clientID, clientSecret, code, redirectURI string) (accessToken string, scope string, err error) {
response, err := GetOAuthResponseContext(ctx, client, clientID, clientSecret, code, redirectURI)
2016-09-05 14:34:37 +00:00
if err != nil {
return "", "", err
}
return response.AccessToken, response.Scope, nil
}
2018-12-01 18:55:35 +00:00
func GetOAuthResponse(client httpClient, clientID, clientSecret, code, redirectURI string) (resp *OAuthResponse, err error) {
return GetOAuthResponseContext(context.Background(), client, clientID, clientSecret, code, redirectURI)
2017-07-16 12:29:46 +00:00
}
2018-12-01 18:55:35 +00:00
func GetOAuthResponseContext(ctx context.Context, client httpClient, clientID, clientSecret, code, redirectURI string) (resp *OAuthResponse, err error) {
2016-09-05 14:34:37 +00:00
values := url.Values{
"client_id": {clientID},
"client_secret": {clientSecret},
"code": {code},
"redirect_uri": {redirectURI},
}
response := &OAuthResponse{}
2018-12-01 18:55:35 +00:00
if err = postSlackMethod(ctx, client, "oauth.access", values, response, discard{}); err != nil {
2016-09-05 14:34:37 +00:00
return nil, err
}
2018-12-01 18:55:35 +00:00
return response, response.Err()
2016-09-05 14:34:37 +00:00
}