<commit_before>// Copyright 2015 ISRG.  All rights reserved
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

'use strict';

var express    = require('express');
var bodyParser = require('body-parser');
var jose       = require('node-jose');
var crypto     = require('../../lib/crypto-utils');
var utils      = require('../../lib/utils');

const PORT       = 3900;
const NONCE_SIZE = 16;

function _setup(app, server) {
  app.use(bodyParser.json());
  app.use(function(req, res, next) {
    var nonce = jose.util.randomBytes(NONCE_SIZE).toString('hex');
    server.nonces.add(nonce);
    res.append('Replay-Nonce', nonce);
    next();
  });
  app.post('/*', function(req, res, next) {
    crypto.verifySignature(req.body)
    .then((sig) => {
      req.sig = sig;
      var nonce = req.sig.header.nonce;

      if (!server.nonces.delete(nonce)) {
        return res.status(401).send({
          type:   'urn:acme:badNonce',
          detail: 'Unknown nonce'
        });
      }
      next();
    }, (er) => {
       // I'm a teapot.
      return res.status(418).send({
        type:    'urn:acme:malformed',
        detail:  'Invalid signature',
        message: er.message
      });
    });
  });

  app.head('/*', function(req, res) {
    res.end();
  });

  app.get('/', function(req, res) {
    res.send(server.directory);
  });

  app.get('/directory', function(req, res) {
    res.send(server.directory);
  });

  app.get('/terms', function(req, res) {
    res.send('BOILERPLATE LEGALESE\n');
  });

  app.post('/new-reg', function(req, res) {
    res.links({
      next:               server.base + 'new-authz',
      recover:            server.base + 'recover-reg',
      'terms-of-service': server.base + 'terms'
    });
    var c = server.count++;
    server.registrations[c] = utils.extract(['contact', 'agreement'], req.sig.payload);
    res.location(server.base + 'reg/' + c);
    res.status(201).send({
      contact: req.sig.payload.contact
    });
  });

  app.post('/reg/:id', function(req, res) {
    var reg = server.registrations[req.params.id];
    if (reg == null) {
      return res.status(404).send({
        type:   'urn:acme:notFound',
        detail: 'Unknown registration'
      });
    }
    // TODO: check jwk for match
    reg = utils.extend(reg, utils.extract(['contact', 'agreement'], req.sig.payload));
    server.registrations[req.params.id] = reg;
    return res.status(200).send(reg);
  });

  app.get('/reg/:id', function(req, res) {
    var reg = server.registrations[req.params.id];
    if (reg == null) {
      return res.status(404).send({
        type:   'urn:acme:notFound',
        detail: 'Unknown registration'
      });
    }
    return res.status(200).send(reg);
  });

  app.post('/new-authz', function(req, res) {
    var authz = req.body;
    authz.challenges = [{
      uri:   server.base + 'challenge',
      token: jose.util.randomBytes(NONCE_SIZE).toString('hex')
    }];
    return res.status(201).send(authz);<commit_msg>tests actually working<commit_after>// Copyright 2015 ISRG.  All rights reserved
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

'use strict';

var express    = require('express');
var bodyParser = require('body-parser');
var jose       = require('node-jose');
var crypto     = require('../../lib/crypto-utils');
var utils      = require('../../lib/utils');

const PORT       = 3900;
const NONCE_SIZE = 16;

function _setup(app, server) {
  app.use(bodyParser.json());
  app.use(function(req, res, next) {
    var nonce = jose.util.randomBytes(NONCE_SIZE).toString('hex');
    server.nonces.add(nonce);
    res.append('Replay-Nonce', nonce);
    next();
  });
  app.post('/*', function(req, res, next) {
    crypto.verifySignature(req.body)
    .then((sig) => {
      req.sig = sig;
      var nonce = req.sig.header.nonce;

      if (!server.nonces.delete(nonce)) {
        return res.status(401).send({
          type:   'urn:acme:badNonce',
          detail: 'Unknown nonce'
        });
      }
      next();
    }, (er) => {
       // I'm a teapot.
      return res.status(418).send({
        type:    'urn:acme:malformed',
        detail:  'Invalid signature',
        message: er.message
      });
    });
  });

  app.head('/*', function(req, res) {
    res.end();
  });

  app.get('/', function(req, res) {
    res.send(server.directory);
  });

  app.get('/directory', function(req, res) {
    res.send(server.directory);
  });

  app.get('/terms', function(req, res) {
    res.send('BOILERPLATE LEGALESE\n');
  });

  app.post('/new-reg', function(req, res) {
    res.links({
      next:               server.base + 'new-authz',
      recover:            server.base + 'recover-reg',
      'terms-of-service': server.base + 'terms'
    });
    var c = server.count++;
    server.registrations[c] = utils.extract(['contact', 'agreement'], req.sig.payload);
    res.location(server.base + 'reg/' + c);
    res.status(201).send({
      contact: req.sig.payload.contact
    });
  });

  app.post('/reg/:id', function(req, res) {
    var reg = server.registrations[req.params.id];
    if (reg == null) {
      return res.status(404).send({
        type:   'urn:acme:notFound',
        detail: 'Unknown registration'
      });
    }
    // TODO: check jwk for match
    reg = utils.extend(reg, utils.extract(['contact', 'agreement'], req.sig.payload));
    server.registrations[req.params.id] = reg;
    return res.status(200).send(reg);
  });

  app.get('/reg/:id', function(req, res) {
    var reg = server.registrations[req.params.id];
    if (reg == null) {
      return res.status(404).send({
        type:   'urn:acme:notFound',
        detail: 'Unknown registration'
      });
    }
    return res.status(200).send(reg);
  });

  app.post('/new-authz', function(req, res) {
    var authz = req.sig.payload;
    authz.challenges = [{
      uri:   server.base + 'challenge',
      token: jose.util.randomBytes(NONCE_SIZE).toString('hex')
    }];
    return res.status(201).send(authz);
{'fraction_non_alphanumeric': 0.18136942675159234, 'fraction_numerical': 0.012101910828025478, 'mean_word_length': 2.544582392776524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1170346', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2104, 'n_words': 435}