<commit_before>#   Copyright 2016 Massachusetts Open Cloud
#
#   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 json
import os

from keystoneauth1 import identity
from keystoneauth1 import session
import requests

from mixmatch.config import CONF
from mixmatch import model


def get_sp_auth(service_provider, user_token, service, remote_project_id=None):
    # Use K2K to get a scoped token for the SP
    # For some reason if I authenticate with the PROJECT_ID
    # It doesn't like me

    url = '%s/projects' % CONF.keystone.auth_url
    headers = {'X-AUTH-TOKEN': user_token}
    resp = requests.get(url=url, headers=headers)
    projects = json.loads(resp.text)

    # Is this the project we're really scoped to?
    local_project_id = projects['projects'][2]['id']
    local_project_name = projects['projects'][2]['name']
    local_project_domain_id = projects['projects'][2]['domain_id']

    auth = model.RemoteAuth.find(local_token=user_token,
                                 service_provider=service_provider)
    print("Auth: %s" % str(auth))
    if auth is None:
        print("Not cached")
        local_auth = identity.v3.Token(auth_url=CONF.keystone.auth_url,
                                       token=user_token,
                                       project_name='admin',
                                       project_domain_id='default')

        remote_auth = identity.v3.Keystone2Keystone(
            local_auth,
            service_provider,
            project_name='admin',
            project_domain_id='default'
        )

        remote_session = session.Session(auth=remote_auth)
        auth_ref = remote_auth.get_auth_ref(remote_session)

        endpoint = auth_ref.service_catalog.url_for(service_type=service)
        remote_project = os.path.basename(endpoint)
        remote_token = auth_ref._auth_token

        auth = model.RemoteAuth(
            local_token=user_token,<commit_msg>use keystone client to get project id<commit_after>#   Copyright 2016 Massachusetts Open Cloud
#
#   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 os
from keystoneauth1 import identity
from keystoneauth1 import session
from keystoneclient import v3
from mixmatch.config import CONF
from mixmatch import model

def get_sp_auth(service_provider, user_token, service, remote_project_id=None):

    idp_auth = identity.Password(auth_url=CONF.keystone.auth_url,
                                 username=CONF.keystone.username,
                                 password=<PASSWORD>,
			         project_name=CONF.keystone.project_name,
			         project_domain_id=CONF.keystone.project_domain_id,
			         user_domain_id=CONF.keystone.user_domain_id)
    local_session = session.Session(auth=idp_auth)
    client = v3.client.Client(session=local_session)
    token = v3.tokens.TokenManager(client)
    token_data = token.validate(token=user_token, include_catalog=False)
    project_id = token_data['project']['id']

    auth = model.RemoteAuth.find(local_token=user_token,
                                 service_provider=service_provider)
    if auth is None:
        print("Not cached")
        local_auth = identity.v3.Token(auth_url=CONF.keystone.auth_url,
                                       token=user_token,
                                       project_id=project_id)

        remote_auth = identity.v3.Keystone2Keystone(
            local_auth,
            service_provider,
            project_name='admin',
            project_domain_id='default'
        )

        remote_session = session.Session(auth=remote_auth)
        auth_ref = remote_auth.get_auth_ref(remote_session)

        endpoint = auth_ref.service_catalog.url_for(service_type=service)
        remote_project = os.path.basename(endpoint)
        remote_token = auth_ref._auth_token

        auth = model.RemoteAuth(
            local_token=user_token,
{'fraction_non_alphanumeric': 0.07549479698020813, 'fraction_numerical': 0.0067333197306672105, 'mean_word_length': 2.7477064220183487, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7008905', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1329, 'n_words': 361}