From a259f419fe86697e5511c43f090e819ee7b00803 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Fri, 29 Jun 2018 21:30:55 -0600 Subject: [PATCH] cleaned stuffs up --- app/components/DeviceView.tsx | 52 ++++++++++----------------- app/components/ProgramTable.tsx | 33 +++++++++-------- server/express/authentication.ts | 62 ++++++++++++++++++-------------- 3 files changed, 72 insertions(+), 75 deletions(-) diff --git a/app/components/DeviceView.tsx b/app/components/DeviceView.tsx index 31bbdb1..b9b12e4 100644 --- a/app/components/DeviceView.tsx +++ b/app/components/DeviceView.tsx @@ -49,19 +49,24 @@ interface DeviceViewProps { } class DeviceView extends React.Component { - device!: SprinklersDevice; - - componentWillMount() { - this.updateDevice(); - } - - componentWillUpdate() { - this.updateDevice(); - } - render() { - const { id, connectionState, sections, programs, sectionRunner } = this.device; - const { uiStore } = this.props.state; + const { uiStore, sprinklersApi } = this.props.state; + const device = sprinklersApi.getDevice(this.props.deviceId); + const { id, connectionState, sections, programs, sectionRunner } = device; + const deviceBody = connectionState.isAvailable && ( + + + + + + + + + + + + + ); return ( @@ -73,32 +78,11 @@ class DeviceView extends React.Component { Raspberry Pi Grinklers Device - {connectionState.isAvailable && - } - {connectionState.isAvailable && - - - - - - - - - } - {connectionState.isAvailable && - - } + {deviceBody} ); } - - private updateDevice() { - const { state, deviceId } = this.props; - if (!this.device || this.device.id !== deviceId) { - this.device = state.sprinklersApi.getDevice(deviceId); - } - } } export default injectState(observer(DeviceView)); diff --git a/app/components/ProgramTable.tsx b/app/components/ProgramTable.tsx index a0dabb2..7401786 100644 --- a/app/components/ProgramTable.tsx +++ b/app/components/ProgramTable.tsx @@ -74,7 +74,7 @@ export default class ProgramTable extends React.Component<{ ); } - private renderRows = (program: Program, i: number): JSX.Element[] | null => { + private renderRows = (program: Program, i: number): JSX.Element | null => { if (!program) { return null; } @@ -87,8 +87,8 @@ export default class ProgramTable extends React.Component<{ ]; }); const cancelOrRun = () => running ? program.cancel() : program.run(); - const rows = [( - + const mainRow = ( + {"" + (i + 1)} {name} {enabled ? "Enabled" : "Not enabled"} @@ -100,17 +100,20 @@ export default class ProgramTable extends React.Component<{ - )]; - if (false) { - rows.push( - - -

Sequence:

{sequenceItems} -

Schedule:

-
-
, - ); - } - return rows; + ); + const detailRow = false && ( + + +

Sequence:

{sequenceItems} +

Schedule:

+
+
+ ); + return ( + + {mainRow} + {detailRow} + + ); } } diff --git a/server/express/authentication.ts b/server/express/authentication.ts index 03d0e61..1b0d84e 100644 --- a/server/express/authentication.ts +++ b/server/express/authentication.ts @@ -6,6 +6,19 @@ import { User } from "../models/User"; import { ServerState } from "../state"; import { ApiError } from "./errors"; +declare global { + namespace Express { + interface Request { + token?: TokenClaims; + } + } +} + +const JWT_SECRET = process.env.JWT_SECRET!; +if (!JWT_SECRET) { + throw new Error("Must specify JWT_SECRET environment variable"); +} + const ACCESS_TOKEN_LIFETIME = (30 * 60); // 30 minutes const REFRESH_TOKEN_LIFETIME = (24 * 60 * 60); // 24 hours @@ -24,9 +37,9 @@ interface TokenClaims { exp: number; } -function signToken(claims: TokenClaims, secret: string): Promise { +function signToken(claims: TokenClaims): Promise { return new Promise((resolve, reject) => { - jwt.sign(claims, secret, (err: Error, encoded: string) => { + jwt.sign(claims, JWT_SECRET, (err: Error, encoded: string) => { if (err) { reject(err); } else { @@ -36,9 +49,9 @@ function signToken(claims: TokenClaims, secret: string): Promise { }); } -function verifyToken(token: string, secret: string): Promise { +function verifyToken(token: string): Promise { return new Promise((resolve, reject) => { - jwt.verify(token, secret, (err, decoded) => { + jwt.verify(token, JWT_SECRET, (err, decoded) => { if (err) { if (err.name === "TokenExpiredError") { reject(new ApiError(401, "The specified token is expired", err)); @@ -63,7 +76,7 @@ function generateAccessToken(user: User, secret: string): Promise { exp: getExpTime(ACCESS_TOKEN_LIFETIME), }; - return signToken(access_token_claims, secret); + return signToken(access_token_claims); } function generateRefreshToken(user: User, secret: string): Promise { @@ -75,14 +88,10 @@ function generateRefreshToken(user: User, secret: string): Promise { exp: getExpTime(REFRESH_TOKEN_LIFETIME), }; - return signToken(refresh_token_claims, secret); + return signToken(refresh_token_claims); } export function authentication(state: ServerState) { - const JWT_SECRET = process.env.JWT_SECRET!; - if (!JWT_SECRET) { - throw new Error("Must specify JWT_SECRET environment variable"); - } const router = Router(); @@ -118,7 +127,7 @@ export function authentication(state: ServerState) { if (!body || !refresh_token) { throw new ApiError(400, "Must specify a refresh_token"); } - const claims = await verifyToken(refresh_token, JWT_SECRET); + const claims = await verifyToken(refresh_token); if (claims.type !== "refresh") { throw new ApiError(400, "Not a refresh token"); } @@ -146,25 +155,26 @@ export function authentication(state: ServerState) { } }); - router.post("/token/verify", async (req, res) => { - const bearer = req.headers.authorization; - if (!bearer) { - throw new ApiError(401, "No bearer token specified"); - } - const matches = /^Bearer (.*)$/.exec(bearer); - if (!matches || !matches[1]) { - throw new ApiError(400, "Invalid bearer token specified"); - } - const token = matches[1]; - - log.info({ token }); - - const decoded = await verifyToken(token, JWT_SECRET); + router.post("/token/verify", authorizeAccess, async (req, res) => { res.json({ ok: true, - decoded, + token: req.token, }); }); return router; } + +export async function authorizeAccess(req: Express.Request, res: Express.Response) { + const bearer = req.headers.authorization; + if (!bearer) { + throw new ApiError(401, "No bearer token specified"); + } + const matches = /^Bearer (.*)$/.exec(bearer); + if (!matches || !matches[1]) { + throw new ApiError(400, "Invalid bearer token specified"); + } + const token = matches[1]; + + req.token = await verifyToken(token); +}