2021-05-07 02:37:30 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2021 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License")
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from google.oauth2 import id_token
|
|
|
|
from google.auth.transport import requests
|
|
|
|
from flask import session
|
|
|
|
|
|
|
|
from framework import basehandlers
|
2022-03-10 23:22:19 +03:00
|
|
|
from framework import xsrf
|
2021-05-07 03:29:03 +03:00
|
|
|
import settings
|
2021-05-07 02:37:30 +03:00
|
|
|
|
2022-03-10 23:22:19 +03:00
|
|
|
|
2021-05-07 02:37:30 +03:00
|
|
|
class LoginAPI(basehandlers.APIHandler):
|
2022-03-11 22:52:54 +03:00
|
|
|
"""Create a session using the credential generated by Google Sign-In."""
|
2021-05-07 02:37:30 +03:00
|
|
|
|
|
|
|
def do_post(self):
|
2022-03-11 22:52:54 +03:00
|
|
|
token = self.get_param('credential')
|
2021-05-07 02:37:30 +03:00
|
|
|
message = "Unable to Authenticate"
|
|
|
|
|
|
|
|
try:
|
2022-03-10 23:22:19 +03:00
|
|
|
idinfo = id_token.verify_oauth2_token(
|
2021-05-07 03:29:03 +03:00
|
|
|
token, requests.Request(),
|
|
|
|
settings.GOOGLE_SIGN_IN_CLIENT_ID)
|
2022-03-10 23:22:19 +03:00
|
|
|
user_info = {
|
|
|
|
'email': idinfo['email'],
|
|
|
|
}
|
|
|
|
signature = xsrf.generate_token(str(user_info))
|
|
|
|
session['signed_user_info'] = user_info, signature
|
2021-05-07 02:37:30 +03:00
|
|
|
message = "Done"
|
|
|
|
# print(idinfo['email'], file=sys.stderr)
|
|
|
|
except ValueError:
|
|
|
|
message = "Invalid token"
|
|
|
|
|
|
|
|
return {'message': message}
|