diff --git a/lemur/auth/views.py b/lemur/auth/views.py index 8f93554e..6161e611 100644 --- a/lemur/auth/views.py +++ b/lemur/auth/views.py @@ -230,5 +230,46 @@ class Ping(Resource): return dict(token=create_token(user)) +class Google(Resource): + + def __init__(self): + self.reqparse = reqparse.RequestParser() + super(Google, self).__init__() + + def post(self): + access_token_url = 'https://accounts.google.com/o/oauth2/token' + people_api_url = 'https://www.googleapis.com/plus/v1/people/me/openIdConnect' + + self.reqparse.add_argument('clientId', type=str, required=True, location='json') + self.reqparse.add_argument('redirectUri', type=str, required=True, location='json') + self.reqparse.add_argument('code', type=str, required=True, location='json') + + args = self.reqparse.parse_args() + + # Step 1. Exchange authorization code for access token + payload = { + 'client_id': args['clientId'], + 'grant_type': 'authorization_code', + 'redirect_uri': args['redirectUri'], + 'code': args['code'], + 'client_secret': current_app.config.get('GOOGLE_SECRET') + } + + r = requests.post(access_token_url, data=payload) + token = r.json() + + # Step 2. Retrieve information about the current user + headers = {'Authorization': 'Bearer {0}'.format(token['access_token'])} + + r = requests.get(people_api_url, headers=headers) + profile = r.json() + + user = user_service.get_by_email(profile['email']) + + if user: + return dict(token=create_token(user)) + + api.add_resource(Login, '/auth/login', endpoint='login') api.add_resource(Ping, '/auth/ping', endpoint='ping') +api.add_resource(Google, '/auth/google', endpoint='google') diff --git a/lemur/static/app/angular/users/user/user.tpl.html b/lemur/static/app/angular/users/user/user.tpl.html index c2e9687b..b19750b1 100644 --- a/lemur/static/app/angular/users/user/user.tpl.html +++ b/lemur/static/app/angular/users/user/user.tpl.html @@ -30,8 +30,7 @@ Password
You must enter an password
+