SmapiAuthTokens that expire, with sonos refreshAuthToken functionality (#81)

Bearer token to Authorization header for stream requests
Versioned SMAPI Tokens
This commit is contained in:
Simon J
2021-12-02 11:03:52 +11:00
committed by GitHub
parent 89340dd454
commit d1300b8119
24 changed files with 1792 additions and 1330 deletions

View File

@@ -4,54 +4,12 @@ import {
randomBytes,
createHash,
} from "crypto";
import jwt from "jsonwebtoken";
import jws from "jws";
const ALGORITHM = "aes-256-cbc";
const IV = randomBytes(16);
function isError(thing: any): thing is Error {
return thing.name && thing.message
}
export type Signer = {
sign: (value: string) => string;
verify: (token: string) => string;
};
export const pSigner = (signer: Signer) => ({
sign: (value: string): Promise<string> => {
return new Promise((resolve, reject) => {
try {
return resolve(signer.sign(value));
} catch(e) {
if(isError(e)) reject(e.message)
else reject(`Failed to sign value: ${e}`);
}
});
},
verify: (token: string): Promise<string> => {
return new Promise((resolve, reject) => {
try {
return resolve(signer.verify(token));
}catch(e) {
if(isError(e)) reject(e.message)
else reject(`Failed to verify value: ${e}`);
}
});
}
});
export const jwtSigner = (secret: string) => ({
sign: (value: string) => jwt.sign(value, secret),
verify: (token: string) => {
try {
return jwt.verify(token, secret) as string;
} catch (e) {
throw new Error(`Failed to verify jwt, try re-authorising account within sonos app`);
}
},
});
export type Hash = {
iv: string;