Skip to content

Instantly share code, notes, and snippets.

@alex3165
Created July 10, 2019 17:25
Show Gist options
  • Save alex3165/72a1c626b5d7ad09e9f365a96f6dc899 to your computer and use it in GitHub Desktop.
Save alex3165/72a1c626b5d7ad09e9f365a96f6dc899 to your computer and use it in GitHub Desktop.
An AWS lambda authorizer that work with google access token
const axios = require("axios");
exports.handler = async event => {
const token = event.authorizationToken;
console.log(`Received token`, token);
let googleAuthRes;
try {
googleAuthRes = await axios.get(
`https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=${token}`
);
} catch (err) {
console.error("Couldn't validate token", err);
return Promise.reject("Unauthorized");
}
return Promise.resolve(
generatePolicy(
googleAuthRes.data.user_id,
"Allow",
event.methodArn,
googleAuthRes.data
)
);
};
const generatePolicy = (principalId, effect, resource, context = {}) => {
const authResponse = { principalId, context };
if (effect && resource) {
authResponse.policyDocument = {
Version: "2012-10-17",
Statement: [
{
Action: "execute-api:Invoke",
Effect: effect,
Resource: resource
}
]
};
}
return authResponse;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment