moving smapi soap related classes into smapi.ts

This commit is contained in:
simojenki
2021-02-26 12:03:51 +11:00
parent 6c5b78cd6e
commit 12fe09dd7f
6 changed files with 215 additions and 213 deletions

View File

@@ -1,26 +1,17 @@
import express, { Express } from "express";
import * as Eta from "eta";
import { listen } from "soap";
import { readFileSync } from "fs";
import path from "path";
import morgan from "morgan";
import crypto from "crypto";
import {
Sonos,
Service,
SOAP_PATH,
STRINGS_PATH,
PRESENTATION_MAP_PATH,
} from "./sonos";
import { SOAP_PATH, STRINGS_ROUTE, PRESENTATION_MAP_ROUTE, LOGIN_ROUTE } from './smapi';
import { LinkCodes, InMemoryLinkCodes } from "./link_codes";
import { MusicService, isSuccess } from "./music_service";
import logger from "./logger";
// import logger from "./logger";
import bindSmapiSoapServiceToExpress from "./smapi";
const WSDL_FILE = path.resolve(
__dirname,
"Sonoswsdl-1.19.4-20190411.142401-3.wsdl"
);
function server(
sonos: Sonos,
@@ -70,14 +61,15 @@ function server(
});
});
app.get("/login", (req, res) => {
app.get(LOGIN_ROUTE, (req, res) => {
res.render("login", {
bonobService,
linkCode: req.query.linkCode,
loginRoute: LOGIN_ROUTE
});
});
app.post("/login", (req, res) => {
app.post(LOGIN_ROUTE, (req, res) => {
const { username, password, linkCode } = req.body;
const authResult = musicService.login({
username,
@@ -101,7 +93,7 @@ function server(
}
});
app.get(STRINGS_PATH, (_, res) => {
app.get(STRINGS_ROUTE, (_, res) => {
res.type("application/xml").send(`<?xml version="1.0" encoding="utf-8" ?>
<stringtables xmlns="http://sonos.com/sonosapi">
<stringtable rev="1" xml:lang="en-US">
@@ -111,94 +103,11 @@ function server(
`);
});
app.get(PRESENTATION_MAP_PATH, (_, res) => {
app.get(PRESENTATION_MAP_ROUTE, (_, res) => {
res.send("");
});
const sonosService = {
Sonos: {
SonosSoap: {
getAppLink: () => {
const linkCode = linkCodes.mint();
return {
getAppLinkResult: {
authorizeAccount: {
appUrlStringId: "AppLinkMessage",
deviceLink: {
regUrl: `${webAddress}/login?linkCode=${linkCode}`,
linkCode: linkCode,
showLinkCode: false,
},
},
},
};
},
getDeviceAuthToken: ({ linkCode }: { linkCode: string }) => {
const association = linkCodes.associationFor(linkCode);
if (association) {
return {
getDeviceAuthTokenResult: {
authToken: association.authToken.value,
privateKey: association.authToken.version,
userInfo: {
nickname: association.nickname,
userIdHashCode: crypto
.createHash("sha256")
.update(association.userId)
.digest("hex"),
},
},
};
} else {
throw {
Fault: {
faultcode: "Client.NOT_LINKED_RETRY",
faultstring: "Link Code not found retry...",
detail: {
ExceptionInfo: "NOT_LINKED_RETRY",
SonosError: "5",
},
},
};
}
},
getSessionId: ({
username,
}: {
username: string;
password: string;
}) => {
return Promise.resolve({
username,
sessionId: "123",
});
},
},
},
};
const x = listen(
app,
SOAP_PATH,
sonosService,
readFileSync(WSDL_FILE, "utf8")
);
x.log = (type, data) => {
switch (type) {
case "info":
logger.info({ level: "info", data });
break;
case "warn":
logger.warn({ level: "warn", data });
break;
case "error":
logger.error({ level: "error", data });
break;
default:
logger.debug({ level: "debug", data });
}
};
bindSmapiSoapServiceToExpress(app, SOAP_PATH, webAddress, linkCodes);
return app;
}

View File

@@ -1,2 +1,134 @@
import crypto from "crypto";
import { Express } from "express";
import { listen } from "soap";
import { readFileSync } from "fs";
import path from "path";
import logger from "./logger";
export type GetAppLinkResult = { getAppLinkResult: { authorizeAccount: { appUrlStringId: string, deviceLink: { regUrl: string, linkCode:string,showLinkCode:boolean }} } }
import { LinkCodes } from "./link_codes";
export const LOGIN_ROUTE = "/login"
export const SOAP_PATH = "/ws/sonos";
export const STRINGS_ROUTE = "/sonos/strings.xml";
export const PRESENTATION_MAP_ROUTE = "/sonos/presentationMap.xml";
const WSDL_FILE = path.resolve(
__dirname,
"Sonoswsdl-1.19.4-20190411.142401-3.wsdl"
);
export type GetAppLinkResult = {
getAppLinkResult: {
authorizeAccount: {
appUrlStringId: string;
deviceLink: { regUrl: string; linkCode: string; showLinkCode: boolean };
};
};
};
export type GetDeviceAuthTokenResult = {
getDeviceAuthTokenResult: {
authToken: string;
privateKey: string;
userInfo: {
nickname: string;
userIdHashCode: string;
};
};
};
class SonosSoap {
linkCodes: LinkCodes;
webAddress: string;
constructor(webAddress: string, linkCodes: LinkCodes) {
this.webAddress = webAddress;
this.linkCodes = linkCodes;
}
getAppLink(): GetAppLinkResult {
const linkCode = this.linkCodes.mint();
return {
getAppLinkResult: {
authorizeAccount: {
appUrlStringId: "AppLinkMessage",
deviceLink: {
regUrl: `${this.webAddress}${LOGIN_ROUTE}?linkCode=${linkCode}`,
linkCode: linkCode,
showLinkCode: false,
},
},
},
};
}
getDeviceAuthToken({
linkCode,
}: {
linkCode: string;
}): GetDeviceAuthTokenResult {
const association = this.linkCodes.associationFor(linkCode);
if (association) {
return {
getDeviceAuthTokenResult: {
authToken: association.authToken.value,
privateKey: association.authToken.version,
userInfo: {
nickname: association.nickname,
userIdHashCode: crypto
.createHash("sha256")
.update(association.userId)
.digest("hex"),
},
},
};
} else {
throw {
Fault: {
faultcode: "Client.NOT_LINKED_RETRY",
faultstring: "Link Code not found retry...",
detail: {
ExceptionInfo: "NOT_LINKED_RETRY",
SonosError: "5",
},
},
};
}
}
}
function bindSmapiSoapServiceToExpress(app: Express, soapPath:string, webAddress: string, linkCodes: LinkCodes) {
const sonosSoap = new SonosSoap(webAddress, linkCodes);
const soapyService = listen(
app,
soapPath,
{
Sonos: {
SonosSoap: {
getAppLink: () => sonosSoap.getAppLink(),
getDeviceAuthToken: ({ linkCode }: { linkCode: string }) =>
sonosSoap.getDeviceAuthToken({ linkCode }),
},
},
},
readFileSync(WSDL_FILE, "utf8")
);
soapyService.log = (type, data) => {
switch (type) {
case "info":
logger.info({ level: "info", data });
break;
case "warn":
logger.warn({ level: "warn", data });
break;
case "error":
logger.error({ level: "error", data });
break;
default:
logger.debug({ level: "debug", data });
}
};
}
export default bindSmapiSoapServiceToExpress;

View File

@@ -5,10 +5,7 @@ import { MusicService } from "@svrooij/sonos/lib/services";
import { head } from "underscore";
import logger from "./logger";
import STRINGS from './strings';
export const SOAP_PATH = "/ws/sonos";
export const STRINGS_PATH = "/sonos/strings.xml";
export const PRESENTATION_MAP_PATH = "/sonos/presentationMap.xml";
import { SOAP_PATH, STRINGS_ROUTE, PRESENTATION_MAP_ROUTE } from './smapi';
export type Device = {
name: string;
@@ -42,11 +39,11 @@ export const bonobService = (
uri: `${stripTailingSlash(bonobRoot)}${SOAP_PATH}`,
secureUri: `${stripTailingSlash(bonobRoot)}${SOAP_PATH}`,
strings: {
uri: `${stripTailingSlash(bonobRoot)}${STRINGS_PATH}`,
uri: `${stripTailingSlash(bonobRoot)}${STRINGS_ROUTE}`,
version: STRINGS.version,
},
presentation: {
uri: `${stripTailingSlash(bonobRoot)}${PRESENTATION_MAP_PATH}`,
uri: `${stripTailingSlash(bonobRoot)}${PRESENTATION_MAP_ROUTE}`,
version: "1",
},
pollInterval: 1200,