Add config option for custom auth header
This commit is contained in:
parent
09bf982a21
commit
954399009e
23
README.md
23
README.md
|
@ -160,3 +160,26 @@ Options are passed to `explorer(app, options)`.
|
||||||
> Default: Read from package.json
|
> Default: Read from package.json
|
||||||
|
|
||||||
> Sets your API version. If not present, will read from your app's package.json.
|
> Sets your API version. If not present, will read from your app's package.json.
|
||||||
|
|
||||||
|
`auth`: **Object**
|
||||||
|
|
||||||
|
> Optional config for setting api access token, can be used to rename the query parameter or set an auth header.
|
||||||
|
|
||||||
|
> The object has 2 keys:
|
||||||
|
> - `in`: either `header` or `query`
|
||||||
|
> - `name`: the name of the query parameter or header
|
||||||
|
>
|
||||||
|
> The default sets the token as a query parameter with the name `access_token`
|
||||||
|
|
||||||
|
> Example for setting the api key in a header named `x-api-key`:
|
||||||
|
> ```
|
||||||
|
> {
|
||||||
|
> "loopback-component-explorer": {
|
||||||
|
> "mountPath": "/explorer",
|
||||||
|
> "auth": {
|
||||||
|
> "in": "header",
|
||||||
|
> "name": "x-api-key"
|
||||||
|
> }
|
||||||
|
> }
|
||||||
|
> }
|
||||||
|
> ```
|
||||||
|
|
1
index.js
1
index.js
|
@ -79,6 +79,7 @@ function routes(loopbackApplication, options) {
|
||||||
}
|
}
|
||||||
res.send({
|
res.send({
|
||||||
url: urlJoin(source, '/' + options.resourcePath),
|
url: urlJoin(source, '/' + options.resourcePath),
|
||||||
|
auth: options.auth,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ $(function() {
|
||||||
validatorUrl: null,
|
validatorUrl: null,
|
||||||
url: config.url || '/swagger/resources',
|
url: config.url || '/swagger/resources',
|
||||||
apiKey: '',
|
apiKey: '',
|
||||||
|
auth: config.auth,
|
||||||
dom_id: 'swagger-ui-container',
|
dom_id: 'swagger-ui-container',
|
||||||
supportHeaderParams: true,
|
supportHeaderParams: true,
|
||||||
onComplete: function(swaggerApi, swaggerUi) {
|
onComplete: function(swaggerApi, swaggerUi) {
|
||||||
|
@ -76,12 +77,15 @@ $(function() {
|
||||||
function setAccessToken(e) {
|
function setAccessToken(e) {
|
||||||
e.stopPropagation(); // Don't let the default #explore handler fire
|
e.stopPropagation(); // Don't let the default #explore handler fire
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
var authOptions = window.swaggerUi.options.auth || {};
|
||||||
|
var keyLocation = authOptions.in || 'query';
|
||||||
|
var keyName = authOptions.name || 'access_token';
|
||||||
var key = $('#input_accessToken')[0].value;
|
var key = $('#input_accessToken')[0].value;
|
||||||
log('key: ' + key);
|
log('key: ' + key);
|
||||||
if (key && key.trim() !== '') {
|
if (key && key.trim() !== '') {
|
||||||
log('added accessToken ' + key);
|
log('added accessToken ' + key);
|
||||||
var apiKeyAuth =
|
var apiKeyAuth =
|
||||||
new SwaggerClient.ApiKeyAuthorization('access_token', key, 'query');
|
new SwaggerClient.ApiKeyAuthorization(keyName, key, keyLocation);
|
||||||
window.swaggerUi.api.clientAuthorizations.add('key', apiKeyAuth);
|
window.swaggerUi.api.clientAuthorizations.add('key', apiKeyAuth);
|
||||||
accessToken = key;
|
accessToken = key;
|
||||||
$('.accessTokenDisplay').text('Token Set.').addClass('set');
|
$('.accessTokenDisplay').text('Token Set.').addClass('set');
|
||||||
|
|
Loading…
Reference in New Issue