Skip to content

Commit 02a28e8

Browse files
committed
Release 1.2.0: Added basic authentication
2 parents e34b0b4 + a10785c commit 02a28e8

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Diff for: CHANGELOG.yml

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Semantic changelog: https://github.com/c4s4/changelog
22

3+
- version: 1.2.0
4+
date: 2015-07-28
5+
summary: Added basic authentication
6+
added:
7+
- "Added basic authentication, passing auth file path on command line."
8+
39
- version: 1.1.0
410
date: 2015-07-28
511
summary: Added package upload

Diff for: README.md

+12
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@ Options on command line :
3434
- *-path* to set the URL path (defaults to *simple*).
3535
- *-root* to set the directory where packages are living (defaults to current directory).
3636
- *-shop* to set the URL of the shop for packages that are not found.
37+
- *-auth* to set the path to the authentication file
3738

3839
The server outputs logs on the terminal. To get help on the console, type `cheeseshop -help`.
3940

41+
The authentication file is made of lines with the username and the MD5 sum of the password separated with a space, such as (for user *foo* with password *bar*):
42+
43+
foo 37b51d194a7513e45b56f6524f2d51f2
44+
45+
To compute MD5 sum for a given password, in order to fill the authentication file, you may type following command :
46+
47+
$ echo -n bar | md5sum
48+
37b51d194a7513e45b56f6524f2d51f2 -
49+
50+
If no *-auth* option is set on command line, you won't have to authenticate to upload a package to *CheeseShop*.
51+
4052
*Enjoy!*

Diff for: cheeseshop.go

+42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"bufio"
5+
"crypto/md5"
46
"flag"
57
"fmt"
68
"io"
@@ -23,6 +25,9 @@ var port = flag.Int("port", 8000, "The port CheeseShop is listening")
2325
var path = flag.String("path", "simple", "The URL path")
2426
var root = flag.String("root", ".", "The root directory for packages")
2527
var shop = flag.String("shop", "http://pypi.python.org", "Redirection when not found")
28+
var auth = flag.String("auth", "", "Path to the authentication file")
29+
30+
var users *map[string]string
2631

2732
func listRoot(w http.ResponseWriter, r *http.Request) {
2833
log.Printf("Listing root %s", *root)
@@ -74,6 +79,16 @@ func servePackage(dir, file string, w http.ResponseWriter, r *http.Request) {
7479
}
7580

7681
func copyFile(w http.ResponseWriter, r *http.Request) {
82+
if users != nil {
83+
u := *users
84+
username, password, ok := r.BasicAuth()
85+
sum := fmt.Sprintf("%x", md5.Sum([]byte(password)))
86+
if !ok || u[username] != sum {
87+
log.Printf("Unauthorized access from %s", username)
88+
http.Error(w, "Not Authorized", http.StatusUnauthorized)
89+
return
90+
}
91+
}
7792
err := r.ParseMultipartForm(100000)
7893
if err != nil {
7994
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -122,6 +137,32 @@ func handler(w http.ResponseWriter, r *http.Request) {
122137
}
123138
}
124139

140+
func loadAuthFile(file string) *map[string]string {
141+
if file == "" {
142+
return nil
143+
} else {
144+
a := make(map[string]string)
145+
f, err := os.Open(file)
146+
defer f.Close()
147+
if err != nil {
148+
log.Fatalf("Error opening authentication file %s", file)
149+
}
150+
scanner := bufio.NewScanner(f)
151+
scanner.Split(bufio.ScanLines)
152+
for scanner.Scan() {
153+
line := strings.TrimSpace(scanner.Text())
154+
if line != "" {
155+
parts := strings.Split(line, " ")
156+
if len(parts) != 2 {
157+
log.Fatalf("Error reading authentication file")
158+
}
159+
a[parts[0]] = parts[1]
160+
}
161+
}
162+
return &a
163+
}
164+
}
165+
125166
func parseCommandLine() {
126167
flag.Parse()
127168
absroot, err := filepath.Abs(*root)
@@ -147,6 +188,7 @@ func parseCommandLine() {
147188
if *port > 65535 || *port < 0 {
148189
log.Fatalf("Bad port number %d", *port)
149190
}
191+
users = loadAuthFile(*auth)
150192
}
151193

152194
func main() {

0 commit comments

Comments
 (0)