LwGoogleLambdaFunction.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. var http = require("https");
  9. var googleClientID = "{{googleClientID}}";
  10. var googleClientSecret = "{{googleClientSecret}}";
  11. var googleRedirectUrl = "{{googleRedirectUrl}}";
  12. var getGoogleRefreshToken = function(code, callback)
  13. {
  14. var options = {
  15. hostname: 'accounts.google.com',
  16. path: '/o/oauth2/token',
  17. port: 443,
  18. method : "POST",
  19. headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
  20. };
  21. var body = "grant_type=authorization_code&";
  22. body += "client_id=" + googleClientID + "&";
  23. body += "redirect_uri=" + googleRedirectUrl + "&";
  24. body += "client_secret=" + googleClientSecret + "&";
  25. body += "code=" + code;
  26. console.log(body);
  27. var req = http.request(options, function(res) {
  28. res.setEncoding('utf8');
  29. var store = "";
  30. res.on('data', function (chunk) {
  31. console.log(chunk);
  32. store += chunk;
  33. });
  34. res.on('end', function() {
  35. var response = JSON.parse(store);
  36. var data = {
  37. access_token : response.access_token,
  38. expires_in : response.expires_in,
  39. refresh_token : response.refresh_token
  40. };
  41. console.log(data);
  42. callback(null, data);
  43. });
  44. req.on('error', function(e) {
  45. console.log('problem with request: ' + e.message);
  46. callback(e);
  47. });
  48. });
  49. req.write(body);
  50. req.end();
  51. };
  52. var refreshAccessToken = function(refreshToken, callback)
  53. {
  54. var options = {
  55. hostname: 'accounts.google.com',
  56. path: '/o/oauth2/token',
  57. port: 443,
  58. method : "POST",
  59. headers : {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
  60. };
  61. var body = "grant_type=refresh_token&";
  62. body += "refresh_token=" + refreshToken + "&";
  63. body += "client_id=" + googleClientID + "&";
  64. body += "client_secret=" + googleClientSecret;
  65. console.log(body);
  66. var req = http.request(options, function(res) {
  67. res.setEncoding('utf8');
  68. var store = "";
  69. res.on('data', function (chunk) {
  70. console.log(chunk);
  71. store += chunk;
  72. });
  73. res.on('end', function() {
  74. var response = JSON.parse(store);
  75. var data = {
  76. access_token : response.access_token,
  77. expires_in : response.expires_in
  78. };
  79. console.log(data);
  80. callback(null, data);
  81. });
  82. req.on('error', function(e) {
  83. console.log('problem with request: ' + e.message);
  84. callback(e);
  85. });
  86. });
  87. req.write(body);
  88. req.end();
  89. };
  90. exports.{{handlerName}} = function(event, context) {
  91. if(typeof event.code != 'undefined') {
  92. var code = event.code;
  93. console.log(code);
  94. getGoogleRefreshToken(code, function(err, data)
  95. {
  96. if(err)
  97. {
  98. context.fail(err);
  99. }
  100. else
  101. {
  102. context.succeed (data);
  103. }
  104. });
  105. }
  106. else if(typeof event.refresh_token != 'undefined') {
  107. var refresh_token = event.refresh_token;
  108. console.log(refresh_token);
  109. refreshAccessToken(refresh_token, function(err, data)
  110. {
  111. if(err)
  112. {
  113. context.fail(err);
  114. }
  115. else
  116. {
  117. context.succeed (data);
  118. }
  119. });
  120. }
  121. };