bigbluebutton-Github/bbb-graphql-middleware/internal/bbb_web/client.go

92 lines
2.5 KiB
Go
Raw Normal View History

package bbb_web
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"os"
"strings"
"time"
2024-05-02 22:06:37 +08:00
"github.com/iMDT/bbb-graphql-middleware/internal/common"
log "github.com/sirupsen/logrus"
)
2024-05-02 22:06:37 +08:00
// authHookUrl is the authentication hook URL obtained from an environment variable.
var authHookUrl = os.Getenv("BBB_GRAPHQL_MIDDLEWARE_AUTH_HOOK_URL")
2024-05-02 22:06:37 +08:00
// BBBWebClient handles the web requests for authentication and returns a map of response headers.
func BBBWebClient(browserConnection *common.BrowserConnection, cookies []*http.Cookie) (map[string]string, error) {
2024-05-02 22:06:37 +08:00
logger := log.WithField("_routine", "BBBWebClient").WithField("browserConnectionId", browserConnection.Id)
logger.Debug("Starting BBBWebClient")
defer logger.Debug("Finished BBBWebClient")
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 {
return nil, fmt.Errorf("failed to create cookie jar: %v", err)
}
2024-05-02 22:06:37 +08:00
client := &http.Client{Jar: jar}
2024-05-02 22:06:37 +08:00
// Check if the authentication hook URL is set.
if authHookUrl == "" {
2024-05-02 22:06:37 +08:00
return nil, fmt.Errorf("BBB_GRAPHQL_MIDDLEWARE_AUTH_HOOK_URL not set")
}
2024-05-02 22:06:37 +08:00
// Create a new HTTP request to the authentication hook URL.
req, err := http.NewRequest("GET", authHookUrl, nil)
if err != nil {
return nil, err
}
2024-05-02 22:06:37 +08:00
// Add cookies to the request.
for _, cookie := range cookies {
req.AddCookie(cookie)
}
2024-05-02 22:06:37 +08:00
// Wait for SessionToken to be provided.
for browserConnection.SessionToken == "" {
time.Sleep(150 * time.Millisecond)
}
2024-05-02 22:06:37 +08:00
// Execute the HTTP request to obtain user session variables (like X-Hasura-Role)
req.Header.Set("x-session-token", browserConnection.SessionToken)
req.Header.Set("User-Agent", "hasura-graphql-engine")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
2024-05-02 22:06:37 +08:00
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
2024-05-02 22:06:37 +08:00
var respBodyAsMap map[string]string
if err := json.Unmarshal(respBody, &respBodyAsMap); err != nil {
return nil, err
}
2024-05-02 22:06:37 +08:00
// Check the response status.
response, ok := respBodyAsMap["response"]
if !ok {
2024-05-02 22:06:37 +08:00
return nil, fmt.Errorf("response key not found in the parsed object")
}
if response != "authorized" {
2024-05-02 22:06:37 +08:00
return nil, fmt.Errorf("auth token not authorized")
}
2024-05-02 22:06:37 +08:00
// Normalize the response header keys.
normalizedResponse := make(map[string]string)
for key, value := range respBodyAsMap {
if strings.HasPrefix(strings.ToLower(key), "x-hasura") {
normalizedResponse[strings.ToLower(key)] = value
}
}
2024-05-02 22:06:37 +08:00
return normalizedResponse, nil
}