/** * Retrieves the username from the HTTP context; the default is to read from the session cookie. * * @param ctx the current request context * @return null if the user is not authenticated. * * @deprecated Deprecated as of 2.7.0. Use {@link #getUsername(Request)} instead. */ @Deprecated public String getUsername(Context ctx) { return ctx.session().get("username"); }
/** * Puts a new value into the current session. * * @param key the key to set into the session * @param value the value to set for <code>key</code> * * @deprecated Deprecated as of 2.7.0. Use {@link Result} instead. */ @Deprecated public static void session(String key, String value) { session().put(key, value); }
/** * Creates a new HTTP context. * * @param id the unique context ID * @param header the request header * @param request the request with body * @param sessionData the session data extracted from the session cookie * @param flashData the flash data extracted from the flash cookie * @param args any arbitrary data to associate with this request context. * @param lang the transient lang to use. * @param components the context components. */ public Context(Long id, play.api.mvc.RequestHeader header, Request request, Map<String,String> sessionData, Map<String,String> flashData, Map<String,Object> args, Lang lang, JavaContextComponents components) { this(id, header, request, new Response(), new Session(sessionData), new Flash(flashData), new HashMap<>(args), lang, components); }
/** * {@inheritDoc} */ public String getOrCreateSessionId(final WebContext context) { final Http.Session session = getPlayWebContext(context).getJavaSession(); // get current sessionId String sessionId = session.get(Pac4jConstants.SESSION_ID); logger.trace("retrieved sessionId: {}", sessionId); // if null, generate a new one if (sessionId == null) { // generate id for session sessionId = java.util.UUID.randomUUID().toString(); logger.debug("generated sessionId: {}", sessionId); // and save it to session session.put(Pac4jConstants.SESSION_ID, sessionId); } return sessionId; }
public Result logout() { CommonHelper.assertNotBlank(Pac4jConstants.DEFAULT_URL, this.defaultUrl); CommonHelper.assertNotBlank(Pac4jConstants.LOGOUT_URL_PATTERN, this.logoutUrlPattern); final WebContext context = new PlayWebContext(ctx(), config.getSessionStore()); final ProfileManager manager = new ProfileManager(context); manager.logout(); ctx().session().remove(Pac4jConstants.SESSION_ID); final String url = context.getRequestParameter(Pac4jConstants.URL); if (url == null) { return ok(); } else { if (Pattern.matches(this.logoutUrlPattern, url)) { return redirect(url); } else { return redirect(this.defaultUrl); } } }
@Override public void set(final PlayWebContext context, final String key, final Object value) { logger.trace("set, key = {}, value = {}", key, value); Object clearedValue = value; if (key.contentEquals(Pac4jConstants.USER_PROFILES)) { clearedValue = clearUserProfiles(value); } final Http.Session session = context.getJavaSession(); byte[] javaSerBytes = JAVA_SER_HELPER.serializeToBytes((Serializable) clearedValue); String serialized = Base64.encodeBase64String(dataEncrypter.encrypt(compressBytes(javaSerBytes))); if (serialized != null) { logger.trace("set, key = {} -> serialized token size = {}", key, serialized.length()); } else { logger.trace("set, key = {} -> null serialized token", key); } session.put(keyPrefix + key, serialized); }
@Override public void set(final PlayWebContext context, final String key, final Object value) { logger.trace("set, key = {}, value = {}", key, value); Object clearedValue = value; if (key.contentEquals(Pac4jConstants.USER_PROFILES)) { clearedValue = clearUserProfiles(value); } final Http.Session session = context.getJavaSession(); byte[] javaSerBytes = JAVA_SER_HELPER.serializeToBytes((Serializable) clearedValue); String serialized = Base64.encodeBase64String(dataEncrypter.encrypt(compressBytes(javaSerBytes))); if (serialized != null) { logger.trace("set, key = {} -> serialized token size = {}", key, serialized.length()); } else { logger.trace("set, key = {} -> null serialized token", key); } session.put(keyPrefix + key, serialized); }
@Override public String getOrCreateSessionId(final PlayWebContext context) { final Http.Session session = context.getJavaSession(); // get current sessionId String sessionId = session.get(Pac4jConstants.SESSION_ID); logger.trace("retrieved sessionId: {}", sessionId); // if null, generate a new one if (sessionId == null) { // generate id for session sessionId = java.util.UUID.randomUUID().toString(); logger.debug("generated sessionId: {}", sessionId); // and save it to session session.put(Pac4jConstants.SESSION_ID, sessionId); } return sessionId; }
@Override public String getOrCreateSessionId(final PlayWebContext context) { final Http.Session session = context.getJavaSession(); // get current sessionId String sessionId = session.get(Pac4jConstants.SESSION_ID); logger.trace("retrieved sessionId: {}", sessionId); // if null, generate a new one if (sessionId == null) { // generate id for session sessionId = java.util.UUID.randomUUID().toString(); logger.debug("generated sessionId: {}", sessionId); // and save it to session session.put(Pac4jConstants.SESSION_ID, sessionId); } return sessionId; }
@Override public Object get(final PlayWebContext context, final String key) { final Http.Session session = context.getJavaSession(); String sessionValue = session.get(keyPrefix + key); if (sessionValue == null) { logger.trace("get, key = {} -> null", key); return null; } else { byte[] inputBytes = Base64.decodeBase64(sessionValue); final Object value = JAVA_SER_HELPER.unserializeFromBytes(uncompressBytes(dataEncrypter.decrypt(inputBytes))); logger.trace("get, key = {} -> value = {}", key, value); return value; } }
@Override public Object get(final PlayWebContext context, final String key) { final Http.Session session = context.getJavaSession(); String sessionValue = session.get(keyPrefix + key); if (sessionValue == null) { logger.trace("get, key = {} -> null", key); return null; } else { byte[] inputBytes = Base64.decodeBase64(sessionValue); final Object value = JAVA_SER_HELPER.unserializeFromBytes(uncompressBytes(dataEncrypter.decrypt(inputBytes))); logger.trace("get, key = {} -> value = {}", key, value); return value; } }
/** * {@inheritDoc} */ @Override public Optional<String> findValueByKey(final String key) { final Optional<String> value = Optional.ofNullable(session.get(key)); if (value.isPresent()) { logger.debug("Loaded from session \"{}\" = {}", key, value.get()); } else { logger.debug("Not found in session \"{}\"", key); } return value; }
@Override public void remove(String key) { Http.Context.current().session().remove(key); }
@Override public boolean renewSession(final PlayWebContext context) { final String oldSessionId = this.getOrCreateSessionId(context); final Map<String, Object> oldData = store.get(getPrefixedSessionKey(oldSessionId)); final Http.Session session = context.getJavaSession(); session.remove(Pac4jConstants.SESSION_ID); final String newSessionId = this.getOrCreateSessionId(context); if (oldData != null) { store.set(getPrefixedSessionKey(newSessionId), oldData); } logger.debug("Renewing session: {} -> {}", oldSessionId, newSessionId); return true; }
@Override public Object getTrackableSession(final PlayWebContext context) { return context.getJavaSession().get(Pac4jConstants.SESSION_ID); }
@Override public void put(String key, String value) { Http.Context.current().session().put(key, value); }
@Override public String get(String key) { return Http.Context.current().session().get(key); }
private static Optional<CountryCode> findCurrentCountry(final Http.Context context) { return Optional.ofNullable(context.session().get(SESSION_COUNTRY)) .map(countryInSession -> CountryCode.getByCode(countryInSession, false)); }
/** * {@inheritDoc} */ @Override public void removeValueByKey(final String key) { final String oldValue = session.remove(key); logger.debug("Removed from session \"{}\" = {}", key, oldValue); } }