use of com.haulmont.cuba.security.global.IdpSession in project cuba by cuba-platform.
the class IdpAuthController method authenticate.
protected OAuth2AccessTokenResult authenticate(String idpTicket, Locale locale, String ipAddress, Map<String, String> parameters) {
IdpSession idpSession = getIdpSession(idpTicket);
if (idpSession == null) {
log.info("REST API authentication failed for IDP ticket: {} {}", idpTicket, ipAddress);
throw new BadCredentialsException("Bad credentials");
}
if (restApiConfig.getStandardAuthenticationUsers().contains(idpSession.getLogin())) {
log.info("User {} is not allowed to use external login in REST API", idpSession.getLogin());
throw new BadCredentialsException("Bad credentials");
}
OAuthTokenIssuer.OAuth2AccessTokenRequest tokenRequest = new OAuthTokenIssuer.OAuth2AccessTokenRequest();
tokenRequest.setLogin(idpSession.getLogin());
tokenRequest.setLocale(locale);
tokenRequest.setTokenDetails(ImmutableMap.of(IDP_SESSION_ID_TOKEN_ATTRIBUTE, idpSession.getId()));
return oAuthTokenIssuer.issueToken(tokenRequest);
}
use of com.haulmont.cuba.security.global.IdpSession in project cuba by cuba-platform.
the class IdpAuthController method getIdpSession.
@Nullable
protected IdpSession getIdpSession(String idpTicket) throws InvalidGrantException {
String idpBaseURL = this.idpBaseURL;
if (!idpBaseURL.endsWith("/")) {
idpBaseURL += "/";
}
String idpTicketActivateUrl = idpBaseURL + "service/activate";
HttpPost httpPost = new HttpPost(idpTicketActivateUrl);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("serviceProviderTicket", idpTicket), new BasicNameValuePair("trustedServicePassword", idpTrustedServicePassword)), StandardCharsets.UTF_8);
httpPost.setEntity(formEntity);
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
String idpResponse;
try {
HttpResponse httpResponse = client.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 410) {
// used old ticket
return null;
}
if (statusCode != 200) {
throw new RuntimeException("Idp respond with status " + statusCode);
}
idpResponse = new BasicResponseHandler().handleResponse(httpResponse);
} catch (IOException e) {
throw new RuntimeException("Unable to connect to IDP", e);
} finally {
connectionManager.shutdown();
}
IdpSession session;
try {
session = new Gson().fromJson(idpResponse, IdpSession.class);
} catch (JsonSyntaxException e) {
throw new RuntimeException("Unable to parse idp response", e);
}
return session;
}
use of com.haulmont.cuba.security.global.IdpSession in project cuba by cuba-platform.
the class IdpServiceController method pingSession.
@RequestMapping(value = "ping", method = RequestMethod.POST)
public void pingSession(@RequestParam("idpSessionId") String idpSessionId, @RequestParam("trustedServicePassword") String trustedServicePassword, HttpServletResponse response) {
if (!Objects.equals(idpConfig.getTrustedServicePassword(), trustedServicePassword)) {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
log.warn("Incorrect trusted client password has been passed {}", trustedServicePassword);
return;
}
log.debug("Ping IDP session {}", idpSessionId);
IdpSession idpSession = idpService.getSession(idpSessionId);
if (idpSession == null) {
log.debug("IDP Session not found for id {}", idpSessionId);
response.setStatus(HttpStatus.GONE.value());
}
log.debug("IDP session {} ping successful", idpSession);
}
Aggregations