#!/usr/bin/python3

import argparse
from oauth2client import client
from urllib import parse
import requests
from http import server
import socket


parser = argparse.ArgumentParser(description='oauthproxy')
parser.add_argument(
    '--api-key',
    dest='api_key',
    action='store',
    required=True)
parser.add_argument(
    '--listen-host',
    dest='listen_host',
    action='store',
    default='::')
parser.add_argument(
    '--listen-port',
    dest='listen_port',
    type=int,
    action='store',
    default=443)
FLAGS = parser.parse_args()


class HTTPServer6(server.HTTPServer):
  address_family = socket.AF_INET6


class OAuthProxy(object):

  def __init__(self, listen_host, listen_port, api_key):
    self._api_key = api_key

    HANDLERS = {
      '/': self._ServeRedirect,
      '/oauth2callback': self._OAuth2Callback,
    }

    class RequestHandler(server.BaseHTTPRequestHandler):
      def do_GET(self):
        self.parsed_url = parse.urlparse(self.path)
        try:
          HANDLERS[self.parsed_url.path](self)
        except KeyError:
          self.send_response(404)
          self.end_headers()

    self._httpd = HTTPServer6((listen_host, listen_port), RequestHandler)

  def Serve(self):
    self._httpd.serve_forever()

  def _GetFlow(self, req):
    return_url = ''.join([
      'http://',
      req.headers['Host'],
      '/oauth2callback',
    ])
    return client.flow_from_clientsecrets(
        'client_secrets.json',
        scope='https://www.googleapis.com/auth/userinfo.email',
        redirect_uri=return_url)

  def _ServeRedirect(self, req):
    req.send_response(302)
    req.send_header('Location', self._GetFlow(req).step1_get_authorize_url())
    req.end_headers()

  def _OAuth2Callback(self, req):
    qs = parse.parse_qs(req.parsed_url.query)
    credentials = self._GetFlow(req).step2_exchange(qs['code'][0])
    result = requests.get(
        'https://www.googleapis.com/plus/v1/people/me?%s' % parse.urlencode({
          'key': self._api_key,
          'access_token': credentials.access_token,
        }))
    emails = [
      x['value']
      for x in result.json()['emails']
      if x['type'] == 'account'
    ]
    req.send_response(200)
    req.end_headers()
    req.wfile.write(emails[0].encode('utf8'))


def main():
  server = OAuthProxy(
      FLAGS.listen_host,
      FLAGS.listen_port,
      FLAGS.api_key)
  server.Serve()


if __name__ == '__main__':
  main()

@@ -140,16 +140,27 @@
  socket%0A
+import ssl%0A
 %0A%0Aparser
@@ -530,16 +530,224 @@
 lt=443)%0A
+parser.add_argument(%0A    '--server-key',%0A    dest='server_key',%0A    action='store',%0A    required=True)%0Aparser.add_argument(%0A    '--server-cert',%0A    dest='server_cert',%0A    action='store',%0A    required=True)%0A
 FLAGS = 
@@ -916,16 +916,41 @@
 en_port,
+ server_key, server_cert,
  api_key
@@ -1439,16 +1439,209 @@
 Handler)
+%0A    self._httpd.socket = ssl.wrap_socket(%0A        self._httpd.socket,%0A        keyfile=server_key,%0A        certfile=server_cert,%0A        server_side=True)%0A    self._httpd.socket.settimeout(5.0)
 %0A%0A  def 
@@ -1751,16 +1751,17 @@
    'http
+s
 ://',%0A  
@@ -2764,16 +2764,16 @@
 n_host,%0A
-
       FL
@@ -2785,24 +2785,73 @@
 isten_port,%0A
+      FLAGS.server_key,%0A      FLAGS.server_cert,%0A
       FLAGS.

Add HTTPS support