Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. function doPTCLogin() {
  2. if (!checkParams()){
  3. return;
  4. }
  5. toggleLogin(true);
  6. jQuery('#ptc_errors').html('');
  7. var username = jQuery('#ptc_username').val(),
  8. password = jQuery('#ptc_password').val();
  9.  
  10. // Reset cookie jar
  11. ptcJar = request.jar();
  12.  
  13. // Get Login session from SSO servers
  14. ptcReq.get('https://sso.pokemon.com/sso/login?service=https%3A%2F%2Fsso.pokemon.com%2Fsso%2Foauth2.0%2FcallbackAuthorize',
  15. function(error, response, body) {
  16. if (!error && response.statusCode == 200) {
  17. doPTCLoginStep2(username, password, JSON.parse(body));
  18. } else {
  19. console.log(error);
  20. toggleLogin(false);
  21. alert('Oops! Something went wrong and we couldn\'t ' +
  22. 'log you in. Please try again. Code 6.');
  23. }
  24. });
  25.  
  26. return false;
  27. }
  28.  
  29. function doPTCLoginStep2(user, pass, session) {
  30. var loginData = {
  31. 'lt': session.lt,
  32. 'execution': session.execution,
  33. '_eventId': 'submit',
  34. 'username': user,
  35. 'password': pass
  36. };
  37.  
  38. ptcReq.post(
  39. 'https://sso.pokemon.com/sso/login?service=https%3A%2F%2Fsso.pokemon.com%2Fsso%2Foauth2.0%2FcallbackAuthorize',
  40. {form: loginData},
  41. function (error, response, body) {
  42. if (!error && response.statusCode == 302) {
  43. var rawRedirect = response.headers.location;
  44. handlePokemonCallback(rawRedirect);
  45. } else {
  46. toggleLogin(false);
  47. var errors = null;
  48. try {
  49. errors = JSON.parse(body).errors;
  50. errors = errors.join(' ');
  51. } catch(e) {}
  52. if (errors) {
  53. jQuery('#ptc_errors').html(errors);
  54. } else {
  55. alert('Oops! Something went wrong and we couldn\'t ' +
  56. 'log you in. Please try again. Code 7.');
  57. }
  58. }
  59. }
  60. );
  61. }
  62.  
  63. function handlePokemonCallback(newUrl) {
  64.  
  65. var parsedUrl = url.parse(newUrl, true);
  66. if (parsedUrl.hostname != 'sso.pokemon.com') {
  67. toggleLogin(false);
  68. alert('Oops! Something went wrong and we couldn\'t ' +
  69. 'log you in. Please try again. Code 8.');
  70. return;
  71. }
  72.  
  73. var raw_code = parsedUrl.query.ticket;
  74. var code = (raw_code && raw_code.length > 1) ? raw_code : null;
  75.  
  76. // If there is a code, proceed to get token from oauth
  77. if (code) {
  78.  
  79. request.post(
  80. 'https://sso.pokemon.com/sso/oauth2.0/accessToken',
  81. { form: {
  82. 'client_id': 'mobile-app_pokemon-go',
  83. 'redirect_uri': 'https://www.nianticlabs.com/pokemongo/error',
  84. 'client_secret': 'w8ScCUXJQc6kXKw8FiOhd8Fixzht18Dq3PEVkUCP5ZPxtgyWsbTvWHFLm2wNY0JR',
  85. 'grant_type': 'refresh_token',
  86. 'code': code,
  87. } },
  88. function (error, response, body) {
  89. if (!error && response.statusCode == 200) {
  90. console.log(querystring.parse(body));
  91. completeLogin('ptc', querystring.parse(body).access_token);
  92. } else {
  93. toggleLogin(false);
  94. alert('Oops! Something went wrong and we couldn\'t ' +
  95. 'log you in. Please try again. Code 3.');
  96. }
  97. }
  98. );
  99.  
  100. } else {
  101. toggleLogin(false);
  102. alert('Oops! Something went wrong and we couldn\'t ' +
  103. 'log you in. Please try again. Code 2.');
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement