private boolean _isAuthenticated(HttpServletRequest request, String serviceUrl) throws IOException { boolean authenticated = false; boolean hasCookieNames = false; String[] cookieNames = _getCookieNames(serviceUrl); for (String cookieName : cookieNames) { if (CookieUtil.get(request, cookieName) != null) { hasCookieNames = true; break; } } if (!hasCookieNames) { if (_log.isWarnEnabled()) { _log.warn("User is not logged in because he has no OpenSSO cookies"); } return false; } String url = serviceUrl.concat(_VALIDATE_TOKEN); URL urlObj = new URL(url); HttpURLConnection httpURLConnection = (HttpURLConnection) urlObj.openConnection(); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); _setCookieProperty(request, httpURLConnection, cookieNames); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream()); outputStreamWriter.write("dummy"); outputStreamWriter.flush(); int responseCode = httpURLConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { String data = StringUtil.read(httpURLConnection.getInputStream()); if (data.toLowerCase().indexOf("boolean=true") != -1) { authenticated = true; } } else if (_log.isDebugEnabled()) { _log.debug("Authentication response code " + responseCode); } return authenticated; }
private void _setCookieProperty( HttpServletRequest request, HttpURLConnection urlc, String[] cookieNames) { if (cookieNames.length == 0) { return; } StringBundler sb = new StringBundler(cookieNames.length * 4); for (String cookieName : cookieNames) { String cookieValue = CookieUtil.get(request, cookieName); sb.append(cookieName); sb.append(StringPool.EQUAL); sb.append(StringPool.QUOTE); sb.append(cookieValue); sb.append(StringPool.QUOTE); sb.append(StringPool.SEMICOLON); } urlc.setRequestProperty("Cookie", sb.toString()); }
private String _getSubjectId(HttpServletRequest request, String serviceUrl) { String cookieName = _getCookieNames(serviceUrl)[0]; return CookieUtil.get(request, cookieName); }