-
Notifications
You must be signed in to change notification settings - Fork 55
Can you connect using an auth token/API key instead of user/password? #49
Comments
Are you looking for share_database, unshare_database? |
No, that's sharing databases with another Cloudant user. The idea is to create an API key using generate_api_key() and then assign permissions to a specific database, or give the API key permission to create a new database itself. Particularly helpful for temporal, per-user, or per-device database use cases. |
I may be misunderstanding what you are looking for but you can use share_database from the database module (It uses the /_api/v2/db/$DB/_security endpoint) and pass it the API Key as the username and then set permissions for |
Also a little confused on your first question re: the base64 auth token. Are you looking to do basic authentication or are you looking to |
Basically, there are two things I'm trying to here, yes, so I'll separate them out and describe with examples. An example using curl would be:
Number two is to use API keys as the stored credentials instead of a Cloudant username, but now I'm thinking this may be moot. I'm planning to use a rolling database model, which means whatever stored credentials are used will need to be able to create and delete databases, and I'm fairly certain API keys only exist on a per-database basis. It's good to know that share_database() can interact with the _security endpoint though. |
Thanks for the clarification. Currently we are set up for cookie authentication only when using the from cloudant import Cloudant
import requests
# Construct a client object but don't provide it with your password.
# However, the password (auth_token) argument must be set to something.
client = Cloudant('bradwbonn', 'not-giving-you-my-password', account='bradwbonn')
# Roll your own connect() routine...
client.r_session = requests.Session()
client.r_session.headers.update({'Authorization': 'Basic <BASE64HASHOFUSERPASSWORDCREDS>'})
client._cloudant_session = client.session()
# Done ... Unfortunately, you cannot use the Cloudant with context manager using this work around as in your original example. Also, if you were using the I hope that works out for you. I'll open a new "Enhancement" Issue for adding Basic authentication and reference this one. |
For added context: take a look at client.connect() to see what exactly you would be overriding. Hopefully seeing that would add a bit more context around the proposed the work around. |
Now that I've had more time to think about it, there's no reason why you could not create your own context manager to replicate what the cloudant context manager does and then you could do all this via with as well. ref: https://github.com/cloudant/python-cloudant/blob/master/src/cloudant/__init__.py#L59 I hope some of this helps you along... I'll stop beating this horse now. :) |
This is really helpful, thank you! I've actually never extended a library in Python before, so this will end up being yet another learning experience for me. ;) |
In your example above: |
not much luck using
Error using Python 3 notebook is after a NameError Traceback (most recent call last) /opt/conda/lib/python3.4/site-packages/cloudant/init.py in () /opt/conda/lib/python3.4/site-packages/cloudant/account.py in () /opt/conda/lib/python3.4/site-packages/cloudant/database.py in () /opt/conda/lib/python3.4/site-packages/cloudant/design_document.py in () /opt/conda/lib/python3.4/site-packages/cloudant/views.py in () /opt/conda/lib/python3.4/site-packages/cloudant/result.py in () NameError: name 'basestring' is not defined |
Ideally, I would like to avoid UID/PW credentials and leverage this type of approach for Cloudant:
Any plans for enabling this approach? |
There appear to be a few questions and misconceptions here around API keys and security. base64 Firstly, @bradwbonn remember base64 doesn't make any difference to security. I suggest storing the plain credentials, because it's easier for others and your future self to understand what the security properties of the script are (if the server's insecure, you've bigger issues anyway :) ) I think this negates the need for accessing the authorization header in your case; but clearly it could be useful for other scenarios, such as authenticating against a proxy server rather than Cloudant itself. I think our workaround works for now, but I do quite like @vinomaster's suggestion -- though it appears Setting permissions To set the security on a database from an API key and password, you'll need to set up the Using API keys with python-cloudant @vinomaster I think your question is around using API keys; let me know if I misunderstood, there's a bunch of issues here :) To use an API key and password, it's important to note that they are just username and password pairs, so you use them as follows (feel free to try it as-is, the key is a valid read key for that db at least for the next few days): USERNAME = "bouninamendouldnimendepa"
PASSWORD = "e6fda548ce40d21ae675d03068bb0f913f2d99f1"
ACCOUNT_NAME = "mikerhodes"
DATABASE_NAME = "animaldb"
DOC_NAME = "badger"
from cloudant.account import Cloudant
client = Cloudant(USERNAME, PASSWORD, account=ACCOUNT_NAME)
# Connect to the account and establish a session cookie
client.connect()
session = client.session()
database = client[DATABASE_NAME]
document = database[DOC_NAME]
print "Got document: ", document
client.disconnect() As @bradwbonn said, this appears to not be working with the context manager, that is, this fails: # With context manager
with Cloudant(USERNAME, PASSWORD, account=ACCOUNT_NAME) as client:
session = client.session()
database = client[DATABASE_NAME]
document = database[DOC_NAME]
print "Got document from context manager: ", document I filed this as issue #53. |
The call needs to be |
@bradwbonn, I'm not sure where we are at with this conversation. Apparently a lot has happened while I was sleeping. I'll be sure never to sleep again :). But yes you found a typo in #49 (comment). The line in question should read |
@vinomaster, this library is not yet supported in Python3. See http://python-cloudant.readthedocs.org/en/latest/compatibility.html. The problem you are seeing is because of the use of |
Below is a summary of "where we are" with this issue, since it seems that we have diverged in a few directions. @bradwbonn, setting permissions can be done for users as well as API keys by using the database module's @vinomaster, the problem you are experiencing is simple. This library has not yet been scrubbed to be used with Python3. Unfortunately, I'm not sure that will help you much. Sorry for that. @mikerhodes, on the topic of the main context manager, I believe the problem you encountered was simply a spelling mistake. A lot of this along with other good stuff can be found in our docs. |
This whole conversation has been super-insightful, thank you! |
@bradwbonn: I have an update on the whole setting of permissions question. See #52 (comment) |
So to summarize:
Remaining questions:
|
So after further testing... using Python 2 under Project Jupyter we are unable to pip install Cloudant.
To be clear this Python 2 kernel functionality is provided via conda
To test try here. At this juncture, until Python3 support it seems Project Jupyter integration with Cloudant is a non-starter. |
@vinomaster Could you file a separate bug for Project Jupyter support? This bug is overloaded. |
@vinomaster, I've moved your issue with the pip install on Project Jupyter to #54. |
I think that all issues here have been either answered or moved off to their own separate issue. |
Is there a way to pass a base64 auth token to Cloudant() for creating a new client? Right now I only see passing it username and password. It calls the attribute "auth_token" but it doesn't seem to work unless I use the explicit password.
Code I'm trying:
Reponse:
Related question:
I see that I can generate an API key pair, but I don't see any calls in the API to set what permissions those keys have. (Such as setting their permissions on a specific database once they're created.) Am I just missing it?
The text was updated successfully, but these errors were encountered: