import json
import urllib
import os
import jupyterhub
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from traitlets import Unicode
from jupyterhub.auth import Authenticator
from tornado import gen

class HttpAuthenticator(Authenticator):

    server = Unicode(
        None,
        allow_none=True,
        config=True,
        help="""
          Http authentication server.
        """
    )
    
    appid = Unicode(
       None,
       allow_none=True,
       config=True,
       help="""
         Application Id recognized by the http authentication server
       """   
    )

    @gen.coroutine
    def authenticate(self, handler, data):
        http_client = AsyncHTTPClient()

        headers = {
            "Accept": "application/json",
            "User-Agent": "JupyterHub",
        }

        params = dict(
            type="json",
            appid=self.appid,
            ac=data['username'],
            pw=data['password']
        )

        req = HTTPRequest(self.server,
                          method="POST",
                          headers=headers,
                          body=urllib.parse.urlencode(params),
                          validate_cert = False
                         )
        
        
        resp = yield http_client.fetch(req)
        reply = json.loads(resp.body.decode('utf8', 'replace'))

        if reply.get("code") == 200:
           return (reply.get("data").get("UserCN"))
        else:
           return None



[((12, 13, 19, 5), 'traitlets.Unicode', 'Unicode', (), '', False, 'from traitlets import Unicode\n'), ((21, 12, 28, 5), 'traitlets.Unicode', 'Unicode', (), '', False, 'from traitlets import Unicode\n'), ((32, 22, 32, 39), 'tornado.httpclient.AsyncHTTPClient', 'AsyncHTTPClient', ({}, {}), '()', False, 'from tornado.httpclient import HTTPRequest, AsyncHTTPClient\n'), ((49, 31, 49, 61), 'urllib.parse.urlencode', 'urllib.parse.urlencode', ({(49, 54, 49, 60): 'params'}, {}), '(params)', False, 'import urllib\n')]