From f2075728e0df6e878f56ca29631615ef78ad53b4 Mon Sep 17 00:00:00 2001 From: jano3 <jano@uafrica.com> Date: Thu, 24 Nov 2022 16:01:16 +0200 Subject: [PATCH] Add function to mask API key --- auth/api_key.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/auth/api_key.go b/auth/api_key.go index 7f6c6ef..935505b 100644 --- a/auth/api_key.go +++ b/auth/api_key.go @@ -32,3 +32,14 @@ func GetApiKeyFromHeaders(headers map[string]string) string { } return "" } + +// 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]) +} -- GitLab