Move API endpoints to /api
This commit is contained in:
parent
2c97136279
commit
185a956fb9
|
@ -0,0 +1,342 @@
|
||||||
|
const request = require("supertest");
|
||||||
|
|
||||||
|
const createServer = require("../createServer");
|
||||||
|
|
||||||
|
const clearBlacklist = require("./utils/clearBlacklist");
|
||||||
|
const withRevokedToken = require("./utils/withRevokedToken");
|
||||||
|
const withToken = require("./utils/withToken");
|
||||||
|
|
||||||
|
describe("The API server", () => {
|
||||||
|
let server;
|
||||||
|
beforeEach(() => {
|
||||||
|
server = createServer();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GET /api/publicKey", () => {
|
||||||
|
it("echoes the public key", done => {
|
||||||
|
request(server)
|
||||||
|
.get("/api/publicKey")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.text).toMatch(/PUBLIC KEY/);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
|
describe("GET /_publicKey", () => {
|
||||||
|
it("echoes the public key", done => {
|
||||||
|
request(server)
|
||||||
|
.get("/_publicKey")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.text).toMatch(/PUBLIC KEY/);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("POST /api/auth", () => {
|
||||||
|
it("creates a new auth token", done => {
|
||||||
|
request(server)
|
||||||
|
.post("/api/auth")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.body).toHaveProperty("token");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
|
describe("POST /_auth", () => {
|
||||||
|
it("creates a new auth token", done => {
|
||||||
|
request(server)
|
||||||
|
.post("/_auth")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.body).toHaveProperty("token");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GET /api/auth", () => {
|
||||||
|
describe("with no auth", () => {
|
||||||
|
it("echoes back null", done => {
|
||||||
|
request(server)
|
||||||
|
.get("/api/auth")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.body).toHaveProperty("auth");
|
||||||
|
expect(res.body.auth).toBe(null);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("with a revoked auth token", () => {
|
||||||
|
it("echoes back null", done => {
|
||||||
|
withRevokedToken({ some: { scope: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.get("/api/auth?token=" + token)
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.body).toHaveProperty("auth");
|
||||||
|
expect(res.body.auth).toBe(null);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("with a valid auth token", () => {
|
||||||
|
it("echoes back the auth payload", done => {
|
||||||
|
withToken({ some: { scope: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.get("/api/auth?token=" + token)
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.body).toHaveProperty("auth");
|
||||||
|
expect(typeof res.body.auth).toBe("object");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
|
describe("GET /_auth", () => {
|
||||||
|
describe("with no auth", () => {
|
||||||
|
it("echoes back null", done => {
|
||||||
|
request(server)
|
||||||
|
.get("/_auth")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.body).toHaveProperty("auth");
|
||||||
|
expect(res.body.auth).toBe(null);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("with a revoked auth token", () => {
|
||||||
|
it("echoes back null", done => {
|
||||||
|
withRevokedToken({ some: { scope: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.get("/_auth?token=" + token)
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.body).toHaveProperty("auth");
|
||||||
|
expect(res.body.auth).toBe(null);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("with a valid auth token", () => {
|
||||||
|
it("echoes back the auth payload", done => {
|
||||||
|
withToken({ some: { scope: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.get("/_auth?token=" + token)
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.body).toHaveProperty("auth");
|
||||||
|
expect(typeof res.body.auth).toBe("object");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("POST /api/blacklist", () => {
|
||||||
|
afterEach(clearBlacklist);
|
||||||
|
|
||||||
|
describe("with no auth", () => {
|
||||||
|
it("is forbidden", done => {
|
||||||
|
request(server)
|
||||||
|
.post("/api/blacklist")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(403);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with the "blacklist.add" scope', () => {
|
||||||
|
it("can add to the blacklist", done => {
|
||||||
|
withToken({ blacklist: { add: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.post("/api/blacklist")
|
||||||
|
.send({ token, packageName: "bad-package" })
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body.ok).toBe(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
|
describe("POST /_blacklist", () => {
|
||||||
|
afterEach(clearBlacklist);
|
||||||
|
|
||||||
|
describe("with no auth", () => {
|
||||||
|
it("is forbidden", done => {
|
||||||
|
request(server)
|
||||||
|
.post("/_blacklist")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(403);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with the "blacklist.add" scope', () => {
|
||||||
|
it("can add to the blacklist", done => {
|
||||||
|
withToken({ blacklist: { add: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.post("/_blacklist")
|
||||||
|
.send({ token, packageName: "bad-package" })
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body.ok).toBe(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GET /api/blacklist", () => {
|
||||||
|
describe("with no auth", () => {
|
||||||
|
it("is forbidden", done => {
|
||||||
|
request(server)
|
||||||
|
.get("/api/blacklist")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(403);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with the "blacklist.read" scope', () => {
|
||||||
|
it("can read the blacklist", done => {
|
||||||
|
withToken({ blacklist: { read: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.get("/api/blacklist?token=" + token)
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
|
describe("GET /_blacklist", () => {
|
||||||
|
describe("with no auth", () => {
|
||||||
|
it("is forbidden", done => {
|
||||||
|
request(server)
|
||||||
|
.get("/_blacklist")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(403);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with the "blacklist.read" scope', () => {
|
||||||
|
it("can read the blacklist", done => {
|
||||||
|
withToken({ blacklist: { read: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.get("/_blacklist?token=" + token)
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("DELETE /api/blacklist", () => {
|
||||||
|
describe("with no auth", () => {
|
||||||
|
it("is forbidden", done => {
|
||||||
|
request(server)
|
||||||
|
.delete("/api/blacklist")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(403);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with the "blacklist.remove" scope', () => {
|
||||||
|
it("can remove a package from the blacklist", done => {
|
||||||
|
withToken({ blacklist: { remove: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.delete("/api/blacklist")
|
||||||
|
.send({ token, packageName: "bad-package" })
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body.ok).toBe(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("can remove a scoped package from the blacklist", done => {
|
||||||
|
withToken({ blacklist: { remove: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.delete("/api/blacklist")
|
||||||
|
.send({ token, packageName: "@scope/bad-package" })
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body.ok).toBe(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
|
describe("DELETE /_blacklist/:packageName", () => {
|
||||||
|
describe("with no auth", () => {
|
||||||
|
it("is forbidden", done => {
|
||||||
|
request(server)
|
||||||
|
.delete("/_blacklist/bad-package")
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(403);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with the "blacklist.remove" scope', () => {
|
||||||
|
it("can remove a package from the blacklist", done => {
|
||||||
|
withToken({ blacklist: { remove: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.delete("/_blacklist/bad-package")
|
||||||
|
.send({ token })
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body.ok).toBe(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("can remove a scoped package from the blacklist", done => {
|
||||||
|
withToken({ blacklist: { remove: true } }, token => {
|
||||||
|
request(server)
|
||||||
|
.delete("/_blacklist/@scope/bad-package")
|
||||||
|
.send({ token })
|
||||||
|
.end((err, res) => {
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body.ok).toBe(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -4,10 +4,8 @@ const createServer = require("../createServer");
|
||||||
|
|
||||||
const clearBlacklist = require("./utils/clearBlacklist");
|
const clearBlacklist = require("./utils/clearBlacklist");
|
||||||
const withBlacklist = require("./utils/withBlacklist");
|
const withBlacklist = require("./utils/withBlacklist");
|
||||||
const withRevokedToken = require("./utils/withRevokedToken");
|
|
||||||
const withToken = require("./utils/withToken");
|
|
||||||
|
|
||||||
describe("The production server", () => {
|
describe("The server", () => {
|
||||||
let server;
|
let server;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
server = createServer();
|
server = createServer();
|
||||||
|
@ -52,178 +50,18 @@ describe("The production server", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not serve blacklisted packages", done => {
|
describe("blacklisted packages", () => {
|
||||||
withBlacklist(["bad-package"], () => {
|
|
||||||
request(server)
|
|
||||||
.get("/bad-package/index.js")
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.statusCode).toBe(403);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GET /_publicKey", () => {
|
|
||||||
it("echoes the public key", done => {
|
|
||||||
request(server)
|
|
||||||
.get("/_publicKey")
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.text).toMatch(/PUBLIC KEY/);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("POST /_auth", () => {
|
|
||||||
it("creates a new auth token", done => {
|
|
||||||
request(server)
|
|
||||||
.post("/_auth")
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.body).toHaveProperty("token");
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GET /_auth", () => {
|
|
||||||
describe("with no auth", () => {
|
|
||||||
it("echoes back null", done => {
|
|
||||||
request(server)
|
|
||||||
.get("/_auth")
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.body).toHaveProperty("auth");
|
|
||||||
expect(res.body.auth).toBe(null);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("with a revoked auth token", () => {
|
|
||||||
it("echoes back null", done => {
|
|
||||||
withRevokedToken({ some: { scope: true } }, token => {
|
|
||||||
request(server)
|
|
||||||
.get("/_auth?token=" + token)
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.body).toHaveProperty("auth");
|
|
||||||
expect(res.body.auth).toBe(null);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("with a valid auth token", () => {
|
|
||||||
it("echoes back the auth payload", done => {
|
|
||||||
withToken({ some: { scope: true } }, token => {
|
|
||||||
request(server)
|
|
||||||
.get("/_auth?token=" + token)
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.body).toHaveProperty("auth");
|
|
||||||
expect(typeof res.body.auth).toBe("object");
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("POST /_blacklist", () => {
|
|
||||||
afterEach(clearBlacklist);
|
afterEach(clearBlacklist);
|
||||||
|
|
||||||
describe("with no auth", () => {
|
it("does not serve blacklisted packages", done => {
|
||||||
it("is forbidden", done => {
|
withBlacklist(["bad-package"], () => {
|
||||||
request(server)
|
request(server)
|
||||||
.post("/_blacklist")
|
.get("/bad-package/index.js")
|
||||||
.end((err, res) => {
|
.end((err, res) => {
|
||||||
expect(res.statusCode).toBe(403);
|
expect(res.statusCode).toBe(403);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with the "blacklist.add" scope', () => {
|
|
||||||
it("can add to the blacklist", done => {
|
|
||||||
withToken({ blacklist: { add: true } }, token => {
|
|
||||||
request(server)
|
|
||||||
.post("/_blacklist")
|
|
||||||
.send({ token, packageName: "bad-package" })
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.headers["content-location"]).toEqual(
|
|
||||||
"/_blacklist/bad-package"
|
|
||||||
);
|
|
||||||
expect(res.body.ok).toBe(true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("GET /_blacklist", () => {
|
|
||||||
describe("with no auth", () => {
|
|
||||||
it("is forbidden", done => {
|
|
||||||
request(server)
|
|
||||||
.get("/_blacklist")
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.statusCode).toBe(403);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('with the "blacklist.read" scope', () => {
|
|
||||||
it("can read the blacklist", done => {
|
|
||||||
withToken({ blacklist: { read: true } }, token => {
|
|
||||||
request(server)
|
|
||||||
.get("/_blacklist?token=" + token)
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("DELETE /_blacklist/:packageName", () => {
|
|
||||||
describe("with no auth", () => {
|
|
||||||
it("is forbidden", done => {
|
|
||||||
request(server)
|
|
||||||
.delete("/_blacklist/bad-package")
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.statusCode).toBe(403);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('with the "blacklist.remove" scope', () => {
|
|
||||||
it("can remove a package from the blacklist", done => {
|
|
||||||
withToken({ blacklist: { remove: true } }, token => {
|
|
||||||
request(server)
|
|
||||||
.delete("/_blacklist/bad-package")
|
|
||||||
.send({ token })
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.body.ok).toBe(true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("can remove a scoped package from the blacklist", done => {
|
|
||||||
withToken({ blacklist: { remove: true } }, token => {
|
|
||||||
request(server)
|
|
||||||
.delete("/_blacklist/@scope/bad-package")
|
|
||||||
.send({ token })
|
|
||||||
.end((err, res) => {
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.body.ok).toBe(true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const BlacklistAPI = require("../../BlacklistAPI");
|
const BlacklistAPI = require("../../BlacklistAPI");
|
||||||
|
|
||||||
function withBlacklist(blacklist, callback) {
|
function withBlacklist(blacklist, done) {
|
||||||
return Promise.all(blacklist.map(BlacklistAPI.addPackage)).then(callback);
|
Promise.all(blacklist.map(BlacklistAPI.addPackage)).then(done);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = withBlacklist;
|
module.exports = withBlacklist;
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
const withToken = require("./withToken");
|
const withToken = require("./withToken");
|
||||||
const AuthAPI = require("../../AuthAPI");
|
const AuthAPI = require("../../AuthAPI");
|
||||||
|
|
||||||
function withRevokedToken(scopes, callback) {
|
function withRevokedToken(scopes, done) {
|
||||||
withToken(scopes, token => {
|
withToken(scopes, token => {
|
||||||
AuthAPI.revokeToken(token).then(() => {
|
AuthAPI.revokeToken(token).then(() => {
|
||||||
callback(token);
|
done(token);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const AuthAPI = require("../../AuthAPI");
|
const AuthAPI = require("../../AuthAPI");
|
||||||
|
|
||||||
function withToken(scopes, callback) {
|
function withToken(scopes, done) {
|
||||||
AuthAPI.createToken(scopes).then(callback);
|
AuthAPI.createToken(scopes).then(done);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = withToken;
|
module.exports = withToken;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const validateNpmPackageName = require("validate-npm-package-name");
|
const validateNpmPackageName = require("validate-npm-package-name");
|
||||||
|
|
||||||
const BlacklistAPI = require("../BlacklistAPI");
|
const BlacklistAPI = require("../BlacklistAPI");
|
||||||
|
|
||||||
function addToBlacklist(req, res) {
|
function addToBlacklist(req, res) {
|
||||||
|
@ -29,7 +30,7 @@ function addToBlacklist(req, res) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.set({ "Content-Location": `/_blacklist/${packageName}` }).send({
|
res.send({
|
||||||
ok: true,
|
ok: true,
|
||||||
message: `Package "${packageName}" was ${
|
message: `Package "${packageName}" was ${
|
||||||
added ? "added to" : "already in"
|
added ? "added to" : "already in"
|
||||||
|
|
|
@ -1,7 +1,27 @@
|
||||||
|
const validateNpmPackageName = require("validate-npm-package-name");
|
||||||
|
|
||||||
const BlacklistAPI = require("../BlacklistAPI");
|
const BlacklistAPI = require("../BlacklistAPI");
|
||||||
|
|
||||||
function removeFromBlacklist(req, res) {
|
function removeFromBlacklist(req, res) {
|
||||||
const packageName = req.packageName;
|
// TODO: Remove req.packageName when DELETE
|
||||||
|
// /_blacklist/:packageName API is removed
|
||||||
|
const packageName = req.body.packageName || req.packageName;
|
||||||
|
|
||||||
|
if (!packageName) {
|
||||||
|
return res
|
||||||
|
.status(403)
|
||||||
|
.send({ error: 'Missing "packageName" body parameter' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const nameErrors = validateNpmPackageName(packageName).errors;
|
||||||
|
|
||||||
|
// Disallow invalid package names.
|
||||||
|
if (nameErrors) {
|
||||||
|
const reason = nameErrors.join(", ");
|
||||||
|
return res.status(403).send({
|
||||||
|
error: `Invalid package name "${packageName}" (${reason})`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
BlacklistAPI.removePackage(packageName).then(
|
BlacklistAPI.removePackage(packageName).then(
|
||||||
removed => {
|
removed => {
|
||||||
|
|
|
@ -50,7 +50,7 @@ class Layout extends React.Component {
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.adjustUnderline();
|
this.adjustUnderline();
|
||||||
|
|
||||||
fetch("/_stats?period=last-month")
|
fetch("/api/stats?period=last-month")
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(stats => this.setState({ stats }));
|
.then(stats => this.setState({ stats }));
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,40 @@ function createRouter() {
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(require("./middleware/userToken"));
|
app.use(require("./middleware/userToken"));
|
||||||
|
|
||||||
|
app.use(
|
||||||
|
"/api",
|
||||||
|
route(app => {
|
||||||
|
app.get("/publicKey", require("./actions/showPublicKey"));
|
||||||
|
|
||||||
|
app.post("/auth", require("./actions/createAuth"));
|
||||||
|
app.get("/auth", require("./actions/showAuth"));
|
||||||
|
|
||||||
|
app.post(
|
||||||
|
"/blacklist",
|
||||||
|
require("./middleware/requireAuth")("blacklist.add"),
|
||||||
|
require("./actions/addToBlacklist")
|
||||||
|
);
|
||||||
|
app.get(
|
||||||
|
"/blacklist",
|
||||||
|
require("./middleware/requireAuth")("blacklist.read"),
|
||||||
|
require("./actions/showBlacklist")
|
||||||
|
);
|
||||||
|
app.delete(
|
||||||
|
"/blacklist",
|
||||||
|
require("./middleware/requireAuth")("blacklist.remove"),
|
||||||
|
require("./actions/removeFromBlacklist")
|
||||||
|
);
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== "test") {
|
||||||
|
app.get("/stats", require("./actions/showStats"));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
app.get("/_publicKey", require("./actions/showPublicKey"));
|
app.get("/_publicKey", require("./actions/showPublicKey"));
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
app.use(
|
app.use(
|
||||||
"/_auth",
|
"/_auth",
|
||||||
route(app => {
|
route(app => {
|
||||||
|
@ -27,6 +59,7 @@ function createRouter() {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
app.use(
|
app.use(
|
||||||
"/_blacklist",
|
"/_blacklist",
|
||||||
route(app => {
|
route(app => {
|
||||||
|
@ -49,6 +82,7 @@ function createRouter() {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// TODO: Remove
|
||||||
if (process.env.NODE_ENV !== "test") {
|
if (process.env.NODE_ENV !== "test") {
|
||||||
app.get("/_stats", require("./actions/showStats"));
|
app.get("/_stats", require("./actions/showStats"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,6 @@ const domainNames = [
|
||||||
|
|
||||||
let cachedZones;
|
let cachedZones;
|
||||||
|
|
||||||
const oneSecond = 1000;
|
|
||||||
const oneMinute = oneSecond * 60;
|
|
||||||
const oneHour = oneMinute * 60;
|
|
||||||
const oneDay = oneHour * 24;
|
|
||||||
|
|
||||||
function getSeconds(date) {
|
function getSeconds(date) {
|
||||||
return Math.floor(date.getTime() / 1000);
|
return Math.floor(date.getTime() / 1000);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ function indexRedirect(req, res, entry) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function stripLeadingSegment(name) {
|
function stripLeadingSegment(name) {
|
||||||
return name.replace(/^[^\/]+\/?/, "");
|
return name.replace(/^[^/]+\/?/, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
function searchEntries(tarballStream, entryName, wantsHTML) {
|
function searchEntries(tarballStream, entryName, wantsHTML) {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
const basicAuth = require("basic-auth");
|
||||||
|
|
||||||
const AuthAPI = require("../AuthAPI");
|
const AuthAPI = require("../AuthAPI");
|
||||||
|
|
||||||
const ReadMethods = { GET: true, HEAD: true };
|
const ReadMethods = { GET: true, HEAD: true };
|
||||||
|
@ -10,7 +12,10 @@ function userToken(req, res, next) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = (ReadMethods[req.method] ? req.query : req.body).token;
|
const credentials = basicAuth(req);
|
||||||
|
const token = credentials
|
||||||
|
? credentials.pass
|
||||||
|
: (ReadMethods[req.method] ? req.query : req.body).token;
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
req.user = null;
|
req.user = null;
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
"babel-preset-react": "^6.24.1",
|
"babel-preset-react": "^6.24.1",
|
||||||
"babel-preset-stage-2": "^6.24.1",
|
"babel-preset-stage-2": "^6.24.1",
|
||||||
"babel-register": "^6.26.0",
|
"babel-register": "^6.26.0",
|
||||||
|
"basic-auth": "^2.0.0",
|
||||||
"body-parser": "^1.18.2",
|
"body-parser": "^1.18.2",
|
||||||
"cors": "^2.8.1",
|
"cors": "^2.8.1",
|
||||||
"countries-list": "^1.3.2",
|
"countries-list": "^1.3.2",
|
||||||
|
|
|
@ -1144,7 +1144,7 @@ base@^0.11.1:
|
||||||
mixin-deep "^1.2.0"
|
mixin-deep "^1.2.0"
|
||||||
pascalcase "^0.1.1"
|
pascalcase "^0.1.1"
|
||||||
|
|
||||||
basic-auth@~2.0.0:
|
basic-auth@^2.0.0, basic-auth@~2.0.0:
|
||||||
version "2.0.0"
|
version "2.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-2.0.0.tgz#015db3f353e02e56377755f962742e8981e7bbba"
|
resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-2.0.0.tgz#015db3f353e02e56377755f962742e8981e7bbba"
|
||||||
dependencies:
|
dependencies:
|
||||||
|
|
Loading…
Reference in New Issue