Login flow working

This commit is contained in:
simojenki
2021-02-24 20:54:05 +11:00
parent c26a325ee1
commit f295d3f015
13 changed files with 416 additions and 63 deletions

View File

@@ -4,6 +4,7 @@ import { listen } from "soap";
import { readFileSync } from "fs";
import path from "path";
import morgan from "morgan";
import crypto from "crypto";
import {
Sonos,
@@ -12,8 +13,8 @@ import {
STRINGS_PATH,
PRESENTATION_MAP_PATH,
} from "./sonos";
import { LinkCodes, InMemoryLinkCodes } from './link_codes'
import { MusicService } from './music_service'
import { LinkCodes, InMemoryLinkCodes } from "./link_codes";
import { MusicService, isSuccess } from "./music_service";
import logger from "./logger";
const WSDL_FILE = path.resolve(
@@ -31,7 +32,7 @@ function server(
const app = express();
app.use(morgan("combined"));
app.use(express.urlencoded({ extended: false }));
app.use(express.urlencoded({ extended: false }));
app.use(express.static("./web/public"));
app.engine("eta", Eta.renderFile);
@@ -65,16 +66,30 @@ function server(
app.get("/login", (req, res) => {
res.render("login", {
bonobService,
linkCode: req.query.linkCode
linkCode: req.query.linkCode,
});
});
app.post("/login", (req, res) => {
const canLogIn = musicService.login({ username: req.body.username, password: req.body.password})
if(canLogIn)
res.send("ok")
else
res.send("boo")
const { username, password, linkCode } = req.body;
const authResult = musicService.login({
username,
password,
});
if (isSuccess(authResult)) {
if (linkCodes.has(linkCode)) {
linkCodes.associate(linkCode, authResult);
res.render("loginOK");
} else {
res.status(400).render("failure", {
message: `Invalid linkCode!`,
});
}
} else {
res.status(403).render("failure", {
message: `Login failed, ${authResult.message}!`,
});
}
});
app.get(STRINGS_PATH, (_, res) => {
@@ -99,16 +114,45 @@ function server(
return {
getAppLinkResult: {
authorizeAccount: {
appUrlStringId: 'AppLinkMessage',
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,
privateKey: "v1",
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,
}: {
@@ -134,16 +178,16 @@ function server(
x.log = (type, data) => {
switch (type) {
case "info":
logger.info({ level:"info", data });
logger.info({ level: "info", data });
break;
case "warn":
logger.warn({ level:"warn", data });
logger.warn({ level: "warn", data });
break;
case "error":
logger.error({ level:"error", data });
logger.error({ level: "error", data });
break;
default:
logger.debug({ level:"debug", data });
logger.debug({ level: "debug", data });
}
};