Add Bearer Authentication in swagger plugin

We can add swagger by plugin https://wordpress.org/plugins/wp-api-swaggerui/ .

As now a days hacking of api is very popular so reduce it we can use JWT Plugin for it .

After implemented jwt for api no api is available without token .

We can get token by route wp-json/jwt-auth/v1/token.

But in swagger how can we add this token part as in swagger plugin only basic authentication is available .

For JWT both authentication required basic +token .

So For it we can use one filter of swagger plugin swagger_api_security_definitions

/**
 * Append JWT Auth to swagger.
 *
 * @param $auth arr exiting auths
 * @return $auth arr  with jwt
 **/
function append_swagger_auth( $auth ) {
	if ( ! is_array( $auth ) ) {
		$auth = array();
	}
	$auth['bearer_token'] = array(
		'type'        => 'apiKey',
		'name'        => 'Authorization',
		'description' => 'Enter token in format (Bearer + token got by jwt) like  Bearer abcdefghijklmnop',
		'in'          => 'header',
	);
	$auth['basic']        = array(
		'type' => 'basic',
	);
	return $auth;
}
add_filter( 'swagger_api_security_definitions', 'append_swagger_auth' );

Let me know by comment if you found any issue or it is helpful

Leave a Comment

Your email address will not be published. Required fields are marked *