feat: add ability to name tokens (#6365)

* add tokens switch

* reorged TokensPage

* using Trans component for description

* using Trans component on DeleteDialog

* add owner col

* simplify hook return

* lint

* type for response

* added flag for name

* fixed auth

* lint, prettier, tests

* added unique index for login type token

* remove tokens by name

* better check for unique constraint

* docs

* test: Fix dbfake to insert token name

* fix doc tests

* Update cli/tokens.go

Co-authored-by: Steven Masley <Emyrk@users.noreply.github.com>

* Update coderd/database/migrations/000102_add_apikey_name.down.sql

Co-authored-by: Steven Masley <Emyrk@users.noreply.github.com>

* add more specificity to IsUniqueViolation check

* fix tests

* Fix AutorizeAllEndpoints

* rename migration

---------

Co-authored-by: Steven Masley <stevenmasley@coder.com>
Co-authored-by: Steven Masley <Emyrk@users.noreply.github.com>
This commit is contained in:
Kira Pilot
2023-03-02 09:39:38 -08:00
committed by GitHub
co-authored by Steven Masley Steven Masley
parent e3a4861e93
commit 71d1e63af0
37 changed files with 447 additions and 63 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ We track the following resources:
| <b>Resource<b> | |
| ----------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| APIKey<br><i>write</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody><tr><td>created_at</td><td>false</td></tr><tr><td>expires_at</td><td>false</td></tr><tr><td>hashed_secret</td><td>false</td></tr><tr><td>id</td><td>false</td></tr><tr><td>ip_address</td><td>false</td></tr><tr><td>last_used</td><td>false</td></tr><tr><td>lifetime_seconds</td><td>false</td></tr><tr><td>login_type</td><td>false</td></tr><tr><td>scope</td><td>false</td></tr><tr><td>updated_at</td><td>false</td></tr><tr><td>user_id</td><td>false</td></tr></tbody></table> |
| APIKey<br><i>write</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody><tr><td>created_at</td><td>false</td></tr><tr><td>expires_at</td><td>false</td></tr><tr><td>hashed_secret</td><td>false</td></tr><tr><td>id</td><td>false</td></tr><tr><td>ip_address</td><td>false</td></tr><tr><td>last_used</td><td>false</td></tr><tr><td>lifetime_seconds</td><td>false</td></tr><tr><td>login_type</td><td>false</td></tr><tr><td>scope</td><td>false</td></tr><tr><td>token_name</td><td>false</td></tr><tr><td>updated_at</td><td>false</td></tr><tr><td>user_id</td><td>false</td></tr></tbody></table> |
| Group<br><i>create, write, delete</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody><tr><td>avatar_url</td><td>true</td></tr><tr><td>id</td><td>true</td></tr><tr><td>members</td><td>true</td></tr><tr><td>name</td><td>true</td></tr><tr><td>organization_id</td><td>false</td></tr><tr><td>quota_allowance</td><td>true</td></tr></tbody></table> |
| GitSSHKey<br><i>create</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody><tr><td>created_at</td><td>false</td></tr><tr><td>private_key</td><td>true</td></tr><tr><td>public_key</td><td>true</td></tr><tr><td>updated_at</td><td>false</td></tr><tr><td>user_id</td><td>true</td></tr></tbody></table> |
| License<br><i>create, delete</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody><tr><td>exp</td><td>true</td></tr><tr><td>id</td><td>false</td></tr><tr><td>jwt</td><td>false</td></tr><tr><td>uploaded_at</td><td>true</td></tr><tr><td>uuid</td><td>true</td></tr></tbody></table> |
+9 -5
View File
@@ -368,6 +368,7 @@
"lifetime_seconds": 0,
"login_type": "password",
"scope": "all",
"token_name": "string",
"updated_at": "2019-08-24T14:15:22Z",
"user_id": "a169451c-8525-4352-b8ca-070dd449a1a5"
}
@@ -384,6 +385,7 @@
| `lifetime_seconds` | integer | true | | |
| `login_type` | [codersdk.LoginType](#codersdklogintype) | true | | |
| `scope` | [codersdk.APIKeyScope](#codersdkapikeyscope) | true | | |
| `token_name` | string | true | | |
| `updated_at` | string | true | | |
| `user_id` | string | true | | |
@@ -1141,16 +1143,18 @@ CreateParameterRequest is a structure used to create a new parameter value for a
```json
{
"lifetime": 0,
"scope": "all"
"scope": "all",
"token_name": "string"
}
```
### Properties
| Name | Type | Required | Restrictions | Description |
| ---------- | -------------------------------------------- | -------- | ------------ | ----------- |
| `lifetime` | integer | false | | |
| `scope` | [codersdk.APIKeyScope](#codersdkapikeyscope) | false | | |
| Name | Type | Required | Restrictions | Description |
| ------------ | -------------------------------------------- | -------- | ------------ | ----------- |
| `lifetime` | integer | false | | |
| `scope` | [codersdk.APIKeyScope](#codersdkapikeyscope) | false | | |
| `token_name` | string | false | | |
#### Enumerated Values
+53 -2
View File
@@ -577,6 +577,7 @@ curl -X GET http://coder-server:8080/api/v2/users/{user}/keys/tokens \
"lifetime_seconds": 0,
"login_type": "password",
"scope": "all",
"token_name": "string",
"updated_at": "2019-08-24T14:15:22Z",
"user_id": "a169451c-8525-4352-b8ca-070dd449a1a5"
}
@@ -603,6 +604,7 @@ Status Code **200**
| `» lifetime_seconds` | integer | true | | |
| `» login_type` | [codersdk.LoginType](schemas.md#codersdklogintype) | true | | |
| `» scope` | [codersdk.APIKeyScope](schemas.md#codersdkapikeyscope) | true | | |
| `» token_name` | string | true | | |
| `» updated_at` | string(date-time) | true | | |
| `» user_id` | string(uuid) | true | | |
@@ -638,7 +640,8 @@ curl -X POST http://coder-server:8080/api/v2/users/{user}/keys/tokens \
```json
{
"lifetime": 0,
"scope": "all"
"scope": "all",
"token_name": "string"
}
```
@@ -667,7 +670,54 @@ curl -X POST http://coder-server:8080/api/v2/users/{user}/keys/tokens \
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Get API key
## Get API key by token name
### Code samples
```shell
# Example request using curl
curl -X GET http://coder-server:8080/api/v2/users/{user}/keys/tokens/{keyname} \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```
`GET /users/{user}/keys/tokens/{keyname}`
### Parameters
| Name | In | Type | Required | Description |
| --------- | ---- | -------------- | -------- | -------------------- |
| `user` | path | string | true | User ID, name, or me |
| `keyname` | path | string(string) | true | Key Name |
### Example responses
> 200 Response
```json
{
"created_at": "2019-08-24T14:15:22Z",
"expires_at": "2019-08-24T14:15:22Z",
"id": "string",
"last_used": "2019-08-24T14:15:22Z",
"lifetime_seconds": 0,
"login_type": "password",
"scope": "all",
"token_name": "string",
"updated_at": "2019-08-24T14:15:22Z",
"user_id": "a169451c-8525-4352-b8ca-070dd449a1a5"
}
```
### Responses
| Status | Meaning | Description | Schema |
| ------ | ------------------------------------------------------- | ----------- | -------------------------------------------- |
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.APIKey](schemas.md#codersdkapikey) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Get API key by ID
### Code samples
@@ -700,6 +750,7 @@ curl -X GET http://coder-server:8080/api/v2/users/{user}/keys/{keyid} \
"lifetime_seconds": 0,
"login_type": "password",
"scope": "all",
"token_name": "string",
"updated_at": "2019-08-24T14:15:22Z",
"user_id": "a169451c-8525-4352-b8ca-070dd449a1a5"
}
+5 -5
View File
@@ -28,8 +28,8 @@ coder tokens [flags]
## Subcommands
| Name | Purpose |
| -------------------------------------------- | --------------- |
| [<code>create</code>](./coder_tokens_create) | Create a tokens |
| [<code>list</code>](./coder_tokens_list) | List tokens |
| [<code>remove</code>](./coder_tokens_remove) | Delete a token |
| Name | Purpose |
| -------------------------------------------- | -------------- |
| [<code>create</code>](./coder_tokens_create) | Create a token |
| [<code>list</code>](./coder_tokens_list) | List tokens |
| [<code>remove</code>](./coder_tokens_remove) | Delete a token |
+8 -1
View File
@@ -2,7 +2,7 @@
# coder tokens create
Create a tokens
Create a token
## Usage
@@ -20,3 +20,10 @@ Specify a duration for the lifetime of the token.
| --- | --- |
| Consumes | <code>$CODER_TOKEN_LIFETIME</code> |
| Default | <code>720h0m0s</code> |
### --name, -n
Specify a human-readable name.
<br/>
| | |
| --- | --- |
+2 -2
View File
@@ -22,11 +22,11 @@ Specifies whether all users' tokens will be listed or not (must have Owner role
### --column, -c
Columns to display in table output. Available columns: id, last used, expires at, created at, owner
Columns to display in table output. Available columns: id, name, last used, expires at, created at, owner
<br/>
| | |
| --- | --- |
| Default | <code>[id,last used,expires at,created at]</code> |
| Default | <code>[id,name,last used,expires at,created at]</code> |
### --output, -o
+1 -1
View File
@@ -7,5 +7,5 @@ Delete a token
## Usage
```console
coder tokens remove [id] [flags]
coder tokens remove [name] [flags]
```