mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
Fix token store
This commit is contained in:
22
src/smapi.ts
22
src/smapi.ts
@@ -423,17 +423,31 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
E.chain((credentials) => {
|
E.chain((credentials) => {
|
||||||
// Check if token/key is associated with a user
|
// Check if token/key is associated with a user
|
||||||
const smapiToken = sonosSoap.getCredentialsForToken(credentials.loginToken.token);
|
const smapiToken = sonosSoap.getCredentialsForToken(credentials.loginToken.token);
|
||||||
if (!smapiToken || smapiToken.key !== credentials.loginToken.key) {
|
if (!smapiToken) {
|
||||||
return E.left(new InvalidTokenError("Token not associated with any user"));
|
return E.left(new InvalidTokenError("Token not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If credentials don't have a key, use the stored one
|
||||||
|
const effectiveKey = credentials.loginToken.key || smapiToken.key;
|
||||||
|
|
||||||
|
if (smapiToken.key !== effectiveKey) {
|
||||||
|
return E.left(new InvalidTokenError("Token key mismatch"));
|
||||||
|
}
|
||||||
|
|
||||||
return pipe(
|
return pipe(
|
||||||
smapiAuthTokens.verify({
|
smapiAuthTokens.verify({
|
||||||
token: credentials.loginToken.token,
|
token: credentials.loginToken.token,
|
||||||
key: credentials.loginToken.key,
|
key: effectiveKey,
|
||||||
}),
|
}),
|
||||||
E.map((serviceToken) => ({
|
E.map((serviceToken) => ({
|
||||||
serviceToken,
|
serviceToken,
|
||||||
credentials,
|
credentials: {
|
||||||
|
...credentials,
|
||||||
|
loginToken: {
|
||||||
|
...credentials.loginToken,
|
||||||
|
key: effectiveKey,
|
||||||
|
},
|
||||||
|
},
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -68,6 +68,13 @@ export class FileSmapiTokenStore implements SmapiTokenStore {
|
|||||||
|
|
||||||
private saveToFile(): void {
|
private saveToFile(): void {
|
||||||
try {
|
try {
|
||||||
|
// Ensure the directory exists before writing
|
||||||
|
const dir = path.dirname(this.filePath);
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
|
logger.info(`Created token storage directory: ${dir}`);
|
||||||
|
}
|
||||||
|
|
||||||
const data = JSON.stringify(this.tokens, null, 2);
|
const data = JSON.stringify(this.tokens, null, 2);
|
||||||
fs.writeFileSync(this.filePath, data, "utf8");
|
fs.writeFileSync(this.filePath, data, "utf8");
|
||||||
logger.debug(`Saved ${Object.keys(this.tokens).length} token(s) to ${this.filePath}`);
|
logger.debug(`Saved ${Object.keys(this.tokens).length} token(s) to ${this.filePath}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user