Alex Mikhalev
7 years ago
5 changed files with 120 additions and 56 deletions
@ -1,7 +1,16 @@
@@ -1,7 +1,16 @@
|
||||
export default interface TokenClaims { |
||||
export interface BaseClaims { |
||||
iss: string; |
||||
exp?: number; |
||||
} |
||||
|
||||
export interface AccessOrRefreshToken extends BaseClaims { |
||||
type: "access" | "refresh"; |
||||
aud: number; |
||||
name: string; |
||||
exp: number; |
||||
} |
||||
|
||||
export interface DeviceRegistrationToken extends BaseClaims { |
||||
type: "device_reg"; |
||||
} |
||||
|
||||
export type TokenClaims = AccessOrRefreshToken | DeviceRegistrationToken; |
||||
|
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
import PromiseRouter from "express-promise-router"; |
||||
import { serialize} from "serializr"; |
||||
|
||||
import ApiError from "@common/ApiError"; |
||||
import { ErrorCode } from "@common/ErrorCode"; |
||||
import * as schema from "@common/sprinklersRpc/schema"; |
||||
import { ServerState } from "../state"; |
||||
import { authentication, verifyAuthorization } from "./authentication"; |
||||
|
||||
export default function createApi(state: ServerState) { |
||||
const router = PromiseRouter(); |
||||
|
||||
router.get("/devices/:deviceId", verifyAuthorization(), (req, res) => { |
||||
// TODO: authorize device
|
||||
const device = state.mqttClient.getDevice(req.params.deviceId); |
||||
const j = serialize(schema.sprinklersDevice, device); |
||||
res.send(j); |
||||
}); |
||||
|
||||
// router.post("/devices/register", verifyAuthorization({
|
||||
// type: "device_reg",
|
||||
// }), (req, res) => {
|
||||
// res.json({ data: "device registered" });
|
||||
// });
|
||||
|
||||
router.get("/users", verifyAuthorization(), (req, res) => { |
||||
state.database.users.find() |
||||
.then((users) => { |
||||
res.json({ |
||||
data: users, |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
router.get("/api/users/:username", (req, res, next) => { |
||||
const { username } = req.params; |
||||
state.database.users.findByUsername(username) |
||||
.then((user) => { |
||||
if (!user) { |
||||
throw new ApiError(`user ${username} does not exist`, ErrorCode.NotFound); |
||||
} |
||||
res.json({ |
||||
data: user, |
||||
}); |
||||
}) |
||||
.catch(next); |
||||
}); |
||||
|
||||
router.use("/", authentication(state)); |
||||
|
||||
return router; |
||||
} |
Loading…
Reference in new issue