sig-auth/server/server.go

119 lines
2.7 KiB
Go
Raw Normal View History

2025-02-14 19:41:22 -05:00
package server
import (
"context"
2025-02-17 20:50:48 -05:00
"crypto"
"encoding/json"
2025-02-14 19:41:22 -05:00
"fmt"
"net/http"
2025-02-17 20:50:48 -05:00
"crispbyte.dev/sig-auth/keydirectory"
2025-02-14 19:41:22 -05:00
"github.com/common-fate/httpsig"
"github.com/common-fate/httpsig/inmemory"
2025-02-17 20:50:48 -05:00
"golang.org/x/crypto/ssh"
2025-02-14 19:41:22 -05:00
)
2025-02-20 23:12:04 -05:00
func Start(keyDir keydirectory.RegistrationDirectory) error {
2025-02-14 19:41:22 -05:00
mux := http.NewServeMux()
2025-02-21 20:09:23 -05:00
validationOptions := httpsig.DefaultValidationOpts()
delete(validationOptions.RequiredCoveredComponents, "content-digest")
2025-02-14 19:41:22 -05:00
verifier := httpsig.Middleware(httpsig.MiddlewareOpts{
NonceStorage: inmemory.NewNonceStorage(),
2025-02-17 19:55:53 -05:00
KeyDirectory: keyDir,
2025-02-16 13:51:12 -05:00
Tag: "auth",
2025-02-14 19:41:22 -05:00
Scheme: "http",
2025-02-21 20:09:23 -05:00
Authority: "localhost:8001",
Validation: &validationOptions,
2025-02-14 19:41:22 -05:00
OnValidationError: func(ctx context.Context, err error) {
fmt.Printf("validation error: %s\n", err)
},
OnDeriveSigningString: func(ctx context.Context, stringToSign string) {
fmt.Printf("string to sign:\n%s\n", stringToSign)
},
})
2025-02-20 23:12:04 -05:00
verifyHandler := verifier(getDefaultHandler())
2025-02-16 13:51:53 -05:00
2025-02-21 20:09:23 -05:00
handler := rewriteHeaders(verifyHandler)
2025-02-16 13:51:53 -05:00
2025-02-20 21:49:05 -05:00
mux.Handle("/auth", handler)
2025-02-17 20:50:48 -05:00
mux.Handle("/register", getRegistrationHandler(keyDir))
2025-02-14 19:41:22 -05:00
err := http.ListenAndServe("localhost:8080", mux)
return err
}
2025-02-17 20:50:48 -05:00
2025-02-20 23:12:04 -05:00
func getDefaultHandler() http.Handler {
2025-02-17 20:50:48 -05:00
handler := func(w http.ResponseWriter, r *http.Request) {
attr := httpsig.AttributesFromContext(r.Context()).(string)
2025-02-20 23:12:04 -05:00
w.Header().Add("Remote-User", attr)
msg := fmt.Sprintf("hello, %s!", attr)
w.Write([]byte(msg))
2025-02-17 20:50:48 -05:00
}
return http.HandlerFunc(handler)
}
func getRegistrationHandler(keyDir keydirectory.RegistrationDirectory) http.Handler {
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Bad request", 400)
return
}
var request RegisterRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
2025-02-20 22:53:18 -05:00
fmt.Println(err)
2025-02-17 20:50:48 -05:00
http.Error(w, fmt.Sprintf("Bad request - %s", err), 400)
return
}
2025-02-20 22:53:18 -05:00
key, err := parsePublicKey(request.Key)
2025-02-17 20:50:48 -05:00
if err != nil {
2025-02-20 22:53:18 -05:00
fmt.Println(err)
2025-02-17 20:50:48 -05:00
http.Error(w, fmt.Sprintf("Bad request - %s", err), 400)
return
}
2025-02-20 23:00:04 -05:00
if !isValidKeyType(key) {
fmt.Println("Attempted to register invalid key type")
http.Error(w, "Invalid key type", 400)
return
}
2025-02-20 22:53:18 -05:00
fmt.Printf("Registering key for %s\n", request.UserId)
2025-02-17 20:50:48 -05:00
2025-02-20 22:53:18 -05:00
keyId, err := keyDir.RegisterKey(key, request.UserId)
if err != nil {
2025-02-20 22:53:18 -05:00
fmt.Println(err)
http.Error(w, fmt.Sprintf("Server error - %s", err), 500)
return
}
w.Write([]byte(keyId))
2025-02-17 20:50:48 -05:00
}
return http.HandlerFunc(handler)
}
2025-02-20 22:53:18 -05:00
func parsePublicKey(input string) (crypto.PublicKey, error) {
2025-02-17 20:50:48 -05:00
pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(input))
2025-02-20 22:53:18 -05:00
if err != nil {
return nil, err
2025-02-17 20:50:48 -05:00
}
2025-02-20 22:53:18 -05:00
return pk.(ssh.CryptoPublicKey).CryptoPublicKey(), err
2025-02-17 20:50:48 -05:00
}