Commit 90f0ac50 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Initial work on server without authentication


Co-authored-by: Tomas's avatarConfiks <confiks@scriptbase.org>
parent e8cfdb07
package sessiontest
import (
"encoding/json"
"path/filepath"
"testing"
"time"
"github.com/Sirupsen/logrus"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/test"
"github.com/privacybydesign/irmago/irmaserver"
"github.com/privacybydesign/irmago/irmaserver/server"
"github.com/stretchr/testify/require"
)
func StartRequestorServer(t *testing.T) {
testdata := test.FindTestdataFolder(t)
logger := logrus.New()
logger.Level = logrus.WarnLevel
logger.Formatter = &logrus.TextFormatter{}
go func() {
server.Start(48682, &irmaserver.Configuration{
Logger: logger,
IrmaConfigurationPath: filepath.Join(testdata, "irma_configuration"),
PrivateKeysPath: filepath.Join(testdata, "privatekeys"),
})
}()
time.Sleep(100 * time.Millisecond)
}
func StopRequestorServer(t *testing.T) {
server.Stop()
}
func serverSessionHelper(t *testing.T, request irma.SessionRequest) *irmaserver.SessionResult {
StartRequestorServer(t)
defer StopRequestorServer(t)
client := parseStorage(t)
defer test.ClearTestStorage(t)
clientChan := make(chan *SessionResult)
transport := irma.NewHTTPTransport("http://localhost:48682")
var qr irma.Qr
err := transport.Post("create", &qr, request)
require.NoError(t, err)
token := qr.URL
qr.URL = "http://localhost:48682/irma/" + qr.URL
h := TestHandler{t, clientChan, client}
j, err := json.Marshal(qr)
require.NoError(t, err)
client.NewSession(string(j), h)
clientResult := <-clientChan
if clientResult != nil {
require.NoError(t, clientResult.Err)
}
var result irmaserver.SessionResult
transport.Get("result/"+token, &result)
return &result
}
func TestRequestorServer(t *testing.T) {
id := irma.NewAttributeTypeIdentifier("irma-demo.RU.studentCard.studentID")
serverResult := serverSessionHelper(t, &irma.DisclosureRequest{
BaseRequest: irma.BaseRequest{Type: irma.ActionDisclosing},
Content: irma.AttributeDisjunctionList([]*irma.AttributeDisjunction{{
Label: "foo",
Attributes: []irma.AttributeTypeIdentifier{id},
}}),
})
require.Nil(t, serverResult.Err)
require.Equal(t, irma.ProofStatusValid, serverResult.ProofStatus)
require.NotEmpty(t, serverResult.Disclosed)
require.Equal(t, id, serverResult.Disclosed[0].Identifier)
require.Equal(t, "456", serverResult.Disclosed[0].Value["en"])
}
package main
import (
"fmt"
"os"
"strconv"
"github.com/go-errors/errors"
"github.com/privacybydesign/irmago/irmaserver"
"github.com/privacybydesign/irmago/irmaserver/server"
)
func main() {
var err error
defer func() {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
os.Exit(0)
}()
if len(os.Args) != 3 {
err = errors.New("Usage: irmaserver port path")
return
}
port, err := strconv.Atoi(os.Args[1])
if err != nil {
err = errors.New("First argument must be an integer")
return
}
err = server.Start(port, &irmaserver.Configuration{
IrmaConfigurationPath: os.Args[2],
})
}
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/go-errors/errors"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/irmaserver"
"github.com/privacybydesign/irmago/irmaserver/irmarequestor"
)
var server *http.Server
func Start(port int, conf *irmaserver.Configuration) error {
mux := http.NewServeMux()
if err := irmarequestor.Initialize(conf); err != nil {
return err
}
mux.HandleFunc("/irma/", irmarequestor.HttpHandlerFunc("/irma/"))
mux.HandleFunc("/create", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeError(w, irmaserver.ErrorInvalidRequest, "")
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writeError(w, irmaserver.ErrorInvalidRequest, err.Error())
return
}
request, err := parseRequest(body)
if err != nil {
writeError(w, irmaserver.ErrorInvalidRequest, err.Error())
return
}
qr, _, err := irmarequestor.StartSession(request, nil)
if err != nil {
writeError(w, irmaserver.ErrorInvalidRequest, err.Error())
return
}
b, _ := json.Marshal(qr)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(b)
})
mux.HandleFunc("/status/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeError(w, irmaserver.ErrorInvalidRequest, "")
return
}
token := r.URL.Path[len("/status/"):]
res := irmarequestor.GetSessionResult(token)
if res == nil {
writeError(w, irmaserver.ErrorSessionUnknown, "")
return
}
b, _ := json.Marshal(res.Status)
w.Write(b)
})
mux.HandleFunc("/result/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeError(w, irmaserver.ErrorInvalidRequest, "")
return
}
token := r.URL.Path[len("/result/"):]
res := irmarequestor.GetSessionResult(token)
if res == nil {
writeError(w, irmaserver.ErrorInvalidRequest, "")
return
}
b, _ := json.Marshal(res)
w.Write(b)
})
server = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: mux}
server.ListenAndServe()
return nil
}
func Stop() {
server.Close()
}
func writeError(w http.ResponseWriter, err irmaserver.Error, msg string) {
status, bts := irmaserver.JsonResponse(nil, irmaserver.RemoteError(err, msg))
w.WriteHeader(status)
w.Write(bts)
}
func parseRequest(bts []byte) (request irma.SessionRequest, err error) {
request = &irma.DisclosureRequest{}
if err = irma.UnmarshalValidate(bts, request); err == nil {
return request, nil
}
request = &irma.SignatureRequest{}
if err = irma.UnmarshalValidate(bts, request); err == nil {
return request, nil
}
request = &irma.IssuanceRequest{}
if err = irma.UnmarshalValidate(bts, request); err == nil {
return request, nil
}
return nil, errors.New("Invalid session type")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment