Commit 18f7cdf8 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Move parseRequest function for reuse

parent 5e82e425
...@@ -2,6 +2,7 @@ package server ...@@ -2,6 +2,7 @@ package server
import ( import (
"encoding/json" "encoding/json"
"errors"
"net/http" "net/http"
"runtime/debug" "runtime/debug"
...@@ -90,3 +91,19 @@ func WriteString(w http.ResponseWriter, str string) { ...@@ -90,3 +91,19 @@ func WriteString(w http.ResponseWriter, str string) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte(str)) w.Write([]byte(str))
} }
func ParseSessionRequest(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 or disabled session type")
}
...@@ -57,7 +57,7 @@ func (NilAuthenticator) Authenticate( ...@@ -57,7 +57,7 @@ func (NilAuthenticator) Authenticate(
if headers.Get("Authentication") != "" || !strings.HasPrefix(headers.Get("Content-Type"), "application/json") { if headers.Get("Authentication") != "" || !strings.HasPrefix(headers.Get("Content-Type"), "application/json") {
return false, nil, "", nil return false, nil, "", nil
} }
request, err := parseRequest(body) request, err := server.ParseSessionRequest(body)
if err != nil { if err != nil {
return true, nil, "", server.RemoteError(server.ErrorInvalidRequest, err.Error()) return true, nil, "", server.RemoteError(server.ErrorInvalidRequest, err.Error())
} }
...@@ -133,7 +133,7 @@ func (pskauth *PresharedKeyAuthenticator) Authenticate( ...@@ -133,7 +133,7 @@ func (pskauth *PresharedKeyAuthenticator) Authenticate(
if !ok { if !ok {
return true, nil, "", server.RemoteError(server.ErrorUnauthorized, "") return true, nil, "", server.RemoteError(server.ErrorUnauthorized, "")
} }
request, err := parseRequest(body) request, err := server.ParseSessionRequest(body)
if err != nil { if err != nil {
return true, nil, "", server.RemoteError(server.ErrorInvalidRequest, err.Error()) return true, nil, "", server.RemoteError(server.ErrorInvalidRequest, err.Error())
} }
...@@ -168,19 +168,3 @@ func jwtPublicKeyExtractor(publickeys map[string]*rsa.PublicKey) func(token *jwt ...@@ -168,19 +168,3 @@ func jwtPublicKeyExtractor(publickeys map[string]*rsa.PublicKey) func(token *jwt
return nil, errors.Errorf("Unknown requestor: %s", requestor) return nil, errors.Errorf("Unknown requestor: %s", requestor)
} }
} }
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 or disabled session type")
}
Supports Markdown
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