Skip to content

Commit 6f93939

Browse files
committed
I messed up
1 parent 1372d58 commit 6f93939

File tree

25 files changed

+79
-104
lines changed

25 files changed

+79
-104
lines changed

.github/.cache_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.19
1+
1.0.21

clients/algoliasearch-client-csharp/algoliasearch/Models/Common/SecuredApiKeyRestrictionHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Algolia.Search.Models.Search;
99
/// <summary>
1010
/// Secured Api Key restrictions
1111
/// </summary>
12-
public partial class SecuredAPIKeyRestrictions
12+
public partial class SecuredApiKeyRestrictions
1313
{
1414

1515
/// <summary>

clients/algoliasearch-client-csharp/algoliasearch/Utils/ClientExtensions.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,14 @@ public static IEnumerable<SynonymHit> BrowseSynonyms(this SearchClient client, s
264264
/// Generate a virtual API Key without any call to the server.
265265
/// </summary>
266266
/// <param name="client"></param>
267-
/// <param name="parentAPIKey">Parent API Key</param>
267+
/// <param name="parentApiKey">Parent API Key</param>
268268
/// <param name="restriction">Restriction to add the key</param>
269269
/// <returns></returns>
270-
public static string GenerateSecuredApiKey(this SearchClient client, string parentAPIKey,
271-
SecuredAPIKeyRestrictions restriction)
270+
public static string GenerateSecuredApiKey(this SearchClient client, string parentApiKey,
271+
SecuredApiKeyRestrictions restriction)
272272
{
273273
var queryParams = restriction.ToQueryString();
274-
var hash = HmacShaHelper.GetHash(parentAPIKey, queryParams);
274+
var hash = HmacShaHelper.GetHash(parentApiKey, queryParams);
275275
return HmacShaHelper.Base64Encode($"{hash}{queryParams}");
276276
}
277277

@@ -280,25 +280,25 @@ public static string GenerateSecuredApiKey(this SearchClient client, string pare
280280
/// Get the remaining validity of a key generated by `GenerateSecuredApiKeys`.
281281
/// </summary>
282282
/// <param name="client"></param>
283-
/// <param name="securedAPIKey">The secured API Key</param>
283+
/// <param name="securedApiKey">The secured API Key</param>
284284
/// <returns></returns>
285285
/// <exception cref="ArgumentNullException"></exception>
286286
/// <exception cref="AlgoliaException"></exception>
287-
public static TimeSpan GetSecuredApiKeyRemainingValidity(this SearchClient client, string securedAPIKey)
287+
public static TimeSpan GetSecuredApiKeyRemainingValidity(this SearchClient client, string securedApiKey)
288288
{
289-
if (string.IsNullOrWhiteSpace(securedAPIKey))
289+
if (string.IsNullOrWhiteSpace(securedApiKey))
290290
{
291-
throw new ArgumentNullException(nameof(securedAPIKey));
291+
throw new ArgumentNullException(nameof(securedApiKey));
292292
}
293293

294-
var decodedKey = Encoding.UTF8.GetString(Convert.FromBase64String(securedAPIKey));
294+
var decodedKey = Encoding.UTF8.GetString(Convert.FromBase64String(securedApiKey));
295295

296296
var regex = new Regex(@"validUntil=\d+");
297297
var matches = regex.Matches(decodedKey);
298298

299299
if (matches.Count == 0)
300300
{
301-
throw new AlgoliaException("The SecuredAPIKey doesn't have a validUntil parameter.");
301+
throw new AlgoliaException("The SecuredApiKey doesn't have a validUntil parameter.");
302302
}
303303

304304
// Select the validUntil parameter and its value

clients/algoliasearch-client-javascript/packages/client-common/src/transporter/helpers.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ export function serializeUrl(
4242
}
4343

4444
export function serializeQueryParameters(parameters: QueryParameters): string {
45-
const isObjectOrArray = (value: any): boolean =>
46-
Object.prototype.toString.call(value) === '[object Object]' ||
47-
Object.prototype.toString.call(value) === '[object Array]';
48-
4945
return Object.keys(parameters)
46+
.filter((key) => parameters[key] !== undefined)
47+
.sort()
5048
.map(
5149
(key) =>
5250
`${key}=${encodeURIComponent(
53-
isObjectOrArray(parameters[key])
54-
? JSON.stringify(parameters[key])
51+
Object.prototype.toString.call(parameters[key]) === '[object Array]'
52+
? parameters[key].join(',')
5553
: parameters[key]
5654
).replaceAll('+', '%20')}`
5755
)

clients/algoliasearch-client-kotlin/client/src/commonMain/kotlin/com/algolia/client/extensions/SearchClient.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,13 @@ public suspend fun <T> SearchClient.replaceAllObjects(
307307
/**
308308
* Generate a virtual API Key without any call to the server.
309309
*
310-
* @param parentAPIKey API key to generate from.
310+
* @param parentApiKey API key to generate from.
311311
* @param restriction Restriction to add the key
312312
* @throws Exception if an error occurs during the encoding
313313
*/
314-
public fun SearchClient.generateSecuredApiKey(parentAPIKey: String, restriction: SecuredAPIKeyRestrictions): String {
314+
public fun SearchClient.generateSecuredApiKey(parentApiKey: String, restriction: SecuredApiKeyRestrictions): String {
315315
val restrictionString = buildRestrictionString(restriction)
316-
val hash = encodeKeySHA256(parentAPIKey, restrictionString)
316+
val hash = encodeKeySHA256(parentApiKey, restrictionString)
317317
return "$hash$restrictionString".encodeBase64()
318318
}
319319

@@ -322,7 +322,7 @@ public fun SearchClient.generateSecuredApiKey(parentAPIKey: String, restriction:
322322
*
323323
* @param apiKey The secured API Key to check.
324324
* @return Duration left before the secured API key expires.
325-
* @throws IllegalArgumentException if [apiKey] doesn't have a [SecuredAPIKeyRestrictions.validUntil].
325+
* @throws IllegalArgumentException if [apiKey] doesn't have a [SecuredApiKeyRestrictions.validUntil].
326326
*/
327327
public fun securedApiKeyRemainingValidity(apiKey: String): Duration {
328328
val decoded = apiKey.decodeBase64String()

clients/algoliasearch-client-kotlin/client/src/commonMain/kotlin/com/algolia/client/extensions/internal/SearchClient.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package com.algolia.client.extensions.internal
22

33
import com.algolia.client.api.SearchClient
44
import com.algolia.client.model.search.SearchParamsObject
5-
import com.algolia.client.model.search.SecuredAPIKeyRestrictions
5+
import com.algolia.client.model.search.SecuredApiKeyRestrictions
66
import io.ktor.http.*
77
import kotlinx.serialization.json.JsonArray
88
import kotlinx.serialization.json.jsonObject
99
import kotlinx.serialization.json.jsonPrimitive
1010

1111
/**
12-
* Builds a restriction string based on provided [SecuredAPIKeyRestrictions].
12+
* Builds a restriction string based on provided [SecuredApiKeyRestrictions].
1313
*/
14-
internal fun SearchClient.buildRestrictionString(restriction: SecuredAPIKeyRestrictions): String {
14+
internal fun SearchClient.buildRestrictionString(restriction: SecuredApiKeyRestrictions): String {
1515
return Parameters.build {
1616
restriction.searchParams?.let { searchParams ->
1717
val json = options.json.encodeToJsonElement(SearchParamsObject.serializer(), searchParams).jsonObject

clients/algoliasearch-client-kotlin/client/src/commonTest/kotlin/com/algolia/client/TestSecureApiKey.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.algolia.client
22

33
import com.algolia.client.api.SearchClient
4-
import com.algolia.client.extensions.SecuredAPIKeyRestrictions
4+
import com.algolia.client.extensions.SecuredApiKeyRestrictions
55
import com.algolia.client.extensions.generateSecuredApiKey
66
import com.algolia.client.extensions.securedApiKeyRemainingValidity
77
import com.algolia.client.model.search.SearchParamsObject
@@ -14,14 +14,14 @@ class TestSecureApiKey {
1414

1515
@Test
1616
fun securedApiKey() {
17-
val parentAPIKey = "SearchOnlyApiKeyKeptPrivate"
18-
val restriction = SecuredAPIKeyRestrictions(
17+
val parentApiKey = "SearchOnlyApiKeyKeptPrivate"
18+
val restriction = SecuredApiKeyRestrictions(
1919
query = SearchParamsObject(filters = "_tags:user_42"),
2020
validUntil = Clock.System.now() + 2.days,
2121
)
2222

2323
val client = SearchClient("appId", "apiKey")
24-
val securedApiKey = client.generateSecuredApiKey(parentAPIKey, restriction)
24+
val securedApiKey = client.generateSecuredApiKey(parentApiKey, restriction)
2525
val validity = securedApiKeyRemainingValidity(securedApiKey)
2626
assertTrue { validity > 1.days }
2727
}

clients/algoliasearch-client-swift/Sources/Search/Extra/SearchHelpers.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,23 +440,23 @@ public extension SearchClient {
440440
}
441441

442442
/// Generate a secured API key
443-
/// - parameter parentAPIKey: The parent API key
443+
/// - parameter parentApiKey: The parent API key
444444
/// - parameter restriction: The restrictions
445445
/// - returns: String?
446446
func generateSecuredApiKey(
447-
parentAPIKey: String,
448-
with restriction: SecuredAPIKeyRestrictions = SecuredAPIKeyRestrictions()
447+
parentApiKey: String,
448+
with restriction: SecuredApiKeyRestrictions = SecuredApiKeyRestrictions()
449449
) throws -> String? {
450450
let queryParams = try restriction.toURLEncodedString()
451-
let hash = queryParams.hmac256(withKey: parentAPIKey)
451+
let hash = queryParams.hmac256(withKey: parentApiKey)
452452
return "\(hash)\(queryParams)".data(using: .utf8)?.base64EncodedString()
453453
}
454454

455455
/// Get the remaining validity of a secured API key
456-
/// - parameter securedAPIKey: The secured API key
456+
/// - parameter securedApiKey: The secured API key
457457
/// - returns: TimeInterval?
458-
func getSecuredApiKeyRemainingValidity(for securedAPIKey: String) -> TimeInterval? {
459-
guard let rawDecodedAPIKey = String(data: Data(base64Encoded: securedAPIKey) ?? Data(), encoding: .utf8),
458+
func getSecuredApiKeyRemainingValidity(for securedApiKey: String) -> TimeInterval? {
459+
guard let rawDecodedAPIKey = String(data: Data(base64Encoded: securedApiKey) ?? Data(), encoding: .utf8),
460460
!rawDecodedAPIKey.isEmpty else {
461461
return nil
462462
}

clients/algoliasearch-client-swift/Sources/Search/Extra/SecuredApiKeyRestrictionExtension.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Core
22
import Foundation
33

4-
public extension SecuredAPIKeyRestrictions {
4+
public extension SecuredApiKeyRestrictions {
55
func toURLEncodedString() throws -> String {
66
var queryDictionary: [String: Any] = [:]
77

playground/csharp/Playground/Playgrounds/Search.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ await PlaygroundHelper.Start("Deleting API Key", async () =>
201201

202202
Console.WriteLine("--- Generate Secured API Keys `GenerateSecuredApiKeys` ---");
203203
var generateSecuredApiKeys = _client.GenerateSecuredApiKey(_configuration.SearchApiKey,
204-
new SecuredAPIKeyRestrictions
204+
new SecuredApiKeyRestrictions
205205
{
206206
RestrictIndices = [DefaultIndex],
207207
});

playground/swift/playground/playground/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ guard let apiKey = Bundle.main.infoDictionary?["ALGOLIA_ADMIN_KEY"] as? String e
1818
}
1919

2020
guard applicationID != "" && apiKey != "" else {
21-
fatalError("AppID and APIKey must be filled in your Info.plist file")
21+
fatalError("AppID and ApiKey must be filled in your Info.plist file")
2222
}
2323

2424
struct Contact: Codable {

specs/search/helpers/generateSecuredApiKey.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ method:
1414
If you want to limit the number of requests that can be made with a secured API key, you must also rate-limit the key that you use to generate it. You can create a rate-limited key in the Algolia dashboard or use the Add API key or Update API key methods of an API client.
1515
parameters:
1616
- in: query
17-
name: parentAPIKey
17+
name: parentApiKey
1818
description: The search-only API key that the secured API key will inherit its restrictions from.
1919
required: true
2020
schema:
@@ -24,7 +24,7 @@ method:
2424
description: The options to add to the secured API key.
2525
required: true
2626
schema:
27-
$ref: '#/securedAPIKeyRestrictions'
27+
$ref: '#/securedApiKeyRestrictions'
2828
responses:
2929
'200':
3030
description: OK
@@ -35,7 +35,7 @@ method:
3535
'400':
3636
$ref: '../../common/responses/IndexNotFound.yml'
3737

38-
securedAPIKeyRestrictions:
38+
securedApiKeyRestrictions:
3939
type: object
4040
additionalProperties: false
4141
properties:

templates/go/search_helpers.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ func (c *APIClient) WaitForApiKeyWithContext(
296296

297297
// GenerateSecuredApiKey generates a public API key intended to restrict access
298298
// to certain records. This new key is built upon the existing key named
299-
// `parentAPIKey` and the following options:
300-
func (c *APIClient) GenerateSecuredApiKey(parentAPIKey string, restrictions *SecuredAPIKeyRestrictions) (string, error) {
301-
h := hmac.New(sha256.New, []byte(parentAPIKey))
299+
// `parentApiKey` and the following options:
300+
func (c *APIClient) GenerateSecuredApiKey(parentApiKey string, restrictions *SecuredApiKeyRestrictions) (string, error) {
301+
h := hmac.New(sha256.New, []byte(parentApiKey))
302302
303303
message, err := encodeRestrictions(restrictions)
304304
if err != nil {
@@ -315,7 +315,7 @@ func (c *APIClient) GenerateSecuredApiKey(parentAPIKey string, restrictions *Sec
315315
return key, nil
316316
}
317317

318-
func encodeRestrictions(restrictions *SecuredAPIKeyRestrictions) (string, error) {
318+
func encodeRestrictions(restrictions *SecuredApiKeyRestrictions) (string, error) {
319319
toSerialize := map[string]any{}
320320
if restrictions.Filters != nil {
321321
toSerialize["filters"] = *restrictions.Filters
@@ -357,4 +357,4 @@ func encodeRestrictions(restrictions *SecuredAPIKeyRestrictions) (string, error)
357357
}
358358

359359
return strings.Join(queryString, "&"), nil
360-
}
360+
}

templates/javascript/clients/client/api/helpers.mustache

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,20 +264,24 @@ searchForFacets(
264264
},
265265

266266
/**
267-
* Helper: Generates a secured API key based on the given `parentAPIKey` and given `restrictions`.
267+
* Helper: Generates a secured API key based on the given `parentApiKey` and given `restrictions`.
268268
*
269-
* @summary Helper: Generates a secured API key based on the given `parentAPIKey` and given `restrictions`.
269+
* @summary Helper: Generates a secured API key based on the given `parentApiKey` and given `restrictions`.
270270
* @param generateSecuredApiKey - The `generateSecuredApiKey` object.
271-
* @param generateSecuredApiKey.parentAPIKey - The base API key from which to generate the new secured one.
271+
* @param generateSecuredApiKey.parentApiKey - The base API key from which to generate the new secured one.
272272
* @param generateSecuredApiKey.restrictions - A set of properties defining the restrictions of the secured API key.
273273
*/
274274
generateSecuredApiKey({
275-
parentAPIKey,
275+
parentApiKey,
276276
restrictions = {},
277277
}: GenerateSecuredApiKeyOptions): string {
278-
const queryParameters = serializeQueryParameters(restrictions);
278+
const queryParameters = serializeQueryParameters({
279+
...restrictions,
280+
...restrictions.searchParams,
281+
searchParams: undefined,
282+
});
279283
return Buffer.from(
280-
createHmac('sha256', parentAPIKey)
284+
createHmac('sha256', parentApiKey)
281285
.update(queryParameters)
282286
.digest('hex') + queryParameters
283287
).toString('base64');
@@ -390,4 +394,4 @@ async replaceAllObjects(
390394
});
391395

392396
return { copyOperationResponse, batchResponses, moveOperationResponse };
393-
},
397+
},

templates/javascript/clients/client/model/clientMethodProps.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ export type GenerateSecuredApiKeyOptions = {
104104
/**
105105
* The base API key from which to generate the new secured one.
106106
*/
107-
parentAPIKey: string;
107+
parentApiKey: string;
108108
109109
/**
110110
* A set of properties defining the restrictions of the secured API key.
111111
*/
112-
restrictions?: SecuredAPIKeyRestrictions;
112+
restrictions?: SecuredApiKeyRestrictions;
113113
}
114114

115115
export type GetSecuredApiKeyRemainingValidityOptions = {

templates/javascript/tests/requests/helpers.mustache

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,7 @@
1-
describe('generateSecuredApiKey', () => {
2-
test('generates a key without restrictions', () => {
3-
const resp = client.generateSecuredApiKey({ parentAPIKey: 'foo' });
4-
expect(resp).toEqual(
5-
'NjgzNzE2ZDlkN2Y4MmVlZDE3NGM2Y2FlYmUwODZlZTkzMzc2Yzc5ZDdjNjFkZDY3MGVhMDBmN2Y4ZDZlYjBhOA=='
6-
);
7-
});
8-
9-
test('generates a key with restrictions', () => {
10-
const resp = client.generateSecuredApiKey({
11-
parentAPIKey: 'foo',
12-
restrictions: {
13-
validUntil: 100,
14-
restrictIndices: ['bar'],
15-
restrictSources: '192,168.1.0/24',
16-
userToken: 'foobarbaz',
17-
searchParams: {
18-
query: 'foo',
19-
},
20-
},
21-
});
22-
expect(resp).toEqual(
23-
'M2RlY2Y5ZjgzMDU1ZDRiYjkyOTdjYjYxYWNjNWNhNTQ5ZGI5Mjc3ZmVjNmNmNjM2ZjAwMTA4OGRjNDI5YjFhOXZhbGlkVW50aWw9MTAwJnJlc3RyaWN0SW5kaWNlcz0lNUIlMjJiYXIlMjIlNUQmcmVzdHJpY3RTb3VyY2VzPTE5MiUyQzE2OC4xLjAlMkYyNCZ1c2VyVG9rZW49Zm9vYmFyYmF6JnNlYXJjaFBhcmFtcz0lN0IlMjJxdWVyeSUyMiUzQSUyMmZvbyUyMiU3RA=='
24-
);
25-
});
26-
});
27-
281
describe('getSecuredApiKeyRemainingValidity', () => {
292
test('is able to check the remaining validity of a key', () => {
303
const resp = client.generateSecuredApiKey({
31-
parentAPIKey: 'foo',
4+
parentApiKey: 'foo',
325
restrictions: { validUntil: 42 },
336
});
347
expect(resp).toEqual(
@@ -154,4 +127,4 @@ describe('replaceAllObjects', () => {
154127
moveOperationResponse: { taskID: 21, updatedAt: 'foobar' },
155128
});
156129
});
157-
});
130+
});

templates/php/api.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,12 @@ use {{invokerPackage}}\Support\Helpers;
498498
/**
499499
* Helper: Generate a secured API Key.
500500
*
501-
* @param string $parentAPIKey Parent API Key
501+
* @param string $parentApiKey Parent API Key
502502
* @param array $restrictions API Key's restrictions
503503
*
504504
* @return string
505505
*/
506-
public static function generateSecuredApiKey($parentAPIKey, $restrictions)
506+
public static function generateSecuredApiKey($parentApiKey, $restrictions)
507507
{
508508
$formattedRestrictions = [];
509509
@@ -525,7 +525,7 @@ use {{invokerPackage}}\Support\Helpers;
525525
$urlEncodedRestrictions = http_build_query($formattedRestrictions, encoding_type: PHP_QUERY_RFC3986);
526526

527527

528-
$content = hash_hmac('sha256', $urlEncodedRestrictions, $parentAPIKey).$urlEncodedRestrictions;
528+
$content = hash_hmac('sha256', $urlEncodedRestrictions, $parentApiKey).$urlEncodedRestrictions;
529529

530530
return base64_encode($content);
531531
}

templates/python/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from algoliasearch.search.models.batch_request import BatchRequest
66
from algoliasearch.search.models.scope_type import ScopeType
77
from algoliasearch.search.models.action import Action
8-
from algoliasearch.search.models.secured_api_key_restrictions import SecuredAPIKeyRestrictions
8+
from algoliasearch.search.models.secured_api_key_restrictions import SecuredApiKeyRestrictions
99
{{/isSearchClient}}
1010

1111
{{#operations}}{{#operation}}{{#imports}}

0 commit comments

Comments
 (0)