5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-14 03:50:26 +00:00
matterbridge/vendor/github.com/slack-go/slack/oauth.go

159 lines
6.5 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
}
2020-03-28 22:50:47 +00:00
// OAuthV2Response ...
type OAuthV2Response struct {
2020-09-04 21:29:13 +00:00
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
BotUserID string `json:"bot_user_id"`
AppID string `json:"app_id"`
Team OAuthV2ResponseTeam `json:"team"`
IncomingWebhook OAuthResponseIncomingWebhook `json:"incoming_webhook"`
Enterprise OAuthV2ResponseEnterprise `json:"enterprise"`
AuthedUser OAuthV2ResponseAuthedUser `json:"authed_user"`
2021-12-11 23:05:15 +00:00
RefreshToken string `json:"refresh_token"`
ExpiresIn int `json:"expires_in"`
2020-03-28 22:50:47 +00:00
SlackResponse
}
// OAuthV2ResponseTeam ...
type OAuthV2ResponseTeam struct {
ID string `json:"id"`
Name string `json:"name"`
}
// OAuthV2ResponseEnterprise ...
type OAuthV2ResponseEnterprise struct {
ID string `json:"id"`
Name string `json:"name"`
}
// OAuthV2ResponseAuthedUser ...
type OAuthV2ResponseAuthedUser struct {
2022-03-12 18:41:07 +00:00
ID string `json:"id"`
Scope string `json:"scope"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
2020-03-28 22:50:47 +00:00
}
2016-09-05 14:34:37 +00:00
// 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
}
// GetBotOAuthToken retrieves top-level and bot AccessToken - https://api.slack.com/legacy/oauth#bot_user_access_tokens
func GetBotOAuthToken(client httpClient, clientID, clientSecret, code, redirectURI string) (accessToken string, scope string, bot OAuthResponseBot, err error) {
return GetBotOAuthTokenContext(context.Background(), client, clientID, clientSecret, code, redirectURI)
}
// GetBotOAuthTokenContext retrieves top-level and bot AccessToken with a custom context
func GetBotOAuthTokenContext(ctx context.Context, client httpClient, clientID, clientSecret, code, redirectURI string) (accessToken string, scope string, bot OAuthResponseBot, err error) {
response, err := GetOAuthResponseContext(ctx, client, clientID, clientSecret, code, redirectURI)
if err != nil {
return "", "", OAuthResponseBot{}, err
}
return response.AccessToken, response.Scope, response.Bot, nil
}
// GetOAuthResponse retrieves OAuth response
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
}
// GetOAuthResponseContext retrieves OAuth response with custom context
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{}
2019-09-07 20:46:58 +00:00
if err = postForm(ctx, client, APIURL+"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
}
2020-03-28 22:50:47 +00:00
// GetOAuthV2Response gets a V2 OAuth access token response - https://api.slack.com/methods/oauth.v2.access
func GetOAuthV2Response(client httpClient, clientID, clientSecret, code, redirectURI string) (resp *OAuthV2Response, err error) {
return GetOAuthV2ResponseContext(context.Background(), client, clientID, clientSecret, code, redirectURI)
}
// GetOAuthV2ResponseContext with a context, gets a V2 OAuth access token response
func GetOAuthV2ResponseContext(ctx context.Context, client httpClient, clientID, clientSecret, code, redirectURI string) (resp *OAuthV2Response, err error) {
values := url.Values{
"client_id": {clientID},
"client_secret": {clientSecret},
"code": {code},
"redirect_uri": {redirectURI},
}
response := &OAuthV2Response{}
if err = postForm(ctx, client, APIURL+"oauth.v2.access", values, response, discard{}); err != nil {
return nil, err
}
return response, response.Err()
}
2021-12-11 23:05:15 +00:00
// RefreshOAuthV2AccessContext with a context, gets a V2 OAuth access token response
func RefreshOAuthV2Token(client httpClient, clientID, clientSecret, refreshToken string) (resp *OAuthV2Response, err error) {
return RefreshOAuthV2TokenContext(context.Background(), client, clientID, clientSecret, refreshToken)
}
// RefreshOAuthV2AccessContext with a context, gets a V2 OAuth access token response
func RefreshOAuthV2TokenContext(ctx context.Context, client httpClient, clientID, clientSecret, refreshToken string) (resp *OAuthV2Response, err error) {
values := url.Values{
"client_id": {clientID},
"client_secret": {clientSecret},
"refresh_token": {refreshToken},
"grant_type": {"refresh_token"},
}
response := &OAuthV2Response{}
if err = postForm(ctx, client, APIURL+"oauth.v2.access", values, response, discard{}); err != nil {
return nil, err
}
return response, response.Err()
}