#   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.

from keystoneauth1 import identity
from keystoneauth1 import session
from keystoneclient import v3
import json

from mixmatch import config
from mixmatch.config import LOG, CONF, get_conf_for_sp


@config.MEMOIZE_SESSION
def get_client():
    """Return a Keystone client capable of validating tokens."""
    LOG.info("Getting Admin Client")
    service_auth = identity.Password(
        auth_url=CONF.keystone.auth_url,
        username=CONF.keystone.username,
        password=CONF.keystone.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=service_auth)
    return v3.client.Client(session=local_session)


@config.MEMOIZE_SESSION
def get_local_auth(user_token):
    """Return a Keystone session for the local cluster."""
    LOG.info("Getting session for %s" % user_token)
    client = get_client()
    token = v3.tokens.TokenManager(client)
    token_data = token.validate(token=user_token, include_catalog=False)
    project_id = token_data['project']['id']

    local_auth = identity.v3.Token(auth_url=CONF.keystone.auth_url,
                                   token=user_token,
                                   project_id=project_id)

    return session.Session(auth=local_auth)


@config.MEMOIZE_SESSION
def get_unscoped_sp_auth(service_provider, user_token):
    """Perform K2K auth, and return an unscoped session."""
    local_auth = get_local_auth(user_token).auth
    LOG.info("Getting unscoped session for (%s, %s)" % (service_provider,
                                                        user_token))
    remote_auth = identity.v3.Keystone2Keystone(
        local_auth,
        service_provider
    )
    return session.Session(auth=remote_auth)


def get_projects_at_sp(service_provider, user_token):
    """Perform K2K auth, and return the projects that can be scoped to."""
    conf = get_conf_for_sp(service_provider)
    unscoped_session = get_unscoped_sp_auth(service_provider, user_token)
    r = json.loads(str(unscoped_session.get(
        conf.auth_url + "/OS-FEDERATION/projects").text))
    return [project[u'id'] for project in r[u'projects']]


@config.MEMOIZE_SESSION
def get_sp_auth(service_provider, user_token, remote_project_id):
    """Perform K2K auth, and return a session for a remote cluster."""
    local_auth = get_local_auth(user_token).auth

    LOG.info("Getting session for (%s, %s, %s)" % (service_provider,
                                                   user_token,
                                                   remote_project_id))

    remote_auth = identity.v3.Keystone2Keystone(
        local_auth,
        service_provider,
        project_id=remote_project_id
    )

    return session.Session(auth=remote_auth)

@@ -2096,24 +2096,69 @@
 session.%22%22%22%0A
+    conf = get_conf_for_sp(service_provider)%0A
     local_au
@@ -2194,16 +2194,16 @@
 n).auth%0A
-
     LOG.
@@ -2414,32 +2414,28 @@
         
-service_provider
+conf.sp_name
 %0A    )%0A 
@@ -3048,24 +3048,69 @@
 cluster.%22%22%22%0A
+    conf = get_conf_for_sp(service_provider)%0A
     local_au
@@ -3428,32 +3428,28 @@
         
-service_provider
+conf.sp_name
 ,%0A      

Use sp_name config option in K2K instead of section name