From fb1fbfdf9016d874a20ea9ed08700bea24ef79a2 Mon Sep 17 00:00:00 2001 From: sananand007 Date: Fri, 10 May 2019 21:40:27 -0700 Subject: [PATCH 1/7] Add code for OAuth2.0 --- .../default/virtualbox/action_provision | 1 + .../default/virtualbox/action_set_name | 1 + .../machines/default/virtualbox/creator_uid | 1 + .vagrant/machines/default/virtualbox/id | 1 + .../machines/default/virtualbox/index_uuid | 1 + .../machines/default/virtualbox/private_key | 27 ++++ .../default/virtualbox/synced_folders | 1 + .../machines/default/virtualbox/vagrant_cwd | 1 + client_secrets.json | 1 + database_setup.pyc | Bin 0 -> 1915 bytes project.py | 139 +++++++++++++++++- restaurantmenu.db | Bin 0 -> 7168 bytes templates/login.html | 73 +++++++++ templates/logingoogle.html | 77 ++++++++++ templates/loginmod.html | 30 ++++ templates/signin.html | 76 ++++++++++ templates/signout.html | 56 +++++++ 17 files changed, 483 insertions(+), 3 deletions(-) create mode 100644 .vagrant/machines/default/virtualbox/action_provision create mode 100644 .vagrant/machines/default/virtualbox/action_set_name create mode 100644 .vagrant/machines/default/virtualbox/creator_uid create mode 100644 .vagrant/machines/default/virtualbox/id create mode 100644 .vagrant/machines/default/virtualbox/index_uuid create mode 100644 .vagrant/machines/default/virtualbox/private_key create mode 100644 .vagrant/machines/default/virtualbox/synced_folders create mode 100644 .vagrant/machines/default/virtualbox/vagrant_cwd create mode 100644 client_secrets.json create mode 100644 database_setup.pyc create mode 100644 restaurantmenu.db create mode 100644 templates/login.html create mode 100644 templates/logingoogle.html create mode 100644 templates/loginmod.html create mode 100755 templates/signin.html create mode 100755 templates/signout.html diff --git a/.vagrant/machines/default/virtualbox/action_provision b/.vagrant/machines/default/virtualbox/action_provision new file mode 100644 index 00000000..29ae3119 --- /dev/null +++ b/.vagrant/machines/default/virtualbox/action_provision @@ -0,0 +1 @@ +1.5:ae006bd7-a544-4168-ac1a-1c541e7e1949 \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/action_set_name b/.vagrant/machines/default/virtualbox/action_set_name new file mode 100644 index 00000000..8333b559 --- /dev/null +++ b/.vagrant/machines/default/virtualbox/action_set_name @@ -0,0 +1 @@ +1557415821 \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/creator_uid b/.vagrant/machines/default/virtualbox/creator_uid new file mode 100644 index 00000000..e37d32ab --- /dev/null +++ b/.vagrant/machines/default/virtualbox/creator_uid @@ -0,0 +1 @@ +1000 \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/id b/.vagrant/machines/default/virtualbox/id new file mode 100644 index 00000000..b0a29957 --- /dev/null +++ b/.vagrant/machines/default/virtualbox/id @@ -0,0 +1 @@ +ae006bd7-a544-4168-ac1a-1c541e7e1949 \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/index_uuid b/.vagrant/machines/default/virtualbox/index_uuid new file mode 100644 index 00000000..d15112f4 --- /dev/null +++ b/.vagrant/machines/default/virtualbox/index_uuid @@ -0,0 +1 @@ +7f6c838ec1a34b6d8ea65476b5dbbcfa \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/private_key b/.vagrant/machines/default/virtualbox/private_key new file mode 100644 index 00000000..303d70c9 --- /dev/null +++ b/.vagrant/machines/default/virtualbox/private_key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpgIBAAKCAQEA28mPxr50HdJrw0+IMt9dqa1LXYobWhBE9q/36Akfhq4wzYJF +OlRusCsgck46ENmSnL3YjveDLDfGTS5jZud9+fNV39MO1iT+GRvOM0z2kLj7B5yR +i495mEJ4hSCmUkay76jFYmP51P/Fds17WmUCxs6AtKhKHWONFLBkgf3JSk/iSIra +YY2Wx2uTlMmXBv6YH4Xe+IVAo4NnULv5qk5KAOPOT93RLcJtpJ8diQnpIcrxh7Um +f3V1jmw7/CvbRXBQ3kzXo6ccoJINRadqfygcN0MYR/sX4uBtsyHv/udv3ThNnUiP +UpOigZaRxF+f2FSMUzAHUA0XQDkR2o8TAUC3QQIDAQABAoIBAQCC3Z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GBANxdkTHzGGKyDVabbbQQEOR8fSBdhbGrtTNEwtnAHT1xYCEg5h4f +HBR8Shq6Pvny/EpXyYp6d9zXFBckdtbDK9ZOVVrSIICofTluhXE/FWrQmiyvx19i +9UbMFaDcrjDLv7lKoiRmVYrkw3nXyQLmYItrzleZen+nH10U/CLlYv0B +-----END RSA PRIVATE KEY----- diff --git a/.vagrant/machines/default/virtualbox/synced_folders b/.vagrant/machines/default/virtualbox/synced_folders new file mode 100644 index 00000000..09146b7f --- /dev/null +++ b/.vagrant/machines/default/virtualbox/synced_folders @@ -0,0 +1 @@ +{"virtualbox":{"/vagrant":{"guestpath":"/vagrant","hostpath":"/home/sandeeppanku/Public/Code/FrontEnd/OAuth2.0","disabled":false,"__vagrantfile":true}}} \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/vagrant_cwd b/.vagrant/machines/default/virtualbox/vagrant_cwd new file mode 100644 index 00000000..a6ed44dd --- /dev/null +++ b/.vagrant/machines/default/virtualbox/vagrant_cwd @@ -0,0 +1 @@ +/home/sandeeppanku/Public/Code/FrontEnd/OAuth2.0 \ No newline at end of file diff --git a/client_secrets.json b/client_secrets.json new file mode 100644 index 00000000..942c3d9b --- /dev/null +++ b/client_secrets.json @@ -0,0 +1 @@ +{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"5SQMMFGghlMnY8UBGszwvx_N","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":"13140951618-15nik769cellkubaqnjk5facdib2dh4d@developer.gserviceaccount.com","redirect_uris":["https://localhost:5000/callback","http://localhost:5000/callback"],"client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/13140951618-15nik769cellkubaqnjk5facdib2dh4d@developer.gserviceaccount.com","client_id":"940158863114-ujf4runphi88nehhmga3c5f4g80h9cai.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","javascript_origins":["http://localhost","https://udacity-oauth.herokuapp.com","http://udacity-oauth.herokuapp.com","http://localhost:5000"]}} \ No newline at end of file diff --git a/database_setup.pyc b/database_setup.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4da65f5d500763d0e32c511725d5622bc6a875e2 GIT binary patch literal 1915 zcmbVMO^+Kz5UrW9y|(uwn@zGT;bMUl&B+#*JwynhL;RF7GIrN=*K~J%y;sQ}hvR!+zy7(P&6CLcD;fTkh*JI$ zRYY|}SwwYASxj|8SweM>vK~d)5mVKtdO+ErTPIZQQ9Yz=*sXhd!-%pGMLeMAMA?|i zKJl2YA}R;;f}s2<+oy8xjdr-z9uSYH9MN;};Sis%kLdaa@gX!(Ii`zrUwn0c$&>bE z76u|7E~-`Cgt1H?En0T-=38EeJy||(0?)bCT^FHs%{+~8>R3d{MP*wX+%wKEt>^B` zfqZGX67{0-Pu#N89*VYT*#^$JnY)J7m|-96sB*uXXQ z_s4d{>YhR10U3T|E6?h;-n?uVOF_=s!1m7V_wn5TdP5l=Vh|1DljtngTfgIG^*C@{ z$OnD4fO#p)cLE0R5d8cLHg5x_gH{hflD`mc12!B{&HfwBc8?HE;U~-zQevC%{fJn& zcC1Hk5}$ygFNCjh=Plq6(P&+2i6VCrhWkNO2E;R{m!i;VD-@#%2%$4!P#F1t6mX^GBEg zcm_l=AB#|I8-$w5crRQloIaDy9#?g69t)%e)VF1AZa|c z-l$(&myGtee`Y^-rO`NmOM~(lwew(_I!OE{`1Z6CY}}vC zW;*}?G1Kz$`xK!mS!I{4iYHvJyCC7W^0Q+ky2EbElz$D=w;HwqZGv`%?KM;BMqQXd zhqC;EbQC3{f!|uzP5VX?q+?n19d&2Z-ECFvN~yE|>r=Sti>~i{i5NU0{4_eoFCN9G KN2hlWlD`26A92tC literal 0 HcmV?d00001 diff --git a/project.py b/project.py index 0c329b4e..ef107813 100644 --- a/project.py +++ b/project.py @@ -1,10 +1,22 @@ -from flask import Flask, render_template, request, redirect,jsonify, url_for, flash -app = Flask(__name__) - +from flask import Flask, render_template, request, redirect, jsonify, url_for, flash from sqlalchemy import create_engine, asc from sqlalchemy.orm import sessionmaker from database_setup import Base, Restaurant, MenuItem +from flask import session as login_session +import random +import string +from oauth2client.client import flow_from_clientsecrets +from oauth2client.client import FlowExchangeError +import httplib2 +import json +from flask import make_response +import requests +app = Flask(__name__) + +CLIENT_ID = json.loads( + open('client_secrets.json', 'r').read())['web']['client_id'] +APPLICATION_NAME = "Restaurant Menu Application" #Connect to Database and create database session engine = create_engine('sqlite:///restaurantmenu.db') @@ -14,6 +26,127 @@ session = DBSession() +@app.route('/login') +def showLogin(): + state = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in xrange(32)) + login_session['state'] = state + # Render the login template + return render_template('logingoogle.html', state = state, GOOGLE_CLIENT_ID=CLIENT_ID) + +@app.route('/gconnect', methods=['POST']) +def gconnect(): + if request.args.get('state') != login_session['state']: + response = make_response(json.dumps('Invalid state parameter.'), 401) + response.headers['Content-Type'] = 'application/json' + return response + # Obtain authorization code + code = request.data + + try: + # Upgrade the authorization code into a credentials object + oauth_flow = flow_from_clientsecrets('client_secrets.json', scope='') + oauth_flow.redirect_uri = 'postmessage' + credentials = oauth_flow.step2_exchange(code) + except FlowExchangeError: + response = make_response( + json.dumps('Failed to upgrade the authorization code.'), 401) + response.headers['Content-Type'] = 'application/json' + return response + + # Check that the access token is valid. + access_token = credentials.access_token + url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s' + % access_token) + h = httplib2.Http() + result = json.loads(h.request(url, 'GET')[1]) + # If there was an error in the access token info, abort. + if result.get('error') is not None: + response = make_response(json.dumps(result.get('error')), 500) + response.headers['Content-Type'] = 'application/json' + return response + + # Verify that the access token is used for the intended user. + gplus_id = credentials.id_token['sub'] + if result['user_id'] != gplus_id: + response = make_response( + json.dumps("Token's user ID doesn't match given user ID."), 401) + response.headers['Content-Type'] = 'application/json' + return response + + # Verify that the access token is valid for this app. + if result['issued_to'] != CLIENT_ID: + response = make_response( + json.dumps("Token's client ID does not match app's."), 401) + print "Token's client ID does not match app's." + response.headers['Content-Type'] = 'application/json' + return response + + stored_access_token = login_session.get('access_token') + stored_gplus_id = login_session.get('gplus_id') + if stored_access_token is not None and gplus_id == stored_gplus_id: + response = make_response(json.dumps('Current user is already connected.'), + 200) + response.headers['Content-Type'] = 'application/json' + return response + + # Store the access token in the session for later use. + login_session['access_token'] = credentials.access_token + login_session['gplus_id'] = gplus_id + + # Get user info + userinfo_url = "https://www.googleapis.com/oauth2/v1/userinfo" + params = {'access_token': credentials.access_token, 'alt': 'json'} + answer = requests.get(userinfo_url, params=params) + + data = answer.json() + login_session['logged_in'] = True + login_session['provider'] = 'google' + login_session['username'] = data['name'] + login_session['picture'] = data['picture'] + login_session['email'] = data['email'] + + output = '' + output += '

Welcome, ' + output += login_session['username'] + output += '!

' + output += ' ' + flash("you are now logged in as %s" % login_session['username']) + print "done!" + return output + # DISCONNECT - Revoke a current user's token and reset their login_session + +@app.route('/gdisconnect') +def gdisconnect(): + access_token = login_session.get('access_token') + if access_token is None: + print 'Access Token is None' + response = make_response(json.dumps('Current user not connected.'), 401) + response.headers['Content-Type'] = 'application/json' + return response + print 'In gdisconnect access token is %s', access_token + print 'User name is: ' + print login_session['username'] + url = 'https://accounts.google.com/o/oauth2/revoke?token=%s' % login_session['access_token'] + h = httplib2.Http() + result = h.request(url, 'GET')[0] + print 'result is ' + print result + if result['status'] == '200': + del login_session['access_token'] + del login_session['gplus_id'] + del login_session['username'] + del login_session['email'] + del login_session['picture'] + response = make_response(json.dumps('Successfully disconnected.'), 200) + response.headers['Content-Type'] = 'application/json' + return response + else: + response = make_response(json.dumps('Failed to revoke token for given user.', 400)) + response.headers['Content-Type'] = 'application/json' + return response + #JSON APIs to view Restaurant Information @app.route('/restaurant//menu/JSON') def restaurantMenuJSON(restaurant_id): diff --git a/restaurantmenu.db b/restaurantmenu.db new file mode 100644 index 0000000000000000000000000000000000000000..4b8eaac73352906befcb2a75a4d0aa1e9368972f GIT binary patch literal 7168 zcmeHM&u<&Y6<%^Bk+Q40j+4-C>S$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*T}0RNIcmv~+sM{_M%QgL_%5?eei|_}ms69I9J>%>m9sT#QIX>NqlFvj z;Iq!Z^gUJ@WyfyWlfYY(qdyXWaxW!5Y9fcK>@7eCFsib93GcZt{At~8+`csPFF|nF ADgXcg literal 0 HcmV?d00001 diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 00000000..4e9bcd9d --- /dev/null +++ b/templates/login.html @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+ + + + + + + + + + \ No newline at end of file diff --git a/templates/logingoogle.html b/templates/logingoogle.html new file mode 100644 index 00000000..a5350ddc --- /dev/null +++ b/templates/logingoogle.html @@ -0,0 +1,77 @@ + + + + + + + + + +
+ + + + \ No newline at end of file diff --git a/templates/loginmod.html b/templates/loginmod.html new file mode 100644 index 00000000..dbd68c27 --- /dev/null +++ b/templates/loginmod.html @@ -0,0 +1,30 @@ + + + +
+ + + + + +
+ + + + Sign in with google + + + + + diff --git a/templates/signin.html b/templates/signin.html new file mode 100755 index 00000000..aca2acd6 --- /dev/null +++ b/templates/signin.html @@ -0,0 +1,76 @@ + + + + + + + + + + + ` + + + + + + +
+ + + +

Sign with your Google Account

+
+ + + + + + + + + + +
+ + + \ No newline at end of file diff --git a/templates/signout.html b/templates/signout.html new file mode 100755 index 00000000..f067f2e2 --- /dev/null +++ b/templates/signout.html @@ -0,0 +1,56 @@ + + + + + + + + + + + ` + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + \ No newline at end of file From d7415f859145403183aef623e7fcb9613f846be9 Mon Sep 17 00:00:00 2001 From: sananand007 Date: Sat, 11 May 2019 12:03:52 -0700 Subject: [PATCH 2/7] Correctly implemented OAuth with JS --- project.py | 49 ++++++++--- templates/header.html | 4 +- templates/logingoogle.html | 175 ++++++++++++++++++++++++------------- templates/signout.html | 31 +++++-- 4 files changed, 177 insertions(+), 82 deletions(-) diff --git a/project.py b/project.py index ef107813..994aeb4a 100644 --- a/project.py +++ b/project.py @@ -117,35 +117,49 @@ def gconnect(): return output # DISCONNECT - Revoke a current user's token and reset their login_session -@app.route('/gdisconnect') -def gdisconnect(): - access_token = login_session.get('access_token') +@app.route('/sign-out') +def signOut(): + access_token = None + if 'access_token' in login_session: + access_token = login_session['access_token'] + print("access token", access_token) if access_token is None: print 'Access Token is None' - response = make_response(json.dumps('Current user not connected.'), 401) + response = make_response(json.dumps( + 'Current user not connected.'), 401) response.headers['Content-Type'] = 'application/json' return response print 'In gdisconnect access token is %s', access_token - print 'User name is: ' - print login_session['username'] - url = 'https://accounts.google.com/o/oauth2/revoke?token=%s' % login_session['access_token'] + print 'Login session: ' + print login_session.keys() + url = 'https://accounts.google.com/o/oauth2/revoke?token={}'.format(access_token) h = httplib2.Http() result = h.request(url, 'GET')[0] print 'result is ' print result if result['status'] == '200': + del login_session['logged_in'] del login_session['access_token'] del login_session['gplus_id'] del login_session['username'] del login_session['email'] del login_session['picture'] - response = make_response(json.dumps('Successfully disconnected.'), 200) - response.headers['Content-Type'] = 'application/json' - return response + del login_session['provider'] + flash("You have been logged out") + return render_template('signout.html', GOOGLE_CLIENT_ID=CLIENT_ID) else: response = make_response(json.dumps('Failed to revoke token for given user.', 400)) response.headers['Content-Type'] = 'application/json' - return response + del login_session['logged_in'] + del login_session['access_token'] + del login_session['gplus_id'] + del login_session['username'] + del login_session['email'] + del login_session['picture'] + del login_session['provider'] + flash("You have been logged out") + return render_template('signout.html', GOOGLE_CLIENT_ID=CLIENT_ID) + #return response #JSON APIs to view Restaurant Information @app.route('/restaurant//menu/JSON') @@ -176,6 +190,8 @@ def showRestaurants(): #Create a new restaurant @app.route('/restaurant/new/', methods=['GET','POST']) def newRestaurant(): + if 'username' not in login_session: + return redirect('/login') if request.method == 'POST': newRestaurant = Restaurant(name = request.form['name']) session.add(newRestaurant) @@ -188,6 +204,8 @@ def newRestaurant(): #Edit a restaurant @app.route('/restaurant//edit/', methods = ['GET', 'POST']) def editRestaurant(restaurant_id): + if 'username' not in login_session: + return redirect('/login') editedRestaurant = session.query(Restaurant).filter_by(id = restaurant_id).one() if request.method == 'POST': if request.form['name']: @@ -201,6 +219,8 @@ def editRestaurant(restaurant_id): #Delete a restaurant @app.route('/restaurant//delete/', methods = ['GET','POST']) def deleteRestaurant(restaurant_id): + if 'username' not in login_session: + return redirect('/login') restaurantToDelete = session.query(Restaurant).filter_by(id = restaurant_id).one() if request.method == 'POST': session.delete(restaurantToDelete) @@ -223,6 +243,8 @@ def showMenu(restaurant_id): #Create a new menu item @app.route('/restaurant//menu/new/',methods=['GET','POST']) def newMenuItem(restaurant_id): + if 'username' not in login_session: + return redirect('/login') restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one() if request.method == 'POST': newItem = MenuItem(name = request.form['name'], description = request.form['description'], price = request.form['price'], course = request.form['course'], restaurant_id = restaurant_id) @@ -236,7 +258,8 @@ def newMenuItem(restaurant_id): #Edit a menu item @app.route('/restaurant//menu//edit', methods=['GET','POST']) def editMenuItem(restaurant_id, menu_id): - + if 'username' not in login_session: + return redirect('/login') editedItem = session.query(MenuItem).filter_by(id = menu_id).one() restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one() if request.method == 'POST': @@ -259,6 +282,8 @@ def editMenuItem(restaurant_id, menu_id): #Delete a menu item @app.route('/restaurant//menu//delete', methods = ['GET','POST']) def deleteMenuItem(restaurant_id,menu_id): + if 'username' not in login_session: + return redirect('/login') restaurant = session.query(Restaurant).filter_by(id = restaurant_id).one() itemToDelete = session.query(MenuItem).filter_by(id = menu_id).one() if request.method == 'POST': diff --git a/templates/header.html b/templates/header.html index e801252a..4cd3f49d 100644 --- a/templates/header.html +++ b/templates/header.html @@ -3,9 +3,9 @@ Show All Restaurants + sign Out
-

Login Link will go Here

- + Login
\ No newline at end of file diff --git a/templates/logingoogle.html b/templates/logingoogle.html index a5350ddc..6ddf2dac 100644 --- a/templates/logingoogle.html +++ b/templates/logingoogle.html @@ -4,74 +4,123 @@ - + + + + -
- - + + + \ No newline at end of file diff --git a/templates/signout.html b/templates/signout.html index f067f2e2..3177b7da 100755 --- a/templates/signout.html +++ b/templates/signout.html @@ -5,13 +5,13 @@ - + - ` + + content="{{ GOOGLE_CLIENT_ID }}"> @@ -22,14 +22,35 @@ - - - - - - - - - - - - - -
- -
- -
- - - - - - - - - - \ No newline at end of file diff --git a/templates/loginmod.html b/templates/loginmod.html deleted file mode 100644 index dbd68c27..00000000 --- a/templates/loginmod.html +++ /dev/null @@ -1,30 +0,0 @@ - - - -
- - - - - -
- - - - Sign in with google - - - - - diff --git a/templates/signin.html b/templates/signin.html deleted file mode 100755 index aca2acd6..00000000 --- a/templates/signin.html +++ /dev/null @@ -1,76 +0,0 @@ - - - - - - - - - - - ` - - - - - - -
- - - -

Sign with your Google Account

-
- - - - - - - - - - -
- - - \ No newline at end of file From 3ad505471038171b608d44f895e746803ff3dda3 Mon Sep 17 00:00:00 2001 From: sananand007 Date: Sat, 11 May 2019 13:37:58 -0700 Subject: [PATCH 5/7] gitignore mod1 --- .gitignore | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7a9db01d..02fe7b72 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,12 @@ *.exe *.o *.so -*.vagrant + +# ignore all vagrant files +**/.vagrant + +# ignore all .json files +*.json # Packages # ############ @@ -26,4 +31,3 @@ *.log *.sql *.sqlite -*.json \ No newline at end of file From d6f746e636066ae0c8d99772a67f9157c9da5c9a Mon Sep 17 00:00:00 2001 From: sananand007 Date: Sat, 11 May 2019 13:51:55 -0700 Subject: [PATCH 6/7] Deleted vagrant files --- .../default/virtualbox/action_provision | 1 - .../default/virtualbox/action_set_name | 1 - .../machines/default/virtualbox/creator_uid | 1 - .vagrant/machines/default/virtualbox/id | 1 - .../machines/default/virtualbox/index_uuid | 1 - .../machines/default/virtualbox/private_key | 27 ------------------- .../default/virtualbox/synced_folders | 1 - .../machines/default/virtualbox/vagrant_cwd | 1 - 8 files changed, 34 deletions(-) delete mode 100644 .vagrant/machines/default/virtualbox/action_provision delete mode 100644 .vagrant/machines/default/virtualbox/action_set_name delete mode 100644 .vagrant/machines/default/virtualbox/creator_uid delete mode 100644 .vagrant/machines/default/virtualbox/id delete mode 100644 .vagrant/machines/default/virtualbox/index_uuid delete mode 100644 .vagrant/machines/default/virtualbox/private_key delete mode 100644 .vagrant/machines/default/virtualbox/synced_folders delete mode 100644 .vagrant/machines/default/virtualbox/vagrant_cwd diff --git a/.vagrant/machines/default/virtualbox/action_provision b/.vagrant/machines/default/virtualbox/action_provision deleted file mode 100644 index 29ae3119..00000000 --- a/.vagrant/machines/default/virtualbox/action_provision +++ /dev/null @@ -1 +0,0 @@ -1.5:ae006bd7-a544-4168-ac1a-1c541e7e1949 \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/action_set_name b/.vagrant/machines/default/virtualbox/action_set_name deleted file mode 100644 index 8333b559..00000000 --- a/.vagrant/machines/default/virtualbox/action_set_name +++ /dev/null @@ -1 +0,0 @@ -1557415821 \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/creator_uid b/.vagrant/machines/default/virtualbox/creator_uid deleted file mode 100644 index e37d32ab..00000000 --- a/.vagrant/machines/default/virtualbox/creator_uid +++ /dev/null @@ -1 +0,0 @@ -1000 \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/id b/.vagrant/machines/default/virtualbox/id deleted file mode 100644 index b0a29957..00000000 --- a/.vagrant/machines/default/virtualbox/id +++ /dev/null @@ -1 +0,0 @@ -ae006bd7-a544-4168-ac1a-1c541e7e1949 \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/index_uuid b/.vagrant/machines/default/virtualbox/index_uuid deleted file mode 100644 index d15112f4..00000000 --- a/.vagrant/machines/default/virtualbox/index_uuid +++ /dev/null @@ -1 +0,0 @@ -7f6c838ec1a34b6d8ea65476b5dbbcfa \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/private_key b/.vagrant/machines/default/virtualbox/private_key deleted file mode 100644 index 303d70c9..00000000 --- a/.vagrant/machines/default/virtualbox/private_key +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEpgIBAAKCAQEA28mPxr50HdJrw0+IMt9dqa1LXYobWhBE9q/36Akfhq4wzYJF -OlRusCsgck46ENmSnL3YjveDLDfGTS5jZud9+fNV39MO1iT+GRvOM0z2kLj7B5yR -i495mEJ4hSCmUkay76jFYmP51P/Fds17WmUCxs6AtKhKHWONFLBkgf3JSk/iSIra -YY2Wx2uTlMmXBv6YH4Xe+IVAo4NnULv5qk5KAOPOT93RLcJtpJ8diQnpIcrxh7Um -f3V1jmw7/CvbRXBQ3kzXo6ccoJINRadqfygcN0MYR/sX4uBtsyHv/udv3ThNnUiP -UpOigZaRxF+f2FSMUzAHUA0XQDkR2o8TAUC3QQIDAQABAoIBAQCC3ZscLV6aOyYr -HvnrwI/WbfbH9Wmf0LwfXltv766z1Jf+ZaiuuINHpgYlyWpNih7/8RI23i1ecL9E -SXCqXv/lifwBz3uwCkq5zJKUfSj6twnRWGpY/M7b2AhGzE/8AVid1pHgqcKiDsEW -ofbmtepXUbHU37dxlEr2nKjbta7a7WcXyHjEJXvdqO9ADVx3GwhlijPH6/eaJKMR -cp9ltRo/59l4oW/mnQlyHkjGNAXEJgQ5S7dBXOkgkKsLC3zJBvA6AcCztxQ8VfoC -sIhWbmLA8sQxnmEr6VYSaXKvklxqbtEJD0nsbb4hoaFhLHQuZuC+SDNy4AnZ06dg -fl1BlAABAoGBAPXyJoPwED6tZLWiLg3HKQdnHpgEqFYPlOWYwdyt13tIuah/6Z2X -+hM/eVTr+dfXoUkAlc9M6vu2LjWr/d+H+YBlJxx5Z49LpUw1/1kxWp1E/4mXa1Tn -R2/6Bm09J3xmun8tUXJYRtER7s0wxoWynLwmSf3r5soaB14tWHHI1olBAoGBAOTF -qMJRifCmVd/etmC+C+o6CHUjEsEMvxfivV8ZiVj0HOmz6KbWgFvexB6hXHUBtgkM -iP+zaZDV9epQts7nJpXlUL/kWuwiJVrilT7hF5TPWkMSbQaUS1DpHqBWai4vtbD+ -vmYMocxB7k/ShsoroOjtPk1WN/vDAQ4eCnd/IK4BAoGBAKVC2liLhg7nqiVWXcXK -Tqp3wxn4/zgc7js84v8rHQDtRDuR/+NyTIqEez3x9obvkKOiLggGslsC9pgAAu4h -yyhYT+iVN14VQXQtUHCZdj75LxBAW9HIOrtmcM0Ua6TuzmBIjlLcOHGhsCnWnX8X -HrRashtaWVDyHoNMywJiJq5BAoGBANXRXAKKfTdb76U2nhTba3120hLRPiUKycwE -UuSAlJt8RU20pfzRPNUg5wKN5EFD4SSh/6WlxUPmhlqDJ+lHqAuvcYJ4ZvaOj4Vb -gwxrC0Q9bOqFXTsp/R7ZU5OeUM1sMBAGeGQe6FZlRNx8MxjgIBu0KWfU7qwzPVg5 -gaTgP+YBAoGBANxdkTHzGGKyDVabbbQQEOR8fSBdhbGrtTNEwtnAHT1xYCEg5h4f -HBR8Shq6Pvny/EpXyYp6d9zXFBckdtbDK9ZOVVrSIICofTluhXE/FWrQmiyvx19i -9UbMFaDcrjDLv7lKoiRmVYrkw3nXyQLmYItrzleZen+nH10U/CLlYv0B ------END RSA PRIVATE KEY----- diff --git a/.vagrant/machines/default/virtualbox/synced_folders b/.vagrant/machines/default/virtualbox/synced_folders deleted file mode 100644 index 09146b7f..00000000 --- a/.vagrant/machines/default/virtualbox/synced_folders +++ /dev/null @@ -1 +0,0 @@ -{"virtualbox":{"/vagrant":{"guestpath":"/vagrant","hostpath":"/home/sandeeppanku/Public/Code/FrontEnd/OAuth2.0","disabled":false,"__vagrantfile":true}}} \ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/vagrant_cwd b/.vagrant/machines/default/virtualbox/vagrant_cwd deleted file mode 100644 index a6ed44dd..00000000 --- a/.vagrant/machines/default/virtualbox/vagrant_cwd +++ /dev/null @@ -1 +0,0 @@ -/home/sandeeppanku/Public/Code/FrontEnd/OAuth2.0 \ No newline at end of file From 39eac015ec31a9575ed8fdf8e41c4628b7e2be07 Mon Sep 17 00:00:00 2001 From: sananand007 Date: Sat, 11 May 2019 14:32:52 -0700 Subject: [PATCH 7/7] Deleted client secrets --- client_secrets.json | 1 - 1 file changed, 1 deletion(-) delete mode 100644 client_secrets.json diff --git a/client_secrets.json b/client_secrets.json deleted file mode 100644 index 942c3d9b..00000000 --- a/client_secrets.json +++ /dev/null @@ -1 +0,0 @@ -{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"5SQMMFGghlMnY8UBGszwvx_N","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":"13140951618-15nik769cellkubaqnjk5facdib2dh4d@developer.gserviceaccount.com","redirect_uris":["https://localhost:5000/callback","http://localhost:5000/callback"],"client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/13140951618-15nik769cellkubaqnjk5facdib2dh4d@developer.gserviceaccount.com","client_id":"940158863114-ujf4runphi88nehhmga3c5f4g80h9cai.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","javascript_origins":["http://localhost","https://udacity-oauth.herokuapp.com","http://udacity-oauth.herokuapp.com","http://localhost:5000"]}} \ No newline at end of file