All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.adorsys.oauth.authdispatcher.matcher.RememberMeAuthMatcher Maven / Gradle / Ivy

There is a newer version: 0.35
Show newest version
/**
 * Copyright (C) 2015 Daniel Straub, Sandro Sonntag, Christian Brandenstein, Francis Pouatcha ([email protected], [email protected], [email protected], [email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.adorsys.oauth.authdispatcher.matcher;

import com.nimbusds.oauth2.sdk.AuthorizationRequest;

import org.apache.catalina.authenticator.AuthenticatorBase;
import org.apache.catalina.connector.Request;
import org.apache.catalina.deploy.LoginConfig;
import org.apache.catalina.valves.ValveBase;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.security.Principal;

public class RememberMeAuthMatcher extends BaseAuthenticatorMatcher {

	public RememberMeAuthMatcher() {
		valve = new AuthenticatorBase() {
			
			@Override
			protected boolean authenticate(Request request, HttpServletResponse response, LoginConfig lc) throws IOException {
				Principal principal = context.getRealm().authenticate("guest", "test");
				if (principal != null) {
					register(request, response, principal, "FORM", "guest", "test");
					return true;
				}
				return false;
			}
		};
	}


	@Override
	public ValveBase match(HttpServletRequest request, AuthorizationRequest authorizationRequest) {
		if (!request.getRequestURI().endsWith("/auth") || authorizationRequest == null || authorizationRequest.getClientID() == null) {
			return null;
		}
		if(!shouldRememberMe(request)) {
			return null;
		}
		Cookie cookieToken = getCookieToken(authorizationRequest.getClientID().getValue(), request);
		return cookieToken != null ? valve : null;
	}
	
	private Cookie getCookieToken(String clientId, HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null) {
			return null;
		}
		for (Cookie cookie : cookies) {
			if (cookie.getName().equals("REMEMBER_" + clientId) && StringUtils.isNotEmpty(cookie.getValue())) {
				return cookie;
			}
		}
		return null;
	}

	private boolean shouldRememberMe(HttpServletRequest request) {
		// Poor man's way to default to true
		return !"false".equals(request.getParameter("rememberme"));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy