mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 05:55:51 +01:00
Add BASIC Authentication for Git repository.
This commit is contained in:
52
src/main/scala/app/BasicAuthenticationFilter.scala
Normal file
52
src/main/scala/app/BasicAuthenticationFilter.scala
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import javax.servlet._
|
||||||
|
import javax.servlet.http._
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides BASIC Authentication for [[app.GitRepositoryServlet]].
|
||||||
|
*/
|
||||||
|
class BasicAuthenticationFilter extends Filter {
|
||||||
|
|
||||||
|
def init(config: FilterConfig) = {}
|
||||||
|
|
||||||
|
def destroy(): Unit = {}
|
||||||
|
|
||||||
|
def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain): Unit = {
|
||||||
|
val request = req.asInstanceOf[HttpServletRequest]
|
||||||
|
val response = res.asInstanceOf[HttpServletResponse]
|
||||||
|
val session = request.getSession
|
||||||
|
|
||||||
|
try {
|
||||||
|
session.getAttribute("USER_INFO") match {
|
||||||
|
case null => request.getHeader("Authorization") match {
|
||||||
|
case null => requireAuth(response)
|
||||||
|
case auth => decodeAuthHeader(auth).split(":") match {
|
||||||
|
// TODO authenticate using registered user info
|
||||||
|
case Array(username, password) if(username == "gitbucket" && password == "password") => {
|
||||||
|
session.setAttribute("USER_INFO", "gitbucket")
|
||||||
|
chain.doFilter(req, res)
|
||||||
|
}
|
||||||
|
case _ => requireAuth(response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case user => chain.doFilter(req, res)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
case _: Exception => requireAuth(response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private def requireAuth(response: HttpServletResponse): Unit = {
|
||||||
|
response.setHeader("WWW-Authenticate", "BASIC realm=\"GitBucket\"")
|
||||||
|
response.sendError(HttpServletResponse.SC_UNAUTHORIZED)
|
||||||
|
}
|
||||||
|
|
||||||
|
private def decodeAuthHeader(header: String): String = {
|
||||||
|
try {
|
||||||
|
new String(new sun.misc.BASE64Decoder().decodeBuffer(header.substring(6)))
|
||||||
|
} catch {
|
||||||
|
case _ => ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/main/scala/app/GitRepositoryServlet.scala
Normal file
34
src/main/scala/app/GitRepositoryServlet.scala
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import javax.servlet.ServletConfig
|
||||||
|
import javax.servlet.ServletException
|
||||||
|
import org.eclipse.jgit.http.server.GitServlet
|
||||||
|
import javax.servlet.ServletContext
|
||||||
|
import util.Directory
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides Git repository via HTTP.
|
||||||
|
*
|
||||||
|
* This servlet provides only Git repository functionality.
|
||||||
|
* Authentication is provided by [[app.BasicAuthenticationFilter]].
|
||||||
|
*/
|
||||||
|
class GitRepositoryServlet extends GitServlet {
|
||||||
|
|
||||||
|
override def init(config: ServletConfig): Unit = {
|
||||||
|
super.init(new ServletConfig(){
|
||||||
|
def getInitParameter(name: String): String = name match {
|
||||||
|
case "base-path" => Directory.RepositoryHome
|
||||||
|
case "export-all" => "true"
|
||||||
|
case name => config.getInitParameter(name)
|
||||||
|
}
|
||||||
|
def getInitParameterNames(): java.util.Enumeration[String] = {
|
||||||
|
config.getInitParameterNames
|
||||||
|
}
|
||||||
|
|
||||||
|
def getServletContext(): ServletContext = config.getServletContext
|
||||||
|
def getServletName(): String = config.getServletName
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -14,21 +14,23 @@
|
|||||||
</listener>
|
</listener>
|
||||||
|
|
||||||
<servlet>
|
<servlet>
|
||||||
<servlet-name>GitServlet</servlet-name>
|
<servlet-name>GitRepositoryServlet</servlet-name>
|
||||||
<servlet-class>org.eclipse.jgit.http.server.GitServlet</servlet-class>
|
<servlet-class>app.GitRepositoryServlet</servlet-class>
|
||||||
<init-param>
|
|
||||||
<param-name>base-path</param-name>
|
|
||||||
<param-value>C:/Users/takezoe/gitbucket/repositories</param-value>
|
|
||||||
</init-param>
|
|
||||||
<init-param>
|
|
||||||
<param-name>export-all</param-name>
|
|
||||||
<param-value>true</param-value>
|
|
||||||
</init-param>
|
|
||||||
</servlet>
|
</servlet>
|
||||||
|
|
||||||
<servlet-mapping>
|
<servlet-mapping>
|
||||||
<servlet-name>GitServlet</servlet-name>
|
<servlet-name>GitRepositoryServlet</servlet-name>
|
||||||
<url-pattern>/git/*</url-pattern>
|
<url-pattern>/git/*</url-pattern>
|
||||||
</servlet-mapping>
|
</servlet-mapping>
|
||||||
|
|
||||||
|
<filter>
|
||||||
|
<filter-name>BasicAuthenticationFilter</filter-name>
|
||||||
|
<filter-class>app.BasicAuthenticationFilter</filter-class>
|
||||||
|
</filter>
|
||||||
|
|
||||||
|
<filter-mapping>
|
||||||
|
<filter-name>BasicAuthenticationFilter</filter-name>
|
||||||
|
<url-pattern>/git/*</url-pattern>
|
||||||
|
</filter-mapping>
|
||||||
|
|
||||||
</web-app>
|
</web-app>
|
||||||
Reference in New Issue
Block a user