Revision dc897a7e

b/kamaki/cli/__init__.py
41 41
from kamaki.cli.utils import print_dict, red, magenta, yellow
42 42
from kamaki.cli.errors import CLIError, CLICmdSpecError
43 43
from kamaki.cli import logger
44
from kamaki.clients.astakos import AstakosClient as AuthCachedClient
45
from kamaki.clients import ClientError
44 46

  
45 47
_help = False
46 48
_debug = False
......
285 287
    for term in ('url', 'token'):
286 288
        try:
287 289
            auth_args[term] = _cnf.get_cloud(cloud, term)
288
        except KeyError:
290
        except KeyError or IndexError:
289 291
            auth_args[term] = ''
290 292
        if not auth_args[term]:
291 293
            raise CLIError(
......
296 298
                    '  kamaki config set cloud.%s.%s <%s>' % (
297 299
                        cloud, term, term.upper())])
298 300

  
299
    from kamaki.clients.astakos import AstakosClient as AuthCachedClient
300 301
    try:
301
        return AuthCachedClient(auth_args['url'], auth_args['token']), cloud
302
        auth_base = None
303
        for token in reversed(auth_args['token'].split()):
304
            try:
305
                if auth_base:
306
                    auth_base.authenticate(token)
307
                else:
308
                    auth_base = AuthCachedClient(
309
                        auth_args['url'], auth_args['token'])
310
                    auth_base.authenticate(token)
311
            except ClientError as ce:
312
                if ce.status in (401, ):
313
                    kloger.warning(
314
                        'WARNING: Failed to authorize token %s' % token)
315
                else:
316
                    raise
317
        return auth_base, cloud
302 318
    except AssertionError as ae:
303 319
        kloger.warning('WARNING: Failed to load authenticator [%s]' % ae)
304 320
        return None, cloud
b/kamaki/cli/commands/astakos.py
45 45

  
46 46
class _user_init(_command_init):
47 47

  
48
    def _write_main_token(self, token):
49
        tokens = self.config.get_cloud(self.cloud, 'token').split()
50
        if token in tokens:
51
            tokens.remove(token)
52
        tokens.insert(0, token)
53
        self.config.set_cloud(self.cloud, 'token', ' '.join(tokens))
54
        self.config.write()
55

  
48 56
    @errors.generic.all
49 57
    @errors.user.load
50 58
    @addLogSettings
......
54 62
            if base_url:
55 63
                token = self._custom_token('astakos')\
56 64
                    or self.config.get_cloud(self.cloud, 'token')
65
                token = token.split()[0] if ' ' in token else token
57 66
                self.client = AstakosClient(base_url=base_url, token=token)
58 67
                return
59 68
        else:
......
91 100
            if (token_bu != self.client.token and
92 101
                    ask_user('Permanently save token as cloud.%s.token ?' % (
93 102
                        self.cloud))):
94
                self.config.set_cloud(
95
                    self.cloud, 'token', self.client.token)
96
                self.config.write()
103
                self._write_main_token(self.client.token)
97 104
        except Exception:
98 105
            #recover old token
99 106
            self.client.token = token_bu
......
156 163
                print('Session user set to %s (%s)' % (
157 164
                        self.client.user_term('name'),
158 165
                        self.client.user_term('id')))
166
                if ask_user('Permanently make %s the main user?' % (
167
                        self.client.user_term('name'))):
168
                    self._write_main_token(self.client.token)
159 169
                return
160 170
        raise CLIError(
161 171
            'User with UUID %s not authenticated in current session' % uuid,

Also available in: Unified diff