Skip to content

Commit

Permalink
Add a schema for the REST API application passwords endpoint. Fixes #21.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Jul 19, 2021
1 parent 3dc25bc commit ad1e60d
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 7 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"wp core multisite-install --url=example.org --title=Example --admin_user=admin [email protected] --skip-email",
"wp language core install de_DE it_IT ar he_IL",
"wp json-dump routes",
"wp json-dump application-password",
"npm run test-rest-api-application-passwords",
"wp json-dump settings",
"npm run test-rest-api-settings",
"wp json-dump rendered-block",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"build-wp-types": "json2ts -i schema.json -o packages/wp-types/index.ts --style.trailingComma=all --style.useTabs && cat packages/wp-types/append.ts >> packages/wp-types/index.ts",
"build-hyper-schema-types": "json2ts -i tests/hyper-schema/hyper-schema.json -o tests/hyper-schema/index.ts --style.trailingComma=all --style.useTabs",
"test-wp-types": "tsc packages/wp-types/tests/test.ts --noEmit --strict",
"test-rest-api-application-passwords": "ajv validate -m tests/hyper-schema/hyper-schema.json -s schemas/rest-api/application-passwords.json -d \"tests/data/rest-api/application-passwords/*.json\" -r \"schemas/**/*.json\"",
"test-rest-api-settings": "ajv validate -m tests/hyper-schema/hyper-schema.json -s schemas/rest-api/settings.json -d \"tests/data/rest-api/settings/*.json\" -r \"schemas/**/*.json\"",
"test-rest-api-rendered-block": "ajv validate -m tests/hyper-schema/hyper-schema.json -s schemas/rest-api/rendered-block.json -d \"tests/data/rest-api/rendered-block/*.json\" -r \"schemas/**/*.json\"",
"test-rest-api-block-directory-items": "ajv validate -m tests/hyper-schema/hyper-schema.json -s schemas/rest-api/block-directory-items.json -d \"tests/data/rest-api/block-directory-items/*.json\" -r \"schemas/**/*.json\"",
Expand Down
40 changes: 40 additions & 0 deletions packages/wp-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export type WP_REST_API_Users = WP_REST_API_User[];
* A collection of search result objects in a REST API context.
*/
export type WP_REST_API_Search_Results = WP_REST_API_Search_Result[];
/**
* A collection of user application passwords in a REST API context.
*/
export type WP_REST_API_Application_Passwords = WP_REST_API_Application_Password[];

