2014-01-15 17:03:59 +08:00
|
|
|
# Copyright 2014 eBay Inc.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
|
|
|
"""Identity v2 Token action implementations"""
|
|
|
|
|
2016-05-13 17:27:12 -05:00
|
|
|
from osc_lib.command import command
|
|
|
|
from osc_lib import exceptions
|
2014-01-15 17:03:59 +08:00
|
|
|
import six
|
|
|
|
|
2016-05-13 13:14:02 -07:00
|
|
|
from openstackclient.i18n import _
|
2014-10-03 22:54:42 -04:00
|
|
|
|
2014-01-15 17:03:59 +08:00
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class IssueToken(command.ShowOne):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Issue new token")
|
2014-01-15 17:03:59 +08:00
|
|
|
|
2016-02-08 11:16:24 -08:00
|
|
|
# scoped token is optional
|
|
|
|
required_scope = False
|
|
|
|
|
2014-01-15 17:03:59 +08:00
|
|
|
def get_parser(self, prog_name):
|
2014-06-26 18:20:35 -05:00
|
|
|
parser = super(IssueToken, self).get_parser(prog_name)
|
2014-01-15 17:03:59 +08:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
2016-02-04 16:45:38 +00:00
|
|
|
auth_ref = self.app.client_manager.auth_ref
|
|
|
|
if not auth_ref:
|
|
|
|
raise exceptions.AuthorizationFailure(
|
|
|
|
"Only an authorized user may issue a new token.")
|
2014-06-26 18:20:35 -05:00
|
|
|
|
2016-02-04 16:45:38 +00:00
|
|
|
data = {}
|
|
|
|
if auth_ref.auth_token:
|
|
|
|
data['id'] = auth_ref.auth_token
|
|
|
|
if auth_ref.expires:
|
2016-09-05 10:15:57 -07:00
|
|
|
datetime_obj = auth_ref.expires
|
|
|
|
expires_str = datetime_obj.strftime('%Y-%m-%dT%H:%M:%S%z')
|
|
|
|
data['expires'] = expires_str
|
2016-02-04 16:45:38 +00:00
|
|
|
if auth_ref.project_id:
|
|
|
|
data['project_id'] = auth_ref.project_id
|
|
|
|
if auth_ref.user_id:
|
|
|
|
data['user_id'] = auth_ref.user_id
|
|
|
|
return zip(*sorted(six.iteritems(data)))
|
2014-05-23 10:17:42 -06:00
|
|
|
|
|
|
|
|
2014-06-26 18:20:35 -05:00
|
|
|
class RevokeToken(command.Command):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Revoke existing token")
|
2014-05-23 10:17:42 -06:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
2014-06-26 18:20:35 -05:00
|
|
|
parser = super(RevokeToken, self).get_parser(prog_name)
|
2014-05-23 10:17:42 -06:00
|
|
|
parser.add_argument(
|
|
|
|
'token',
|
|
|
|
metavar='<token>',
|
2014-10-03 22:54:42 -04:00
|
|
|
help=_('Token to be deleted'),
|
2014-05-23 10:17:42 -06:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
2014-06-26 18:20:35 -05:00
|
|
|
|
2014-05-23 10:17:42 -06:00
|
|
|
identity_client.tokens.delete(parsed_args.token)
|