Skip to content
Snippets Groups Projects
Select Git revision
  • c1db85ef9417ea72191f64facd34139ff256d13a
  • dev default protected
  • prod protected
  • 1.0.58
  • 1.0.57
  • 1.0.52
  • 1.0.56
  • 1.0.51
  • 1.0.50
  • 1.0.33
  • 1.0.32
  • 1.0.31
  • 1.0.30
  • 1.0.29
  • 1.0.28
  • 1.0.27
  • 1.0.26
  • 1.0.25
  • 1.0.24
  • 1.0.23
  • 1.0.22
  • 1.0.21
  • 1.0.20
23 results

.DS_Store

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])
    }