2020-01-16 20:39:18 +01:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2019-2020 Musing Studio LLC.
|
2020-01-16 20:39:18 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
|
|
|
|
2019-12-28 21:15:47 +01:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-02 21:50:54 +01:00
|
|
|
"errors"
|
2024-09-28 01:20:43 +02:00
|
|
|
"github.com/gosimple/slug"
|
2019-12-28 21:15:47 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type slackOauthClient struct {
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
TeamID string
|
|
|
|
CallbackLocation string
|
|
|
|
HttpClient HttpClient
|
|
|
|
}
|
|
|
|
|
|
|
|
type slackExchangeResponse struct {
|
2020-01-02 21:50:54 +01:00
|
|
|
OK bool `json:"ok"`
|
2019-12-28 21:15:47 +01:00
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
TeamName string `json:"team_name"`
|
|
|
|
TeamID string `json:"team_id"`
|
2020-01-02 21:50:54 +01:00
|
|
|
Error string `json:"error"`
|
2019-12-28 21:15:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type slackIdentity struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type slackTeam struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type slackUserIdentityResponse struct {
|
|
|
|
OK bool `json:"ok"`
|
|
|
|
User slackIdentity `json:"user"`
|
|
|
|
Team slackTeam `json:"team"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
slackAuthLocation = "https://slack.com/oauth/authorize"
|
|
|
|
slackExchangeLocation = "https://slack.com/api/oauth.access"
|
|
|
|
slackIdentityLocation = "https://slack.com/api/users.identity"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ oauthClient = slackOauthClient{}
|
|
|
|
|
|
|
|
func (c slackOauthClient) GetProvider() string {
|
|
|
|
return "slack"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c slackOauthClient) GetClientID() string {
|
|
|
|
return c.ClientID
|
|
|
|
}
|
|
|
|
|
2020-01-07 21:22:25 +01:00
|
|
|
func (c slackOauthClient) GetCallbackLocation() string {
|
|
|
|
return c.CallbackLocation
|
|
|
|
}
|
|
|
|
|
2019-12-28 21:15:47 +01:00
|
|
|
func (c slackOauthClient) buildLoginURL(state string) (string, error) {
|
|
|
|
u, err := url.Parse(slackAuthLocation)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
q := u.Query()
|
|
|
|
q.Set("client_id", c.ClientID)
|
|
|
|
q.Set("scope", "identity.basic identity.email identity.team")
|
|
|
|
q.Set("redirect_uri", c.CallbackLocation)
|
|
|
|
q.Set("state", state)
|
|
|
|
|
|
|
|
// If this param is not set, the user can select which team they
|
|
|
|
// authenticate through and then we'd have to match the configured team
|
|
|
|
// against the profile get. That is extra work in the post-auth phase
|
|
|
|
// that we don't want to do.
|
|
|
|
q.Set("team", c.TeamID)
|
|
|
|
|
|
|
|
// The Slack OAuth docs don't explicitly list this one, but it is part of
|
|
|
|
// the spec, so we include it anyway.
|
|
|
|
q.Set("response_type", "code")
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c slackOauthClient) exchangeOauthCode(ctx context.Context, code string) (*TokenResponse, error) {
|
|
|
|
form := url.Values{}
|
|
|
|
// The oauth.access documentation doesn't explicitly mention this
|
|
|
|
// parameter, but it is part of the spec, so we include it anyway.
|
|
|
|
// https://api.slack.com/methods/oauth.access
|
|
|
|
form.Add("grant_type", "authorization_code")
|
|
|
|
form.Add("redirect_uri", c.CallbackLocation)
|
|
|
|
form.Add("code", code)
|
|
|
|
req, err := http.NewRequest("POST", slackExchangeLocation, strings.NewReader(form.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.WithContext(ctx)
|
2020-08-18 18:22:04 +02:00
|
|
|
req.Header.Set("User-Agent", ServerUserAgent(""))
|
2019-12-28 21:15:47 +01:00
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.SetBasicAuth(c.ClientID, c.ClientSecret)
|
|
|
|
|
|
|
|
resp, err := c.HttpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-02 21:50:54 +01:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.New("unable to exchange code for access token")
|
|
|
|
}
|
2019-12-28 21:15:47 +01:00
|
|
|
|
|
|
|
var tokenResponse slackExchangeResponse
|
|
|
|
if err := limitedJsonUnmarshal(resp.Body, tokenRequestMaxLen, &tokenResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-02 21:50:54 +01:00
|
|
|
if !tokenResponse.OK {
|
|
|
|
return nil, errors.New(tokenResponse.Error)
|
|
|
|
}
|
2019-12-28 21:15:47 +01:00
|
|
|
return tokenResponse.TokenResponse(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c slackOauthClient) inspectOauthAccessToken(ctx context.Context, accessToken string) (*InspectResponse, error) {
|
|
|
|
req, err := http.NewRequest("GET", slackIdentityLocation, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.WithContext(ctx)
|
2020-08-18 18:22:04 +02:00
|
|
|
req.Header.Set("User-Agent", ServerUserAgent(""))
|
2019-12-28 21:15:47 +01:00
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
|
|
|
|
|
|
resp, err := c.HttpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-02 21:50:54 +01:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.New("unable to inspect access token")
|
|
|
|
}
|
2019-12-28 21:15:47 +01:00
|
|
|
|
|
|
|
var inspectResponse slackUserIdentityResponse
|
|
|
|
if err := limitedJsonUnmarshal(resp.Body, infoRequestMaxLen, &inspectResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-02 21:50:54 +01:00
|
|
|
if !inspectResponse.OK {
|
|
|
|
return nil, errors.New(inspectResponse.Error)
|
|
|
|
}
|
2019-12-28 21:15:47 +01:00
|
|
|
return inspectResponse.InspectResponse(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (resp slackUserIdentityResponse) InspectResponse() *InspectResponse {
|
|
|
|
return &InspectResponse{
|
2020-01-02 21:55:28 +01:00
|
|
|
UserID: resp.User.ID,
|
2020-04-22 15:17:25 +02:00
|
|
|
Username: slug.Make(resp.User.Name),
|
2020-01-02 21:55:28 +01:00
|
|
|
DisplayName: resp.User.Name,
|
|
|
|
Email: resp.User.Email,
|
2019-12-28 21:15:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (resp slackExchangeResponse) TokenResponse() *TokenResponse {
|
|
|
|
return &TokenResponse{
|
|
|
|
AccessToken: resp.AccessToken,
|
|
|
|
}
|
|
|
|
}
|