Skip to content
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

Improve functionality of identity provider token retrieval #8873

Merged
merged 7 commits into from
Feb 23, 2018

Conversation

mshaposhnik
Copy link
Contributor

What does this PR do?

This PR intoduces a dedicated HTTP client for Keycloak operations, which is more accurately covers some cases like:

  • Handling error messages from Keycloak and preserving original HTTP status codes;
  • Fixes constant OAuth error when current user haven't identity provider link

What issues does this PR fix or reference?

#8288

Release Notes

Fix constant OAuth error when current user haven't identity provider link

Docs PR

N/A

*
* @author Max Shaposhnik (mshaposh@redhat.com)
*/
public class KeycloakServiceClient {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be Singleton?


private KeycloakSettings keycloakSettings;

private final Pattern assotiateUserPattern =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be static?

*
* @param oauthProvider provider name
* @return KeycloakTokenResponse token response
* @throws ForbiddenException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like poor javadoc 😸

int delimiterIndex = p.indexOf("=");
queryPairs.put(p.substring(0, delimiterIndex), p.substring(delimiterIndex + 1));
});
return new Gson().toJson(queryPairs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It think it'd better to have singleton GSON instance in static field

@DTO
public interface KeycloakTokenResponse {

String getAccess_token();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to rewrite it in the following way:

@JsonFieldName("access_token")
String getAccessToken();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ups, forgot this trick, tnx

// If user has no link with identity provider yet,
// we should threat this as unauthorized and send to oAuth login page.
throw new UnauthorizedException(e.getMessage());
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove else here

&& contentType != null
&& !(contentType.startsWith(MediaType.APPLICATION_JSON)
|| contentType.startsWith("application/vnd.api+json"))) {
throw new IOException(conn.getResponseMessage());
Copy link
Contributor

@akorneta akorneta Feb 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it is better to throw BadRequestException with message like 'unsupported format' or add meaningful message, wdyt?

@mshaposhnik mshaposhnik merged commit d2fcdb7 into master Feb 23, 2018
@mshaposhnik mshaposhnik deleted the CHE-8288 branch February 23, 2018 08:33
@mshaposhnik
Copy link
Contributor Author

Fixed;

@benoitf benoitf added this to the 6.2.0 milestone Feb 23, 2018
@benoitf benoitf added the kind/bug Outline of a bug - must adhere to the bug report template. label Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Outline of a bug - must adhere to the bug report template.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants