Select Git revision
api-logs.go
-
Francé Wilke authoredFrancé Wilke authored
api-logs.go 6.94 KiB
package api_logs
import (
"github.com/thoas/go-funk"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/number_utils"
"net/url"
"strings"
"time"
"github.com/aws/aws-lambda-go/events"
)
func GenerateIncomingAPILog(startTime time.Time, requestID *string, claim map[string]interface{}, req events.APIGatewayProxyRequest, res events.APIGatewayProxyResponse) ApiLog {
endTime := time.Now()
currentRequestID := ""
if requestID != nil {
currentRequestID = *requestID
}
var authType string
var authUsername string
if req.RequestContext.Identity.CognitoAuthenticationType != "" {
authType = "cognito"
split := strings.Split(req.RequestContext.Identity.CognitoAuthenticationProvider, ":")
if len(split) > 0 {
authUsername = split[len(split)-1] // = part after last ':'
}
} else {
authType = "iam"
split := strings.Split(req.RequestContext.Identity.UserArn, ":user/")
if len(split) > 0 {
authUsername = split[len(split)-1] // = part after ':user/'
}
}
userID, _ := claim["UserID"].(int64)
username, _ := claim["Username"].(string)
accountID, _ := claim["AccountID"].(int64)
providerID, _ := claim["ProviderID"].(int64)
if accountID == 0 {
if accountIDParam, ok := req.QueryStringParameters["account_id"]; ok {
if i64, err := number_utils.StringToInt64(accountIDParam); err == nil && i64 > 0 {
accountID = i64
}
}
}
if providerID == 0 {
if providerIDParam, ok := req.QueryStringParameters["provider_id"]; ok {
if i64, err := number_utils.StringToInt64(providerIDParam); err == nil && i64 > 0 {
providerID = i64
}
}
}
typeString := "api-incoming"
if funk.Contains(req.Path, "webhook") {
typeString = "webhook-incoming"
}
// Remove the API key in the header
if req.Headers["authorization"] != "" {
req.Headers["authorization"] = "***"
}
if req.Headers["Authorization"] != "" {
req.Headers["Authorization"] = "***"
}