-
Notifications
You must be signed in to change notification settings - Fork 17
Add API key methods #478
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
Merged
Merged
Add API key methods #478
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
code: true | ||
type: page | ||
title: createApiKey | ||
description: Creates a new API key for the currently loggued user. | ||
--- | ||
|
||
# createApiKey | ||
|
||
<SinceBadge version="7.1.0" /> | ||
|
||
<SinceBadge version="Kuzzle 2.1.0" /> | ||
|
||
Creates a new API key for the currently loggued user. | ||
|
||
<br /> | ||
|
||
```js | ||
createApiKey(description, [options]); | ||
``` | ||
|
||
<br /> | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| `description` | <pre>string</pre> | API key description | | ||
| `options` | <pre>object</pre> | Additional options | | ||
|
||
### options | ||
|
||
Additional query options | ||
|
||
| Property | Type<br />(default) | Description | | ||
| --- | --- | --- | | ||
| `expiresIn` | <pre>string/number</pre><br />(`-1`) | Expiration duration | | ||
| `_id` | <pre>string</pre><br />(`null`) | API key unique ID | | ||
| `refresh` | <pre>boolean</pre><br />(`false`) | If set to `wait_for`, Kuzzle will not respond until the API key is indexed | | ||
|
||
**Notes**: | ||
- `expiresIn`: | ||
- if a raw number is provided (not enclosed between quotes), then the expiration delay is in milliseconds. Example: `86400000` | ||
- if this value is a string, then its content is parsed by the [ms](https://www.npmjs.com/package/ms) library. Examples: `"6d"`, `"10h"` | ||
- if `-1` is provided, the token will never expire | ||
|
||
## Resolves | ||
|
||
An object containing the newly created API key: | ||
|
||
| Name | Type | Description | | ||
| --------- | ----------------- | ---------------- | | ||
| `_id` | <pre>string</pre> | ID of the newly created API key | | ||
| `_source` | <pre>object</pre> | API key content | | ||
|
||
The API key content has the following properties: | ||
|
||
| Name | Type | Description | | ||
| --------- | ----------------- | ---------------- | | ||
| `userId` | <pre>string</pre> | User kuid | | ||
| `expiresAt` | <pre>number</pre> | Aexpiration date in UNIX micro-timestamp format (`-1` if the token never expires) | | ||
| `ttl` | <pre>number</pre> | Original TTL | | ||
| `description` | <pre>string</pre> | API key description | | ||
| `token` | <pre>string</pre> | Authentication token associated with this API key | | ||
|
||
::: warning | ||
The authentication token `token` will never be returned by Kuzzle again. If you lose it, you'll have to delete the API key and recreate a new one. | ||
::: | ||
|
||
## Usage | ||
|
||
<<< ./snippets/create-api-key.js |
23 changes: 23 additions & 0 deletions
23
doc/7/controllers/auth/create-api-key/snippets/create-api-key.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
try { | ||
await kuzzle.auth.login('local', { username: 'john.doe', password: 'password' }); | ||
|
||
const apiKey = await kuzzle.auth.createApiKey('Sigfox API key'); | ||
|
||
console.log(apiKey); | ||
/* | ||
{ | ||
_id: '-fQRa28BsidO6V_wmOcL', | ||
_source: { | ||
description: 'Sigfox API key', | ||
userId: 'john.doe', | ||
expiresAt: -1, | ||
ttl: -1, | ||
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiJqb2huLmRvZSIsImlhdCI6MTU3ODA0OTMxMn0.XO_keW6EtsCGmPzxVJCChU9VREakEECDGg-N5lhCfF8' | ||
} | ||
} | ||
*/ | ||
|
||
console.log('API key successfully created'); | ||
} catch (e) { | ||
console.error(e); | ||
} |
22 changes: 22 additions & 0 deletions
22
doc/7/controllers/auth/create-api-key/snippets/create-api-key.test.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: auth#createApiKey | ||
description: Creates a new API key for the currently loggued user. | ||
hooks: | ||
before: > | ||
curl -H "Content-type: application/json" -d '{ | ||
"content": { | ||
"profileIds": ["default"] | ||
}, | ||
"credentials": { | ||
"local": { | ||
"username": "john.doe", | ||
"password": "password" | ||
} | ||
} | ||
}' "kuzzle:7512/users/john.doe/_create?refresh=wait_for" | ||
after: | ||
curl -XDELETE kuzzle:7512/users/john.doe | ||
template: default | ||
expected: | ||
- "description: 'Sigfox API key'" | ||
- "userId: 'john.doe'" | ||
- API key successfully created |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
code: true | ||
type: page | ||
title: deleteApiKey | ||
description: Deletes an API key for the currently loggued user. | ||
--- | ||
|
||
# deleteApiKey | ||
|
||
<SinceBadge version="7.1.0" /> | ||
|
||
<SinceBadge version="Kuzzle 2.1.0" /> | ||
|
||
Deletes an API key for the currently loggued user. | ||
|
||
<br /> | ||
|
||
```js | ||
deleteApiKey(id, [options]); | ||
``` | ||
|
||
<br /> | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| `id` | <pre>string</pre> | API key unique ID | | ||
| `options` | <pre>object</pre> | Additional options | | ||
|
||
### options | ||
|
||
Additional query options | ||
|
||
| Property | Type<br />(default) | Description | | ||
| --- | --- | --- | | ||
| `refresh` | <pre>boolean</pre><br />(`false`) | If set to `wait_for`, Kuzzle will not respond until the API key is indexed | | ||
|
||
## Resolves | ||
|
||
Resolves if the API key is successfully deleted. | ||
|
||
## Usage | ||
|
||
<<< ./snippets/delete-api-key.js |
9 changes: 9 additions & 0 deletions
9
doc/7/controllers/auth/delete-api-key/snippets/delete-api-key.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
try { | ||
await kuzzle.auth.login('local', { username: 'john.doe', password: 'password' }); | ||
|
||
await kuzzle.auth.deleteApiKey('fQRa28BsidO6V_wmOcL'); | ||
|
||
console.log('API key successfully deleted'); | ||
} catch (e) { | ||
console.error(e); | ||
} |
24 changes: 24 additions & 0 deletions
24
doc/7/controllers/auth/delete-api-key/snippets/delete-api-key.test.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: auth#deleteApiKey | ||
description: Deletes an API key for the currently loggued user | ||
hooks: | ||
before: > | ||
curl -H "Content-type: application/json" -d '{ | ||
"content": { | ||
"profileIds": ["default"] | ||
}, | ||
"credentials": { | ||
"local": { | ||
"username": "john.doe", | ||
"password": "password" | ||
} | ||
} | ||
}' "kuzzle:7512/users/john.doe/_create?refresh=wait_for" | ||
&& | ||
curl -XPOST -H "Content-type: application/json" -d '{ | ||
"description": "Sigfox API key" | ||
}' "kuzzle:7512/users/john.doe/api-keys/_create?_id=fQRa28BsidO6V_wmOcL&refresh=wait_for" | ||
after: | ||
curl -XDELETE kuzzle:7512/users/john.doe | ||
template: default | ||
expected: | ||
- API key successfully deleted |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
code: true | ||
type: page | ||
title: searchApiKeys | ||
description: Searches API keys for the currently loggued user. | ||
--- | ||
|
||
# searchApiKeys | ||
|
||
<SinceBadge version="7.1.0" /> | ||
|
||
<SinceBadge version="Kuzzle 2.1.0" /> | ||
|
||
Searches API keys for the currently loggued user. | ||
|
||
<br /> | ||
|
||
```js | ||
searchApiKeys([query], [options]); | ||
``` | ||
|
||
<br /> | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| `query` | <pre>object</pre> | Search query | | ||
| `options` | <pre>object</pre> | Additional options | | ||
|
||
### query | ||
|
||
The search query to apply to API keys content, using [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 loggued user. | ||
|
||
### options | ||
|
||
Additional query options | ||
|
||
| Property | Type<br/>(default) | Description | | ||
| ---------- | ------------------ | ------------ | | ||
| `from` | <pre>number</pre><br/>(`0`) | Offset of the first document to fetch | | ||
| `size` | <pre>number</pre><br/>(`10`) | Maximum number of documents to retrieve per page | | ||
|
||
## Resolves | ||
|
||
Resolves an object with the following properties: | ||
|
||
| Name | Type | Description | | ||
| --------- | ----------------- | ---------------- | | ||
| `hits` | <pre>object[]</pre> | Array of objects representing found API keys | | ||
| `total` | <pre>number</pre> | Total number of API keys found. Depending on pagination options, this can be greater than the actual number of API keys in a single result page | | ||
|
||
Each object of the `hits` array has the following properties: | ||
|
||
| Name | Type | Description | | ||
| --------- | ----------------- | ---------------- | | ||
| `_id_` | <pre>string</pre> | API key unique ID | | ||
| `_source` | <pre>object</pre> | API key definition without the `token` field | | ||
|
||
|
||
## Usage | ||
|
||
<<< ./snippets/search-api-keys.js |
57 changes: 57 additions & 0 deletions
57
doc/7/controllers/auth/search-api-keys/snippets/search-api-keys.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
try { | ||
const promises = []; | ||
|
||
// Create some API keys for user "john.doe" | ||
promises.push( | ||
kuzzle.security.createApiKey('john.doe', 'Sigfox API key')); | ||
promises.push( | ||
kuzzle.security.createApiKey('john.doe', 'LoRa 6 month API key', { | ||
expiresIn: '6m' | ||
})); | ||
promises.push( | ||
kuzzle.security.createApiKey('john.doe', 'LoRa permanent API key', { | ||
refresh: 'wait_for' | ||
})); | ||
|
||
await Promise.all(promises); | ||
|
||
// Log as "john.doe" | ||
await kuzzle.auth.login('local', { username: 'john.doe', password: 'password' }); | ||
|
||
const results = await kuzzle.auth.searchApiKeys({ | ||
match: { | ||
description: 'LoRa' | ||
} | ||
}); | ||
|
||
console.log(results); | ||
/* | ||
{ | ||
"total": 2, | ||
"hits": [ | ||
{ | ||
"_id": "znEwbG8BJASM_0-bWU-q", | ||
"_source": { | ||
"description": "LoRa permanent API key", | ||
"userId": "john.doe", | ||
"expiresAt": -1, | ||
"ttl": -1 | ||
} | ||
}, | ||
{ | ||
"_id": "zXEwbG8BJASM_0-bWU-q", | ||
"_source": { | ||
"description": "LoRa 1 year API key", | ||
"userId": "john.doe", | ||
"expiresAt": 31557600000, | ||
"ttl": 360000 | ||
} | ||
} | ||
] | ||
} | ||
*/ | ||
|
||
console.log(`Found ${results.total} API keys matching "LoRa"`); | ||
} catch (e) { | ||
console.error(e); | ||
} |
20 changes: 20 additions & 0 deletions
20
doc/7/controllers/auth/search-api-keys/snippets/search-api-keys.test.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: auth#searchApiKeys | ||
description: Searches API keys for the currently loggued user. | ||
hooks: | ||
before: > | ||
curl -H "Content-type: application/json" -d '{ | ||
"content": { | ||
"profileIds": ["default"] | ||
}, | ||
"credentials": { | ||
"local": { | ||
"username": "john.doe", | ||
"password": "password" | ||
} | ||
} | ||
}' "kuzzle:7512/users/john.doe/_create?refresh=wait_for" | ||
after: | ||
curl -XDELETE kuzzle:7512/users/john.doe | ||
template: default | ||
expected: | ||
- Found 2 API keys matching "LoRa" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Food for thoughts: I wonder if instead of this, we should link to the underlying API method instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember that we already discuss about adding a link to the original API on each SDK method.
But why did you say that about the Since badge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we don't need it if we have links to the underlying API. Moreover, I don't like having to maintain since badges in multiple places, I'd prefer to have only to worry about those badges in the API section of the documentation when updating an API feature.