diff --git a/authenticating-users/.eslintrc.yml b/authenticating-users/.eslintrc.yml new file mode 100644 index 000000000..7275058fd --- /dev/null +++ b/authenticating-users/.eslintrc.yml @@ -0,0 +1,3 @@ +--- +rules: + require-atomic-updates: off \ No newline at end of file diff --git a/authenticating-users/app.js b/authenticating-users/app.js index f94985d68..20ea8a08d 100644 --- a/authenticating-users/app.js +++ b/authenticating-users/app.js @@ -15,6 +15,7 @@ 'use strict'; +// [START getting_started_auth_all] const express = require('express'); const got = require('got'); const jwt = require('jsonwebtoken'); @@ -25,6 +26,7 @@ const app = express(); let certs; let aud; +// [START getting_started_auth_certs] async function certificates() { if (!certs) { let response = await got('https://www.gstatic.com/iap/verify/public_key'); @@ -33,6 +35,7 @@ async function certificates() { return certs; } +// [END getting_started_auth_certs] async function get_metadata(itemName) { const endpoint = 'http://metadata.google.internal'; @@ -40,11 +43,12 @@ async function get_metadata(itemName) { const url = endpoint + path + itemName; let response = await got(url, { - headers: {'Metadata-Flavor': 'Google'} + headers: {'Metadata-Flavor': 'Google'}, }); return response.body; } +// [START getting_started_auth_metadata] async function audience() { if (!aud) { let project_number = await get_metadata('numeric-project-id'); @@ -55,7 +59,9 @@ async function audience() { return aud; } +// [END getting_started_auth_metadata] +// [START getting_started_auth_audience] async function validate_assertion(assertion) { // Decode the header to determine which certificate signed the assertion const encodedHeader = assertion.split('.')[0]; @@ -75,28 +81,32 @@ async function validate_assertion(assertion) { // Return the two relevant pieces of information return { - 'email': payload.email, - 'sub': payload.sub, + email: payload.email, + sub: payload.sub, }; - } +// [END getting_started_auth_audience] +// [START getting_started_auth_front_controller] app.get('/', (req, res) => { const assertion = req.header('X-Goog-IAP-JWT-Assertion'); - validate_assertion(assertion).then((info) => { - res - .status(200) - .send('Hello ' + info.email) - .end(); - }).catch((error) => { - console.log(error); - - res - .status(200) - .send('Hello None') - .end(); - }); + validate_assertion(assertion) + .then(info => { + res + .status(200) + .send('Hello ' + info.email) + .end(); + }) + .catch(error => { + console.log(error); + + res + .status(200) + .send('Hello None') + .end(); + }); }); +// [END getting_started_auth_front_controller] // Start the server const PORT = process.env.PORT || 8080; @@ -105,4 +115,6 @@ app.listen(PORT, () => { console.log('Press Ctrl+C to quit.'); }); +// [END getting_started_auth_all] + module.exports = app; diff --git a/authenticating-users/package.json b/authenticating-users/package.json index 09a3e43f1..f21cb4662 100644 --- a/authenticating-users/package.json +++ b/authenticating-users/package.json @@ -13,11 +13,9 @@ "node": ">=10.0.0" }, "scripts": { - "deploy": "gcloud app deploy", "start": "node app.js", "system-test": "mocha --exit test/*.test.js", - "test": "npm run system-test", - "e2e-test": "repo-tools test deploy" + "test": "npm run system-test" }, "dependencies": { "express": "^4.17.1", @@ -25,17 +23,7 @@ "jsonwebtoken": "^8.5.1" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.3.0", "mocha": "^6.1.4", "supertest": "^4.0.2" - }, - "cloud-repo-tools": { - "test": { - "app": { - "msg": "Hello, world!" - } - }, - "requiresKeyFile": true, - "requiresProjectId": true } } diff --git a/package.json b/package.json index 1c5f6441f..f9b364e5e 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,8 @@ "private": true, "scripts": { "lint": "eslint '**/*.js'", - "ava": "ava -s -t 30s --tap \"**/*.test.js\"", - "test": "npm run lint && npm run ava" + "mocha": "cd authenticating-users; mocha \"**/*.test.js\" --exit", + "test": "npm run lint && npm run mocha" }, "dependencies": { "@google-cloud/connect-datastore": "^3.0.0", @@ -29,6 +29,7 @@ "express": "^4.16.2", "express-session": "^1.15.6", "express-winston": "^2.5.0", + "jsonwebtoken": "^8.5.1", "lodash": "^4.17.5", "mongodb": "^3.0.2", "multer": "^1.3.0", @@ -43,12 +44,12 @@ "winston": "^2.4.0" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0", "ava": "^0.25.0", "eslint": "^6.0.0", "eslint-config-prettier": "^6.0.0", "eslint-plugin-node": "^9.0.0", "eslint-plugin-prettier": "^3.0.0", + "mocha": "^6.2.0", "prettier": "^1.15.1", "proxyquire": "^2.0.0", "sinon": "^7.0.0",