Skip to content

Commit

Permalink
[Communication] - Identity - Auth with Token Credential (Azure#12906)
Browse files Browse the repository at this point in the history
* [communicationIdentityClient] adding new constructor using token credential

* [communicationIdentityClient] new implementation to take pass in token credential to constructor

* Revert changes to this file

* [communicationIdentityClient] adding linted files

* [communicationIdentityClient] remove unused import

* [communicationIdentityClient] fixing the comments as per PR feedback

* [communicationIdentityClient] import for bearerTokenAuthenticationPolicy

* [Token] adding in live tests for constructor with token credential

* [karma] added the environment vars to karma for browser

* [recordedClient] adding in a mock token credential for playback mode

* [Recordings] removing the port from endpoint url

* [recordedClient] using ClientSecretCredential instead of DefaultAzureCredential

* [communicationIdentityClientWithToken] for browser we stick to the old way of authentication for the time being

* [createRecordedCommunicationIdentityClient] formatting fixes

* [README] update the read me samples and the changelog

* [communicationIdentityClientWithToken] updating test to skip if the token is not created properly

* [communicationAuthPolicy] Created new auth policy to so client has cleaner implementation

* [recordedClient] remove the sinon lib

* Update sdk/communication/communication-administration/src/communicationIdentity/communicationIdentityClient.ts

Co-authored-by: Dominik <domessin@microsoft.com>
  • Loading branch information
JoshuaLai and DominikMe committed Jan 13, 2021
1 parent d0789f5 commit 7cd87e9
Show file tree
Hide file tree
Showing 18 changed files with 408 additions and 16 deletions.
4 changes: 4 additions & 0 deletions sdk/communication/communication-administration/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- Fixed a bug where poller options were ignored for `beginReleasePhoneNumbers`, `beginReservePhoneNumbers` and `beginPurchaseReservation`.
- Fixed paging for `listPhoneNumbers`, `listPhonePlanGroups`, `listPhonePlans`, `listReleases`, `listSearches`, `listSupportedCountries`.

### Added

- `CommunicationIdentityClient` added a constructor that supports `TokenCredential`.

## 1.0.0-beta.3 (2020-11-16)

### Added
Expand Down
9 changes: 9 additions & 0 deletions sdk/communication/communication-administration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ const connectionString = `endpoint=HOST;accessKey=KEY`;
const client = new CommunicationIdentityClient(connectionString);
```

### Using a `TokenCredential`

```typescript
import { CommunicationIdentityClient } from "@azure/communication-administration";

let credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(HOST, credential);
```

If you use a key to initialize the client you will also need to provide the appropriate endpoint. You can get this endpoint from your Communication Services resource in [Azure Portal][azure_portal].

## Usage
Expand Down
10 changes: 9 additions & 1 deletion sdk/communication/communication-administration/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ module.exports = function(config) {
// inject following environment values into browser testing with window.__env__
// environment values MUST be exported or set with same console running "karma start"
// https://www.npmjs.com/package/karma-env-preprocessor
envPreprocessor: ["TEST_MODE", "COMMUNICATION_CONNECTION_STRING", "INCLUDE_PHONENUMBER_TESTS"],
envPreprocessor: [
"TEST_MODE",
"COMMUNICATION_CONNECTION_STRING",
"INCLUDE_PHONENUMBER_TESTS",
"COMMUNICATION_ENDPOINT",
"AZURE_CLIENT_ID",
"AZURE_CLIENT_SECRET",
"AZURE_TENANT_ID"
],

// test results reporter to use
// possible values: 'dots', 'progress'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/test-utils-recorder": "^1.0.0",
"@azure/identity": "^1.1.0",
"@microsoft/api-documenter": "~7.8.17",
"@microsoft/api-extractor": "7.7.11",
"@rollup/plugin-commonjs": "11.0.2",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PagedAsyncIterableIterator } from '@azure/core-paging';
import { PipelineOptions } from '@azure/core-http';
import { PollerLike } from '@azure/core-lro';
import { PollOperationState } from '@azure/core-lro';
import { TokenCredential } from '@azure/core-auth';

// @public
export interface AcquiredPhoneNumber {
Expand Down Expand Up @@ -78,6 +79,7 @@ export interface CarrierDetails {
export class CommunicationIdentityClient {
constructor(connectionString: string, options?: CommunicationIdentityOptions);
constructor(url: string, credential: KeyCredential, options?: CommunicationIdentityOptions);
constructor(url: string, credential: TokenCredential, options?: CommunicationIdentityOptions);
createUser(options?: OperationOptions): Promise<CreateUserResponse>;
deleteUser(user: CommunicationUserIdentifier, options?: OperationOptions): Promise<VoidResponse>;
issueToken(user: CommunicationUserIdentifier, scopes: TokenScope[], options?: OperationOptions): Promise<IssueTokenResponse>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Licensed under the MIT license.

import {
createCommunicationAccessKeyCredentialPolicy,
createCommunicationAuthPolicy,
parseClientArguments,
isKeyCredential,
CommunicationUserIdentifier
} from "@azure/communication-common";
import { KeyCredential } from "@azure/core-auth";
import { isTokenCredential, KeyCredential, TokenCredential } from "@azure/core-auth";
import {
InternalPipelineOptions,
createPipelineFromOptions,
Expand All @@ -30,7 +30,7 @@ import { VoidResponse } from "../common/models";
import { attachHttpResponse } from "../common/mappers";

const isCommunicationIdentityOptions = (options: any): options is CommunicationIdentityOptions =>
options && !isKeyCredential(options);
options && !isTokenCredential(options) && !isKeyCredential(options);

/**
* Client class for interacting with Azure Communication Services User Token Management.
Expand Down Expand Up @@ -60,7 +60,17 @@ export class CommunicationIdentityClient {
credential: KeyCredential,
options?: CommunicationIdentityOptions
);

/**
* Initializes a new instance of the CommunicationIdentity class using a TokenCredential.
* @param url The endpoint of the service (ex: https://contoso.eastus.communications.azure.net)
* @param credential TokenCredential that is used to authenticate requests to the service.
* @param options Optional. Options to configure the HTTP pipeline.
*/
public constructor(
url: string,
credential: TokenCredential,
options?: CommunicationIdentityOptions
);
/**
* Creates an instance of CommunicationIdentity.
*
Expand All @@ -70,7 +80,7 @@ export class CommunicationIdentityClient {
*/
public constructor(
connectionStringOrUrl: string,
credentialOrOptions?: KeyCredential | CommunicationIdentityOptions,
credentialOrOptions?: KeyCredential | CommunicationIdentityOptions | TokenCredential,
maybeOptions: CommunicationIdentityOptions = {}
) {
const { url, credential } = parseClientArguments(connectionStringOrUrl, credentialOrOptions);
Expand Down Expand Up @@ -98,9 +108,8 @@ export class CommunicationIdentityClient {
}
};

const authPolicy = createCommunicationAccessKeyCredentialPolicy(credential);
const authPolicy = createCommunicationAuthPolicy(credential);
const pipeline = createPipelineFromOptions(internalPipelineOptions, authPolicy);

this.client = new IdentityRestClient(url, pipeline).communicationIdentity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class PhoneNumberAdministrationClient {
}
};

const authPolicy = createCommunicationAccessKeyCredentialPolicy(credential);
const authPolicy = createCommunicationAccessKeyCredentialPolicy(credential as KeyCredential);
const pipeline = createPipelineFromOptions(internalPipelineOptions, authPolicy);
this.client = new PhoneNumberRestClient(url, pipeline).phoneNumberAdministration;
}
Expand Down
Loading

0 comments on commit 7cd87e9

Please sign in to comment.