mirror of
https://github.com/google/nomulus.git
synced 2025-05-14 16:37:13 +02:00
Clean up the tattered shreds of SessionMetadata
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=125334811
This commit is contained in:
parent
2a3a3fbc30
commit
bb82f5bc05
11 changed files with 169 additions and 185 deletions
|
@ -14,42 +14,77 @@
|
|||
|
||||
package google.registry.flows;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static com.google.common.base.MoreObjects.toStringHelper;
|
||||
import static google.registry.util.CollectionUtils.nullToEmpty;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Optional;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
/** A metadata class that is a wrapper around {@link HttpSession}. */
|
||||
public class HttpSessionMetadata extends SessionMetadata {
|
||||
public class HttpSessionMetadata implements SessionMetadata {
|
||||
|
||||
private static final String CLIENT_ID = "CLIENT_ID";
|
||||
private static final String SERVICE_EXTENSIONS = "SERVICE_EXTENSIONS";
|
||||
private static final String FAILED_LOGIN_ATTEMPTS = "FAILED_LOGIN_ATTEMPTS";
|
||||
|
||||
private final HttpSession session;
|
||||
private boolean isValid = true;
|
||||
|
||||
public HttpSessionMetadata(HttpSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkValid() {
|
||||
checkState(isValid, "This session has been invalidated.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate() {
|
||||
session.invalidate();
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setProperty(String key, Object value) {
|
||||
if (value == null) {
|
||||
session.removeAttribute(key);
|
||||
} else {
|
||||
session.setAttribute(key, value);
|
||||
}
|
||||
public String getClientId() {
|
||||
return (String) session.getAttribute(CLIENT_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getProperty(String key) {
|
||||
return session.getAttribute(key);
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<String> getServiceExtensionUris() {
|
||||
return (Set<String>) session.getAttribute(SERVICE_EXTENSIONS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClientId(String clientId) {
|
||||
session.setAttribute(CLIENT_ID, clientId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServiceExtensionUris(Set<String> serviceExtensionUris) {
|
||||
session.setAttribute(SERVICE_EXTENSIONS, serviceExtensionUris);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFailedLoginAttempts() {
|
||||
return Optional.fromNullable((Integer) session.getAttribute(FAILED_LOGIN_ATTEMPTS)).or(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incrementFailedLoginAttempts() {
|
||||
session.setAttribute(FAILED_LOGIN_ATTEMPTS, getFailedLoginAttempts() + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetFailedLoginAttempts() {
|
||||
session.removeAttribute(FAILED_LOGIN_ATTEMPTS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toStringHelper(getClass())
|
||||
.add("system hash code", System.identityHashCode(this))
|
||||
.add("clientId", getClientId())
|
||||
.add("failedLoginAttempts", getFailedLoginAttempts())
|
||||
.add("serviceExtensionUris", Joiner.on('.').join(nullToEmpty(getServiceExtensionUris())))
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,95 +14,29 @@
|
|||
|
||||
package google.registry.flows;
|
||||
|
||||
import static com.google.common.base.MoreObjects.toStringHelper;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static google.registry.util.CollectionUtils.nullToEmpty;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Optional;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/** Class to allow setting and retrieving session information in flows. */
|
||||
public abstract class SessionMetadata {
|
||||
|
||||
/** The key used for looking up the current client id on the session object. */
|
||||
protected static final String CLIENT_ID_KEY = "CLIENT_ID";
|
||||
|
||||
/** The key used for looking up the service extensions on the session object. */
|
||||
protected static final String SERVICE_EXTENSIONS_KEY = "SERVICE_EXTENSIONS";
|
||||
|
||||
/** The key used for looking up the number of failed login attempts. */
|
||||
protected static final String FAILED_LOGIN_ATTEMPTS_KEY = "FAILED_LOGIN_ATTEMPTS";
|
||||
|
||||
protected abstract void setProperty(String key, Object value);
|
||||
|
||||
protected abstract Object getProperty(String key);
|
||||
/** Object to allow setting and retrieving session information in flows. */
|
||||
public interface SessionMetadata {
|
||||
|
||||
/**
|
||||
* Invalidates the session. A new instance must be created after this for future sessions.
|
||||
* Attempts to invoke methods of this class after this method has been called will throw
|
||||
* {@code IllegalStateException}.
|
||||
*/
|
||||
public abstract void invalidate();
|
||||
void invalidate();
|
||||
|
||||
/** Subclasses can override this to verify that this is a valid session. */
|
||||
protected void checkValid() {}
|
||||
String getClientId();
|
||||
|
||||
/** Check that the session is valid and set a property. */
|
||||
private void setPropertyChecked(String key, Object value) {
|
||||
checkValid();
|
||||
setProperty(key, value);
|
||||
}
|
||||
Set<String> getServiceExtensionUris();
|
||||
|
||||
/**
|
||||
* Check that the session is valid and get a property as a given type.
|
||||
*
|
||||
* @param clazz type to return, specified as a param to enforce typesafe generics
|
||||
* @see "http://errorprone.info/bugpattern/TypeParameterUnusedInFormals"
|
||||
*/
|
||||
private <T> T getProperty(Class<T> clazz, String key) {
|
||||
checkValid();
|
||||
return clazz.cast(getProperty(key));
|
||||
}
|
||||
void setClientId(String clientId);
|
||||
|
||||
public String getClientId() {
|
||||
return getProperty(String.class, CLIENT_ID_KEY);
|
||||
}
|
||||
void setServiceExtensionUris(Set<String> serviceExtensionUris);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<String> getServiceExtensionUris() {
|
||||
return getProperty(Set.class, SERVICE_EXTENSIONS_KEY);
|
||||
}
|
||||
int getFailedLoginAttempts();
|
||||
|
||||
public void setClientId(String clientId) {
|
||||
setPropertyChecked(CLIENT_ID_KEY, clientId);
|
||||
}
|
||||
void incrementFailedLoginAttempts();
|
||||
|
||||
public void setServiceExtensionUris(Set<String> serviceExtensionUris) {
|
||||
setPropertyChecked(SERVICE_EXTENSIONS_KEY, checkNotNull(serviceExtensionUris));
|
||||
}
|
||||
|
||||
public int getFailedLoginAttempts() {
|
||||
return Optional.fromNullable(getProperty(Integer.class, FAILED_LOGIN_ATTEMPTS_KEY))
|
||||
.or(0);
|
||||
}
|
||||
|
||||
public void incrementFailedLoginAttempts() {
|
||||
setPropertyChecked(FAILED_LOGIN_ATTEMPTS_KEY, getFailedLoginAttempts() + 1);
|
||||
}
|
||||
|
||||
public void resetFailedLoginAttempts() {
|
||||
setPropertyChecked(FAILED_LOGIN_ATTEMPTS_KEY, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toStringHelper(getClass())
|
||||
.add("system hash code", System.identityHashCode(this))
|
||||
.add("clientId", getClientId())
|
||||
.add("failedLoginAttempts", getFailedLoginAttempts())
|
||||
.add("serviceExtensionUris", Joiner.on('.').join(nullToEmpty(getServiceExtensionUris())))
|
||||
.toString();
|
||||
}
|
||||
void resetFailedLoginAttempts();
|
||||
}
|
||||
|
|
|
@ -14,19 +14,25 @@
|
|||
|
||||
package google.registry.flows;
|
||||
|
||||
import static com.google.common.base.MoreObjects.toStringHelper;
|
||||
import static dagger.internal.Preconditions.checkNotNull;
|
||||
import static google.registry.util.CollectionUtils.nullToEmpty;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/** A read-only {@link SessionMetadata} that doesn't support login/logout. */
|
||||
public class StatelessRequestSessionMetadata extends SessionMetadata {
|
||||
public class StatelessRequestSessionMetadata implements SessionMetadata {
|
||||
|
||||
private final String clientId;
|
||||
private final Set<String> serviceExtensionUris;
|
||||
private final ImmutableSet<String> serviceExtensionUris;
|
||||
|
||||
public StatelessRequestSessionMetadata(
|
||||
String clientId,
|
||||
Set<String> serviceExtensionUris) {
|
||||
this.clientId = clientId;
|
||||
this.serviceExtensionUris = serviceExtensionUris;
|
||||
String clientId, ImmutableSet<String> serviceExtensionUris) {
|
||||
this.clientId = checkNotNull(clientId);
|
||||
this.serviceExtensionUris = checkNotNull(serviceExtensionUris);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -45,14 +51,38 @@ public class StatelessRequestSessionMetadata extends SessionMetadata {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected void setProperty(String key, Object value) {
|
||||
public void setClientId(String clientId) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getProperty(String key) {
|
||||
// We've overridden the getters of all of the properties that we care about. Return null for
|
||||
// everything else so that toString() continues to work.
|
||||
return null;
|
||||
public void setServiceExtensionUris(Set<String> serviceExtensionUris) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFailedLoginAttempts() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incrementFailedLoginAttempts() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetFailedLoginAttempts() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toStringHelper(getClass())
|
||||
.add("system hash code", System.identityHashCode(this))
|
||||
.add("clientId", getClientId())
|
||||
.add("serviceExtensionUris", Joiner.on('.').join(nullToEmpty(getServiceExtensionUris())))
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@ import com.beust.jcommander.Parameters;
|
|||
|
||||
import google.registry.flows.EppRequestSource;
|
||||
import google.registry.flows.FlowRunner;
|
||||
import google.registry.flows.HttpSessionMetadata;
|
||||
import google.registry.flows.SessionMetadata;
|
||||
import google.registry.flows.TlsCredentials;
|
||||
import google.registry.flows.session.LoginFlow;
|
||||
import google.registry.model.eppcommon.Trid;
|
||||
|
@ -42,11 +42,11 @@ import google.registry.tools.Command.GtechCommand;
|
|||
import google.registry.tools.Command.RemoteApiCommand;
|
||||
import google.registry.tools.params.PathParameter;
|
||||
import google.registry.tools.soy.LoginSoyInfo;
|
||||
import google.registry.util.BasicHttpSession;
|
||||
import google.registry.util.SystemClock;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
|
@ -100,12 +100,13 @@ final class ValidateLoginCredentialsCommand implements RemoteApiCommand, GtechCo
|
|||
.setData(new SoyMapData("clientIdentifier", clientIdentifier, "password", password))
|
||||
.render()
|
||||
.getBytes(UTF_8);
|
||||
|
||||
System.out.println(new String(marshalWithLenientRetry(
|
||||
new FlowRunner(
|
||||
LoginFlow.class,
|
||||
unmarshal(EppInput.class, inputXmlBytes),
|
||||
Trid.create(null),
|
||||
new HttpSessionMetadata(new BasicHttpSession()),
|
||||
new StubSessionMetadata(),
|
||||
new TlsCredentials(
|
||||
clientCertificateHash,
|
||||
Optional.of(clientIpAddress),
|
||||
|
@ -117,4 +118,38 @@ final class ValidateLoginCredentialsCommand implements RemoteApiCommand, GtechCo
|
|||
null,
|
||||
new SystemClock()).run()), UTF_8));
|
||||
}
|
||||
|
||||
/** A {@link SessionMetadata} that ignores setters rather than throwing exceptions. */
|
||||
private static class StubSessionMetadata implements SessionMetadata {
|
||||
|
||||
@Override
|
||||
public void setClientId(String clientId) {}
|
||||
|
||||
@Override
|
||||
public void setServiceExtensionUris(Set<String> serviceExtensionUris) {}
|
||||
|
||||
@Override
|
||||
public void incrementFailedLoginAttempts() {}
|
||||
|
||||
@Override
|
||||
public void resetFailedLoginAttempts() {}
|
||||
|
||||
@Override
|
||||
public void invalidate() {}
|
||||
|
||||
@Override
|
||||
public String getClientId() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getServiceExtensionUris() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFailedLoginAttempts() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,127 +0,0 @@
|
|||
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 google.registry.util;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.servlet.http.HttpSessionContext;
|
||||
|
||||
/** An {@link HttpSession} that only provides support for getting/setting attributes. */
|
||||
@SuppressWarnings("deprecation")
|
||||
public class BasicHttpSession implements HttpSession {
|
||||
|
||||
private final Map<String, Object> map = new HashMap<>();
|
||||
boolean isValid = true;
|
||||
|
||||
@Override
|
||||
public long getCreationTime() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastAccessedTime() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletContext getServletContext() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxInactiveInterval() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpSessionContext getSessionContext() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(@Nullable String name) {
|
||||
checkValid();
|
||||
return map.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue(@Nullable String name) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<?> getAttributeNames() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getValueNames() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(@Nullable String name, @Nullable Object value) {
|
||||
checkValid();
|
||||
map.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putValue(@Nullable String name, @Nullable Object value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAttribute(@Nullable String name) {
|
||||
checkValid();
|
||||
map.remove(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeValue(@Nullable String name) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate() {
|
||||
isValid = false;
|
||||
map.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNew() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private void checkValid() {
|
||||
if (!isValid) {
|
||||
throw new IllegalStateException("This session has been invalidated.");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue