Last active
October 7, 2017 12:36
-
-
Save behrangsa/49a3aca294ba8324255b3df0e75b6eb9 to your computer and use it in GitHub Desktop.
Stuck implementing Cognito Federated Authentication for a Web App using Lambda, API Gateway, and Twitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.handler = function (event, context, callback) { | |
let body = ` | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Serverless Home</title> | |
<style> | |
.hidden { | |
display: none; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Lambda, API Gateway, and Cognito</h1> | |
<h2>Federated Authentication with Twitter</h2> | |
<div class="hidden" id="login-panel"> | |
<a href="/dev/api/auth/twitter">Login with your Twitter account</a> | |
</div> | |
<div class="hidden" id="logout-panel"> | |
<a href="/dev/api/auth/logout">Logout</a> | |
</div> | |
<div class="hidden" id="profile-panel"> | |
<p> | |
Your email address is: <span id="email-address"></span>. | |
</p> | |
</div> | |
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> | |
<script> | |
function isLoggedIn() { | |
// TODO: Implement | |
return false; | |
} | |
function getEmailAddress() { | |
// TODO: Implement | |
return '???'; | |
} | |
$(document).ready(function() { | |
if (isLoggedIn()) { | |
$("#email-address").text(getEmailAddress()); | |
$("#logout-panel").removeClass('hidden'); | |
$("#profile-panel").removeClass('hidden'); | |
} else { | |
$("#login-panel").removeClass('hidden'); | |
} | |
}); | |
</script> | |
</body> | |
</html> | |
`; | |
callback(null, body); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
let oauth = require('oauth'); | |
let CONSUMER_KEY = 'USE YOUR CONSUMER KEY'; | |
let CONSUMER_SECRET = 'USE YOUR CONSUMER SECRET'; | |
module.exports.handler = function (event, context, callback) { | |
let oa = new oauth.OAuth( | |
'https://api.twitter.com/oauth/request_token', | |
'https://api.twitter.com/oauth/access_token', | |
CONSUMER_KEY, | |
CONSUMER_SECRET, | |
'1.0A', | |
null, | |
'HMAC-SHA1' | |
); | |
oa.getOAuthRequestToken( | |
function (error, oAuthToken, oAuthTokenSecret, results) { | |
if (error) { | |
console.log(`Error: ${JSON.stringify(error)}`); | |
callback(error, null); | |
} else { | |
// Do not log token and token secret in real-world scenarios | |
console.log({ | |
oAuthToken: oAuthToken, | |
oAuthTokenSecret: oAuthTokenSecret, | |
results: results | |
}); | |
let response = { | |
statusCode: 302, | |
headers: { | |
'Location': `https://api.twitter.com/oauth/authenticate?oauth_token=${oAuthToken}` | |
} | |
}; | |
callback(null, response); | |
} | |
} | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service: twitter-auth-service | |
provider: | |
name: aws | |
runtime: nodejs6.10 | |
functions: | |
# Home page | |
home: | |
handler: home.handler | |
events: | |
- http: | |
method: get | |
path: index.html | |
integration: lambda | |
response: | |
headers: | |
Content-Type: "'text/html'" | |
template: $input.path('$') | |
# Redirect user to Twitter | |
obtainRequestToken: | |
handler: obtain-request-token.handler | |
events: | |
- http: | |
path: api/auth/twitter | |
method: get | |
cors: | |
origins: | |
- '*' | |
# Twitter redirects back to here | |
authCallback: | |
handler: auth-callback.handler | |
events: | |
- http: | |
path: api/auth/twitter/callback | |
method: get | |
cors: | |
origins: | |
- '*' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment