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

76 lines
2.1 KiB
Go
Raw Normal View History

package akka_apps
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"strings"
)
// sessionVarsHookUrl is the authentication hook URL obtained from an environment variable.
var sessionVarsHookUrl = os.Getenv("BBB_GRAPHQL_MIDDLEWARE_SESSION_VARS_HOOK_URL")
2024-07-11 05:30:01 +08:00
func AkkaAppsGetSessionVariablesFrom(browserConnectionId string, sessionToken string) (map[string]string, error) {
logger := log.WithField("_routine", "AkkaAppsClient").WithField("browserConnectionId", browserConnectionId)
logger.Debug("Starting AkkaAppsClient")
defer logger.Debug("Finished AkkaAppsClient")
// Create a new HTTP client with a cookie jar.
client := &http.Client{}
// Check if the authentication hook URL is set.
if sessionVarsHookUrl == "" {
return nil, fmt.Errorf("BBB_GRAPHQL_MIDDLEWARE_SESSION_VARS_HOOK_URL not set")
}
2024-07-11 05:30:01 +08:00
log.Trace("Get user session vars from: " + sessionVarsHookUrl + "?sessionToken=" + sessionToken)
// Create a new HTTP request to the authentication hook URL.
2024-07-11 05:30:01 +08:00
req, err := http.NewRequest("GET", sessionVarsHookUrl, nil)
if err != nil {
return nil, err
}
// Execute the HTTP request to obtain user session variables (like X-Hasura-Role)
2024-07-11 05:30:01 +08:00
req.Header.Set("x-session-token", sessionToken)
req.Header.Set("User-Agent", "bbb-graphql-middleware")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var respBodyAsMap map[string]string
if err := json.Unmarshal(respBody, &respBodyAsMap); err != nil {
return nil, err
}
// Check the response status.
response, ok := respBodyAsMap["response"]
if !ok {
return nil, fmt.Errorf("response key not found in the parsed object")
}
if response != "authorized" {
logger.Error(response)
return nil, fmt.Errorf("user not authorized")
}
// 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
}
}
return normalizedResponse, nil
}