2024-05-02 21:45:32 +08:00
|
|
|
package bbb_web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-15 00:43:05 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-05-02 21:45:32 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-05-02 22:06:37 +08:00
|
|
|
// authHookUrl is the authentication hook URL obtained from an environment variable.
|
2024-05-02 21:45:32 +08:00
|
|
|
var authHookUrl = os.Getenv("BBB_GRAPHQL_MIDDLEWARE_AUTH_HOOK_URL")
|
|
|
|
|
2024-06-15 00:43:05 +08:00
|
|
|
func BBBWebClient(browserConnectionId string, sessionToken string, cookies []*http.Cookie) (string, string, error) {
|
2024-05-30 04:43:17 +08:00
|
|
|
logger := log.WithField("_routine", "BBBWebClient").WithField("browserConnectionId", browserConnectionId)
|
2024-05-02 22:06:37 +08:00
|
|
|
logger.Debug("Starting BBBWebClient")
|
|
|
|
defer logger.Debug("Finished BBBWebClient")
|
2024-05-02 21:45:32 +08:00
|
|
|
|
2024-05-02 22:06:37 +08:00
|
|
|
// Create a new HTTP client with a cookie jar.
|
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
2024-06-15 00:43:05 +08:00
|
|
|
return "", "", fmt.Errorf("failed to create cookie jar: %v", err)
|
2024-05-02 21:45:32 +08:00
|
|
|
}
|
2024-05-02 22:06:37 +08:00
|
|
|
client := &http.Client{Jar: jar}
|
2024-05-02 21:45:32 +08:00
|
|
|
|
2024-05-02 22:06:37 +08:00
|
|
|
// Check if the authentication hook URL is set.
|
2024-05-02 21:45:32 +08:00
|
|
|
if authHookUrl == "" {
|
2024-06-15 00:43:05 +08:00
|
|
|
return "", "", fmt.Errorf("BBB_GRAPHQL_MIDDLEWARE_AUTH_HOOK_URL not set")
|
2024-05-02 21:45:32 +08:00
|
|
|
}
|
|
|
|
|
2024-05-02 22:06:37 +08:00
|
|
|
// Create a new HTTP request to the authentication hook URL.
|
2024-05-02 21:45:32 +08:00
|
|
|
req, err := http.NewRequest("GET", authHookUrl, nil)
|
|
|
|
if err != nil {
|
2024-06-15 00:43:05 +08:00
|
|
|
return "", "", err
|
2024-05-02 21:45:32 +08:00
|
|
|
}
|
|
|
|
|
2024-05-02 22:06:37 +08:00
|
|
|
// Add cookies to the request.
|
2024-05-02 21:45:32 +08:00
|
|
|
for _, cookie := range cookies {
|
|
|
|
req.AddCookie(cookie)
|
|
|
|
}
|
|
|
|
|
2024-05-02 22:06:37 +08:00
|
|
|
// Execute the HTTP request to obtain user session variables (like X-Hasura-Role)
|
2024-06-15 00:43:05 +08:00
|
|
|
req.Header.Set("x-original-uri", authHookUrl+"?sessionToken="+sessionToken)
|
|
|
|
//req.Header.Set("User-Agent", "hasura-graphql-engine")
|
2024-05-02 21:45:32 +08:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-06-15 00:43:05 +08:00
|
|
|
return "", "", err
|
2024-05-02 21:45:32 +08:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2024-05-02 22:06:37 +08:00
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
2024-05-02 21:45:32 +08:00
|
|
|
if err != nil {
|
2024-06-15 00:43:05 +08:00
|
|
|
return "", "", err
|
2024-05-02 21:45:32 +08:00
|
|
|
}
|
|
|
|
|
2024-06-15 00:43:05 +08:00
|
|
|
var respBodyAsString = string(respBody)
|
2024-05-02 21:45:32 +08:00
|
|
|
|
2024-06-15 00:43:05 +08:00
|
|
|
if respBodyAsString != "authorized" {
|
|
|
|
return "", "", fmt.Errorf("auth token not authorized")
|
2024-05-02 21:45:32 +08:00
|
|
|
}
|
|
|
|
|
2024-06-15 00:43:05 +08:00
|
|
|
var userId string
|
|
|
|
var meetingId string
|
|
|
|
|
|
|
|
//Get userId and meetingId from response Header
|
|
|
|
for key, values := range resp.Header {
|
|
|
|
for _, value := range values {
|
|
|
|
log.Debug("%s: %s\n", key, value)
|
|
|
|
|
|
|
|
if key == "User-Id" {
|
|
|
|
userId = value
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == "Meeting-Id" {
|
|
|
|
meetingId = value
|
|
|
|
}
|
2024-05-02 21:45:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-15 00:43:05 +08:00
|
|
|
return meetingId, userId, nil
|
2024-05-02 21:45:32 +08:00
|
|
|
}
|