-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
136 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
import { ApitallyClient } from "../common/client"; | ||
|
||
export const requireApiKey = ({ | ||
scopes, | ||
customHeader, | ||
}: { | ||
scopes?: string | string[]; | ||
customHeader?: string; | ||
} = {}) => { | ||
return async (req: Request, res: Response, next: NextFunction) => { | ||
let apiKey: string | undefined; | ||
|
||
if (!customHeader) { | ||
if (!req.headers.authorization) { | ||
res | ||
.status(401) | ||
.set("WWW-Authenticate", "ApiKey") | ||
.json({ error: "Missing authorization header" }); | ||
return; | ||
} | ||
const authorizationParts = req.headers.authorization.split(" "); | ||
if ( | ||
authorizationParts.length === 2 && | ||
authorizationParts[0].toLowerCase() === "apikey" | ||
) { | ||
apiKey = authorizationParts[1]; | ||
} else { | ||
res | ||
.status(401) | ||
.set("WWW-Authenticate", "ApiKey") | ||
.json({ error: "Invalid authorization scheme" }); | ||
return; | ||
} | ||
} else if (customHeader) { | ||
const customHeaderValue = req.headers[customHeader.toLowerCase()]; | ||
if (typeof customHeaderValue === "string") { | ||
apiKey = customHeaderValue; | ||
} else if (Array.isArray(customHeaderValue)) { | ||
apiKey = customHeaderValue[0]; | ||
} | ||
} | ||
|
||
if (!apiKey) { | ||
res.status(403).json({ error: "Missing API key" }); | ||
return; | ||
} | ||
|
||
const client = ApitallyClient.getInstance(); | ||
const keyInfo = await client.keyRegistry.get(apiKey); | ||
if (!keyInfo) { | ||
res.status(403).json({ error: "Invalid API key" }); | ||
return; | ||
} | ||
if (scopes && !keyInfo.hasScopes(scopes)) { | ||
res.status(403).json({ error: "Permission denied" }); | ||
return; | ||
} | ||
|
||
res.locals.keyInfo = keyInfo; | ||
next(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { requireApiKey, useApitally } from "./middleware"; | ||
export { requireApiKey } from "./auth"; | ||
export { useApitally } from "./middleware"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Koa from "koa"; | ||
|
||
import { ApitallyClient } from "../common/client"; | ||
|
||
export const requireApiKey = ({ | ||
scopes, | ||
customHeader, | ||
}: { | ||
scopes?: string | string[]; | ||
customHeader?: string; | ||
} = {}) => { | ||
return async (ctx: Koa.Context, next: Koa.Next) => { | ||
let apiKey: string | undefined; | ||
|
||
if (!customHeader) { | ||
if (!ctx.headers.authorization) { | ||
ctx.status = 401; | ||
ctx.set("WWW-Authenticate", "ApiKey"); | ||
ctx.body = { error: "Missing authorization header" }; | ||
return; | ||
} | ||
const authorizationParts = ctx.headers.authorization.split(" "); | ||
if ( | ||
authorizationParts.length === 2 && | ||
authorizationParts[0].toLowerCase() === "apikey" | ||
) { | ||
apiKey = authorizationParts[1]; | ||
} else { | ||
ctx.status = 401; | ||
ctx.set("WWW-Authenticate", "ApiKey"); | ||
ctx.body = { error: "Invalid authorization scheme" }; | ||
return; | ||
} | ||
} else if (customHeader) { | ||
const customHeaderValue = ctx.headers[customHeader.toLowerCase()]; | ||
if (typeof customHeaderValue === "string") { | ||
apiKey = customHeaderValue; | ||
} else if (Array.isArray(customHeaderValue)) { | ||
apiKey = customHeaderValue[0]; | ||
} | ||
} | ||
|
||
if (!apiKey) { | ||
ctx.status = 403; | ||
ctx.body = { error: "Missing API key" }; | ||
return; | ||
} | ||
|
||
const client = ApitallyClient.getInstance(); | ||
const keyInfo = await client.keyRegistry.get(apiKey); | ||
if (!keyInfo) { | ||
ctx.status = 403; | ||
ctx.body = { error: "Invalid API key" }; | ||
return; | ||
} | ||
if (scopes && !keyInfo.hasScopes(scopes)) { | ||
ctx.status = 403; | ||
ctx.body = { error: "Permission denied" }; | ||
return; | ||
} | ||
|
||
ctx.state.keyInfo = keyInfo; | ||
await next(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { requireApiKey, useApitally } from "./middleware"; | ||
export { requireApiKey } from "./auth"; | ||
export { useApitally } from "./middleware"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { ApitallyApiKeyGuard, Scopes, useApitally } from "./middleware"; | ||
export { useApitally } from "../express"; | ||
export { ApitallyApiKeyGuard, Scopes } from "./auth"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters