diff --git a/auth/api_key.go b/auth/api_key.go new file mode 100644 index 0000000000000000000000000000000000000000..2f4fabbbd4806d92f5d5d6eb65f88720647970fd --- /dev/null +++ b/auth/api_key.go @@ -0,0 +1,30 @@ +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 +func GetApiKeyFromHeaders(headers map[string]string) string { + key := headers["Authorization"] + if strings.HasPrefix(strings.ToLower(key), "bearer ") { + return strings.TrimPrefix(strings.ToLower(key), "bearer") + } + return "" +} diff --git a/auth/oauth.go b/oauth/oauth.go similarity index 100% rename from auth/oauth.go rename to oauth/oauth.go