| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/url" |
| 6 | ) |
| 7 | |
| 8 | // OAuthResponseIncomingWebhook ... |
| 9 | type OAuthResponseIncomingWebhook struct { |
| 10 | URL string `json:"url"` |
| 11 | Channel string `json:"channel"` |
| 12 | ChannelID string `json:"channel_id,omitempty"` |
| 13 | ConfigurationURL string `json:"configuration_url"` |
| 14 | } |
| 15 | |
| 16 | // OAuthResponseBot ... |
| 17 | type OAuthResponseBot struct { |
| 18 | BotUserID string `json:"bot_user_id"` |
| 19 | BotAccessToken string `json:"bot_access_token"` |
| 20 | } |
| 21 | |
| 22 | // OAuthResponse ... |
| 23 | type OAuthResponse struct { |
| 24 | AccessToken string `json:"access_token"` |
| 25 | Scope string `json:"scope"` |
| 26 | TeamName string `json:"team_name"` |
| 27 | TeamID string `json:"team_id"` |
| 28 | IncomingWebhook OAuthResponseIncomingWebhook `json:"incoming_webhook"` |
| 29 | Bot OAuthResponseBot `json:"bot"` |
| 30 | UserID string `json:"user_id,omitempty"` |
| 31 | SlackResponse |
| 32 | } |
| 33 | |
| 34 | // OAuthV2Response ... |
| 35 | type OAuthV2Response struct { |
| 36 | AccessToken string `json:"access_token"` |
| 37 | TokenType string `json:"token_type"` |
| 38 | Scope string `json:"scope"` |
| 39 | BotUserID string `json:"bot_user_id"` |
| 40 | AppID string `json:"app_id"` |
| 41 | Team OAuthV2ResponseTeam `json:"team"` |
| 42 | IncomingWebhook OAuthResponseIncomingWebhook `json:"incoming_webhook"` |
| 43 | Enterprise OAuthV2ResponseEnterprise `json:"enterprise"` |
| 44 | AuthedUser OAuthV2ResponseAuthedUser `json:"authed_user"` |
| 45 | RefreshToken string `json:"refresh_token"` |
| 46 | ExpiresIn int `json:"expires_in"` |
| 47 | SlackResponse |
| 48 | } |
| 49 | |
| 50 | // OAuthV2ResponseTeam ... |
| 51 | type OAuthV2ResponseTeam struct { |
| 52 | ID string `json:"id"` |
| 53 | Name string `json:"name"` |
| 54 | } |
| 55 | |
| 56 | // OAuthV2ResponseEnterprise ... |
| 57 | type OAuthV2ResponseEnterprise struct { |
| 58 | ID string `json:"id"` |
| 59 | Name string `json:"name"` |
| 60 | } |
| 61 | |
| 62 | // OAuthV2ResponseAuthedUser ... |
| 63 | type OAuthV2ResponseAuthedUser struct { |
| 64 | ID string `json:"id"` |
| 65 | Scope string `json:"scope"` |
| 66 | AccessToken string `json:"access_token"` |
| 67 | ExpiresIn int `json:"expires_in"` |
| 68 | RefreshToken string `json:"refresh_token"` |
| 69 | TokenType string `json:"token_type"` |
| 70 | } |
| 71 | |
| 72 | // GetOAuthToken retrieves an AccessToken |
| 73 | func GetOAuthToken(client httpClient, clientID, clientSecret, code, redirectURI string) (accessToken string, scope string, err error) { |
| 74 | return GetOAuthTokenContext(context.Background(), client, clientID, clientSecret, code, redirectURI) |
| 75 | } |
| 76 | |
| 77 | // GetOAuthTokenContext retrieves an AccessToken with a custom context |
| 78 | func GetOAuthTokenContext(ctx context.Context, client httpClient, clientID, clientSecret, code, redirectURI string) (accessToken string, scope string, err error) { |
| 79 | response, err := GetOAuthResponseContext(ctx, client, clientID, clientSecret, code, redirectURI) |
| 80 | if err != nil { |
| 81 | return "", "", err |
| 82 | } |
| 83 | return response.AccessToken, response.Scope, nil |
| 84 | } |
| 85 | |
| 86 | // GetBotOAuthToken retrieves top-level and bot AccessToken - https://api.slack.com/legacy/oauth#bot_user_access_tokens |
| 87 | func GetBotOAuthToken(client httpClient, clientID, clientSecret, code, redirectURI string) (accessToken string, scope string, bot OAuthResponseBot, err error) { |
| 88 | return GetBotOAuthTokenContext(context.Background(), client, clientID, clientSecret, code, redirectURI) |
| 89 | } |
| 90 | |
| 91 | // GetBotOAuthTokenContext retrieves top-level and bot AccessToken with a custom context |
| 92 | func GetBotOAuthTokenContext(ctx context.Context, client httpClient, clientID, clientSecret, code, redirectURI string) (accessToken string, scope string, bot OAuthResponseBot, err error) { |
| 93 | response, err := GetOAuthResponseContext(ctx, client, clientID, clientSecret, code, redirectURI) |
| 94 | if err != nil { |
| 95 | return "", "", OAuthResponseBot{}, err |
| 96 | } |
| 97 | return response.AccessToken, response.Scope, response.Bot, nil |
| 98 | } |
| 99 | |
| 100 | // GetOAuthResponse retrieves OAuth response |
| 101 | func GetOAuthResponse(client httpClient, clientID, clientSecret, code, redirectURI string) (resp *OAuthResponse, err error) { |
| 102 | return GetOAuthResponseContext(context.Background(), client, clientID, clientSecret, code, redirectURI) |
| 103 | } |
| 104 | |
| 105 | // GetOAuthResponseContext retrieves OAuth response with custom context |
| 106 | func GetOAuthResponseContext(ctx context.Context, client httpClient, clientID, clientSecret, code, redirectURI string) (resp *OAuthResponse, err error) { |
| 107 | values := url.Values{ |
| 108 | "client_id": {clientID}, |
| 109 | "client_secret": {clientSecret}, |
| 110 | "code": {code}, |
| 111 | "redirect_uri": {redirectURI}, |
| 112 | } |
| 113 | response := &OAuthResponse{} |
| 114 | if err = postForm(ctx, client, APIURL+"oauth.access", values, response, discard{}); err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | return response, response.Err() |
| 118 | } |
| 119 | |
| 120 | // GetOAuthV2Response gets a V2 OAuth access token response - https://api.slack.com/methods/oauth.v2.access |
| 121 | func GetOAuthV2Response(client httpClient, clientID, clientSecret, code, redirectURI string) (resp *OAuthV2Response, err error) { |
| 122 | return GetOAuthV2ResponseContext(context.Background(), client, clientID, clientSecret, code, redirectURI) |
| 123 | } |
| 124 | |
| 125 | // GetOAuthV2ResponseContext with a context, gets a V2 OAuth access token response |
| 126 | func GetOAuthV2ResponseContext(ctx context.Context, client httpClient, clientID, clientSecret, code, redirectURI string) (resp *OAuthV2Response, err error) { |
| 127 | values := url.Values{ |
| 128 | "client_id": {clientID}, |
| 129 | "client_secret": {clientSecret}, |
| 130 | "code": {code}, |
| 131 | "redirect_uri": {redirectURI}, |
| 132 | } |
| 133 | response := &OAuthV2Response{} |
| 134 | if err = postForm(ctx, client, APIURL+"oauth.v2.access", values, response, discard{}); err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | return response, response.Err() |
| 138 | } |
| 139 | |
| 140 | // RefreshOAuthV2AccessContext with a context, gets a V2 OAuth access token response |
| 141 | func RefreshOAuthV2Token(client httpClient, clientID, clientSecret, refreshToken string) (resp *OAuthV2Response, err error) { |
| 142 | return RefreshOAuthV2TokenContext(context.Background(), client, clientID, clientSecret, refreshToken) |
| 143 | } |
| 144 | |
| 145 | // RefreshOAuthV2AccessContext with a context, gets a V2 OAuth access token response |
| 146 | func RefreshOAuthV2TokenContext(ctx context.Context, client httpClient, clientID, clientSecret, refreshToken string) (resp *OAuthV2Response, err error) { |
| 147 | values := url.Values{ |
| 148 | "client_id": {clientID}, |
| 149 | "client_secret": {clientSecret}, |
| 150 | "refresh_token": {refreshToken}, |
| 151 | "grant_type": {"refresh_token"}, |
| 152 | } |
| 153 | response := &OAuthV2Response{} |
| 154 | if err = postForm(ctx, client, APIURL+"oauth.v2.access", values, response, discard{}); err != nil { |
| 155 | return nil, err |
| 156 | } |
| 157 | return response, response.Err() |
| 158 | } |