71 lines
2.4 KiB
Go
71 lines
2.4 KiB
Go
package v1
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const instagramAuthorizationScope = "instagram_business_basic,instagram_business_manage_messages"
|
|
|
|
const tiktokAuthorizationScope = "user.info.basic,user.info.username,user.info.stats,user.info.profile,user.account.type,user.insights,message.list.read,message.list.send,message.list.manage"
|
|
|
|
func authorizationReturnTo(c *gin.Context) string {
|
|
if value := strings.TrimSpace(c.Query("return_to")); value != "" {
|
|
return value
|
|
}
|
|
var payload struct {
|
|
ReturnTo string `json:"return_to" form:"return_to"`
|
|
}
|
|
_ = c.ShouldBind(&payload)
|
|
return strings.TrimSpace(payload.ReturnTo)
|
|
}
|
|
|
|
func buildInstagramChatwootAuthorizationURL(accountID uint, returnTo string) (string, error) {
|
|
clientID := strings.TrimSpace(os.Getenv("INSTAGRAM_APP_ID"))
|
|
clientSecret := strings.TrimSpace(os.Getenv("INSTAGRAM_APP_SECRET"))
|
|
if clientID == "" || clientSecret == "" {
|
|
return "", fmt.Errorf("Instagram OAuth is not configured")
|
|
}
|
|
|
|
state, err := signedChatwootOAuthStateWithReturnTo(accountID, clientSecret, returnTo)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
frontendURL := strings.TrimRight(envOrDefaultV1("FRONTEND_URL", "http://localhost:3000"), "/")
|
|
params := url.Values{}
|
|
params.Set("client_id", clientID)
|
|
params.Set("redirect_uri", frontendURL+"/instagram/callback")
|
|
params.Set("scope", instagramAuthorizationScope)
|
|
params.Set("enable_fb_login", "0")
|
|
params.Set("force_authentication", "1")
|
|
params.Set("response_type", "code")
|
|
params.Set("state", state)
|
|
return "https://api.instagram.com/oauth/authorize?" + params.Encode(), nil
|
|
}
|
|
|
|
func buildTikTokChatwootAuthorizationURL(accountID uint, returnTo string) (string, error) {
|
|
clientID := strings.TrimSpace(os.Getenv("TIKTOK_APP_ID"))
|
|
clientSecret := strings.TrimSpace(os.Getenv("TIKTOK_APP_SECRET"))
|
|
if clientID == "" || clientSecret == "" {
|
|
return "", fmt.Errorf("TikTok OAuth is not configured")
|
|
}
|
|
|
|
state, err := signedChatwootOAuthStateWithReturnTo(accountID, clientSecret, returnTo)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
frontendURL := strings.TrimRight(envOrDefaultV1("FRONTEND_URL", "http://localhost:3000"), "/")
|
|
params := url.Values{}
|
|
params.Set("client_id", clientID)
|
|
params.Set("client_key", clientID)
|
|
params.Set("redirect_uri", frontendURL+"/tiktok/callback")
|
|
params.Set("response_type", "code")
|
|
params.Set("scope", tiktokAuthorizationScope)
|
|
params.Set("state", state)
|
|
return "https://www.tiktok.com/v2/auth/authorize?" + params.Encode(), nil
|
|
}
|