-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bundle-server: implement plugin-based auth
Implement and document auth configurations using user-specified runtime plugins. The goal of this change is to allow users to customize their access control to a more specific application or domain than is supported by built-in modes. A plugin is loaded in 'git-bundle-web-server' from its 'path' by first comparing the file contents to a specified SHA256 checksum; the web server fails to start if there is a mismatch. Otherwise, the plugin is loaded and the specified 'initializer' symbol is looked up. If that symbol exists, the initializer is called, creating the 'AuthMiddleware' instance and/or an error. From there, 'git-bundle-web-server' passes the middleware's 'Authorize' function reference to the created 'bundleWebServer' so that it is invoked after parsing the route of each request. Additionally, update technical documentation and add an example plugin (built from a standalone '.go' file) & config. Signed-off-by: Victoria Dye <[email protected]>
- Loading branch information
Showing
6 changed files
with
287 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ | |
/_docs/ | ||
/_test/ | ||
node_modules/ | ||
|
||
*.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/sha256" | ||
"crypto/subtle" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/git-ecosystem/git-bundle-server/pkg/auth" | ||
) | ||
|
||
type simplePluginAuth struct { | ||
usernameHash [32]byte | ||
} | ||
|
||
// Example auth plugin: basic auth with username "admin" and password | ||
// "{owner}_{repo}" (based on the owner & repo from the route). | ||
// DO NOT USE THIS IN A PRODUCTION BUNDLE SERVER. | ||
func NewSimplePluginAuth(_ json.RawMessage) (auth.AuthMiddleware, error) { | ||
return &simplePluginAuth{ | ||
usernameHash: sha256.Sum256([]byte("admin")), | ||
}, nil | ||
} | ||
|
||
// Nearly identical to Basic auth, but with a per-request password | ||
func (a *simplePluginAuth) Authorize(r *http.Request, owner string, repo string) *auth.AuthResult { | ||
username, password, ok := r.BasicAuth() | ||
if ok { | ||
usernameHash := sha256.Sum256([]byte(username)) | ||
passwordHash := sha256.Sum256([]byte(password)) | ||
|
||
perRoutePasswordHash := sha256.Sum256([]byte(owner + "_" + repo)) | ||
|
||
usernameMatch := (subtle.ConstantTimeCompare(usernameHash[:], a.usernameHash[:]) == 1) | ||
passwordMatch := (subtle.ConstantTimeCompare(passwordHash[:], perRoutePasswordHash[:]) == 1) | ||
|
||
if usernameMatch && passwordMatch { | ||
return auth.Authorized() | ||
} else { | ||
return auth.Forbidden() | ||
} | ||
} | ||
|
||
return auth.Unauthorized(auth.Header("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"mode": "plugin", | ||
"path": "examples/auth/_plugins/simple-plugin.so", | ||
"initializer": "NewSimplePluginAuth", | ||
"sha256": "<plugin SHA256 checksum>" | ||
} |