Fix bug where streaming didnt work due to correct use of Bearer token (#84)

This commit is contained in:
Simon J
2021-12-03 13:51:51 +11:00
committed by GitHub
parent 075538f029
commit 7c0db619c9
4 changed files with 49 additions and 49 deletions

View File

@@ -36,7 +36,7 @@ import morgan from "morgan";
import { takeWithRepeats } from "./utils";
import { parse } from "./burn";
import { axiosImageFetcher, ImageFetcher } from "./subsonic";
import { JWTSmapiLoginTokens, SmapiAuthTokens, SmapiToken } from "./smapi_auth";
import { JWTSmapiLoginTokens, SmapiAuthTokens, smapiTokenFromString } from "./smapi_auth";
export const BONOB_ACCESS_TOKEN_HEADER = "bat";
@@ -371,7 +371,7 @@ function server(
logger.info(
`${trace} bnb<- ${req.method} ${req.path}?${JSON.stringify(
req.query
)}, headers=${JSON.stringify({ ...req.headers, "authorization": "***" })}`
)}, headers=${JSON.stringify({ ...req.headers, "authorization": "*****" })}`
);
const authHeader = E.fromNullable("Missing header");
@@ -384,7 +384,7 @@ function server(
E.map(match => match[1]!)
)),
E.chain(bearerToken => pipe(
smapiAuthTokens.verify(bearerToken as unknown as SmapiToken),
smapiAuthTokens.verify(smapiTokenFromString(bearerToken)),
E.mapLeft(_ => "Bearer token failed to verify")
)),
E.getOrElseW(() => undefined)

View File

@@ -459,30 +459,30 @@ function bindSmapiSoapServiceToExpress(
},
}),
refreshAuthToken: async (_, _2, soapyHeaders: SoapyHeaders) =>
pipe(
auth(soapyHeaders?.credentials),
E.map(({ serviceToken }) => smapiAuthTokens.issue(serviceToken)),
E.map((newToken) => ({
authToken: newToken.token,
privateKey: newToken.key,
})),
E.orElse((fault) =>
pipe(
fault.toSmapiFault(smapiAuthTokens),
E.fromPredicate(isSmapiRefreshTokenResultFault, (_) => fault),
E.map((it) => it.Fault.detail.refreshAuthTokenResult)
)
),
E.map((newToken) => ({
refreshAuthTokenResult: {
authToken: newToken.authToken,
privateKey: newToken.privateKey,
},
})),
E.getOrElseW((fault) => {
throw fault.toSmapiFault(smapiAuthTokens);
})
pipe(
auth(soapyHeaders?.credentials),
E.map(({ serviceToken }) => smapiAuthTokens.issue(serviceToken)),
E.map((newToken) => ({
authToken: newToken.token,
privateKey: newToken.key,
})),
E.orElse((fault) =>
pipe(
fault.toSmapiFault(smapiAuthTokens),
E.fromPredicate(isSmapiRefreshTokenResultFault, (_) => fault),
E.map((it) => it.Fault.detail.refreshAuthTokenResult)
)
),
E.map((newToken) => ({
refreshAuthTokenResult: {
authToken: newToken.authToken,
privateKey: newToken.privateKey,
},
})),
E.getOrElseW((fault) => {
throw fault.toSmapiFault(smapiAuthTokens);
})
),
getMediaURI: async (
{ id }: { id: string },
_,

View File

@@ -57,17 +57,17 @@ export class InvalidTokenError extends Error implements ToSmapiFault {
export class ExpiredTokenError extends Error implements ToSmapiFault {
_tag = "ExpiredTokenError";
authToken: string;
serviceToken: string;
expiredAt: number;
constructor(authToken: string, expiredAt: number) {
constructor(serviceToken: string, expiredAt: number) {
super("SMAPI token has expired");
this.authToken = authToken;
this.serviceToken = serviceToken;
this.expiredAt = expiredAt;
}
toSmapiFault = (smapiAuthTokens: SmapiAuthTokens) => {
const newToken = smapiAuthTokens.issue(this.authToken)
const newToken = smapiAuthTokens.issue(this.serviceToken)
return {
Fault: {
faultcode: "Client.TokenRefreshRequired",
@@ -142,8 +142,8 @@ export class JWTSmapiLoginTokens implements SmapiAuthTokens {
return right((jwt.verify(smapiToken.token, this.secret + this.version + smapiToken.key) as any).serviceToken);
} catch (e) {
if(isTokenExpiredError(e)) {
const x = ((jwt.verify(smapiToken.token, this.secret + this.version + smapiToken.key, { ignoreExpiration: true })) as any).serviceToken;
return left(new ExpiredTokenError(x, e.expiredAt))
const serviceToken = ((jwt.verify(smapiToken.token, this.secret + this.version + smapiToken.key, { ignoreExpiration: true })) as any).serviceToken;
return left(new ExpiredTokenError(serviceToken, e.expiredAt))
} else if(isError(e))
return left(new InvalidTokenError(e.message));
else