From 8d80685563c8b7a07aa10e4c20208a2e39d1ad7a Mon Sep 17 00:00:00 2001 From: Luca Milanesio <luca@milanesio.org> Date: Sun, 02 Dec 2012 03:53:09 -0500 Subject: [PATCH] Include pom.xml template for publishing GitBlit as Maven artifact. --- src/com/gitblit/utils/HttpUtils.java | 86 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 86 insertions(+), 0 deletions(-) diff --git a/src/com/gitblit/utils/HttpUtils.java b/src/com/gitblit/utils/HttpUtils.java index ad7d58c..b40088c 100644 --- a/src/com/gitblit/utils/HttpUtils.java +++ b/src/com/gitblit/utils/HttpUtils.java @@ -15,7 +15,18 @@ */ package com.gitblit.utils; +import java.security.cert.CertificateExpiredException; +import java.security.cert.CertificateNotYetValidException; +import java.security.cert.X509Certificate; +import java.text.MessageFormat; +import java.util.Date; + import javax.servlet.http.HttpServletRequest; + +import org.slf4j.LoggerFactory; + +import com.gitblit.models.UserModel; +import com.gitblit.utils.X509Utils.X509Metadata; /** * Collection of utility methods for http requests. @@ -92,4 +103,79 @@ sb.append(context); return sb.toString(); } + + /** + * Returns a user model object built from attributes in the SSL certificate. + * This model is not retrieved from the user service. + * + * @param httpRequest + * @param checkValidity ensure certificate can be used now + * @param usernameOIDs if unspecified, CN is used as the username + * @return a UserModel, if a valid certificate is in the request, null otherwise + */ + public static UserModel getUserModelFromCertificate(HttpServletRequest httpRequest, boolean checkValidity, String... usernameOIDs) { + if (httpRequest.getAttribute("javax.servlet.request.X509Certificate") != null) { + X509Certificate[] certChain = (X509Certificate[]) httpRequest + .getAttribute("javax.servlet.request.X509Certificate"); + if (certChain != null) { + X509Certificate cert = certChain[0]; + // ensure certificate is valid + if (checkValidity) { + try { + cert.checkValidity(new Date()); + } catch (CertificateNotYetValidException e) { + LoggerFactory.getLogger(HttpUtils.class).info(MessageFormat.format("X509 certificate {0} is not yet valid", cert.getSubjectDN().getName())); + return null; + } catch (CertificateExpiredException e) { + LoggerFactory.getLogger(HttpUtils.class).info(MessageFormat.format("X509 certificate {0} has expired", cert.getSubjectDN().getName())); + return null; + } + } + return getUserModelFromCertificate(cert, usernameOIDs); + } + } + return null; + } + + /** + * Creates a UserModel from a certificate + * @param cert + * @param usernameOids if unspecified CN is used as the username + * @return + */ + public static UserModel getUserModelFromCertificate(X509Certificate cert, String... usernameOIDs) { + X509Metadata metadata = X509Utils.getMetadata(cert); + + UserModel user = new UserModel(metadata.commonName); + user.emailAddress = metadata.emailAddress; + user.isAuthenticated = false; + + if (usernameOIDs == null || usernameOIDs.length == 0) { + // use default usename<->CN mapping + usernameOIDs = new String [] { "CN" }; + } + + // determine username from OID fingerprint + StringBuilder an = new StringBuilder(); + for (String oid : usernameOIDs) { + String val = metadata.getOID(oid.toUpperCase(), null); + if (val != null) { + an.append(val).append(' '); + } + } + user.username = an.toString().trim(); + return user; + } + + public static X509Metadata getCertificateMetadata(HttpServletRequest httpRequest) { + if (httpRequest.getAttribute("javax.servlet.request.X509Certificate") != null) { + X509Certificate[] certChain = (X509Certificate[]) httpRequest + .getAttribute("javax.servlet.request.X509Certificate"); + if (certChain != null) { + X509Certificate cert = certChain[0]; + return X509Utils.getMetadata(cert); + } + } + return null; + } } -- Gitblit v1.9.1