Commit 893a711d authored by Lukas Matt's avatar Lukas Matt

Separated parse function for Message struct

parent c0e42f19
......@@ -22,6 +22,7 @@ import (
"github.com/Zauberstuhl/go-xml"
"encoding/base64"
"time"
"strings"
)
type Message struct {
......@@ -65,6 +66,49 @@ func (m Message) SignatureText(order string) []string {
}
}
func (message *Message) Parse() (entity Entity, err error) {
if !strings.EqualFold(message.Encoding, BASE64_URL) {
logger.Error("Encoding doesn't match",
"message", message.Encoding, "lib", BASE64_URL)
return entity, errors.New("Encoding doesn't match")
}
if !strings.EqualFold(message.Alg, RSA_SHA256) {
logger.Error("Algorithm doesn't match",
"message", message.Alg, "lib", RSA_SHA256)
return entity, errors.New("Algorithm doesn't match")
}
keyId, err := base64.StdEncoding.DecodeString(message.Sig.KeyId)
if err != nil {
logger.Error("Cannot decode signature key ID", "err", err)
return entity, err
}
message.Sig.KeyId = string(keyId)
logger.Info("Entity sender", message.Sig.KeyId)
data, err := base64.URLEncoding.DecodeString(message.Data.Data)
if err != nil {
logger.Error("Cannot decode message data", "err", err)
return entity, err
}
logger.Info("Entity raw", string(data))
entity.SignatureOrder, err = FetchEntityOrder(data)
if err != nil {
logger.Error("Cannot fetch entity order", "err", err)
return entity, err
}
logger.Info("Entity order", entity.SignatureOrder)
err = xml.Unmarshal(data, &entity)
if err != nil {
logger.Error("Cannot unmarshal data", "err", err)
return entity, err
}
return entity, nil
}
func (t *Time) New(newTime time.Time) *Time {
*t = Time(newTime.UTC().Format(TIME_FORMAT))
return t
......
......@@ -20,8 +20,6 @@ package federation
import (
"crypto/rsa"
"github.com/Zauberstuhl/go-xml"
"encoding/base64"
"strings"
)
func ParseDecryptedRequest(entityXML []byte) (message Message, entity Entity, err error) {
......@@ -30,44 +28,7 @@ func ParseDecryptedRequest(entityXML []byte) (message Message, entity Entity, er
logger.Error(err)
return
}
if !strings.EqualFold(message.Encoding, BASE64_URL) {
logger.Error(err)
return
}
if !strings.EqualFold(message.Alg, RSA_SHA256) {
logger.Error(err)
return
}
keyId, err := base64.StdEncoding.DecodeString(message.Sig.KeyId)
if err != nil {
logger.Error(err)
return
}
message.Sig.KeyId = string(keyId)
logger.Info("Entity sender", message.Sig.KeyId)
data, err := base64.URLEncoding.DecodeString(message.Data.Data)
if err != nil {
logger.Error(err)
return
}
logger.Info("Entity raw", string(data))
entity.SignatureOrder, err = FetchEntityOrder(data)
if err != nil {
logger.Error(err)
return
}
logger.Info("Entity order", entity.SignatureOrder)
err = xml.Unmarshal(data, &entity)
if err != nil {
logger.Error(err)
return
}
entity, err = message.Parse()
return
}
......
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