/**
* WordPress is open source software you can use to create a beautiful website, blog, or app.
Expand Down Expand Up @@ -134,6 +138,8 @@ export interface WP {
Taxonomies: WP_REST_API_Taxonomies;
Type: WP_REST_API_Type;
Types: WP_REST_API_Types;
Application_Password: WP_REST_API_Application_Password;
Application_Passwords: WP_REST_API_Application_Passwords;
Error: WP_REST_API_Error;
};
}
Expand Down Expand Up @@ -2809,6 +2815,40 @@ export interface WP_REST_API_Type {
export interface WP_REST_API_Types {
[k: string]: WP_REST_API_Type;
}
/**
* A user application password in a REST API context.
*/
export interface WP_REST_API_Application_Password {
/**
* The unique identifier for the application password.
*/
uuid: string;
/**
* A UUID provided by the application to uniquely identify it. It is recommended to use an UUID v5 with the URL or DNS namespace.
*/
app_id: string;
/**
* The name of the application password.
*/
name: string;
/**
* The generated password. Only available after adding an application.
*/
password?: string;
/**
* The GMT date the application password was created.
*/
created: WP_REST_API_Date_Time;
/**
* The GMT date the application password was last used.
*/
last_used: WP_REST_API_Date_Time | null;
/**
* The IP address the application password was last used by.
*/
last_ip: string | null;
_links?: WP_REST_API_Object_Links;
}
/**
* A REST API error response.
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/wp-types/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ Route | Schema
/wp/v2/types/{type} | `WP_REST_API_Type`
/wp/v2/users | `WP_REST_API_Users`
/wp/v2/users/({id}\|me) | `WP_REST_API_User`
/wp/v2/users/({id}\|me)/application-passwords | Todo
/wp/v2/users/({id}\|me)/application-passwords/{uuid} | Todo
/wp/v2/users/({id}\|me)/application-passwords/introspect| Todo
/wp/v2/users/({id}\|me)/application-passwords | `WP_REST_API_Application_Passwords`
/wp/v2/users/({id}\|me)/application-passwords/{uuid} | `WP_REST_API_Application_Password`
/wp/v2/users/({id}\|me)/application-passwords/introspect| `WP_REST_API_Application_Password`
Any enveloped REST API response | `WP_REST_API_Envelope<T>`
Any REST API error | `WP_REST_API_Error`

Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ Route | Schema
/wp/v2/types/{type} | `WP_REST_API_Type`
/wp/v2/users | `WP_REST_API_Users`
/wp/v2/users/({id}\|me) | `WP_REST_API_User`
/wp/v2/users/({id}\|me)/application-passwords | Todo
/wp/v2/users/({id}\|me)/application-passwords/{uuid} | Todo
/wp/v2/users/({id}\|me)/application-passwords/introspect| Todo
/wp/v2/users/({id}\|me)/application-passwords | `WP_REST_API_Application_Passwords`
/wp/v2/users/({id}\|me)/application-passwords/{uuid} | `WP_REST_API_Application_Password`
/wp/v2/users/({id}\|me)/application-passwords/introspect| `WP_REST_API_Application_Password`
Any REST API error | `WP_REST_API_Error`

The REST API schemas use JSON Hyper-Schema.
Expand Down
8 changes: 8 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@
"Types": {
"$ref": "schemas/rest-api/types.json"
},
"Application_Password": {
"$ref": "schemas/rest-api/application-password.json"
},
"Application_Passwords": {
"$ref": "schemas/rest-api/application-passwords.json"
},
"Error": {
"$ref": "schemas/rest-api/error.json"
}
Expand Down Expand Up @@ -186,6 +192,8 @@
"Taxonomies",
"Type",
"Types",
"Application_Password",
"Application_Passwords",
"Error"
],
"additionalProperties": false
Expand Down
72 changes: 72 additions & 0 deletions schemas/rest-api/application-password.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "https://raw.githubusercontent.com/johnbillion/wp-json-schemas/trunk/schemas/rest-api/application-password.json",
"title": "WP_REST_API_Application_Password",
"description": "A user application password in a REST API context.",
"type": "object",
"additionalProperties": false,
"required": [
"uuid",
"app_id",
"name",
"created",
"last_used",
"last_ip"
],
"properties": {
"uuid": {
"description": "The unique identifier for the application password.",
"type": "string",
"format": "uuid",
"readOnly": true
},
"app_id": {
"description": "A UUID provided by the application to uniquely identify it. It is recommended to use an UUID v5 with the URL or DNS namespace.",
"type": "string"
},
"name": {
"description": "The name of the application password.",
"type": "string",
"minLength": 1,
"pattern": ".*\\S.*"
},
"password": {
"description": "The generated password. Only available after adding an application.",
"type": "string",
"readOnly": true
},
"created": {
"description": "The GMT date the application password was created.",
"anyOf": [
{
"$ref": "properties/date-time.json"
}
],
"readOnly": true
},
"last_used": {
"description": "The GMT date the application password was last used.",
"anyOf": [
{
"$ref": "properties/date-time.json"
},
{
"type": "null"
}
],
"readOnly": true
},
"last_ip": {
"description": "The IP address the application password was last used by.",
"type": [
"string",
"null"
],
"format": "ipv4",
"readOnly": true
},
"_links": {
"$ref": "properties/object-links.json"
}
}
}
10 changes: 10 additions & 0 deletions schemas/rest-api/application-passwords.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/hyper-schema#",
"$id": "https://raw.githubusercontent.com/johnbillion/wp-json-schemas/trunk/schemas/rest-api/application-passwords.json",
"title": "WP_REST_API_Application_Passwords",
"description": "A collection of user application passwords in a REST API context.",
"type": "array",
"items": {
"$ref": "application-password.json"
}
}
20 changes: 20 additions & 0 deletions tests/output/application-password.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace WPJsonSchemas;

$user = wp_get_current_user();
$args = [
'name' => 'Hello',
];
$created = \WP_Application_Passwords::create_new_application_password( $user->ID, wp_slash( $args ) );

$view_data = get_rest_response( 'GET', '/wp/v2/users/me/application-passwords', [
'context' => 'view',
] );
$edit_data = get_rest_response( 'GET', '/wp/v2/users/me/application-passwords', [
'context' => 'edit',
] );
save_rest_array( [
$view_data,
$edit_data,
], 'application-passwords' );
3 changes: 2 additions & 1 deletion tests/wp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
Dotenv\Dotenv::createImmutable( $_env_dir )->load();
}

// Features we don't need during testing:
// Configuration needed during testing:
define( 'WP_DEBUG', true );
define( 'DISABLE_WP_CRON', true );
define( 'WPMU_PLUGIN_DIR', __DIR__ . '/mu-plugins' );
define( 'WP_ENVIRONMENT_TYPE', 'local' );

// WARNING WARNING WARNING!
// These tests will DROP ALL TABLES in the database with the prefix named below.
Expand Down

0 comments on commit ad1e60d

Please sign in to comment.