Skip to content
Snippets Groups Projects
Select Git revision
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
  • v1.277.0
30 results

api-logs.go

Blame
  • 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"] = "***"
    	}