Implement test server
This commit is contained in:
parent
b223a25055
commit
6bc1ce6679
6 changed files with 152 additions and 6 deletions
40
server/key_directory.go
Normal file
40
server/key_directory.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto"
|
||||
"crypto/ed25519"
|
||||
"fmt"
|
||||
|
||||
"github.com/common-fate/httpsig/alg_ed25519"
|
||||
"github.com/common-fate/httpsig/verifier"
|
||||
)
|
||||
|
||||
type KeyEntry struct {
|
||||
alg string
|
||||
publicKey crypto.PublicKey
|
||||
userId string
|
||||
}
|
||||
|
||||
type InMemoryDirectory struct {
|
||||
records map[string]KeyEntry
|
||||
}
|
||||
|
||||
func (dir *InMemoryDirectory) GetKey(ctx context.Context, keyId string, _ string) (verifier.Algorithm, error) {
|
||||
entry := dir.records[keyId]
|
||||
|
||||
var alg verifier.Algorithm
|
||||
var err error
|
||||
|
||||
switch entry.alg {
|
||||
case "ed25519":
|
||||
alg = alg_ed25519.Ed25519{
|
||||
PublicKey: entry.publicKey.(ed25519.PublicKey),
|
||||
Attrs: entry.userId,
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("unknown algoritm: %s", entry.alg)
|
||||
}
|
||||
|
||||
return alg, err
|
||||
}
|
56
server/server.go
Normal file
56
server/server.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/common-fate/httpsig"
|
||||
"github.com/common-fate/httpsig/inmemory"
|
||||
)
|
||||
|
||||
func Start(publicKey crypto.PublicKey) error {
|
||||
keyDir := InMemoryDirectory{
|
||||
records: map[string]KeyEntry{},
|
||||
}
|
||||
|
||||
keyId := "test-id"
|
||||
|
||||
keyDir.records[keyId] = KeyEntry{
|
||||
alg: "ed25519",
|
||||
publicKey: publicKey,
|
||||
userId: "test_user",
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
verifier := httpsig.Middleware(httpsig.MiddlewareOpts{
|
||||
NonceStorage: inmemory.NewNonceStorage(),
|
||||
KeyDirectory: &keyDir,
|
||||
Tag: "test-tag",
|
||||
Scheme: "http",
|
||||
Authority: "localhost:8080",
|
||||
|
||||
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)
|
||||
},
|
||||
})
|
||||
|
||||
mux.Handle("/", verifier(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("Responding...\n")
|
||||
attr := httpsig.AttributesFromContext(r.Context()).(string)
|
||||
fmt.Printf("User is %s\n", attr)
|
||||
msg := fmt.Sprintf("hello, %s!", attr)
|
||||
w.Write([]byte(msg))
|
||||
fmt.Printf("Responded...\n")
|
||||
})))
|
||||
|
||||
err := http.ListenAndServe("localhost:8080", mux)
|
||||
|
||||
return err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue