Skip to content

Commit 453eeb9

Browse files
authored
Add Bearer authentication to PHP API client (#2013)
* add php bearer auth support * add partial mustache * add bearer format * update php ze-ph samples
1 parent 6801741 commit 453eeb9

23 files changed

+153
-55
lines changed

modules/openapi-generator/src/main/resources/php/Configuration.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Configuration
4646
protected $apiKeyPrefixes = [];
4747
4848
/**
49-
* Access token for OAuth
49+
* Access token for OAuth/Bearer authentication
5050
*
5151
* @var string
5252
*/

modules/openapi-generator/src/main/resources/php/README.mustache

+21-19
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,9 @@ Please follow the [installation procedure](#installation--usage) and then run th
6666
```php
6767
<?php
6868
require_once(__DIR__ . '/vendor/autoload.php');
69-
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}
70-
// Configure HTTP basic authorization: {{{name}}}
71-
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()
72-
->setUsername('YOUR_USERNAME')
73-
->setPassword('YOUR_PASSWORD');{{/isBasic}}{{#isApiKey}}
74-
// Configure API key authorization: {{{name}}}
75-
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKey('{{{keyParamName}}}', 'YOUR_API_KEY');
76-
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
77-
// $config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKeyPrefix('{{{keyParamName}}}', 'Bearer');{{/isApiKey}}{{#isOAuth}}
78-
// Configure OAuth2 access token for authorization: {{{name}}}
79-
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');{{/isOAuth}}{{/authMethods}}
80-
{{/hasAuthMethods}}
8169

70+
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
71+
{{> php_doc_auth_partial}}
8272
$apiInstance = new {{invokerPackage}}\Api\{{classname}}(
8373
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
8474
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -114,21 +104,33 @@ Class | Method | HTTP request | Description
114104

115105
## Documentation For Authorization
116106

117-
{{^authMethods}} All endpoints do not require authorization.
118-
{{/authMethods}}{{#authMethods}}{{#last}} Authentication schemes defined for the API:{{/last}}{{/authMethods}}
119-
{{#authMethods}}## {{{name}}}
107+
{{^authMethods}}
108+
All endpoints do not require authorization.
109+
{{/authMethods}}
110+
{{#authMethods}}
111+
{{#last}} Authentication schemes defined for the API:{{/last}}
112+
## {{{name}}}
120113

121-
{{#isApiKey}}- **Type**: API key
114+
{{#isApiKey}}
115+
- **Type**: API key
122116
- **API key parameter name**: {{{keyParamName}}}
123117
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}}
124118
{{/isApiKey}}
125-
{{#isBasic}}- **Type**: HTTP basic authentication
119+
{{#isBasic}}
120+
{{^isBasicBearer}}
121+
- **Type**: HTTP basic authentication
122+
{{/isBasicBearer}}
123+
{{#isBasicBearer}}
124+
- **Type**: Bearer authentication{{#bearerFormat}} ({{{.}}}){{/bearerFormat}}
125+
{{/isBasicBearer}}
126126
{{/isBasic}}
127-
{{#isOAuth}}- **Type**: OAuth
127+
{{#isOAuth}}
128+
- **Type**: OAuth
128129
- **Flow**: {{{flow}}}
129130
- **Authorization URL**: {{{authorizationUrl}}}
130131
- **Scopes**: {{^scopes}}N/A{{/scopes}}
131-
{{#scopes}} - **{{{scope}}}**: {{{description}}}
132+
{{#scopes}}
133+
- **{{{scope}}}**: {{{description}}}
132134
{{/scopes}}
133135
{{/isOAuth}}
134136

modules/openapi-generator/src/main/resources/php/api.mustache

+8
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,18 @@ use {{invokerPackage}}\ObjectSerializer;
506506
}
507507
{{/isApiKey}}
508508
{{#isBasic}}
509+
{{^isBasicBearer}}
509510
// this endpoint requires HTTP basic authentication
510511
if ($this->config->getUsername() !== null || $this->config->getPassword() !== null) {
511512
$headers['Authorization'] = 'Basic ' . base64_encode($this->config->getUsername() . ":" . $this->config->getPassword());
512513
}
514+
{{/isBasicBearer}}
515+
{{#isBasicBearer}}
516+
// this endpoint requires Bearer{{#bearerFormat}} ({{{.}}}){{/bearerFormat}} authentication (access token)
517+
if ($this->config->getAccessToken() !== null) {
518+
$headers['Authorization'] = 'Bearer ' . $this->config->getAccessToken();
519+
}
520+
{{/isBasicBearer}}
513521
{{/isBasic}}
514522
{{#isOAuth}}
515523
// this endpoint requires OAuth (access token)

modules/openapi-generator/src/main/resources/php/api_doc.mustache

+1-21
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,7 @@ Method | HTTP request | Description
2222
<?php
2323
require_once(__DIR__ . '/vendor/autoload.php');
2424

25-
{{#hasAuthMethods}}
26-
{{#authMethods}}
27-
{{#isBasic}}
28-
// Configure HTTP basic authorization: {{{name}}}
29-
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()
30-
->setUsername('YOUR_USERNAME')
31-
->setPassword('YOUR_PASSWORD');
32-
{{/isBasic}}
33-
{{#isApiKey}}
34-
// Configure API key authorization: {{{name}}}
35-
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKey('{{{keyParamName}}}', 'YOUR_API_KEY');
36-
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
37-
// $config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKeyPrefix('{{{keyParamName}}}', 'Bearer');
38-
{{/isApiKey}}
39-
{{#isOAuth}}
40-
// Configure OAuth2 access token for authorization: {{{name}}}
41-
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
42-
{{/isOAuth}}
43-
{{/authMethods}}
44-
45-
{{/hasAuthMethods}}
25+
{{> php_doc_auth_partial}}
4626
$apiInstance = new {{invokerPackage}}\Api\{{classname}}(
4727
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
4828
// This is optional, `GuzzleHttp\Client` will be used as default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{{#hasAuthMethods}}
2+
{{#authMethods}}
3+
{{#isBasic}}
4+
{{^isBasicBearer}}
5+
// Configure HTTP basic authorization: {{{name}}}
6+
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()
7+
->setUsername('YOUR_USERNAME')
8+
->setPassword('YOUR_PASSWORD');
9+
{{/isBasicBearer}}
10+
{{#isBasicBearer}}
11+
// Configure Bearer{{#bearerFormat}} ({{{.}}}){{/bearerFormat}} authorization: {{{name}}}
12+
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
13+
{{/isBasicBearer}}
14+
{{/isBasic}}
15+
{{#isApiKey}}
16+
// Configure API key authorization: {{{name}}}
17+
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKey('{{{keyParamName}}}', 'YOUR_API_KEY');
18+
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
19+
// $config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setApiKeyPrefix('{{{keyParamName}}}', 'Bearer');
20+
{{/isApiKey}}
21+
{{#isOAuth}}
22+
// Configure OAuth2 access token for authorization: {{{name}}}
23+
$config = {{{invokerPackage}}}\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
24+
{{/isOAuth}}
25+
26+
{{/authMethods}}
27+
{{/hasAuthMethods}}

samples/client/petstore/php/OpenAPIClient-php/README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Please follow the [installation procedure](#installation--usage) and then run th
5656
<?php
5757
require_once(__DIR__ . '/vendor/autoload.php');
5858

59+
60+
5961
$apiInstance = new OpenAPI\Client\Api\AnotherFakeApi(
6062
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
6163
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -167,24 +169,27 @@ Class | Method | HTTP request | Description
167169
- **API key parameter name**: api_key
168170
- **Location**: HTTP header
169171

172+
170173
## api_key_query
171174

172175
- **Type**: API key
173176
- **API key parameter name**: api_key_query
174177
- **Location**: URL query string
175178

179+
176180
## http_basic_test
177181

178182
- **Type**: HTTP basic authentication
179183

184+
180185
## petstore_auth
181186

182187
- **Type**: OAuth
183188
- **Flow**: implicit
184189
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
185190
- **Scopes**:
186-
- **write:pets**: modify pets in your account
187-
- **read:pets**: read your pets
191+
- **write:pets**: modify pets in your account
192+
- **read:pets**: read your pets
188193

189194

190195
## Author

samples/client/petstore/php/OpenAPIClient-php/docs/Api/AnotherFakeApi.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ To test special tags and operation ID starting with number
1919
<?php
2020
require_once(__DIR__ . '/vendor/autoload.php');
2121

22+
2223
$apiInstance = new OpenAPI\Client\Api\AnotherFakeApi(
2324
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
2425
// This is optional, `GuzzleHttp\Client` will be used as default.

samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeApi.md

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ this route creates an XmlItem
3131
<?php
3232
require_once(__DIR__ . '/vendor/autoload.php');
3333

34+
3435
$apiInstance = new OpenAPI\Client\Api\FakeApi(
3536
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
3637
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -79,6 +80,7 @@ Test serialization of outer boolean types
7980
<?php
8081
require_once(__DIR__ . '/vendor/autoload.php');
8182

83+
8284
$apiInstance = new OpenAPI\Client\Api\FakeApi(
8385
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
8486
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -128,6 +130,7 @@ Test serialization of object with outer number type
128130
<?php
129131
require_once(__DIR__ . '/vendor/autoload.php');
130132

133+
131134
$apiInstance = new OpenAPI\Client\Api\FakeApi(
132135
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
133136
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -177,6 +180,7 @@ Test serialization of outer number types
177180
<?php
178181
require_once(__DIR__ . '/vendor/autoload.php');
179182

183+
180184
$apiInstance = new OpenAPI\Client\Api\FakeApi(
181185
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
182186
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -226,6 +230,7 @@ Test serialization of outer string types
226230
<?php
227231
require_once(__DIR__ . '/vendor/autoload.php');
228232

233+
229234
$apiInstance = new OpenAPI\Client\Api\FakeApi(
230235
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
231236
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -275,6 +280,7 @@ For this test, the body for this request much reference a schema named `File`.
275280
<?php
276281
require_once(__DIR__ . '/vendor/autoload.php');
277282

283+
278284
$apiInstance = new OpenAPI\Client\Api\FakeApi(
279285
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
280286
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -321,6 +327,7 @@ No authorization required
321327
<?php
322328
require_once(__DIR__ . '/vendor/autoload.php');
323329

330+
324331
$apiInstance = new OpenAPI\Client\Api\FakeApi(
325332
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
326333
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -371,6 +378,7 @@ To test \"client\" model
371378
<?php
372379
require_once(__DIR__ . '/vendor/autoload.php');
373380

381+
374382
$apiInstance = new OpenAPI\Client\Api\FakeApi(
375383
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
376384
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -425,6 +433,7 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()
425433
->setUsername('YOUR_USERNAME')
426434
->setPassword('YOUR_PASSWORD');
427435

436+
428437
$apiInstance = new OpenAPI\Client\Api\FakeApi(
429438
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
430439
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -500,6 +509,7 @@ To test enum parameters
500509
<?php
501510
require_once(__DIR__ . '/vendor/autoload.php');
502511

512+
503513
$apiInstance = new OpenAPI\Client\Api\FakeApi(
504514
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
505515
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -562,6 +572,7 @@ Fake endpoint to test group parameters (optional)
562572
<?php
563573
require_once(__DIR__ . '/vendor/autoload.php');
564574

575+
565576
$apiInstance = new OpenAPI\Client\Api\FakeApi(
566577
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
567578
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -620,6 +631,7 @@ test inline additionalProperties
620631
<?php
621632
require_once(__DIR__ . '/vendor/autoload.php');
622633

634+
623635
$apiInstance = new OpenAPI\Client\Api\FakeApi(
624636
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
625637
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -666,6 +678,7 @@ test json serialization of form data
666678
<?php
667679
require_once(__DIR__ . '/vendor/autoload.php');
668680

681+
669682
$apiInstance = new OpenAPI\Client\Api\FakeApi(
670683
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
671684
// This is optional, `GuzzleHttp\Client` will be used as default.

samples/client/petstore/php/OpenAPIClient-php/docs/Api/FakeClassnameTags123Api.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('ap
2424
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
2525
// $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('api_key_query', 'Bearer');
2626

27+
2728
$apiInstance = new OpenAPI\Client\Api\FakeClassnameTags123Api(
2829
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
2930
// This is optional, `GuzzleHttp\Client` will be used as default.

samples/client/petstore/php/OpenAPIClient-php/docs/Api/PetApi.md

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
2828
// Configure OAuth2 access token for authorization: petstore_auth
2929
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
3030

31+
3132
$apiInstance = new OpenAPI\Client\Api\PetApi(
3233
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
3334
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -78,6 +79,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
7879
// Configure OAuth2 access token for authorization: petstore_auth
7980
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
8081

82+
8183
$apiInstance = new OpenAPI\Client\Api\PetApi(
8284
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
8385
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -132,6 +134,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
132134
// Configure OAuth2 access token for authorization: petstore_auth
133135
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
134136

137+
135138
$apiInstance = new OpenAPI\Client\Api\PetApi(
136139
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
137140
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -185,6 +188,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
185188
// Configure OAuth2 access token for authorization: petstore_auth
186189
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
187190

191+
188192
$apiInstance = new OpenAPI\Client\Api\PetApi(
189193
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
190194
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -240,6 +244,7 @@ $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('ap
240244
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
241245
// $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('api_key', 'Bearer');
242246

247+
243248
$apiInstance = new OpenAPI\Client\Api\PetApi(
244249
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
245250
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -291,6 +296,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
291296
// Configure OAuth2 access token for authorization: petstore_auth
292297
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
293298

299+
294300
$apiInstance = new OpenAPI\Client\Api\PetApi(
295301
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
296302
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -341,6 +347,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
341347
// Configure OAuth2 access token for authorization: petstore_auth
342348
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
343349

350+
344351
$apiInstance = new OpenAPI\Client\Api\PetApi(
345352
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
346353
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -395,6 +402,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
395402
// Configure OAuth2 access token for authorization: petstore_auth
396403
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
397404

405+
398406
$apiInstance = new OpenAPI\Client\Api\PetApi(
399407
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
400408
// This is optional, `GuzzleHttp\Client` will be used as default.
@@ -450,6 +458,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
450458
// Configure OAuth2 access token for authorization: petstore_auth
451459
$config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
452460

461+
453462
$apiInstance = new OpenAPI\Client\Api\PetApi(
454463
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
455464
// This is optional, `GuzzleHttp\Client` will be used as default.

0 commit comments

Comments
 (0)