Skip to content

Commit 0d43d03

Browse files
authored
[communication-common] Add options bag for getToken function, mark @hidden where applicable (Azure#14046)
* [communication-common] Add options bag for getToken function, mark @hidden where applicable * rename url to endpoint in CommunicationIdentityClient constructor
1 parent afb7a26 commit 0d43d03

18 files changed

+142
-98
lines changed

sdk/communication/communication-chat/src/credential/communicationTokenCredentialPolicy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const createCommunicationTokenCredentialPolicy = (
1414
): RequestPolicyFactory => {
1515
return bearerTokenAuthenticationPolicy(
1616
{
17-
getToken: (_scopes, options) => credential.getToken(options?.abortSignal)
17+
getToken: (_scopes, options) => credential.getToken({ abortSignal: options?.abortSignal })
1818
},
1919
[]
2020
);
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { CommunicationSignalingClient, SignalingClient } from "@azure/communication-signaling";
4+
import {
5+
CommunicationSignalingClient,
6+
CommunicationUserCredential,
7+
SignalingClient
8+
} from "@azure/communication-signaling";
59
import { CommunicationTokenCredential } from "@azure/communication-common";
610
import { AzureLogger } from "@azure/logger";
11+
import { AbortSignalLike, AccessToken } from "@azure/core-http";
712

813
export const getSignalingClient = (
914
credential: CommunicationTokenCredential,
1015
logger: AzureLogger
1116
): SignalingClient | undefined => {
12-
return new CommunicationSignalingClient(credential, logger);
17+
return new CommunicationSignalingClient(new SignalingCredentialType(credential), logger);
1318
};
19+
20+
/**
21+
* Bridge credential until @azure/communication-signaling has been updated
22+
* to match common's CommunicationTokenCredential interface.
23+
*/
24+
class SignalingCredentialType implements CommunicationUserCredential {
25+
constructor(private credential: CommunicationTokenCredential) {}
26+
27+
getToken(abortSignal?: AbortSignalLike): Promise<AccessToken> {
28+
return this.credential.getToken({ abortSignal });
29+
}
30+
31+
dispose() {
32+
this.credential.dispose();
33+
}
34+
}

sdk/communication/communication-common/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
## 1.0.0-beta.6 (Unreleased)
44

5-
65
## 1.0.0-beta.5 (2021-02-09)
76

87
### Breaking Changes
@@ -11,6 +10,7 @@
1110
- Removed `id` from `CommunicationUserIdentifier`.
1211
- Renamed `id` to `rawId` in `PhoneNumberIdentifier`.
1312
- Renamed `id` to `rawId` in `MicrosoftTeamsUserIdentifier`.
13+
- Replaced `abortSignal?` argument in `CommunicationTokenCredential.getToken` with `options?: CommunicationGetTokenOptions`.
1414

1515
## 1.0.0-beta.4 (2021-01-25)
1616

sdk/communication/communication-common/review/communication-common.api.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ export class AzureCommunicationTokenCredential implements CommunicationTokenCred
1515
constructor(token: string);
1616
constructor(refreshOptions: CommunicationTokenRefreshOptions);
1717
dispose(): void;
18-
getToken(abortSignal?: AbortSignalLike): Promise<AccessToken>;
18+
getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken>;
1919
}
2020

21+
// @public
22+
export interface CommunicationGetTokenOptions {
23+
abortSignal?: AbortSignalLike;
24+
}
25+
2126
// @public
2227
export type CommunicationIdentifier = CommunicationUserIdentifier | PhoneNumberIdentifier | MicrosoftTeamsUserIdentifier | UnknownIdentifier;
2328

@@ -27,7 +32,7 @@ export type CommunicationIdentifierKind = CommunicationUserKind | PhoneNumberKin
2732
// @public
2833
export interface CommunicationTokenCredential {
2934
dispose(): void;
30-
getToken(abortSignal?: AbortSignalLike): Promise<AccessToken>;
35+
getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken>;
3136
}
3237

3338
// @public
@@ -53,8 +58,8 @@ export const createCommunicationAccessKeyCredentialPolicy: (credential: KeyCrede
5358
// @public
5459
export const createCommunicationAuthPolicy: (credential: KeyCredential | TokenCredential) => RequestPolicyFactory;
5560

56-
// @internal
57-
export const _deserializeCommunicationIdentifier: (serializedIdentifier: _SerializedCommunicationIdentifier) => CommunicationIdentifierKind;
61+
// @public
62+
export const deserializeCommunicationIdentifier: (serializedIdentifier: SerializedCommunicationIdentifier) => CommunicationIdentifierKind;
5863

5964
// @public
6065
export interface EndpointCredential {
@@ -110,34 +115,34 @@ export interface PhoneNumberKind extends PhoneNumberIdentifier {
110115
kind: "phoneNumber";
111116
}
112117

113-
// @internal
114-
export const _serializeCommunicationIdentifier: (identifier: CommunicationIdentifier) => _SerializedCommunicationIdentifier;
118+
// @public
119+
export const serializeCommunicationIdentifier: (identifier: CommunicationIdentifier) => SerializedCommunicationIdentifier;
115120

116-
// @internal
117-
export type _SerializedCommunicationCloudEnvironment = "public" | "dod" | "gcch";
121+
// @public
122+
export type SerializedCommunicationCloudEnvironment = "public" | "dod" | "gcch";
118123

119-
// @internal
120-
export interface _SerializedCommunicationIdentifier {
121-
communicationUser?: _SerializedCommunicationUserIdentifier;
122-
microsoftTeamsUser?: _SerializedMicrosoftTeamsUserIdentifier;
123-
phoneNumber?: _SerializedPhoneNumberIdentifier;
124+
// @public
125+
export interface SerializedCommunicationIdentifier {
126+
communicationUser?: SerializedCommunicationUserIdentifier;
127+
microsoftTeamsUser?: SerializedMicrosoftTeamsUserIdentifier;
128+
phoneNumber?: SerializedPhoneNumberIdentifier;
124129
rawId?: string;
125130
}
126131

127-
// @internal
128-
export interface _SerializedCommunicationUserIdentifier {
132+
// @public
133+
export interface SerializedCommunicationUserIdentifier {
129134
id: string;
130135
}
131136

132-
// @internal
133-
export interface _SerializedMicrosoftTeamsUserIdentifier {
134-
cloud?: _SerializedCommunicationCloudEnvironment;
137+
// @public
138+
export interface SerializedMicrosoftTeamsUserIdentifier {
139+
cloud?: SerializedCommunicationCloudEnvironment;
135140
isAnonymous?: boolean;
136141
userId: string;
137142
}
138143

139-
// @internal
140-
export interface _SerializedPhoneNumberIdentifier {
144+
// @public
145+
export interface SerializedPhoneNumberIdentifier {
141146
value: string;
142147
}
143148

sdk/communication/communication-common/src/autoRefreshTokenCredential.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { AbortSignalLike, AccessToken } from "@azure/core-http";
55
import { parseToken } from "./tokenParser";
6-
import { TokenCredential } from "./communicationTokenCredential";
6+
import { TokenCredential, CommunicationGetTokenOptions } from "./communicationTokenCredential";
77

88
/**
99
* Options for auto-refreshing a Communication Token credential.
@@ -53,12 +53,12 @@ export class AutoRefreshTokenCredential implements TokenCredential {
5353
}
5454
}
5555

56-
public async getToken(abortSignal?: AbortSignalLike): Promise<AccessToken> {
56+
public async getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken> {
5757
if (!this.isCurrentTokenExpiringSoon) {
5858
return this.currentToken;
5959
}
6060

61-
const updatePromise = this.updateTokenAndReschedule(abortSignal);
61+
const updatePromise = this.updateTokenAndReschedule(options?.abortSignal);
6262

6363
if (!this.isCurrentTokenValid) {
6464
await updatePromise;

sdk/communication/communication-common/src/communicationTokenCredential.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@ import {
1111

1212
export type TokenCredential = Pick<AzureCommunicationTokenCredential, "getToken" | "dispose">;
1313

14+
/**
15+
* Options for `CommunicationTokenCredential`'s `getToken` function.
16+
*/
17+
export interface CommunicationGetTokenOptions {
18+
/**
19+
* An implementation of `AbortSignalLike` to cancel the operation.
20+
*/
21+
abortSignal?: AbortSignalLike;
22+
}
23+
1424
/**
1525
* The Azure Communication Services token credential.
1626
*/
1727
export interface CommunicationTokenCredential {
1828
/**
1929
* Gets an `AccessToken` for the user. Throws if already disposed.
20-
* @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.
30+
* @param options - Additional options.
2131
*/
22-
getToken(abortSignal?: AbortSignalLike): Promise<AccessToken>;
32+
getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken>;
2333
/**
2434
* Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.
2535
*/
@@ -56,9 +66,9 @@ export class AzureCommunicationTokenCredential implements CommunicationTokenCred
5666
* Gets an `AccessToken` for the user. Throws if already disposed.
5767
* @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.
5868
*/
59-
public async getToken(abortSignal?: AbortSignalLike): Promise<AccessToken> {
69+
public async getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken> {
6070
this.throwIfDisposed();
61-
const token = await this.tokenCredential.getToken(abortSignal);
71+
const token = await this.tokenCredential.getToken(options);
6272
this.throwIfDisposed();
6373
return token;
6474
}

sdk/communication/communication-common/src/credential/clientArguments.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const isKeyCredential = (credential: unknown): credential is KeyCredentia
4141

4242
/**
4343
* The URL and credential from parsing the arguments of a communication client.
44+
* @hidden
4445
*/
4546
export type UrlWithCredential = {
4647
url: string;
@@ -49,6 +50,7 @@ export type UrlWithCredential = {
4950

5051
/**
5152
* Parses arguments passed to a communication client.
53+
* @hidden
5254
*/
5355
export const parseClientArguments = (
5456
connectionStringOrUrl: string,

sdk/communication/communication-common/src/credential/communicationAccessKeyCredentialPolicy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import {
1616
import { shaHash, shaHMAC } from "./cryptoUtils";
1717

1818
/**
19-
* Creates an HTTP pipeline policy to authenticate a request
20-
* using an `KeyCredential`
19+
* Creates an HTTP pipeline policy to authenticate a request using a `KeyCredential`.
20+
* @hidden
2121
*
22-
* @param credential - The key credential
22+
* @param credential - The key credential.
2323
*/
2424
export const createCommunicationAccessKeyCredentialPolicy = (
2525
credential: KeyCredential

sdk/communication/communication-common/src/credential/communicationAuthPolicy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { bearerTokenAuthenticationPolicy, RequestPolicyFactory } from "@azure/co
66
import { createCommunicationAccessKeyCredentialPolicy } from "./communicationAccessKeyCredentialPolicy";
77
/**
88
* Creates a pipeline policy to authenticate request based
9-
* on the credential passed in
9+
* on the credential passed in.
10+
* @hidden
1011
*
11-
* @param credential - The key credential
12+
* @param credential - The KeyCredential or TokenCredential.
1213
*/
1314
export const createCommunicationAuthPolicy = (
1415
credential: KeyCredential | TokenCredential

sdk/communication/communication-common/src/credential/connectionString.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import { AzureKeyCredential, KeyCredential } from "@azure/core-auth";
55
/**
66
* Represents different properties of connection string
7-
* using format "/endpoint=(.*);accesskey=(.*)"
7+
* using format "/endpoint=(.*);accesskey=(.*)".
8+
* @hidden
89
*/
910
export interface EndpointCredential {
1011
/**
@@ -28,7 +29,8 @@ const tryParseConnectionString = (s: string): EndpointCredential | undefined =>
2829
return undefined;
2930
};
3031
/**
31-
* Returns an EndpointCredential to easily access properties of the connection string
32+
* Returns an EndpointCredential to easily access properties of the connection string.
33+
* @hidden
3234
*
3335
* @param connectionString - The connection string to parse
3436
* @returns Object to access the endpoint and the credenials

0 commit comments

Comments
 (0)