Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine API access controls #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ For a full list of API endpoints, see https://ourboard.io/api-docs.

All POST and PUT endpoints accept application/json content.

API requests against boards with restricted access require you to supply an API_TOKEN header with a valid API token.
The token is returned in the response of the POST request used to create the board.
API requests against boards with restricted access require you to supply a board-specific API_TOKEN
as a HTTP header. The token is returned in the response of the POST request used to create the board.
As a result, only boards created using the API can have an API_TOKEN at the moment.

### POST /api/v1/board

Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/board-csv-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const boardCSVGet = route
.get("/api/v1/board/:boardId/csv")
.use(apiTokenHeader)
.handler((request) =>
checkBoardAPIAccess(request, async (boardState) => {
checkBoardAPIAccess("read", request, async (boardState) => {
const board = boardState.board
const textItemsWithParent = Object.values(board.items).filter(
(i) => i.containerId !== undefined && (i.type === "text" || i.type === "note"),
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/board-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const boardGet = route
.get("/api/v1/board/:boardId")
.use(apiTokenHeader)
.handler((request) =>
checkBoardAPIAccess(request, async (boardState) => {
checkBoardAPIAccess("read", request, async (boardState) => {
return ok({ board: boardState.board })
}),
)
2 changes: 1 addition & 1 deletion backend/src/api/board-hierarchy-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const boardHierarchyGet = route
.get("/api/v1/board/:boardId/hierarchy")
.use(apiTokenHeader)
.handler((request) =>
checkBoardAPIAccess(request, async (boardState) => {
checkBoardAPIAccess("read", request, async (boardState) => {
const board = boardState.board
return ok({ board: getBoardHierarchy(boardState.board) })
}),
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/board-history-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const boardHistoryGet = route
.get("/api/v1/board/:boardId/history")
.use(apiTokenHeader)
.handler((request) =>
checkBoardAPIAccess(request, async (board) => {
checkBoardAPIAccess("read", request, async (board) => {
return ok(
streamingJSONBody("history", async (callback) => {
await withDBClient(
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/board-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const boardUpdate = route
.put("/api/v1/board/:boardId")
.use(apiTokenHeader, body(t.type({ name: NonEmptyString, accessPolicy: BoardAccessPolicyCodec })))
.handler((request) =>
checkBoardAPIAccess(request, async () => {
checkBoardAPIAccess("write", request, async () => {
const { boardId } = request.routeParams
const { name, accessPolicy } = request.body
await updateBoard({ boardId, name, accessPolicy })
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/item-create-or-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const itemCreateOrUpdate = route
),
)
.handler((request) =>
checkBoardAPIAccess(request, async (board) => {
checkBoardAPIAccess("write", request, async (board) => {
const { itemId } = request.routeParams
let {
type,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/item-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const itemCreate = route
body(t.type({ type: t.literal("note"), text: t.string, color: t.string, container: t.string })),
)
.handler((request) =>
checkBoardAPIAccess(request, async (board) => {
checkBoardAPIAccess("write", request, async (board) => {
const { type, text, color, container } = request.body
console.log(`POST item for board ${board.board.id}: ${JSON.stringify(request.req.body)}`)
addItem(board, type, text, color, container)
Expand Down
13 changes: 11 additions & 2 deletions backend/src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
AppEvent,
Board,
BoardHistoryEntry,
canRead,
canWrite,
checkBoardAccess,
Color,
Container,
EventUserInfo,
Expand All @@ -23,6 +26,7 @@ export const route = applyMiddleware(wrapNative(bodyParser.json()))
export const apiTokenHeader = headers(t.partial({ API_TOKEN: t.string }))

export async function checkBoardAPIAccess<T>(
accessType: "read" | "write",
request: { routeParams: { boardId: string }; headers: { API_TOKEN?: string | undefined } },
fn: (board: ServerSideBoardState) => Promise<T>,
) {
Expand All @@ -31,9 +35,14 @@ export async function checkBoardAPIAccess<T>(
try {
const board = await getBoard(boardId)
if (!board) return notFound()
if (board.board.accessPolicy || board.accessTokens.length) {
const accessLevel = checkBoardAccess(board.board.accessPolicy, {
nickname: "anonymous",
userType: "unidentified",
})
const publicAccessOk = accessType === "read" ? canRead(accessLevel) : canWrite(accessLevel)
if (!publicAccessOk) {
if (!apiToken) {
return badRequest("API_TOKEN header is missing")
return badRequest("API_TOKEN header required for this board")
}
if (!board.accessTokens.some((t) => t === apiToken)) {
console.log(`API_TOKEN ${apiToken} not on list ${board.accessTokens}`)
Expand Down