Skip to content

Commit

Permalink
Merge pull request eclipse-thingweb#750 from danielpeintner/cleanup-t…
Browse files Browse the repository at this point in the history
…d-definititions-v2

Cleanup td definititions (v2)
  • Loading branch information
relu91 authored May 16, 2022
2 parents 19b7222 + 495a3f4 commit 22f7b2a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 152 deletions.
2 changes: 1 addition & 1 deletion packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export default class HttpServer implements ProtocolServer {
const oAuthScheme = thing.securityDefinitions[thing.security[0] as string] as OAuth2SecurityScheme;

// TODO: Support security schemes defined at affordance level
const scopes = oAuthScheme.scopes ?? [];
const scopes = Helpers.toStringArray(oAuthScheme.scopes); // validate call requires array of strings while oAuthScheme.scopes can be string or array of strings
let valid = false;

try {
Expand Down
5 changes: 3 additions & 2 deletions packages/binding-http/src/oauth-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
********************************************************************************/

import { OAuth2SecurityScheme } from "@node-wot/td-tools";
import { Helpers } from "@node-wot/core";
import ClientOAuth2 from "client-oauth2";
import { request, RequestOptions } from "https";
import { OAuthCredential } from "./credential";
Expand Down Expand Up @@ -80,7 +81,7 @@ export default class OAuthManager {
clientId: credentials.clientId,
clientSecret: credentials.clientSecret,
accessTokenUri: securityScheme.token,
scopes: securityScheme.scopes,
scopes: Helpers.toStringArray(securityScheme.scopes),
body: {
// TODO: some server implementation may require client_id and secret inside
// the request body
Expand All @@ -103,7 +104,7 @@ export default class OAuthManager {
clientId: credentials.clientId,
clientSecret: credentials.clientSecret,
accessTokenUri: securityScheme.token,
scopes: securityScheme.scopes,
scopes: Helpers.toStringArray(securityScheme.scopes),
},
createRequestFunction(false)
);
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/exposed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
********************************************************************************/

import * as WoT from "wot-typescript-definitions";
import * as TDT from "wot-thing-description-types";

import { Subject } from "rxjs/Subject";

Expand All @@ -39,8 +40,10 @@ import {
} from "./protocol-interfaces";

export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
security: Array<string>;
securityDefinitions: { [key: string]: TD.SecurityType };
security: string | [string, ...string[]];
securityDefinitions: {
[key: string]: TDT.SecurityScheme;
};

id: string;
title: string;
Expand Down Expand Up @@ -121,7 +124,7 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
addDefaultLanguage(thing: ExposedThing): void {
// add @language : "en" if no @language set
if (Array.isArray(thing["@context"])) {
const arrayContext = thing["@context"];
const arrayContext: TDT.ThingContext = thing["@context"];
let languageSet = false;
for (const arrayEntry of arrayContext) {
if (typeof arrayEntry === "object") {
Expand All @@ -131,7 +134,7 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
}
}
if (!languageSet) {
arrayContext.push({
(arrayContext as Exclude<typeof arrayContext, []>).push({
"@language": TD.DEFAULT_CONTEXT_LANGUAGE,
});
}
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ export default class Helpers implements Resolver {
}
}

public static toStringArray(input: string[] | string): string[] {
if (input) {
if (typeof input === "string") {
return [input];
} else {
return input;
}
} else {
return [];
}
}

// TODO: specialize fetch to retrieve just thing descriptions
public fetch(uri: string): Promise<unknown> {
return new Promise<unknown>((resolve, reject) => {
Expand Down
7 changes: 4 additions & 3 deletions packages/td-tools/src/td-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as TD from "./thing-description";

import isAbsoluteUrl = require("is-absolute-url");
import URLToolkit = require("url-toolkit");
import { ThingContext } from "wot-thing-description-types";

/** Parses a TD into a Thing object */

Expand Down Expand Up @@ -52,7 +53,7 @@ export function parseTD(td: string, normalize?: boolean): Thing {
if (thing["@context"] === undefined) {
thing["@context"] = [TD.DEFAULT_CONTEXT_V1, TD.DEFAULT_CONTEXT_V11];
} else if (Array.isArray(thing["@context"])) {
let semContext: Array<string> = thing["@context"];
let semContext = thing["@context"] as Array<string>;
const indexV1 = semContext.indexOf(TD.DEFAULT_CONTEXT_V1);
const indexV11 = semContext.indexOf(TD.DEFAULT_CONTEXT_V11);
if (indexV1 === -1 && indexV11 === -1) {
Expand Down Expand Up @@ -87,7 +88,7 @@ export function parseTD(td: string, normalize?: boolean): Thing {
semContext.unshift(TD.DEFAULT_CONTEXT_V11);
}
}
thing["@context"] = semContext;
thing["@context"] = semContext as ThingContext;
}
} else if (thing["@context"] !== TD.DEFAULT_CONTEXT_V1 && thing["@context"] !== TD.DEFAULT_CONTEXT_V11) {
const semContext = thing["@context"];
Expand Down Expand Up @@ -157,7 +158,7 @@ export function parseTD(td: string, normalize?: boolean): Thing {
}

// collect all forms for normalization and use iterations also for checking
const allForms: TD.Form[] = [];
const allForms = [];
// properties
for (const propName in thing.properties) {
const prop: TD.ThingProperty = thing.properties[propName];
Expand Down
Loading

0 comments on commit 22f7b2a

Please sign in to comment.