зеркало из https://github.com/Azure/ms-rest-js.git
Port Azure/azure-sdk-for-node#2311: added support for apiKeyCredentials and CognitiveServicesCredentials in ms-rest and ms-rest-azure
This commit is contained in:
Родитель
73500b00bb
Коммит
24cd5fe2c3
|
@ -0,0 +1,78 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
|
||||
import { WebResource } from "../webResource";
|
||||
import { ServiceClientCredentials } from "./serviceClientCredentials";
|
||||
|
||||
/**
|
||||
* @interface ApiKeyCredentialOptions
|
||||
* Describes the options to be provided while creating an instance of ApiKeyCredentials
|
||||
*/
|
||||
export interface ApiKeyCredentialOptions {
|
||||
/**
|
||||
* @property {object} [inHeader] A key value pair of the header parameters that need to be applied to the request.
|
||||
*/
|
||||
inHeader?: { [x: string]: any };
|
||||
/**
|
||||
* @property {object} [inQuery] A key value pair of the query parameters that need to be applied to the request.
|
||||
*/
|
||||
inQuery?: { [x: string]: any };
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticates to a service using an API key.
|
||||
*/
|
||||
export class ApiKeyCredentials implements ServiceClientCredentials {
|
||||
private readonly inHeader?: { [x: string]: any };
|
||||
private readonly inQuery?: { [x: string]: any };
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {object} options Specifies the options to be provided for auth. Either header or query needs to be provided.
|
||||
* @param {object} [inHeader] A key value pair of the header parameters that need to be applied to the request.
|
||||
* @param {object} [inQuery] A key value pair of the query parameters that need to be applied to the request.
|
||||
*/
|
||||
constructor(options: ApiKeyCredentialOptions) {
|
||||
if (!options || (options && !options.inHeader && !options.inQuery)) {
|
||||
throw new Error(`options cannot be null or undefined. Either "inHeader" or "inQuery" property of the options object needs to be provided.`);
|
||||
}
|
||||
this.inHeader = options.inHeader;
|
||||
this.inQuery = options.inQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a request with the values provided in the inHeader and inQuery parameter.
|
||||
*
|
||||
* @param {WebResource} The WebResource to be signed.
|
||||
* @returns {Promise<WebResource>} - The signed request object.
|
||||
*/
|
||||
signRequest(webResource: WebResource) {
|
||||
if (!webResource) {
|
||||
return Promise.reject(new Error(`webResource cannot be null or undefined and must be of type "object".`));
|
||||
}
|
||||
|
||||
if (this.inHeader) {
|
||||
if (!webResource.headers) {
|
||||
webResource.headers = {};
|
||||
}
|
||||
Object.assign(webResource.headers, this.inHeader);
|
||||
}
|
||||
|
||||
if (this.inQuery) {
|
||||
if (!webResource.url) {
|
||||
return Promise.reject(new Error(`url cannot be null in the request object.`));
|
||||
}
|
||||
if (webResource.url.indexOf("?") < 0) {
|
||||
webResource.url += "?";
|
||||
}
|
||||
for (const key in this.inQuery) {
|
||||
if (!webResource.url.endsWith("?")) {
|
||||
webResource.url += "&";
|
||||
}
|
||||
webResource.url += `${key}=${this.inQuery[key]}`;
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.resolve(webResource);
|
||||
}
|
||||
}
|
|
@ -29,6 +29,7 @@ import {
|
|||
// Credentials
|
||||
import { TokenCredentials } from "./credentials/tokenCredentials";
|
||||
import { BasicAuthenticationCredentials } from "./credentials/basicAuthenticationCredentials";
|
||||
import { ApiKeyCredentials, ApiKeyCredentialOptions } from "./credentials/apiKeyCredentials";
|
||||
import { ServiceClientCredentials } from "./credentials/serviceClientCredentials";
|
||||
import * as isStream from "is-stream";
|
||||
|
||||
|
@ -36,7 +37,7 @@ export {
|
|||
BaseMapperType, CompositeMapper, DictionaryMapper, EnumMapper, Mapper, MapperConstraints, MapperType,
|
||||
PolymorphicDiscriminator, SequenceMapper, UrlParameterValue, Serializer, serializeObject, TokenCredentials,
|
||||
WebResource, RequestPrepareOptions, HttpMethods, ParameterValue, HttpOperationResponse, ServiceClient, Constants, RequestPipeline,
|
||||
BasicAuthenticationCredentials, ServiceClientCredentials, BaseFilter, LogFilter, ServiceClientOptions, ExponentialRetryPolicyFilter,
|
||||
BasicAuthenticationCredentials, ApiKeyCredentials, ApiKeyCredentialOptions, ServiceClientCredentials, BaseFilter, LogFilter, ServiceClientOptions, ExponentialRetryPolicyFilter,
|
||||
SystemErrorRetryPolicyFilter, SigningFilter, MsRestUserAgentFilter, stripRequest, stripResponse, delay, executePromisesSequentially,
|
||||
generateUuid, isValidUuid, encodeUri, RestError, RequestOptionsBase, RequestFunction, ServiceCallback, promiseToCallback,
|
||||
promiseToServiceCallback, isStream, dispatchRequest, RedirectFilter, applyMixins, isNode
|
||||
|
|
|
@ -5,6 +5,7 @@ import * as msRest from "../lib/msRest";
|
|||
const should = require("should");
|
||||
const TokenCredentials = msRest.TokenCredentials;
|
||||
const BasicAuthenticationCredentials = msRest.BasicAuthenticationCredentials;
|
||||
const ApiKeyCredentials = msRest.ApiKeyCredentials;
|
||||
const dummyToken = "A-dummy-access-token";
|
||||
const fakeScheme = "fake-auth-scheme";
|
||||
const dummyuserName = "dummy@mummy.com";
|
||||
|
@ -106,4 +107,68 @@ describe("Basic Authentication credentials", () => {
|
|||
// }).should.throw();
|
||||
// });
|
||||
});
|
||||
|
||||
describe('ApiKey credentials', () => {
|
||||
describe('usage', function () {
|
||||
it('should set header parameters properly in request', async function () {
|
||||
var creds = new ApiKeyCredentials({inHeader: {'key1': 'value1', 'key2': 'value2'}});
|
||||
var request = {
|
||||
headers: {}
|
||||
} as msRest.WebResource;
|
||||
|
||||
await creds.signRequest(request);
|
||||
|
||||
request.headers.should.have.property('key1');
|
||||
request.headers.should.have.property('key2');
|
||||
request.headers['key1'].should.match(new RegExp('^value1$'));
|
||||
request.headers['key2'].should.match(new RegExp('^value2$'));
|
||||
});
|
||||
|
||||
it('should set query parameters properly in the request url without any query parameters', async function () {
|
||||
var creds = new ApiKeyCredentials({inQuery: {'key1': 'value1', 'key2': 'value2'}});
|
||||
var request = {
|
||||
headers: {},
|
||||
url: 'https://example.com'
|
||||
} as msRest.WebResource;
|
||||
|
||||
await creds.signRequest(request);
|
||||
request.url.should.equal('https://example.com?key1=value1&key2=value2');
|
||||
});
|
||||
|
||||
it('should set query parameters properly in the request url with existing query parameters', async function () {
|
||||
var creds = new ApiKeyCredentials({inQuery: {'key1': 'value1', 'key2': 'value2'}});
|
||||
var request = {
|
||||
headers: {},
|
||||
url: 'https://example.com?q1=v2'
|
||||
} as msRest.WebResource;
|
||||
|
||||
await creds.signRequest(request);
|
||||
request.url.should.equal('https://example.com?q1=v2&key1=value1&key2=value2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('construction', function () {
|
||||
|
||||
it('should fail with options.inHeader and options.inQuery set to null or undefined', function (done) {
|
||||
(function () {
|
||||
new ApiKeyCredentials({ inHeader: null, inQuery: undefined } as any);
|
||||
}).should.throw();
|
||||
done();
|
||||
});
|
||||
|
||||
it('should fail without options', function (done) {
|
||||
(function () {
|
||||
new (ApiKeyCredentials as any)();
|
||||
}).should.throw();
|
||||
done();
|
||||
});
|
||||
|
||||
it('should fail with empty options', function (done) {
|
||||
(function () {
|
||||
new ApiKeyCredentials({});
|
||||
}).should.throw();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче