2024-06-15 00:43:05 +08:00
|
|
|
package akka_apps
|
|
|
|
|
|
|
|
import (
|
2024-08-30 05:40:52 +08:00
|
|
|
"bbb-graphql-middleware/config"
|
2024-06-15 00:43:05 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// sessionVarsHookUrl is the authentication hook URL obtained from an environment variable.
|
2024-08-30 05:40:52 +08:00
|
|
|
var sessionVarsHookUrl = config.GetConfig().SessionVarsHook.Url
|
2024-06-15 00:43:05 +08:00
|
|
|
|
2024-09-05 08:22:49 +08:00
|
|
|
var internalError = fmt.Errorf("server internal error")
|
|
|
|
var internalErrorId = "internal_error"
|
|
|
|
|
|
|
|
func AkkaAppsGetSessionVariablesFrom(browserConnectionId string, sessionToken string) (map[string]string, error, string) {
|
2024-09-18 20:48:36 +08:00
|
|
|
logger := log.WithField("_routine", "AkkaAppsClient").
|
|
|
|
WithField("browserConnectionId", browserConnectionId).
|
|
|
|
WithField("sessionToken", sessionToken)
|
|
|
|
|
2024-07-11 05:30:01 +08:00
|
|
|
logger.Debug("Starting AkkaAppsClient")
|
|
|
|
defer logger.Debug("Finished AkkaAppsClient")
|
2024-06-15 00:43:05 +08:00
|
|
|
|
|
|
|
// Create a new HTTP client with a cookie jar.
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
// Check if the authentication hook URL is set.
|
|
|
|
if sessionVarsHookUrl == "" {
|
2024-09-05 08:22:49 +08:00
|
|
|
log.Error("BBB_GRAPHQL_MIDDLEWARE_SESSION_VARS_HOOK_URL not set")
|
|
|
|
return nil, internalError, internalErrorId
|
2024-06-15 00:43:05 +08:00
|
|
|
}
|
|
|
|
|
2024-07-11 05:30:01 +08:00
|
|
|
log.Trace("Get user session vars from: " + sessionVarsHookUrl + "?sessionToken=" + sessionToken)
|
2024-06-15 00:43:05 +08:00
|
|
|
|
|
|
|
// 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)
|
2024-06-15 00:43:05 +08:00
|
|
|
if err != nil {
|
2024-09-05 08:22:49 +08:00
|
|
|
log.Error(err)
|
|
|
|
return nil, internalError, internalErrorId
|
2024-06-15 00:43:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
2024-06-15 00:43:05 +08:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-09-05 08:22:49 +08:00
|
|
|
return nil, internalError, internalErrorId
|
2024-06-15 00:43:05 +08:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-09-05 08:22:49 +08:00
|
|
|
return nil, internalError, internalErrorId
|
2024-06-15 00:43:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var respBodyAsMap map[string]string
|
|
|
|
if err := json.Unmarshal(respBody, &respBodyAsMap); err != nil {
|
2024-09-05 08:22:49 +08:00
|
|
|
return nil, internalError, internalErrorId
|
2024-06-15 00:43:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the response status.
|
|
|
|
response, ok := respBodyAsMap["response"]
|
2024-09-05 08:22:49 +08:00
|
|
|
message, _ := respBodyAsMap["message"]
|
|
|
|
messageId, _ := respBodyAsMap["message_id"]
|
2024-06-15 00:43:05 +08:00
|
|
|
if !ok {
|
2024-09-05 08:22:49 +08:00
|
|
|
log.Error("response key not found in the parsed object")
|
|
|
|
return nil, internalError, internalErrorId
|
2024-06-15 00:43:05 +08:00
|
|
|
}
|
|
|
|
if response != "authorized" {
|
2024-09-27 00:32:22 +08:00
|
|
|
logger.Errorf("not authorized: Response: %s, Message: %s, MessageId: %s", response, message, messageId)
|
2024-09-05 08:22:49 +08:00
|
|
|
return nil, fmt.Errorf(message), messageId
|
2024-06-15 00:43:05 +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-09-05 08:22:49 +08:00
|
|
|
return normalizedResponse, nil, ""
|
2024-06-15 00:43:05 +08:00
|
|
|
}
|