2016-02-11 11:57:16 -08:00
|
|
|
package vain
|
2016-02-08 00:15:22 -08:00
|
|
|
|
2016-02-11 12:12:13 -08:00
|
|
|
import (
|
2016-02-13 01:18:06 -08:00
|
|
|
"encoding/json"
|
2016-02-11 12:12:13 -08:00
|
|
|
"fmt"
|
2016-04-11 20:43:18 -07:00
|
|
|
"log"
|
2016-02-11 12:12:13 -08:00
|
|
|
"net/http"
|
2016-04-22 23:50:23 -07:00
|
|
|
"net/mail"
|
2016-02-13 01:18:06 -08:00
|
|
|
"strings"
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
verrors "mcquay.me/vain/errors"
|
2016-02-11 12:12:13 -08:00
|
|
|
)
|
2016-02-08 00:15:22 -08:00
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
const apiPrefix = "/api/v0/"
|
|
|
|
|
|
|
|
var prefix map[string]string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prefix = map[string]string{
|
|
|
|
"pkgs": apiPrefix + "db/",
|
|
|
|
"register": apiPrefix + "register/",
|
|
|
|
"confirm": apiPrefix + "confirm/",
|
|
|
|
"forgot": apiPrefix + "forgot/",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 01:10:14 -08:00
|
|
|
// NewServer populates a server, adds the routes, and returns it for use.
|
2016-04-11 20:43:18 -07:00
|
|
|
func NewServer(sm *http.ServeMux, store *DB) *Server {
|
2016-02-15 01:10:14 -08:00
|
|
|
s := &Server{
|
2016-04-11 20:43:18 -07:00
|
|
|
db: store,
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-02-23 22:09:29 -08:00
|
|
|
addRoutes(sm, s)
|
2016-02-15 01:10:14 -08:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server serves up the http.
|
2016-02-08 00:15:22 -08:00
|
|
|
type Server struct {
|
2016-04-11 20:43:18 -07:00
|
|
|
db *DB
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
|
|
|
|
2016-02-11 12:12:13 -08:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2016-04-11 20:43:18 -07:00
|
|
|
if req.Method == "GET" {
|
|
|
|
// TODO: perhaps have a nicely formatted page with info as root if
|
|
|
|
// go-get=1 not in request?
|
2016-02-13 01:18:06 -08:00
|
|
|
fmt.Fprintf(w, "<!DOCTYPE html>\n<html><head>\n")
|
2016-04-11 20:43:18 -07:00
|
|
|
for _, p := range s.db.Pkgs() {
|
2016-02-13 01:18:06 -08:00
|
|
|
fmt.Fprintf(w, "%s\n", p)
|
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
fmt.Fprintf(w, "</head>\n<body><p>go tool metadata in head</p></body>\n</html>\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const prefix = "Bearer "
|
|
|
|
var tok string
|
|
|
|
auth := req.Header.Get("Authorization")
|
|
|
|
if strings.HasPrefix(auth, prefix) {
|
|
|
|
tok = strings.TrimPrefix(auth, prefix)
|
|
|
|
}
|
|
|
|
if tok == "" {
|
|
|
|
http.Error(w, "missing token", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ns, err := parseNamespace(req.URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("could not parse namespace:%v", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := verrors.ToHTTP(s.db.NSForToken(ns, tok)); err != nil {
|
|
|
|
http.Error(w, err.Message, err.Code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Method {
|
2016-02-11 12:12:13 -08:00
|
|
|
case "POST":
|
2016-02-13 01:18:06 -08:00
|
|
|
if req.URL.Path == "/" {
|
|
|
|
http.Error(w, fmt.Sprintf("invalid path %q", req.URL.Path), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := Package{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&p); err != nil {
|
2016-02-15 01:10:14 -08:00
|
|
|
http.Error(w, fmt.Sprintf("unable to parse json from body: %v", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.Repo == "" {
|
2016-04-11 20:43:18 -07:00
|
|
|
http.Error(w, fmt.Sprintf("invalid repository %q", p.Repo), http.StatusBadRequest)
|
2016-02-13 01:18:06 -08:00
|
|
|
return
|
|
|
|
}
|
2016-02-23 22:09:29 -08:00
|
|
|
if p.Vcs == "" {
|
|
|
|
p.Vcs = "git"
|
|
|
|
}
|
|
|
|
if !valid(p.Vcs) {
|
|
|
|
http.Error(w, fmt.Sprintf("invalid vcs %q", p.Vcs), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
p.Path = fmt.Sprintf("%s/%s", req.Host, strings.Trim(req.URL.Path, "/"))
|
|
|
|
p.Ns = ns
|
|
|
|
if !Valid(p.Path, s.db.Pkgs()) {
|
2016-02-13 10:44:41 -08:00
|
|
|
http.Error(w, fmt.Sprintf("invalid path; prefix already taken %q", req.URL.Path), http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
if err := s.db.AddPackage(p); err != nil {
|
2016-02-14 22:19:41 -08:00
|
|
|
http.Error(w, fmt.Sprintf("unable to add package: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-03-01 23:34:31 -08:00
|
|
|
case "DELETE":
|
|
|
|
p := fmt.Sprintf("%s/%s", req.Host, strings.Trim(req.URL.Path, "/"))
|
2016-04-11 20:43:18 -07:00
|
|
|
if !s.db.PackageExists(p) {
|
2016-03-01 23:34:31 -08:00
|
|
|
http.Error(w, fmt.Sprintf("package %q not found", p), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
if err := s.db.RemovePackage(p); err != nil {
|
2016-03-01 23:34:31 -08:00
|
|
|
http.Error(w, fmt.Sprintf("unable to delete package: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-02-11 12:12:13 -08:00
|
|
|
default:
|
2016-03-01 23:34:31 -08:00
|
|
|
http.Error(w, fmt.Sprintf("unsupported method %q; accepted: POST, GET, DELETE", req.Method), http.StatusMethodNotAllowed)
|
2016-02-11 12:12:13 -08:00
|
|
|
}
|
|
|
|
}
|
2016-02-23 22:09:29 -08:00
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
func (s *Server) register(w http.ResponseWriter, req *http.Request) {
|
|
|
|
req.ParseForm()
|
|
|
|
email, ok := req.Form["email"]
|
|
|
|
if !ok || len(email) != 1 {
|
|
|
|
http.Error(w, "must provide one email parameter", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-04-22 23:50:23 -07:00
|
|
|
|
|
|
|
addr := email[0]
|
|
|
|
if _, err := mail.ParseAddress(addr); err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("invalid email detected: %v", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := s.db.Register(addr)
|
2016-04-11 20:43:18 -07:00
|
|
|
if err := verrors.ToHTTP(err); err != nil {
|
|
|
|
http.Error(w, err.Message, err.Code)
|
|
|
|
return
|
|
|
|
}
|
2016-04-17 18:26:45 -07:00
|
|
|
proto := "https"
|
|
|
|
if req.TLS == nil {
|
|
|
|
proto = "http"
|
|
|
|
}
|
|
|
|
log.Printf("%s://%s/api/v0/confirm/%+v", proto, req.Host, tok)
|
2016-04-11 20:43:18 -07:00
|
|
|
fmt.Fprintf(w, "please check your email\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) confirm(w http.ResponseWriter, req *http.Request) {
|
|
|
|
tok := req.URL.Path[len(prefix["confirm"]):]
|
|
|
|
tok = strings.TrimRight(tok, "/")
|
|
|
|
if tok == "" {
|
|
|
|
http.Error(w, "must provide one email parameter", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tok, err := s.db.Confirm(tok)
|
|
|
|
if err := verrors.ToHTTP(err); err != nil {
|
|
|
|
http.Error(w, err.Message, err.Code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "new token: %s\n", tok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) forgot(w http.ResponseWriter, req *http.Request) {
|
|
|
|
req.ParseForm()
|
|
|
|
email, ok := req.Form["email"]
|
|
|
|
if !ok || len(email) != 1 {
|
|
|
|
http.Error(w, "must provide one email parameter", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-04-22 23:50:23 -07:00
|
|
|
|
|
|
|
addr := email[0]
|
|
|
|
if _, err := mail.ParseAddress(addr); err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("invalid email detected: %v", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := s.db.forgot(addr)
|
2016-04-11 20:43:18 -07:00
|
|
|
if err := verrors.ToHTTP(err); err != nil {
|
|
|
|
http.Error(w, err.Message, err.Code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("http://%s/api/v0/confirm/%+v", req.Host, tok)
|
|
|
|
fmt.Fprintf(w, "please check your email\n")
|
|
|
|
}
|
|
|
|
func (s *Server) pkgs(w http.ResponseWriter, req *http.Request) {
|
2016-02-23 22:09:29 -08:00
|
|
|
w.Header().Set("Content-type", "application/json")
|
2016-04-11 20:43:18 -07:00
|
|
|
json.NewEncoder(w).Encode(s.db.Pkgs())
|
2016-02-23 22:09:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func addRoutes(sm *http.ServeMux, s *Server) {
|
|
|
|
sm.Handle("/", s)
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
sm.HandleFunc(prefix["pkgs"], s.pkgs)
|
|
|
|
sm.HandleFunc(prefix["register"], s.register)
|
|
|
|
sm.HandleFunc(prefix["confirm"], s.confirm)
|
|
|
|
sm.HandleFunc(prefix["forgot"], s.forgot)
|
2016-02-23 22:09:29 -08:00
|
|
|
}
|