Skip to content

Commit

Permalink
CR fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yshayy committed Oct 1, 2023
1 parent 999c6ec commit 927fac7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
8 changes: 4 additions & 4 deletions tunnel-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import http from 'http'
import { Logger } from 'pino'
import { KeyObject } from 'crypto'
import { SessionStore } from './session'
import { Claims, cliTokenIssuer, jwtAuthenticator, saasJWTIssuer } from './auth'
import { Claims, cliIdentityProvider, jwtAuthenticator, saasIdentityProvider } from './auth'
import { ActiveTunnelStore } from './tunnel-store'
import { editUrl } from './url'
import { Proxy } from './proxy'
Expand All @@ -22,7 +22,7 @@ export const app = ({ proxy, sessionStore, baseUrl, activeTunnelStore, log, logi
saasPublicKey: KeyObject
jwtSaasIssuer: string
}) => {
const saasIssuer = saasJWTIssuer(jwtSaasIssuer, saasPublicKey)
const saasIdp = saasIdentityProvider(jwtSaasIssuer, saasPublicKey)
return Fastify({
serverFactory: handler => {
const baseHostname = baseUrl.hostname
Expand Down Expand Up @@ -83,7 +83,7 @@ export const app = ({ proxy, sessionStore, baseUrl, activeTunnelStore, log, logi
if (!session.user) {
const auth = jwtAuthenticator(
activeTunnel.publicKeyThumbprint,
[saasIssuer, cliTokenIssuer(activeTunnel.publicKey, activeTunnel.publicKeyThumbprint)]
[saasIdp, cliIdentityProvider(activeTunnel.publicKey, activeTunnel.publicKeyThumbprint)]
)
const result = await auth(req.raw)
if (!result.isAuthenticated) {
Expand All @@ -108,7 +108,7 @@ export const app = ({ proxy, sessionStore, baseUrl, activeTunnelStore, log, logi

const auth = jwtAuthenticator(
profileId,
[saasIssuer, cliTokenIssuer(tunnels[0].publicKey, tunnels[0].publicKeyThumbprint)]
[saasIdp, cliIdentityProvider(tunnels[0].publicKey, tunnels[0].publicKeyThumbprint)]
)

const result = await auth(req.raw)
Expand Down
24 changes: 7 additions & 17 deletions tunnel-server/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type BearerAuthorizationHeader = {
}

export type AuthorizationHeader = BasicAuthorizationHeader | BearerAuthorizationHeader
export type JWTIssuer = {
export type IdentityProvider = {
issuer: string
publicKey: KeyObject
mapClaims: (issuer: JWTPayload, context: { pkThumbprint: string }) => Claims
Expand Down Expand Up @@ -86,7 +86,7 @@ const extractAuthorizationHeader = (req: IncomingMessage): AuthorizationHeader |

export const jwtAuthenticator = (
publicKeyThumbprint: string,
issuers: JWTIssuer[]
identityProviders: IdentityProvider[]
) : Authenticator => async req => {
const authHeader = extractAuthorizationHeader(req)
const jwt = match(authHeader)
Expand All @@ -101,12 +101,12 @@ export const jwtAuthenticator = (
const parsedJwt = decodeJwt(jwt)
if (parsedJwt.iss === undefined) throw new AuthError('Could not find issuer in JWT')

const jwtIssuer = issuers.find(x => x.issuer === parsedJwt.iss)
if (!jwtIssuer) {
const idp = identityProviders.find(x => x.issuer === parsedJwt.iss)
if (!idp) {
return { isAuthenticated: false }
}

const { publicKey, mapClaims } = jwtIssuer
const { publicKey, mapClaims } = idp

let token: JWTVerifyResult
try {
Expand All @@ -124,7 +124,7 @@ export const jwtAuthenticator = (
}
}

export const saasJWTIssuer = (sassIssuer:string, saasPublicKey: KeyObject): JWTIssuer => ({
export const saasIdentityProvider = (sassIssuer:string, saasPublicKey: KeyObject): IdentityProvider => ({
issuer: sassIssuer,
publicKey: saasPublicKey,
mapClaims: (token, { pkThumbprint: profile }) => {
Expand All @@ -149,7 +149,7 @@ export const saasJWTIssuer = (sassIssuer:string, saasPublicKey: KeyObject): JWTI
},
})

export const cliTokenIssuer = (publicKey: KeyObject, publicKeyThumbprint:string): JWTIssuer => ({
export const cliIdentityProvider = (publicKey: KeyObject, publicKeyThumbprint:string): IdentityProvider => ({
issuer: `preevy://${publicKeyThumbprint}`,
publicKey,
mapClaims: (token, { pkThumbprint: profile }) => ({
Expand All @@ -160,13 +160,3 @@ export const cliTokenIssuer = (publicKey: KeyObject, publicKeyThumbprint:string)
sub: `preevy-profile:${profile}`,
}),
})

/* not really in use, can be if we support non-jwt authenticators
export const combineAuthenticators = (authenticators: Authenticator[]) =>
async (req: IncomingMessage):Promise<AuthenticationResult> => {
const authInfos = (await Promise.all(authenticators.map(authn => authn(req))))
const found = authInfos.find(info => info.isAuthenticated)
if (found !== undefined) return found
return { isAuthenticated: false }
}
*/
10 changes: 5 additions & 5 deletions tunnel-server/src/proxy/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import httpProxy from 'http-proxy'
import { IncomingMessage, ServerResponse } from 'http'
import { IncomingMessage } from 'http'
import net from 'net'
import type { Logger } from 'pino'
import { inspect } from 'util'
import { KeyObject } from 'crypto'
import stream from 'stream'
import { ActiveTunnel, ActiveTunnelStore } from '../tunnel-store'
import { requestsCounter } from '../metrics'
import { Claims, jwtAuthenticator, AuthenticationResult, AuthError, saasJWTIssuer } from '../auth'
import { Claims, jwtAuthenticator, AuthenticationResult, AuthError, saasIdentityProvider } from '../auth'
import { SessionStore } from '../session'
import { BadGatewayError, BadRequestError, BasicAuthUnauthorizedError, RedirectError, UnauthorizedError, errorHandler, errorUpgradeHandler, tryHandler, tryUpgradeHandler } from '../http-server-helpers'
import { TunnelFinder, proxyRouter } from './router'
Expand Down Expand Up @@ -47,7 +47,7 @@ export const proxy = ({
theProxy.on('proxyRes', injectScripts)

const loginRedirectUrlForRequest = loginRedirectUrl(loginUrl)
const saasIssuer = saasJWTIssuer(jwtSaasIssuer, saasPublicKey)
const saasIdp = saasIdentityProvider(jwtSaasIssuer, saasPublicKey)

const validatePrivateTunnelRequest = async (
req: IncomingMessage,
Expand All @@ -62,7 +62,7 @@ export const proxy = ({

const authenticate = jwtAuthenticator(
tunnel.publicKeyThumbprint,
[saasIssuer]
[saasIdp]
)

let authResult: AuthenticationResult
Expand Down Expand Up @@ -166,7 +166,7 @@ export const proxy = ({
const { req: mutatedReq, activeTunnel } = await validateProxyRequest(
tunnelFinder,
req,
pkThumbprint => sessionStore(req, new ServerResponse(req), pkThumbprint),
pkThumbprint => sessionStore(req, undefined, pkThumbprint),
)

const upgrade = mutatedReq.headers.upgrade?.toLowerCase()
Expand Down
4 changes: 2 additions & 2 deletions tunnel-server/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export function cookieSessionStore<T>(opts: {domain: string; schema: z.ZodSchema
const keys = opts.keys ?? [generateInsecureSecret()]
return function getSession(
req: IncomingMessage,
res: ServerResponse<IncomingMessage>,
res: ServerResponse<IncomingMessage> | undefined,
thumbprint: string
) {
const cookies = new Cookies(req, res, {
const cookies = new Cookies(req, res ?? new ServerResponse(req), {
secure: true,
keys,
})
Expand Down

0 comments on commit 927fac7

Please sign in to comment.