mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-10-31 14:15:50 +00:00 
			
		
		
		
	fix: fix key normalization function handing more cases than it needs to
This commit is contained in:
		| @@ -18,10 +18,14 @@ func NormalizeKeys(keys map[string]string, rootName string, sep string) map[stri | |||||||
|  |  | ||||||
| 		finalKey = append(finalKey, rootName) | 		finalKey = append(finalKey, rootName) | ||||||
| 		finalKey = append(finalKey, "providers") | 		finalKey = append(finalKey, "providers") | ||||||
| 		kebabKey := strings.ToLower(k) | 		lowerKey := strings.ToLower(k) | ||||||
|  |  | ||||||
|  | 		if !strings.HasPrefix(lowerKey, "providers"+sep) { | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		for _, known := range knownKeys { | 		for _, known := range knownKeys { | ||||||
| 			if strings.HasSuffix(kebabKey, strings.ReplaceAll(known, "-", sep)) { | 			if strings.HasSuffix(lowerKey, strings.ReplaceAll(known, "-", sep)) { | ||||||
| 				suffix = known | 				suffix = known | ||||||
| 				break | 				break | ||||||
| 			} | 			} | ||||||
| @@ -31,7 +35,11 @@ func NormalizeKeys(keys map[string]string, rootName string, sep string) map[stri | |||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		clientNameParts := strings.Split(strings.TrimPrefix(strings.TrimSuffix(kebabKey, sep+strings.ReplaceAll(suffix, "-", sep)), "providers"+sep), sep) | 		if strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(lowerKey, "providers"+sep), strings.ReplaceAll(suffix, "-", sep))) == "" { | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		clientNameParts := strings.Split(strings.TrimPrefix(strings.TrimSuffix(lowerKey, sep+strings.ReplaceAll(suffix, "-", sep)), "providers"+sep), sep) | ||||||
|  |  | ||||||
| 		for i, p := range clientNameParts { | 		for i, p := range clientNameParts { | ||||||
| 			if i == 0 { | 			if i == 0 { | ||||||
|   | |||||||
| @@ -14,6 +14,8 @@ func TestNormalizeKeys(t *testing.T) { | |||||||
| 		"PROVIDERS_CLIENT1_CLIENT_SECRET":                "my-client-secret", | 		"PROVIDERS_CLIENT1_CLIENT_SECRET":                "my-client-secret", | ||||||
| 		"PROVIDERS_MY_AWESOME_CLIENT_CLIENT_ID":          "my-awesome-client-id", | 		"PROVIDERS_MY_AWESOME_CLIENT_CLIENT_ID":          "my-awesome-client-id", | ||||||
| 		"PROVIDERS_MY_AWESOME_CLIENT_CLIENT_SECRET_FILE": "/path/to/secret", | 		"PROVIDERS_MY_AWESOME_CLIENT_CLIENT_SECRET_FILE": "/path/to/secret", | ||||||
|  | 		"I_LOOK_LIKE_A_KEY_CLIENT_ID":                    "should-not-appear", | ||||||
|  | 		"PROVIDERS_CLIENT_ID":                            "should-not-appear", | ||||||
| 	} | 	} | ||||||
| 	expected := map[string]string{ | 	expected := map[string]string{ | ||||||
| 		"tinyauth.providers.client1.clientId":                 "my-client-id", | 		"tinyauth.providers.client1.clientId":                 "my-client-id", | ||||||
| @@ -31,6 +33,9 @@ func TestNormalizeKeys(t *testing.T) { | |||||||
| 		"providers-client1-client-secret":                "my-client-secret", | 		"providers-client1-client-secret":                "my-client-secret", | ||||||
| 		"providers-my-awesome-client-client-id":          "my-awesome-client-id", | 		"providers-my-awesome-client-client-id":          "my-awesome-client-id", | ||||||
| 		"providers-my-awesome-client-client-secret-file": "/path/to/secret", | 		"providers-my-awesome-client-client-secret-file": "/path/to/secret", | ||||||
|  | 		"providers-should-not-appear-client":             "should-not-appear", | ||||||
|  | 		"i-look-like-a-key-client-id":                    "should-not-appear", | ||||||
|  | 		"providers-client-id":                            "should-not-appear", | ||||||
| 	} | 	} | ||||||
| 	expected = map[string]string{ | 	expected = map[string]string{ | ||||||
| 		"tinyauth.providers.client1.clientId":                 "my-client-id", | 		"tinyauth.providers.client1.clientId":                 "my-client-id", | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Stavros
					Stavros