Skip to content

Commit c11c3e6

Browse files
committed
feat(core): add support for AutoSecurityScheme #1421
1 parent e5bbddc commit c11c3e6

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

packages/core/src/thing-description.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ export interface NullSchema extends BaseSchema {
134134
type: "null";
135135
}
136136

137-
// TODO AutoSecurityScheme
138137
export type SecurityType =
139138
| NoSecurityScheme
139+
| AutoSecurityScheme
140140
| BasicSecurityScheme
141141
| DigestSecurityScheme
142142
| BearerSecurityScheme
@@ -193,6 +193,9 @@ export interface AllOfSecurityScheme extends SecurityScheme {
193193
}
194194
export type ComboSecurityScheme = AllOfSecurityScheme | OneOfSecurityScheme;
195195

196+
export interface AutoSecurityScheme extends SecurityScheme, TDT.AutoSecurityScheme {
197+
scheme: "auto";
198+
}
196199
/** Implements the Thing Property description */
197200
export abstract class ThingProperty extends BaseSchema {
198201
// writable: boolean;

packages/core/test/ClientTest.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ import { Subscription } from "rxjs/Subscription";
2727

2828
import Servient from "../src/servient";
2929
import ConsumedThing from "../src/consumed-thing";
30-
import { AllOfSecurityScheme, Form, OneOfSecurityScheme, SecurityScheme } from "../src/thing-description";
30+
import {
31+
AllOfSecurityScheme,
32+
AutoSecurityScheme,
33+
Form,
34+
OAuth2SecurityScheme,
35+
OneOfSecurityScheme,
36+
SecurityScheme,
37+
} from "../src/thing-description";
3138
import { ProtocolClient, ProtocolClientFactory } from "../src/protocol-interfaces";
3239
import { Content } from "../src/content";
3340
import { ContentSerdes } from "../src/content-serdes";
@@ -1045,4 +1052,27 @@ class WoTClientTest {
10451052
ct.ensureClientSecurity(pc, form);
10461053
}, /Combo SecurityScheme 'combo_without_oneOf_and_without_allof' is invalid/);
10471054
}
1055+
1056+
@test "with auto security scheme selection"() {
1057+
const ct = new ConsumedThing(WoTClientTest.servient);
1058+
ct.securityDefinitions = {
1059+
auto_sc: {
1060+
scheme: "auto",
1061+
},
1062+
};
1063+
ct.security = ["auto_sc"];
1064+
const pc = new TestProtocolClient();
1065+
const form: Form = {
1066+
href: "https://example.com/",
1067+
};
1068+
ct.ensureClientSecurity(pc, form);
1069+
expect(pc.securitySchemes.length).equals(1);
1070+
expect(pc.securitySchemes[0].scheme).equal("auto");
1071+
1072+
if (pc.securitySchemes[0].scheme === "auto") {
1073+
// casting to AutoSecurityScheme should not bother the IDE.
1074+
const autoScheme = pc.securitySchemes[0] as AutoSecurityScheme;
1075+
expect(autoScheme.scheme).to.equal("auto");
1076+
}
1077+
}
10481078
}

0 commit comments

Comments
 (0)