# 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
            return True, accessinfo['access_token'], accessinfo['refresh_token']
        except Exception:
            return False, '', ''

    def refresh_auth_token(self, refresh_token):
        try:
            auth_msg = {
                'client_id': self.client_id,
                'grant_type': 'refresh_token',
                'client_secret': self.client_secret,
                'refresh_token': refresh_token,
                'redirect_uri': self.redirect_uri
            }
            result = requests.request(
                "GET", self.token_endpoint,
                headers=None,
                params=auth_msg)
            # TODO http status code should be 200 if success
            # if refresh_token was expired, http status code should be 400
            accessinfo = result.json()
            assert accessinfo['access_token']
            assert accessinfo['refresh_token']
            return True, accessinfo['access_token'], accessinfo['refresh_token']
        except Exception:
            return False, '', ''

    @classmethod
    def save_token(cls, phase, access_token, refresh_token):
        try:
            save_config(phase, SSO_TOKEN_KEY, access_token)

# 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

            return True, accessinfo['access_token'], accessinfo['refresh_token']
        except Exception:
            return False, '', ''

    def refresh_auth_token(self, refresh_token):
        try:
            auth_msg = {
                'client_id': self.client_id,
                'grant_type': 'refresh_token',
                'client_secret': self.client_secret,
                'refresh_token': refresh_token,
                'redirect_uri': self.redirect_uri
            }
            result = requests.request(
                "GET", self.token_endpoint,
                headers=None,
                params=auth_msg)
            # TODO http status code should be 200 if success
            # if refresh_token was expired, http status code should be 400
            accessinfo = result.json()
            assert accessinfo['access_token']
            assert accessinfo['refresh_token']
            return True, accessinfo['access_token'], accessinfo['refresh_token']
        except Exception:
            return False, '', ''

    @classmethod
    def save_token(cls, phase, access_token, refresh_token):
        try:
            save_config(phase, SSO_TOKEN_KEY, access_token)

            save_config(phase, SSO_REFRESH_TOKEN_KEY, refresh_token)