Skip to content
Snippets Groups Projects
Select Git revision
  • 99a358c775aab5acaa28baba7d275fa63c3396b4
  • 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.297.0
  • 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
31 results

api_key.go

Blame
  • api_key.go 1.22 KiB
    package auth
    
    import (
    	"github.com/google/uuid"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils"
    	"strings"
    )
    
    // GenerateNewApiKey generates a 32 character API key. If the build environment is dev or stage the key will start with
    // "dev_" or "stage_" respectively.
    func GenerateNewApiKey() string {
    	uniqueKey := uuid.New().String()
    	uniqueKey = strings.ReplaceAll(uniqueKey, "-", "")
    
    	env := utils.GetEnv("ENVIRONMENT", "")
    	if env == "dev" || env == "stage" {
    		uniqueKey = env + "_" + uniqueKey
    	}
    
    	return uniqueKey
    }
    
    // GetApiKeyFromHeaders checks if a bearer token is passed as part of the Authorization header and returns that key.
    // NOTE: This function is deprecated. It simply calls the more generic GetBearerTokenFromHeaders.
    func GetApiKeyFromHeaders(headers map[string]string) string {
    	return GetBearerTokenFromHeaders(headers)
    }
    
    // MaskAPIKey masks an API key in the form "abc***xyz"
    func MaskAPIKey(key string) string {
    	keyRunes := []rune(key)
    	keyLength := len(keyRunes)
    	if keyLength > 6 {
    		return string(keyRunes[:3]) + "***" + string(keyRunes[keyLength-3:])
    	}
    	// This shouldn't happen, but if we don't have more than 6 characters, mask in the form "***z"
    	return "***" + string(keyRunes[keyLength-1])
    }