# Path: lain_cli/utils.py
# def get_domain(phase):
#     domain = user_config.get_config().get(phase, {}).get(DOMAIN_KEY, None)
#     if not domain:
#         error('please set %s domain by "lain config save %s %s ${%s_domain}"'
#               % (phase, phase, DOMAIN_KEY, phase))
#         exit(1)
#     return domain
# 
# def save_config(phase, prop, value):
#     p_value = {prop: value}
#     _kw = {phase: p_value}
#     user_config.set_config(**_kw)


import re
import requests
import lain_sdk.mydocker as docker
from urllib import urlencode
from urlparse import urlparse, parse_qs
from lain_sdk.yaml.conf import user_config
from lain_sdk.util import error, warn
from lain_cli.utils import get_domain, save_config
    save_token_success = sso_access.save_token(phase, access_token, refresh_token)
    if not save_token_success:
        warn('save_token failed')
        return False
    return True


def docker_login(phase, username, password):
    domain = get_domain(phase)
    return docker.login(username, password, 'registry.%s' % domain)


def sso_refresh(phase):
    sso_access = SSOAccess.new(phase, None, None, None)
    refresh_token = sso_access.get_refresh_token(phase)
    if not refresh_token:
        warn('refresh failed, no refresh token got')
        return False
    refresh_success, new_access_token, new_refresh_token = sso_access.refresh_auth_token(refresh_token)
    if not refresh_success:
        warn('refresh failed, refresh_auth_token failed ')
        return False
    save_token_success = sso_access.save_token(phase, new_access_token, new_refresh_token)
    if not save_token_success:
        warn('refresh failed, save_token failed')
        return False
    return True


def is_console_auth_activated(phase):

# Path: lain_cli/utils.py
# def get_domain(phase):
#     domain = user_config.get_config().get(phase, {}).get(DOMAIN_KEY, None)
#     if not domain:
#         error('please set %s domain by "lain config save %s %s ${%s_domain}"'
#               % (phase, phase, DOMAIN_KEY, phase))
#         exit(1)
#     return domain
# 
# def save_config(phase, prop, value):
#     p_value = {prop: value}
#     _kw = {phase: p_value}
#     user_config.set_config(**_kw)


# Path: lain_cli/auth.py
import re
import requests
import lain_sdk.mydocker as docker
from urllib import urlencode
from urlparse import urlparse, parse_qs
from lain_sdk.yaml.conf import user_config
from lain_sdk.util import error, warn
from lain_cli.utils import get_domain, save_config

    save_token_success = sso_access.save_token(phase, access_token, refresh_token)
    if not save_token_success:
        warn('save_token failed')
        return False
    return True


def docker_login(phase, username, password):
    domain = get_domain(phase)
    return docker.login(username, password, 'registry.%s' % domain)


def sso_refresh(phase):
    sso_access = SSOAccess.new(phase, None, None, None)
    refresh_token = sso_access.get_refresh_token(phase)
    if not refresh_token:
        warn('refresh failed, no refresh token got')
        return False
    refresh_success, new_access_token, new_refresh_token = sso_access.refresh_auth_token(refresh_token)
    if not refresh_success:
        warn('refresh failed, refresh_auth_token failed ')
        return False
    save_token_success = sso_access.save_token(phase, new_access_token, new_refresh_token)
    if not save_token_success:
        warn('refresh failed, save_token failed')
        return False
    return True


def is_console_auth_activated(phase):

    domain = get_domain(phase)