Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auth:searchApiKeys #39

Merged
merged 10 commits into from
Jan 7, 2021
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ tasks.withType<Jar> {
)
)
}
from(configurations.compileClasspath.get().map { if (it.isDirectory()) it else zipTree(it) })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol.

}
92 changes: 92 additions & 0 deletions doc/1/controllers/auth/search-api-keys/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
code: true
type: page
title: searchApiKeys
description: Searches API keys for the currently logged user.
---

# searchApiKeys

<SinceBadge version="auto-version" />

<SinceBadge version="Kuzzle 2.1.0" />

Searches API keys for the currently logged user.

:::: tabs
::: tab Java

## Arguments

```java
public CompletableFuture<SearchResult> searchApiKeys(
ConcurrentHashMap<String, Object> query) throws NotConnectedException, InternalException

public CompletableFuture<SearchResult> searchApiKeys(
ConcurrentHashMap<String, Object> query,
Integer from) throws NotConnectedException, InternalException

public CompletableFuture<SearchResult> searchApiKeys(
ConcurrentHashMap<String, Object> query
Integer from
Integer size) throws NotConnectedException, InternalException
```


| Property | Type | Description |
| --- | --- | --- |
| `query` | <pre>ConcurrentHashMap<String, Object></pre> | Search query |
| `from` | <pre>Integer</pre><br/>(`0`) | (optional) Offset of the first document to fetch |
| `size` | <pre>Integer</pre> | (optional) Maximum number of documents to retrieve per page |

### query

The search query to apply to API keys content, using the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/query-dsl.html) syntax.

If left empty, the result will return all available API keys for the currently logged user.

## Return

Returns a [SearchResult](/sdk/jvm/1/core-classes/search-result) object.


## Usage

<<< ./snippets/search-api-keys-java.java

:::
::: tab Kotlin

## Arguments

```kotlin
fun searchApiKeys(
query: ConcurrentHashMap<String, Any?>,
from: Int = 0,
size: Int? = null): CompletableFuture<SearchResult>
```


| Property | Type | Description |
| --- | --- | --- |
| `query` | <pre>ConcurrentHashMap<String, Any?></pre> | Search query |
| `from` | <pre>Int</pre><br/>(`0`) | (optional) Offset of the first document to fetch |
| `size` | <pre>Int</pre> | (optional) Maximum number of documents to retrieve per page |

### query

The search query to apply to API keys content, using the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/query-dsl.html) syntax.

If left empty, the result will return all available API keys of the currently logged user.

## Return

Returns a [SearchResult](/sdk/jvm/1/core-classes/search-result) object.


## Usage

<<< ./snippets/search-api-keys-kotlin.kt

:::
::::
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

ConcurrentHashMap<String, Object> description = new ConcurrentHashMap<>();
description.put("description", "LoRa permanent API Key");

ConcurrentHashMap<String, Object> query = new ConcurrentHashMap<>();
query.put("controller", "security");
query.put("action", "createApiKey");
query.put("userId", "jared.doe");
query.put("refresh", "wait_for");
query.put("body", description);

kuzzle.query(query).get();

description.put("description", "Sigfox API key");
query.put("body", description);
kuzzle.query(query).get();

description.put("description", "LoRa 6 month API key");
query.put("body", description);
query.put("expiresIn", 36000);
kuzzle.query(query).get();

ConcurrentHashMap<String, Object> credentials = new ConcurrentHashMap<>();
credentials.put("username", "jared.doe");
credentials.put("password", "password");

kuzzle.getAuthController().login("local", credentials).get();

ConcurrentHashMap<String, Object> match = new ConcurrentHashMap<>();
match.put("description", "LoRa");

ConcurrentHashMap<String, Object> squery = new ConcurrentHashMap<>();
squery.put("match", match);

SearchResult results = kuzzle.getAuthController()
.searchApiKeys(squery).get();

String output = String.format("Found %d API keys matching 'LoRa'", results.total);

System.out.println(output);

