-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add personal access token authentication #122
Add personal access token authentication #122
Conversation
@martinda not a bad first go but we can simplify things some more. Taking into account your snippet above we should be able to do something like this:
The link I posted in the ISSUE you opened shows how you can implement this here. I'm thinking of copying quite literally that impl and use it here so that we can do something like:
You could probably simplify and optimize that even more using a StringBuilder if you're up for it. The big difference this solution provides over the current is that instead of throwing an exception if things are not base64 encoded we will instead assume they want to pass in a token and go with that. As for testing you can do:
|
It bothers me too, but without the "Bearer " prefix, the personal access token matches the regex. |
I haven't looked too much into |
The Bearer token is passed as is to Bitbucket. When I decode it, it looks like garbage. |
@martinda is there anything descriptive about the format/structure of the String that we could use to detect if it's a "Bearer" value and without having to bring in external libraries? I see for JWT style formatting it goes something like |
@cdancy I found the RFC documentation for Basic and Bearer Authorization. The bearer tokens I get from Bitbucket match both the basic expression AND the bearer expression. Decoding the Bitbucket bearer token sometimes results in a string with a colon, so decoding does make them distinguishable. It is interesting to note that in both RFCs, the Authorization header does specify the type of authorization explicitly (the "Basic" and "Bearer" strings). Being explicit about the header type is probably the only way to distinguish them. |
@martinda yeah I was reading up on that a bit more last night and walked away with the same conclusions. However, I did have a potential epiphany that could work here and keep things sane without the user having to pass in the respective header. When a user is using "Basic" auth they are passing along a username and password. When a user is using "Bearer" auth they are passing along a key or token which I suppose you could say is analogous to a password but doesn't contain a username (hopefully that came out right). Here's what I'm thinking: If the user wants "Basic" auth they can continue to do so in the normal way but if they want to use "Bearer" they can still pass in credentials in the normal way but omit the username. So instead of doing:
They could do:
In the case where things are already encoded we'd have to find a way to tell if it's in the format of |
Or ... we could optionally provide the credentials to be passed in like:
OR
To not break the existing api we will default to the former if no Let me know what you think. |
Unlike basic authorization, it is not possible to distinguish a non-encoded string from an encoded string when it comes to bearer authorization. Thus, I would amend your suggestion to remove the reference to encoding:
This gives the following ways of specifying the various authorizations, which is backwards compatible for existing users:
If you think this works, I can complete the pull-request, but I am still not sure how to modify the block of code starting at src/test/java/com/cdancy/bitbucket/rest/TestUtilities.java. |
@martinda away on vacation at the moment but will get back to this sometime this weekend to run some local tests here. However, and looking through this PR briefly on my phone, everything looks OK. |
@martinda been looking over this PR the past couple days and am going to send in some more commits to your branch sometime today. I found a potentially "easier" way to track and define the type of authentication being used after spending some time with your work. Stay tuned and let me know what you think. |
@martinda I refactored things quite a bit. Have a look over and let me know what you think. Some highlights include:
|
FYI: as this is a breaking change I'm mulling over bumping |
@cdancy I like your implementation better. I have not tried it yet, but will probably do so before Monday. You could use the What should be done about src/test/java/com/cdancy/bitbucket/rest/TestUtilities.java ? |
@martinda good catch and need to address that. As base64 encoding, if required, will now be done within the |
…o account the 'identity' property which we've hijacked for our own purposes.
@cdancy I ran the PR against my real world use case and it works. I am okay if you bump the major number since it is a breaking change. |
@martinda thanks for the additional check and confirmation. I'm going to do another round of cleanups today and will merge sometime at the beginning of the week and do a new release. |
@cdancy Thank you. I also want to say that I learned a lot from reading your code, specifically with regards to the builder pattern and in the way externalities like credentials and endpoint are provided to the application. |
…ketAuthentication.
…tion to allow users to easily connect either anonymously, with basic auth, or bearer auth.
…her basic or bearer authentication when running integ tests.
… we can simplify immensely how we pass along authentication headers and in the case of Basic auth no longer have to base64 every credential string upon every request.
…side from being able to supply a new 'token' auth type as part of the builder the only thing client facing that has changed was the introduction of a new constructor for passing in a BitbucketAuthentication object.
…itbucket. Currently this is anonymous, basic, or bearer.
…egy put in place. Many more files were touched on account of various fixes/cleanups that went in over the course of this refactoring.
… if there are plugins installed or not.
@martinda after playing with this for a while I decided to do things the "right" way and more/less do a complete refactor of how authentication is handled. The end-user/client shouldn't see a difference one way or another. Feel free to give things a go if you get a minute. Have a few more things I'd like to add before doing another release. |
@cdancy Thanks again. I ran my tests again and everything is fine. |
Any idea when you will release 1.0.6? I am writing a gradle plugin where we will include your library. But in order to get it working we need to use the personal access tokens. |
By the end of this week. I planned on releasing this past weekend but we had a feature request here to be able to pass in jclouds overrides properties, both through the client and via system properties or environment variables, to configure things in a very devops/CI sort of way much like I do with setting the endpoint and credentials. |
Version |
My first attempt to fix issue #121.
Bitbucket recently added personal access token authorization for accessing the REST API.
Please guide me on how to add test for this feature, because I am not sure the basic and base64 authorization methods were explicitly tested. Specifically, I am not sure how to assign a value to the username in src/test/java/com/cdancy/bitbucket/rest/TestUtilities.java because the Bearer authorization does not need it.
I tested this change against a local bitbucket instance with the following code: