Skip to content

Commit

Permalink
Add apiKeys and apiKeys admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
milesthedisch committed Sep 6, 2022
1 parent 1ed7361 commit 90afd9b
Show file tree
Hide file tree
Showing 21 changed files with 387 additions and 16 deletions.
10 changes: 10 additions & 0 deletions db/apiKeys/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { prisma } from "../client";
import crypto from "crypto";

export default async function create(user: string) {
return prisma.apiKey.create({
data: {
user,
},
});
}
12 changes: 12 additions & 0 deletions db/apiKeys/deleteOne.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { prisma } from "../client";
import crypto from "crypto";

export default async function deleteOne(apiKeyID: string) {
const apiKey = crypto.randomUUID();

return prisma.apiKey.delete({
where: {
apiKeyID,
},
});
}
5 changes: 5 additions & 0 deletions db/apiKeys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { prisma } from "../client";

export default async function getAll() {
return prisma.apiKey.findMany();
}
2 changes: 1 addition & 1 deletion db/strategyBalance/createMany.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prisma } from "@prisma/client";
import prisma from "../client";
import { prisma } from "../client";

export default async function createStrategyBalances(newBalances) {
const strats = await prisma.strategies.findMany();
Expand Down
69 changes: 69 additions & 0 deletions pages/admin/apiKeys.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Container, Grid } from "@mui/material";

import PageTitle from "../../src/components/PageTitle";
import PageTitleWrapper from "../../src/components/PageTitleWrapper";
import SuspenseLoader from "../../src/components/SuspenseLoader";
import ApiKeyTable from "../../src/components/ApiKeyTable";

import Head from "next/head";

import useSWR, { useSWRConfig } from "swr";
import { withPageAuthRequired } from "@auth0/nextjs-auth0";
import { format } from "date-fns";
import type { ApiKey } from "@prisma/client";

const fetcher = async (uri: string) => {
const response = await fetch(uri);
return response.json();
};

export default withPageAuthRequired(function () {
const { data, error, isValidating } = useSWR("/api/apiKeys", fetcher);

if (isValidating) {
return (
<Container
sx={{ height: "80vh", display: "flex", justifyContent: "center" }}
>
{/* The default value size is 64 */}
<SuspenseLoader size={64 * 1.5} />
</Container>
);
}

return (
<>
<Head>
<title>Api Keys</title>
</Head>
<PageTitleWrapper>
<PageTitle
sx={{ width: "auto" }}
heading="API Keys"
subHeading="List of API Keys"
noDoc={true}
/>
</PageTitleWrapper>
<Container
sx={{
display: "flex",
flexDirection: "column",
justifyContent: "center",
}}
maxWidth="lg"
>
<Grid
container
direction="row"
justifyContent="center"
alignItems="stretch"
spacing={3}
>
<Grid item xs={12}>
<ApiKeyTable apiKeys={data} />
</Grid>
</Grid>
</Container>
</>
);
});
2 changes: 1 addition & 1 deletion pages/admin/audit/[account]/[trades].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const Sub = (props) => {
return (
<>
<Head>
<title>Sub Account {`${account}`}</title>
<title>Sub Account / Trades {`${account}`}</title>
</Head>
<PageTitleWrapper>
<Box sx={{ display: "flex", justifyContent: "space-between" }}>
Expand Down
2 changes: 1 addition & 1 deletion pages/admin/strategies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default withPageAuthRequired(function (props) {
return (
<>
<Head>
<title>Transactions - Applications</title>
<title>Strategies</title>
</Head>
<PageTitleWrapper>
<Box sx={{ display: "flex", justifyContent: "space-between" }}>
Expand Down
2 changes: 1 addition & 1 deletion pages/admin/users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ApplicationsTransactions = ({
}) => (
<>
<Head>
<title>Transactions - Applications</title>
<title>Users</title>
</Head>
<PageTitleWrapper>
<PageHeader />
Expand Down
24 changes: 24 additions & 0 deletions pages/api/apiKeys/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { NextApiRequest, NextApiResponse } from "next";
import getAll from "../../../db/apiKeys";
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
import type { ApiKey } from "@prisma/client";

export default withApiAuthRequired(async function (
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(403).send("Incorrect HTTP code");
}

let records: ApiKey[];

try {
records = await getAll();
} catch (e) {
console.error(e);
return res.status(400).send("Unauthorized, Api-Key not found.");
}

return res.status(200).json({ data: records });
});
26 changes: 26 additions & 0 deletions pages/api/apiKeys/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { NextApiRequest, NextApiResponse } from "next";
import deleteOne from "../../../db/apiKeys/deleteOne";
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
import type { ApiKey } from "@prisma/client";

export default withApiAuthRequired(async function (
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "DELETE") {
return res.status(403).send("Incorrect HTTP code");
}

const apiKeyID = req.body.apiKeyID;

let records: ApiKey;

try {
records = await deleteOne(apiKeyID);
} catch (e) {
console.error(e);
return res.status(400).send("Unauthorized, Api-Key not found.");
}

return res.status(200).json({ data: records });
});
24 changes: 24 additions & 0 deletions pages/api/apiKeys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { NextApiRequest, NextApiResponse } from "next";
import getAll from "../../../db/apiKeys";
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
import type { ApiKey } from "@prisma/client";

export default withApiAuthRequired(async function (
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "GET") {
return res.status(403).send("Incorrect HTTP code");
}

let records: ApiKey[];

try {
records = await getAll();
} catch (e) {
console.error(e);
return res.status(400).send("Unauthorized, Api-Key not found.");
}

return res.status(200).json({ data: records });
});
2 changes: 1 addition & 1 deletion pages/api/auth0seedOneTime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import createUser from "../../../db/user/create";
import { AuthenticationClient, ManagementClient } from "auth0";
import { nanoid } from "nanoid/non-secure";
import prisma from "../../../db/client";
import { prisma } from "../../../db/client";
import type { User } from "auth0";
import sgMail from "@sendgrid/mail";

Expand Down
13 changes: 11 additions & 2 deletions pages/api/ftx/accounts/aggregatePnl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
import { withApiAuthRequired, getSession } from "@auth0/nextjs-auth0";
import { RestClient } from "ftx-api";
import matchPositionsWithBalanes from "../../../../util/matchPositions";
import { getAllFunding } from "../../../../util/ftx";

const getSeconds = (millis) => ~~(millis / 1000).toFixed(0);
Expand All @@ -15,6 +14,16 @@ export default withApiAuthRequired(async function ftx(
req: NextApiRequest,
res: NextApiResponse
) {
const session = getSession(req, res);
const user = session.user;
const roles = user["https://app.balmoral.digital/roles"];

if (!user || !roles.includes("admin")) {
return res
.status(401)
.json({ redirect: "401", message: "Unauthorized", success: false });
}

try {
const subAccounts = await client.getSubaccounts();

Expand Down
13 changes: 12 additions & 1 deletion pages/api/ftx/accounts/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
import { withApiAuthRequired, getSession } from "@auth0/nextjs-auth0";
import { RestClient } from "ftx-api";

// TODO: Make env variables

const key = "vN0h2BlDFk2EBaHxglTtXtD4EB9_NRQYx0mVlPFU";
const secret = "ILNvroZ6SUDuvTg9mzc2-u-JnTTX9S1wMjDLwaES";

Expand All @@ -11,6 +13,15 @@ export default withApiAuthRequired(async function ftx(
req: NextApiRequest,
res: NextApiResponse
) {
const session = getSession(req, res);
const user = session.user;
const roles = user["https://app.balmoral.digital/roles"];

if (!user || !roles.includes("admin")) {
return res
.status(401)
.json({ redirect: "401", message: "Unauthorized", success: false });
}
try {
const subAccounts = await client.getSubaccounts();

Expand Down
16 changes: 13 additions & 3 deletions pages/api/sharePrice/create.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
import createSharePrice from "../../../db/sharePrice/create";
import { withApiAuthRequired, getSession } from "@auth0/nextjs-auth0";

async function handler(req: NextApiRequest, res: NextApiResponse) {
const session = getSession(req, res);
const user = session.user;
const roles = user["https://app.balmoral.digital/roles"];

if (!user || !roles.includes("admin")) {
return res
.status(401)
.json({ redirect: "401", message: "Unauthorized", success: false });
}

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === "POST") {
const { sharePrice } = JSON.parse(req.body);

Expand All @@ -25,6 +35,6 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
.status(405)
.json({ message: "Method not allowed", success: false });
}
};
}

export default withApiAuthRequired(handler);
4 changes: 2 additions & 2 deletions pages/api/sharePrice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
import getSharePrice, { getLatestSharePrice } from "../../../db/sharePrice";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "GET") {
const { from, to, latest } = req.query;

Expand Down Expand Up @@ -30,6 +30,6 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
.status(405)
.json({ message: "Method not allowed", success: false });
}
};
}

export default withApiAuthRequired(handler);
12 changes: 11 additions & 1 deletion pages/api/strategies/create.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import type { NextApiRequest, NextApiResponse } from "next";
import createStrategy from "../../../db/strategies/create";
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
import { withApiAuthRequired, getSession } from "@auth0/nextjs-auth0";

export default withApiAuthRequired(async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const session = getSession(req, res);
const user = session.user;
const roles = user["https://app.balmoral.digital/roles"];

if (!user || !roles.includes("admin")) {
return res
.status(401)
.json({ redirect: "401", message: "Unauthorized", success: false });
}

if (req.method === "POST") {
const data = req.body;

Expand Down
2 changes: 1 addition & 1 deletion pages/api/strategies/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from "next";
import getStrategy from "../../../db/strategies";
import { withApiAuthRequired } from "@auth0/nextjs-auth0";
import { withApiAuthRequired, getSession } from "@auth0/nextjs-auth0";

export default withApiAuthRequired(async function handler(
req: NextApiRequest,
Expand Down
2 changes: 1 addition & 1 deletion pages/api/user/txs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import prisma from "../../../db/client";
import { prisma } from "../../../db/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { withApiAuthRequired, getSession } from "@auth0/nextjs-auth0";
import protectRoute from "../../../util/protectRoute";
Expand Down
Loading

1 comment on commit 90afd9b

@vercel
Copy link

@vercel vercel bot commented on 90afd9b Sep 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.