示例#1
0
 def testNotImplementedException(self):
     message = "A string unlikely to occur at random."
     exception = exceptions.NotImplementedException(message)
     response = frontend.handleException(exception)
     self.assertEquals(response.status_code, 501)
     gaException = self.getGa4ghException(response.data)
     self.assertEquals(gaException.message, message)
     self.assertFalse(frontend.app.log_exception.called)
示例#2
0
def oidcCallback():
    """
    Once the authorization provider has cleared the user, the browser
    is returned here with a code. This function takes that code and
    checks it with the authorization provider to prove that it is valid,
    and get a bit more information about the user (which we don't use).

    A token is generated and given to the user, and the authorization info
    retrieved above is stored against this token. Later, when a client
    connects with this token, it is assumed to be a valid user.

    :return: A display of the authentication token to use in the client. If
    OIDC is not configured, raises a NotImplementedException.
    """
    if app.oidcClient is None:
        raise exceptions.NotImplementedException()
    response = dict(flask.request.args.iteritems(multi=True))
    aresp = app.oidcClient.parse_response(
        message.AuthorizationResponse,
        info=response,
        sformat='dict')
    sessState = flask.session.get('state')
    respState = aresp['state']
    if (not isinstance(aresp, message.AuthorizationResponse) or
            respState != sessState):
        raise exceptions.NotAuthenticatedException()

    args = {
        "code": aresp['code'],
        "redirect_uri": app.oidcClient.redirect_uris[0],
        "client_id": app.oidcClient.client_id,
        "client_secret": app.oidcClient.client_secret
    }
    atr = app.oidcClient.do_access_token_request(
        scope="openid",
        state=respState,
        request_args=args)

    if not isinstance(atr, message.AccessTokenResponse):
        raise exceptions.NotAuthenticatedException()

    atrDict = atr.to_dict()
    if flask.session.get('nonce') != atrDict['id_token']['nonce']:
        raise exceptions.NotAuthenticatedException()
    key = oic.oauth2.rndstr(SECRET_KEY_LENGTH)
    flask.session['key'] = key
    token_data = aresp["code"], respState, atrDict
    app.cache.set(key, token_data)
    # flask.url_for is broken. It relies on SERVER_NAME for both name
    # and port, and defaults to 'localhost' if not found. Therefore
    # we need to fix the returned url
    indexUrl = flask.url_for('index', _external=True)
    indexParts = list(urlparse.urlparse(indexUrl))
    if ':' not in indexParts[1]:
        indexParts[1] = '{}:{}'.format(socket.gethostname(), app.myPort)
        indexUrl = urlparse.urlunparse(indexParts)
    response = flask.redirect(indexUrl)
    return response
示例#3
0
 def _getPrefix(self, url):
     """
     Given a url return namespace prefix.
     Leverages prefixes already in graph namespace
     Ex.  "http://www.drugbank.ca/drugs/" -> "Drugbank"
     """
     for prefix, namespace in self._rdfGraph.namespaces():
         if namespace.toPython() == url or namespace == url:
             return prefix
     raise exceptions.NotImplementedException(
         "No namespace found for url {}".format(url))
示例#4
0
 def _toNamespaceURL(self, term):
     """
     Given an ontologyterm.term return namespace identifier.
     Leverages prefixes already in graph namespace
     Ex.  "DrugBank:DB01268" -> "http://www.drugbank.ca/drugs/DB01268"
     """
     termPrefix, termId = term.split(':')
     for prefix, namespace in self._rdfGraph.namespaces():
         if prefix == termPrefix:
             return "".join([namespace, termId])
     raise exceptions.NotImplementedException(
         "Term has a prefix not found in this instance. {}".format(term))