Skip to content

Add region tags for authentication-users sample #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions authenticating-users/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
rules:
require-atomic-updates: off
46 changes: 29 additions & 17 deletions authenticating-users/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

'use strict';

// [START getting_started_auth_all]
const express = require('express');
const got = require('got');
const jwt = require('jsonwebtoken');
Expand All @@ -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');
Expand All @@ -33,18 +35,20 @@ async function certificates() {

return certs;
}
// [END getting_started_auth_certs]

async function get_metadata(itemName) {
const endpoint = 'http://metadata.google.internal';
const path = '/computeMetadata/v1/project/';
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');
Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -105,4 +115,6 @@ app.listen(PORT, () => {
console.log('Press Ctrl+C to quit.');
});

// [END getting_started_auth_all]

module.exports = app;
14 changes: 1 addition & 13 deletions authenticating-users/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,17 @@
"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",
"got": "^9.6.0",
"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
}
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down