Skip to content
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

Use async/await in authenticating-users #344

Merged
merged 3 commits into from
Sep 19, 2019
Merged
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
37 changes: 17 additions & 20 deletions authenticating-users/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function certificates() {
}
// [END getting_started_auth_certs]

async function get_metadata(itemName) {
async function getMetadata(itemName) {
const endpoint = 'http://metadata.google.internal';
const path = '/computeMetadata/v1/project/';
const url = endpoint + path + itemName;
Expand All @@ -51,8 +51,8 @@ async function get_metadata(itemName) {
// [START getting_started_auth_metadata]
async function audience() {
if (!aud) {
let project_number = await get_metadata('numeric-project-id');
let project_id = await get_metadata('project-id');
let project_number = await getMetadata('numeric-project-id');
let project_id = await getMetadata('project-id');

aud = '/projects/' + project_number + '/apps/' + project_id;
}
Expand All @@ -62,7 +62,7 @@ async function audience() {
// [END getting_started_auth_metadata]

// [START getting_started_auth_audience]
async function validate_assertion(assertion) {
async function validateAssertion(assertion) {
if (!assertion) {
return {};
}
Expand Down Expand Up @@ -91,24 +91,21 @@ async function validate_assertion(assertion) {
// [END getting_started_auth_audience]

// [START getting_started_auth_front_controller]
app.get('/', (req, res) => {
app.get('/', async (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();
});
let email = 'None';
try {
const info = await validateAssertion(assertion);
email = info.email
} catch (error) {
console.log(error);
}
res
.status(200)
.send(`Hello ${email}`)
.end();
});

// [END getting_started_auth_front_controller]

// Start the server
Expand Down