/*
{
"total"=2,
"hits"=[
{
"_id"="znEwbG8BJASM_0-bWU-q",
"_source"={
"description"="LoRa permanent API key",
"userId"="jared.doe",
"expiresAt"=-1,
"fingerprint": "4ee98cb8c614e99213e7695f822e42325d86c93cfaf39cb40e860939e784c8e6",
"ttl"=-1
}
},
{
"_id"="zXEwbG8BJASM_0-bWU-q",
"_source"={
"description"="LoRa 6 month API key",
"userId"="jared.doe",
"expiresAt"=31557600000,
"fingerprint": "4ee98cb8c614e99213e7695f822e42325d86c93cfaf39cb40e860939e784c8e6",
"ttl"=360000
}
}
]
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: java-auth#searchApiKeys
description: Searches API keys for the currently loggued user.
hooks:
before: >
curl --fail -H "Content-type: application/json" -d '{
"content": {
"profileIds": ["default"]
},
"credentials": {
"local": {
"username": "jared.doe",
"password": "password"
}
}
}' "kuzzle:7512/users/jared.doe/_create?refresh=wait_for"
after:
curl -XDELETE kuzzle:7512/users/jared.doe
template: default
expected:
- Found 2 API keys matching 'LoRa'
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

val description: ConcurrentHashMap<String?, Any?> = ConcurrentHashMap<String?, Any?>().apply {
put("description", "LoRa permanent API Key")
}

val query: ConcurrentHashMap<String?, Any?> = ConcurrentHashMap<String?, Any?>().apply {
put("controller", "security")
put("action", "createApiKey")
put("userId", "jared.doe")
put("refresh", "wait_for")
put("body", description)
}

kuzzle.query(query).get()

description.put("description", "Sigfox API key")
query.put("body", description);
kuzzle.query(query).get()

description.put("description", "LoRa 6 month API key")
query.put("body", description);
query.put("expiresIn", 36000);
kuzzle.query(query).get()

kuzzle.authController.login("local", ConcurrentHashMap<String, Any?>().apply {
put("username", "jared.doe")
put("password", "password")
}).get()

val match: ConcurrentHashMap<String, Any?> =
ConcurrentHashMap<String, Any?>().apply {
put("description", "LoRa")
}

val squery: ConcurrentHashMap<String, Any?> =
ConcurrentHashMap<String, Any?>().apply {
put("match", match)
}

val results = kuzzle
.authController
.searchApiKeys(squery).get();

print("Found ${results.total} API keys matching 'LoRa'");

/*
{
"total"=2,
"hits"=[
{
"_id"="znEwbG8BJASM_0-bWU-q",
"_source"={
"description"="LoRa permanent API key",
"userId"="jared.doe",
"expiresAt"=-1,
"fingerprint": "4ee98cb8c614e99213e7695f822e42325d86c93cfaf39cb40e860939e784c8e6",
"ttl"=-1
}
},
{
"_id"="zXEwbG8BJASM_0-bWU-q",
"_source"={
"description"="LoRa 6 month API key",
"userId"="jared.doe",
"expiresAt"=31557600000,
"fingerprint": "4ee98cb8c614e99213e7695f822e42325d86c93cfaf39cb40e860939e784c8e6",
"ttl"=360000
}
}
]
}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: kotlin-auth#searchApiKeys
description: Searches API keys for the currently loggued user.
hooks:
before: >
curl --fail -H "Content-type: application/json" -d '{
"content": {
"profileIds": ["default"]
},
"credentials": {
"local": {
"username": "jared.doe",
"password": "password"
}
}
}' "kuzzle:7512/users/jared.doe/_create?refresh=wait_for"
after:
curl -XDELETE kuzzle:7512/users/jared.doe
template: default
expected:
- Found 2 API keys matching 'LoRa'
25 changes: 25 additions & 0 deletions doc/1/controllers/document/search/snippets/search-kotlin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,28 @@ val searchQuery: ConcurrentHashMap<String, Any?> =
val results = kuzzle
.documentController
.search("nyc-open-data", "yellow-taxi", searchQuery).get();

/*
{
"aggregations"=undefined,
"hits"=[
{
"_id"="AWgi6A1POQUM6ucJ3q06",
"_score"=0.046520017,
"_source"={
"category"="suv",
"_kuzzle_info"={
"author"="-1",
"createdAt"=1546773859655,
"updatedAt"=null,
"updater"=null
}
}
},
...
]
},
"total"=5,
"fetched"=5,
"scroll_id"=undefined
*/
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
ConcurrentHashMap<String, Object> searchQuery = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> match = new ConcurrentHashMap<>();
match.put("capacity", 4);
searchQuery.put("match", match);
ConcurrentHashMap<String, Object> searchQuery = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> match = new ConcurrentHashMap<>();
match.put("capacity", 4);
searchQuery.put("match", match);

ConcurrentHashMap<String, Object> changes = new ConcurrentHashMap<>();
changes.put("capacity", 42);
ConcurrentHashMap<String, Object> changes = new ConcurrentHashMap<>();
changes.put("capacity", 42);

ConcurrentHashMap<String, ArrayList<Object>> result = kuzzle
.getDocumentController()
.updateByQuery("nyc-open-data", "yellow-taxi", searchQuery, changes)
.get();
ConcurrentHashMap<String, ArrayList<Object>> result = kuzzle
.getDocumentController()
.updateByQuery("nyc-open-data", "yellow-taxi", searchQuery, changes)
.get();

/*
{
successes=[
{
_id=<document-id>,
_source=<updated document> // if source set to true
status=200
},
{
_id=<document id>,
_source=<updated document> // if source set to true
status=200
}
],
errors=[]
}
*/
/*
{
successes=[
{
_id=<document-id>,
_source=<updated document> // if source set to true
status=200
},
{
_id=<document id>,
_source=<updated document> // if source set to true
status=200
}
],
errors=[]
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ val result: ConcurrentHashMap<String, ArrayList<Any?>> =
.documentController
.updateByQuery("nyc-open-data", "yellow-taxi", searchQuery, changes)
.get()

/*
{
successes=[
{
_id=<document-id>,
_source=<updated document> // if source set to true
status=200
},
{
_id=<document id>,
_source=<updated document> // if source set to true
status=200
}
],
errors=[]
}
*/
Loading