Skip to content

Instantly share code, notes, and snippets.

@jamielennox
Created May 10, 2016 04:39
Show Gist options
  • Save jamielennox/013bbc9e32cfc886fd7b211d191a8909 to your computer and use it in GitHub Desktop.
Save jamielennox/013bbc9e32cfc886fd7b211d191a8909 to your computer and use it in GitHub Desktop.
Create an OAuth Delegation to yourself, Create the delegation then test the oauth plugin against it.
# source devstack/accrc/admin/admin credentials
import argparse
import pprint
import sys
from keystoneclient import v3
from keystoneauth1 import loading
from keystoneauth1.extras import oauth1
parser = argparse.ArgumentParser()
loading.register_session_argparse_arguments(parser)
loading.register_auth_argparse_arguments(parser, sys.argv[1:])
opts = parser.parse_args(sys.argv[1:])
user_auth = loading.load_auth_from_argparse_arguments(opts)
session = loading.load_session_from_argparse_arguments(opts)
client = v3.Client(session=session, auth=user_auth)
# create the consumer, get the consumer secret
consumer = client.oauth1.consumers.create(description="Testing Oauth Consumer")
# consumer creates a request token with no authorizations
req_token = client.oauth1.request_tokens.create(
consumer_key=consumer.id,
consumer_secret=consumer.secret,
project=session.get_project_id(auth=user_auth))
# user adds some roles to the request token
roles = [client.roles.find(name='admin').id]
verifier = client.oauth1.request_tokens.authorize(req_token.id, roles)
# get an access token from the request token
acc_token = client.oauth1.access_tokens.create(
consumer_key=consumer.id,
consumer_secret=consumer.secret,
request_key=req_token.key,
request_secret=req_token.secret,
verifier=verifier.oauth_verifier)
print "export OS_AUTH_URL=http://localhost:5000/v3"
print "export OS_AUTH_TYPE=v3oauth1"
print "export OS_CONSUMER_KEY=%s" % consumer.id
print "export OS_CONSUMER_SECRET=%s" % consumer.secret
print "export OS_ACCESS_KEY=%s" % acc_token.key
print "export OS_ACCESS_SECRET=%s" % acc_token.secret
# # create a keystoneauth plugin with the access token
# # NOTE: project_name is different to admin project, token is still in admin
# # project.
# oauth_plugin = oauth1.OAuth1(auth_url='http://localhost:5000/v3',
# consumer_key=consumer.id,
# consumer_secret=consumer.secret,
# access_key=acc_token.key,
# access_secret=acc_token.secret,
# project_name='demo',
# project_domain_id='default')
#
# # do the token fetch and print it
# pprint.pprint(oauth_plugin.get_auth_ref(session)._data)
import argparse
import pprint
import sys
from keystoneauth1 import loading
parser = argparse.ArgumentParser()
loading.register_session_argparse_arguments(parser)
loading.register_auth_argparse_arguments(parser, sys.argv[1:])
opts = parser.parse_args(sys.argv[1:])
user_auth = loading.load_auth_from_argparse_arguments(opts)
session = loading.load_session_from_argparse_arguments(opts)
# do the token fetch and print it
pprint.pprint(user_auth.get_auth_ref(session)._data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment