Skip to content

Commit

Permalink
Ensure the CLI is called back in the access-denied and error case
Browse files Browse the repository at this point in the history
  • Loading branch information
n4bb12 committed Jul 29, 2020
1 parent cddc2b6 commit a9c259d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
30 changes: 27 additions & 3 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import express from "express"
import open from "open"

import { buildStatusPage } from "../statusPage"
import {
cliDeniedCallbackPath,
cliErrorCallbackPath,
cliPort,
cliSuccessCallbackPath,
} from "../constants"
import {
accessDeniedPage,
buildErrorPage,
buildStatusPage,
} from "../statusPage"
import { getConfigFile, getRegistry, save } from "./npm"
import { printUsage } from "./usage"

Expand All @@ -22,12 +32,26 @@ const successPage = buildStatusPage(`
`)

const server = express()
.get("/", (req, res) => {
.get(cliSuccessCallbackPath, (req, res) => {
const token = decodeURIComponent(req.query.token as string)
save(registry, token)
res.setHeader("Content-Type", "text/html")
res.send(successPage)
server.close()
process.exit(0)
})
.listen(8239)
.get(cliDeniedCallbackPath, (req, res) => {
res.setHeader("Content-Type", "text/html")
res.status(401)
res.send(accessDeniedPage)
server.close()
process.exit(1)
})
.get(cliErrorCallbackPath, (req, res) => {
res.setHeader("Content-Type", "text/html")
res.status(500)
res.send(buildErrorPage(req.query.message))
server.close()
process.exit(1)
})
.listen(cliPort)
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export const publicRoot = __dirname + "/public"
export const staticPath = "/-/static/" + pluginName
export const authorizePath = "/-/oauth/authorize"
export const callbackPath = "/-/oauth/callback"
export const cliSuccessCallbackPath = "/success"
export const cliDeniedCallbackPath = "/denied"
export const cliErrorCallbackPath = "/error"
export const cliPort = 8239
26 changes: 20 additions & 6 deletions src/server/flows/CliFlow.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { IPluginMiddleware } from "@verdaccio/types"
import { Application, Handler } from "express"
import qs from "query-string"

import {
cliDeniedCallbackPath,
cliErrorCallbackPath,
cliPort,
cliSuccessCallbackPath,
} from "../../constants"
import { logger } from "../../logger"
import { AuthCore } from "../plugin/AuthCore"
import { AuthProvider } from "../plugin/AuthProvider"
import { Verdaccio } from "../verdaccio"
import { accessDeniedPage, WebFlow } from "./WebFlow"
import { WebFlow } from "./WebFlow"

const cliAuthorizeUrl = "/oauth/authorize"
const cliCallbackUrl = "http://localhost:8239?token="
const cliCallbackUrl = `http://localhost:${cliPort}`
const providerId = "cli"

const pluginAuthorizeUrl = WebFlow.getAuthorizePath(providerId)
Expand All @@ -34,6 +41,8 @@ export class CliFlow implements IPluginMiddleware<any> {
}

callback: Handler = async (req, res, next) => {
let redirectUrl: string

try {
const code = await this.provider.getCode(req)
const token = await this.provider.getToken(code)
Expand All @@ -43,15 +52,20 @@ export class CliFlow implements IPluginMiddleware<any> {
if (this.core.authenticate(username, groups)) {
const user = this.core.createAuthenticatedUser(username)
const npmToken = await this.verdaccio.issueNpmToken(token, user)
const cli = cliCallbackUrl + encodeURIComponent(npmToken)
const params = qs.stringify({ token: npmToken })

res.redirect(cli)
redirectUrl = cliCallbackUrl + cliSuccessCallbackPath + "?" + params
} else {
res.status(401).send(accessDeniedPage)
redirectUrl = cliCallbackUrl + cliDeniedCallbackPath
}
} catch (error) {
logger.error(error)
next(error)

const params = qs.stringify({ message: error.message || error })

redirectUrl = cliCallbackUrl + cliErrorCallbackPath + "?" + params
}

res.redirect(redirectUrl)
}
}

0 comments on commit a9c259d

Please sign in to comment.