package com.perforce.spark.connection; import java.util.Base64; import java.util.Map; import spark.ModelAndView; import spark.Request; import spark.Response; import com.perforce.p4java.exception.P4JavaException; import com.perforce.p4java.server.IOptionsServer; import com.perforce.p4java.server.callback.ICommandCallback; import com.perforce.spark.ErrorModel; import com.perforce.spark.navigation.NavBar; public class ConnectionSession extends ErrorModel { public static String TICKET = "ticket"; public static String USER = "user"; static String PASS = "pass"; public static boolean basicAuth(Request request, Response response) { String authHeader = request.headers("Proxy-Authorization"); // exit early if not Basic Auth if (authHeader == null || !authHeader.startsWith("Basic ")) { return false; } String baseHeader = authHeader.substring("basic ".length()); byte[] bytes = Base64.getDecoder().decode(baseHeader); String decode = new String(bytes); String bits[] = decode.split(":"); String user = bits[0]; String pass = bits[1]; String ticket; try { ticket = login(user, pass); } catch (P4JavaException e) { return false; } request.session().attribute(ConnectionSession.USER, user); request.session().attribute(ConnectionSession.TICKET, ticket); return true; } public static IOptionsServer get(Request request, Response response) throws P4JavaException { // use Perforce ticket from session String user = request.session().attribute(ConnectionSession.USER); String ticket = request.session().attribute(ConnectionSession.TICKET); try { IOptionsServer p4 = getP4(user, ticket); // Register logging callback ICommandCallback logging = new ConnectionLogging(); p4.registerCallback(logging); return p4; } catch (P4JavaException e) { // wipe out cookie and session logout(request, response); throw e; } } public static boolean isValid(Request request) { String user = request.session().attribute(ConnectionSession.USER); String ticket = request.session().attribute(ConnectionSession.TICKET); try { getP4(user, ticket); return true; } catch (P4JavaException e) { return false; } } public static IOptionsServer getP4(String user, String ticket) throws P4JavaException { String uri = ConnectionConfig.toUri(); IOptionsServer p4 = ConnectionFactory.getConnection(uri); p4.connect(); p4.setUserName(user); p4.setAuthTicket(ticket); // verify connection try { isLogin(p4); } catch (P4JavaException e) { throw e; } return p4; } private static String login(String user, String pass) throws P4JavaException { String uri = ConnectionConfig.toUri(); IOptionsServer p4 = ConnectionFactory.getConnection(uri); // fail if server is down. p4.connect(); // login and use Perforce ticket for session p4.setUserName(user); // fetch ticket String ticket = null; p4.login(pass); // check for null ticket ticket = p4.getAuthTicket(); if (ticket == null) { throw new P4JavaException("Ticket value is null."); } return ticket; } public static ModelAndView login(Request request, Response response) { Map<String, Object> model; model = NavBar.attributes("cover", request); String user = request.queryParams(USER); String pass = request.queryParams(PASS); String ticket = null; try { ticket = login(user, pass); } catch (Exception e) { return error(e); } // store ticket in session request.session().attribute(USER, user); request.session().attribute(TICKET, ticket); // save cookie String store = request.queryParams("store"); if (store != null) { response.cookie(USER, user); response.cookie(TICKET, ticket); } response.redirect("/"); return new ModelAndView(model, "page-cover.html"); } public static void logout(Request request, Response response) { request.session().removeAttribute(USER); request.session().removeAttribute(TICKET); response.removeCookie(USER); response.removeCookie(TICKET); } public static void cookie(Request request, Response response) { String user = request.cookie(USER); String ticket = request.cookie(TICKET); try { getP4(user, ticket); } catch (P4JavaException e) { logout(request, response); return; } if (user != null) { // store ticket in session request.session().attribute(USER, user); request.session().attribute(TICKET, ticket); } } private static void isLogin(IOptionsServer p4) throws P4JavaException { String status = p4.getLoginStatus(); if (status.contains("not necessary")) { return; } if (status.contains("ticket expires in")) { return; } // If there is a broker or something else that swallows the message if (status.isEmpty()) { return; } throw new P4JavaException(status); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 14863 | Paul Allen | Change File/Dir browse to support extended options | ||
#2 | 14742 | Paul Allen | update - still issues with upload. | ||
#1 | 14228 | Paul Allen |
Moved View over to the new model. Retactor connection to package. |
||
//guest/paul_allen/p4am/src/main/java/com/perforce/spark/ConnectionSession.java | |||||
#9 | 14181 | Paul Allen | Lots of updates and refactoring. | ||
#8 | 14017 | Paul Allen | Proxy caching to Perforce and SLF4J logging. | ||
#7 | 14013 | Paul Allen | Basic proxy | ||
#6 | 13869 | Paul Allen |
- Fix logout when testing cookie. - Refactor ftl->html - Add local bootstrap |
||
#5 | 13863 | Paul Allen | Minor fix to session vs cookie for login check. | ||
#4 | 13798 | Paul Allen | Basic file upload and login/logout cookie tidyup | ||
#3 | 13746 | Paul Allen | Basic File/Dir browsing and Cookie management. | ||
#2 | 13728 | Paul Allen | Error support | ||
#1 | 13722 | Paul Allen | Login UX |