Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
n4bb12 committed May 15, 2021
1 parent 57094fb commit 76a7858
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
5 changes: 4 additions & 1 deletion lib/fastify-cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { Config } from "./types"
//
// https://github.com/fastify/fastify-cookie#fastify-cookie
//
export function registerCookieMiddleware(server: FastifyInstance, config: Config) {
export function registerCookieMiddleware(
server: FastifyInstance,
config: Config,
) {
server.register(fastifyCookie, { secret: config.cryptoSecret })
}
4 changes: 3 additions & 1 deletion lib/fastify-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { FastifyInstance } from "fastify"
// https://www.fastify.io/docs/latest/Serverless/#vercel
// https://vercel.com/docs/serverless-functions/supported-languages#using-typescript
//
export const createLambdaHandler: (server: FastifyInstance) => NowApiHandler = server => {
export const createLambdaHandler: (server: FastifyInstance) => NowApiHandler = (
server,
) => {
return async (req: NowRequest, res: NowResponse) => {
await server.ready()
server.server.emit("request", req, res)
Expand Down
51 changes: 40 additions & 11 deletions lib/github-oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"
import { nanoid } from "nanoid"
import { URLSearchParams } from "url"

import { Config, GitHubAccessToken, GitHubOrgMembership, GitHubUser, OAuthState, RoutePrams } from "./types"
import {
Config,
GitHubAccessToken,
GitHubOrgMembership,
GitHubUser,
OAuthState,
RoutePrams,
} from "./types"

export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
const secureCookies = !!process.env.VERCEL_URL
Expand Down Expand Up @@ -59,7 +66,10 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
//
// https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#web-application-flow
//
const redirectToGitHub = async (req: FastifyRequest<RoutePrams>, res: FastifyReply) => {
const redirectToGitHub = async (
req: FastifyRequest<RoutePrams>,
res: FastifyReply,
) => {
const query = formatQueryParams({
client_id: config.githubClientId,
scope: "read:user",
Expand All @@ -78,7 +88,9 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
})
}

const getGitHubAccessToken = async (code: string): Promise<GitHubAccessToken> => {
const getGitHubAccessToken = async (
code: string,
): Promise<GitHubAccessToken> => {
const url = urls.githubToken
const headers = {
Accept: "application/json",
Expand All @@ -94,7 +106,9 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
return data
}

const getGitHubUser = async (tokenData: GitHubAccessToken): Promise<GitHubUser> => {
const getGitHubUser = async (
tokenData: GitHubAccessToken,
): Promise<GitHubUser> => {
const url = urls.githubUserDetails
const headers = {
Accept: "application/json",
Expand All @@ -118,11 +132,20 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
return data
}

const retrieveState = (req: FastifyRequest<RoutePrams>, res: FastifyReply) => {
const retrieveState = (
req: FastifyRequest<RoutePrams>,
res: FastifyReply,
) => {
const state: OAuthState = unsignCookie(res, req.query.state || "")
const expectedState: OAuthState = unsignCookie(res, req.cookies[cookieNames.state] || "")

if (!state?.randomToken || state.randomToken !== expectedState?.randomToken) {
const expectedState: OAuthState = unsignCookie(
res,
req.cookies[cookieNames.state] || "",
)

if (
!state?.randomToken ||
state.randomToken !== expectedState?.randomToken
) {
throw new Error("State mismatch")
}

Expand All @@ -147,11 +170,17 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
server.addHook<RoutePrams>("preValidation", async (req, res) => {
try {
if (req.url === urls.localMembershipError) {
return denyAccess(res, "It appears you are not a member of the required GitHub organization.")
return denyAccess(
res,
"It appears you are not a member of the required GitHub organization.",
)
}

if (req.url === urls.localGenericError) {
return denyAccess(res, "It appears that the authentication request was initiated or processed incorrectly.")
return denyAccess(
res,
"It appears that the authentication request was initiated or processed incorrectly.",
)
}

if (req.url === urls.localAuthorize) {
Expand All @@ -177,7 +206,7 @@ export function registerGitHubOAuth(server: FastifyInstance, config: Config) {
const user = await getGitHubUser(tokenData)
const members = await getGitHubOrgMemberships()

if (!members.find(member => member.id === user.id)) {
if (!members.find((member) => member.id === user.id)) {
return res.redirect(302, urls.localMembershipError)
}

Expand Down
16 changes: 13 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ import { registerServeStatic } from "./fastify-static"
import { registerGitHubOAuth } from "./github-oauth"
import { Config } from "./types"

export const createLambdaProxyAuthHandler: (config: Config) => NowApiHandler = config => {
export const createLambdaProxyAuthHandler: (config: Config) => NowApiHandler = (
config,
) => {
assert(config.cryptoSecret, "config.cryptoSecret", assert.string.nonEmpty)
assert(config.githubClientId, "config.githubClientId", assert.string.nonEmpty)
assert(config.githubClientSecret, "config.githubClientSecret", assert.string.nonEmpty)
assert(config.githubOrgAdminToken, "config.githubOrgAdminToken", assert.string.nonEmpty)
assert(
config.githubClientSecret,
"config.githubClientSecret",
assert.string.nonEmpty,
)
assert(
config.githubOrgAdminToken,
"config.githubOrgAdminToken",
assert.string.nonEmpty,
)
assert(config.githubOrgName, "config.githubOrgName", assert.string.nonEmpty)

const server = fastify({ logger: true })
Expand Down

0 comments on commit 76a7858

Please sign in to comment.