Skip to content

Commit 3cc2908

Browse files
authored
[core-auth] Add AzureNamedKeyCredential class and NamedKeyCredential interface (Azure#14371)
* [core-auth] Adds AzureNamedKeyCredential * [core-auth] update package version to 1.3.0
1 parent 7ba6fee commit 3cc2908

File tree

6 files changed

+171
-5
lines changed

6 files changed

+171
-5
lines changed

sdk/core/core-auth/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Release History
22

3-
## 1.2.1 (Unreleased)
3+
## 1.3.0 (Unreleased)
44

5+
- Adds the `AzureNamedKeyCredential` class which supports credential rotation and a corresponding `NamedKeyCredential` interface to support the use of static string-based names and keys in Azure clients.
56

67
## 1.2.0 (2021-02-08)
78

sdk/core/core-auth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@azure/core-auth",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "Provides low-level interfaces and helper methods for authentication in Azure SDK",
55
"sdk-type": "client",
66
"main": "dist/index.js",

sdk/core/core-auth/review/core-auth.api.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ export class AzureKeyCredential implements KeyCredential {
1919
update(newKey: string): void;
2020
}
2121

22+
// @public
23+
export class AzureNamedKeyCredential implements NamedKeyCredential {
24+
constructor(name: string, key: string);
25+
get key(): string;
26+
get name(): string;
27+
update(newName: string, newKey: string): void;
28+
}
29+
2230
// @public
2331
export class AzureSASCredential implements SASCredential {
2432
constructor(signature: string);
@@ -45,6 +53,12 @@ export interface KeyCredential {
4553
readonly key: string;
4654
}
4755

56+
// @public
57+
export interface NamedKeyCredential {
58+
readonly key: string;
59+
readonly name: string;
60+
}
61+
4862
// @public
4963
export interface SASCredential {
5064
readonly signature: string;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
/**
5+
* Represents a credential defined by a static API name and key.
6+
*/
7+
export interface NamedKeyCredential {
8+
/**
9+
* The value of the API key represented as a string
10+
*/
11+
readonly key: string;
12+
/**
13+
* The value of the API name represented as a string.
14+
*/
15+
readonly name: string;
16+
}
17+
18+
/**
19+
* A static name/key-based credential that supports updating
20+
* the underlying name and key values.
21+
*/
22+
export class AzureNamedKeyCredential implements NamedKeyCredential {
23+
private _key: string;
24+
private _name: string;
25+
26+
/**
27+
* The value of the key to be used in authentication.
28+
*/
29+
public get key(): string {
30+
return this._key;
31+
}
32+
33+
/**
34+
* The value of the name to be used in authentication.
35+
*/
36+
public get name(): string {
37+
return this._name;
38+
}
39+
40+
/**
41+
* Create an instance of an AzureNamedKeyCredential for use
42+
* with a service client.
43+
*
44+
* @param name - The initial value of the name to use in authentication.
45+
* @param key - The initial value of the key to use in authentication.
46+
*/
47+
constructor(name: string, key: string) {
48+
if (!name || !key) {
49+
throw new TypeError("name and key must be non-empty strings");
50+
}
51+
52+
this._name = name;
53+
this._key = key;
54+
}
55+
56+
/**
57+
* Change the value of the key.
58+
*
59+
* Updates will take effect upon the next request after
60+
* updating the key value.
61+
*
62+
* @param newName - The new name value to be used.
63+
* @param newKey - The new key value to be used.
64+
*/
65+
public update(newName: string, newKey: string): void {
66+
if (!newName || !newKey) {
67+
throw new TypeError("newName and newKey must be non-empty strings");
68+
}
69+
70+
this._name = newName;
71+
this._key = newKey;
72+
}
73+
}

sdk/core/core-auth/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
export { AzureKeyCredential, KeyCredential } from "./azureKeyCredential";
5+
export { AzureNamedKeyCredential, NamedKeyCredential } from "./azureNamedKeyCredential";
56
export { AzureSASCredential, SASCredential } from "./azureSASCredential";
67

78
export {

sdk/core/core-auth/test/index.spec.ts

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

44
import assert from "assert";
55

6-
import { AzureKeyCredential } from "../src/azureKeyCredential";
7-
import { AzureSASCredential } from "../src/azureSASCredential";
8-
import { isTokenCredential } from "../src/tokenCredential";
6+
import {
7+
AzureKeyCredential,
8+
AzureNamedKeyCredential,
9+
AzureSASCredential,
10+
isTokenCredential
11+
} from "../src/index";
912

1013
describe("AzureKeyCredential", () => {
1114
it("credential constructor throws on invalid key", () => {
@@ -28,6 +31,80 @@ describe("AzureKeyCredential", () => {
2831
});
2932
});
3033

34+
describe("AzureNamedKeyCredential", () => {
35+
it("credential constructor throws on invalid name or key", () => {
36+
assert.throws(() => {
37+
void new AzureNamedKeyCredential("name", "");
38+
}, /name and key must be non-empty strings/);
39+
assert.throws(() => {
40+
void new AzureNamedKeyCredential("name", (null as unknown) as string);
41+
}, /name and key must be non-empty strings/);
42+
assert.throws(() => {
43+
void new AzureNamedKeyCredential("name", (undefined as unknown) as string);
44+
}, /name and key must be non-empty strings/);
45+
assert.throws(() => {
46+
void new AzureNamedKeyCredential("", "key");
47+
}, /name and key must be non-empty strings/);
48+
assert.throws(() => {
49+
void new AzureNamedKeyCredential((null as unknown) as string, "key");
50+
}, /name and key must be non-empty strings/);
51+
assert.throws(() => {
52+
void new AzureNamedKeyCredential((undefined as unknown) as string, "key");
53+
}, /name and key must be non-empty strings/);
54+
assert.throws(() => {
55+
void new AzureNamedKeyCredential("", "");
56+
}, /name and key must be non-empty strings/);
57+
assert.throws(() => {
58+
void new AzureNamedKeyCredential((null as unknown) as string, (null as unknown) as string);
59+
}, /name and key must be non-empty strings/);
60+
assert.throws(() => {
61+
void new AzureNamedKeyCredential(
62+
(undefined as unknown) as string,
63+
(undefined as unknown) as string
64+
);
65+
}, /name and key must be non-empty strings/);
66+
});
67+
68+
it("credential correctly updates", () => {
69+
const credential = new AzureNamedKeyCredential("name1", "credential1");
70+
assert.equal(credential.name, "name1");
71+
assert.equal(credential.key, "credential1");
72+
credential.update("name2", "credential2");
73+
assert.equal(credential.name, "name2");
74+
assert.equal(credential.key, "credential2");
75+
});
76+
77+
it("credential update throws on invalid name or key", () => {
78+
const credential = new AzureNamedKeyCredential("name1", "credential1");
79+
assert.equal(credential.name, "name1");
80+
assert.equal(credential.key, "credential1");
81+
82+
// invalid name
83+
assert.throws(() => {
84+
credential.update("", "credential2");
85+
}, /newName and newKey must be non-empty strings/);
86+
// parameters unchanged
87+
assert.equal(credential.name, "name1");
88+
assert.equal(credential.key, "credential1");
89+
90+
// invalid key
91+
assert.throws(() => {
92+
credential.update("name2", "");
93+
}, /newName and newKey must be non-empty strings/);
94+
// parameters unchanged
95+
assert.equal(credential.name, "name1");
96+
assert.equal(credential.key, "credential1");
97+
98+
// invalid name and key
99+
assert.throws(() => {
100+
credential.update("", "");
101+
}, /newName and newKey must be non-empty strings/);
102+
// parameters unchanged
103+
assert.equal(credential.name, "name1");
104+
assert.equal(credential.key, "credential1");
105+
});
106+
});
107+
31108
describe("AzureSASCredential", () => {
32109
it("credential constructor throws on invalid signature", () => {
33110
assert.throws(() => {

0 commit comments

Comments
 (0)