Skip to content

Commit b8a7759

Browse files
authored
[Identity] Success logging (Both in Interactive and in DeviceCode) (Azure#11807)
This is a very small issue addressing Azure#11672. When I made Azure#11672 I conflated two different issues related to success logging of Identity credentials (at least definitely in my mind I did conflate these two). Since the fix to these issues is very small, in number of lines changed, I thought it would be ok to make a single pull request to address them both. In this pull request, I have: - Changed the success message of InteractiveBrowserCredential to `Authentication Complete. You can close the browser and return to the application.`. - Changed the success logging message of `DeviceCodeCredential` to be similar to: `DeviceCodeCredential succeeded. Scopes: {1} ExpiresOn: {3}`. - When I did this, I decided to change the other logging messages to make them consistent. **Important:** I decided to re-use the existing logging tools, so the messages are more like `ClientCertificateCredential => getToke() => SUCCESS. Scopes: {1}` If approved, Fixes Azure#11672 Feedback appreciated!
1 parent 1e8ad24 commit b8a7759

24 files changed

+84
-72
lines changed

sdk/identity/identity/src/credentials/authorizationCodeCredential.browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export class AuthorizationCodeCredential implements TokenCredential {
2727
options?: TokenCredentialOptions
2828
);
2929
constructor() {
30-
logger.info(formatError(BrowserNotSupportedError));
30+
logger.info(formatError("", BrowserNotSupportedError));
3131
throw BrowserNotSupportedError;
3232
}
3333

3434
public getToken(): Promise<AccessToken | null> {
35-
logger.getToken.info(formatError(BrowserNotSupportedError));
35+
logger.getToken.info(formatError("", BrowserNotSupportedError));
3636
throw BrowserNotSupportedError;
3737
}
3838
}

sdk/identity/identity/src/credentials/authorizationCodeCredential.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export class AuthorizationCodeCredential implements TokenCredential {
197197
code,
198198
message: err.message
199199
});
200-
logger.getToken.info(formatError(err));
200+
logger.getToken.info(formatError(scopes, err));
201201
throw err;
202202
} finally {
203203
span.end();

sdk/identity/identity/src/credentials/azureCliCredential.browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ const logger = credentialLogger("AzureCliCredential");
99

1010
export class AzureCliCredential implements TokenCredential {
1111
constructor() {
12-
logger.info(formatError(BrowserNotSupportedError));
12+
logger.info(formatError("", BrowserNotSupportedError));
1313
throw BrowserNotSupportedError;
1414
}
1515

1616
getToken(): Promise<AccessToken | null> {
17-
logger.getToken.info(formatError(BrowserNotSupportedError));
17+
logger.getToken.info(formatError("", BrowserNotSupportedError));
1818
throw BrowserNotSupportedError;
1919
}
2020
}

sdk/identity/identity/src/credentials/azureCliCredential.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class AzureCliCredential implements TokenCredential {
7575
// Check to make sure the scope we get back is a valid scope
7676
if (!scope.match(/^[0-9a-zA-Z-.:/]+$/)) {
7777
const error = new Error("Invalid scope was specified by the user or calling client");
78-
logger.getToken.info(formatError(error));
78+
logger.getToken.info(formatError(scopes, error));
7979
throw error;
8080
}
8181

@@ -93,17 +93,17 @@ export class AzureCliCredential implements TokenCredential {
9393
const error = new CredentialUnavailable(
9494
"Azure CLI could not be found. Please visit https://aka.ms/azure-cli for installation instructions and then, once installed, authenticate to your Azure account using 'az login'."
9595
);
96-
logger.getToken.info(formatError(error));
96+
logger.getToken.info(formatError(scopes, error));
9797
throw error;
9898
} else if (isLoginError) {
9999
const error = new CredentialUnavailable(
100100
"Please run 'az login' from a command prompt to authenticate before using this credential."
101101
);
102-
logger.getToken.info(formatError(error));
102+
logger.getToken.info(formatError(scopes, error));
103103
throw error;
104104
}
105105
const error = new CredentialUnavailable(obj.stderr);
106-
logger.getToken.info(formatError(error));
106+
logger.getToken.info(formatError(scopes, error));
107107
throw error;
108108
} else {
109109
responseData = obj.stdout;
@@ -126,7 +126,7 @@ export class AzureCliCredential implements TokenCredential {
126126
code,
127127
message: err.message
128128
});
129-
logger.getToken.info(formatError(err));
129+
logger.getToken.info(formatError(scopes, err));
130130
reject(err);
131131
});
132132
});

sdk/identity/identity/src/credentials/chainedTokenCredential.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class ChainedTokenCredential implements TokenCredential {
6767
if (err instanceof CredentialUnavailable) {
6868
errors.push(err);
6969
} else {
70-
logger.getToken.info(formatError(err));
70+
logger.getToken.info(formatError(scopes, err));
7171
throw err;
7272
}
7373
}
@@ -79,7 +79,7 @@ export class ChainedTokenCredential implements TokenCredential {
7979
code: CanonicalCode.UNAUTHENTICATED,
8080
message: err.message
8181
});
82-
logger.getToken.info(formatError(err));
82+
logger.getToken.info(formatError(scopes, err));
8383
throw err;
8484
}
8585

sdk/identity/identity/src/credentials/clientCertificateCredential.browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ const logger = credentialLogger("ClientCertificateCredential");
1111

1212
export class ClientCertificateCredential implements TokenCredential {
1313
constructor() {
14-
logger.info(formatError(BrowserNotSupportedError));
14+
logger.info(formatError("", BrowserNotSupportedError));
1515
throw BrowserNotSupportedError;
1616
}
1717

1818
public getToken(): Promise<AccessToken | null> {
19-
logger.getToken.info(formatError(BrowserNotSupportedError));
19+
logger.getToken.info(formatError("", BrowserNotSupportedError));
2020
throw BrowserNotSupportedError;
2121
}
2222
}

sdk/identity/identity/src/credentials/clientCertificateCredential.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class ClientCertificateCredential implements TokenCredential {
8585
const error = new Error(
8686
"The file at the specified path does not contain a PEM-encoded certificate."
8787
);
88-
logger.info(formatError(error));
88+
logger.info(formatError("", error));
8989
throw error;
9090
}
9191

@@ -187,7 +187,7 @@ export class ClientCertificateCredential implements TokenCredential {
187187
code,
188188
message: err.message
189189
});
190-
logger.getToken.info(formatError(err));
190+
logger.getToken.info(formatError("", err));
191191
throw err;
192192
} finally {
193193
span.end();

sdk/identity/identity/src/credentials/clientSecretCredential.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { TokenCredentialOptions, IdentityClient } from "../client/identityClient
77
import { createSpan } from "../util/tracing";
88
import { AuthenticationErrorName } from "../client/errors";
99
import { CanonicalCode } from "@opentelemetry/api";
10-
import { credentialLogger, formatSuccess } from "../util/logging";
10+
import { credentialLogger, formatError, formatSuccess } from "../util/logging";
1111
import { getIdentityTokenEndpointSuffix } from "../util/identityTokenEndpoint";
1212

1313
const logger = credentialLogger("ClientSecretCredential");
@@ -97,7 +97,7 @@ export class ClientSecretCredential implements TokenCredential {
9797
code,
9898
message: err.message
9999
});
100-
logger.getToken.info(err);
100+
logger.getToken.info(formatError(scopes, err));
101101
throw err;
102102
} finally {
103103
span.end();

sdk/identity/identity/src/credentials/deviceCodeCredential.browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ const logger = credentialLogger("DeviceCodeCredential");
99

1010
export class DeviceCodeCredential implements TokenCredential {
1111
constructor() {
12-
logger.info(formatError(BrowserNotSupportedError));
12+
logger.info(formatError("", BrowserNotSupportedError));
1313
throw BrowserNotSupportedError;
1414
}
1515

1616
public getToken(): Promise<AccessToken | null> {
17-
logger.getToken.info(formatError(BrowserNotSupportedError));
17+
logger.getToken.info(formatError("", BrowserNotSupportedError));
1818
throw BrowserNotSupportedError;
1919
}
2020
}

sdk/identity/identity/src/credentials/deviceCodeCredential.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ export class DeviceCodeCredential implements TokenCredential {
113113
* @param options The options used to configure any requests this
114114
* TokenCredential implementation might make.
115115
*/
116-
getToken(scopes: string | string[], options?: GetTokenOptions): Promise<AccessToken | null> {
116+
async getToken(
117+
scopes: string | string[],
118+
options?: GetTokenOptions
119+
): Promise<AccessToken | null> {
117120
const { span } = createSpan("DeviceCodeCredential-getToken", options);
118121

119122
const scopeArray = typeof scopes === "object" ? scopes : [scopes];
@@ -123,12 +126,14 @@ export class DeviceCodeCredential implements TokenCredential {
123126
scopes: scopeArray
124127
};
125128

126-
logger.info("Sending devicecode request");
129+
logger.info(`DeviceCodeCredential invoked. Scopes: ${scopeArray.join(", ")}`);
127130

128-
return this.msalClient.acquireTokenFromCache(scopeArray).catch((e) => {
131+
return this.msalClient.acquireTokenFromCache(scopeArray).catch(async (e) => {
129132
if (e instanceof AuthenticationRequired) {
130133
try {
131-
return this.acquireTokenByDeviceCode(deviceCodeRequest, scopeArray);
134+
const token = await this.acquireTokenByDeviceCode(deviceCodeRequest, scopeArray);
135+
logger.getToken.info(formatSuccess(scopeArray));
136+
return token;
132137
} catch (err) {
133138
const code =
134139
err.name === AuthenticationErrorName
@@ -138,7 +143,7 @@ export class DeviceCodeCredential implements TokenCredential {
138143
code,
139144
message: err.message
140145
});
141-
logger.getToken.info(formatError(err));
146+
logger.getToken.info(formatError(scopeArray, err));
142147
throw err;
143148
} finally {
144149
span.end();
@@ -155,9 +160,10 @@ export class DeviceCodeCredential implements TokenCredential {
155160
): Promise<AccessToken | null> {
156161
try {
157162
const deviceResponse = await this.msalClient.acquireTokenByDeviceCode(deviceCodeRequest);
163+
const expiresOnTimestamp = deviceResponse.expiresOn.getTime();
158164
logger.getToken.info(formatSuccess(scopes));
159165
return {
160-
expiresOnTimestamp: deviceResponse.expiresOn.getTime(),
166+
expiresOnTimestamp,
161167
token: deviceResponse.accessToken
162168
};
163169
} catch (error) {

0 commit comments

Comments
 (0)