-
Notifications
You must be signed in to change notification settings - Fork 6k
[Retrofit] add authorizations to retrofit client #1154
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
Conversation
added basic auth, api key and oauth support to the service generator using okhttp interceptors Fix swagger-api#962
removed gson version in pom and added a space between the parameters
Hi @cbornet, I tried the petstore sample and got this error:
I used this code to test: ServiceGenerator gen = new ServiceGenerator("petstore_auth", "test_client_id", "test_client_secret", "test_username", "test_password");
PetApi api = gen.createService(PetApi.class);
List<Pet> pets = api.findPetsByStatus(Arrays.asList("available"));
System.out.println(pets.size()); My guess is that the empty if (authName == "petstore_auth") {
auth = new OauthAuthorization(OauthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} Could you explain how to use each auth with the generated retrofit client? For example, for HTTP basic auth, API key auth, and each of the 4 flows of OAuth2. |
@xhh There is indeed no tokenURL when using implicit flow. So the access token should be set prior to calling the API. You can do this by calling String url = gen.getAuthorizationEndPoint()
.setClientId("your-application-client-id")
.setRedirectURI("http://www.your-web-site.com/redirect")
.buildQueryMessage()
.getLocationUri(); Then open/redirect the navigator (prefered) or a open a webview (discouraged since it breaks the point of 3-legged auth) to this URL. When the user has entered its credential, the 3rd party server will redirect to your http://www.your-web-site.com/redirect with the code or accessToken as parameter which you can use to configure the ServiceGenerator with gen.setAccessToken(token) for implicit flow and gen.getTokenEndPoint().setCode(code) for authorization code. As these flows need the use of components external to Java/retrofit such as webviews or a web server, they cannot really be included in a generic client lib. To test the other flows you can do : //Api key
ServiceGenerator gen = new ServiceGenerator("api_key", "my_api_key");
//Basic auth
ServiceGenerator gen = new ServiceGenerator("basic", "my_login", "my_password");
//Password oauth
ServiceGenerator gen = new ServiceGenerator("password_oauth", "my_client_id", "my_client_secret", "my_login", "my_password");
//Application oauth
ServiceGenerator gen = new ServiceGenerator("application_oauth");
gen.getTokenEndPoint().setClientId("my_client_id").setClientSecret("my_client_secret"); I agree that a generated README would be useful. I will add one when I have time. |
@cbornet Thanks for the explanation!
|
"security": [
{
"api_key": [],
"petstore_auth": [
"write:pets",
"read:pets"
]
}
] Since it is the application role to chose which authorization to use, it is not enforced automatically onto the operation (it could be if the operation has only one way to authenticate such as getInventory but I don't see how to do that properly with the generator). |
If no one has further question, I'll merge this PR tomorrow (Wed) |
[Retrofit] add authorizations to retrofit client
Added basic auth, api key and oauth support to the service generator using okhttp interceptors.
For oauth2, the apache oltu lib is used.
Fix #962