Commit fa9ae993 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Simplify protocol version calculation

parent f2060279
......@@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"math/big"
......@@ -81,22 +80,6 @@ var supportedVersions = map[int][]int{
}
func calcVersion(qr *irma.Qr) (*irma.ProtocolVersion, error) {
// Parse range supported by server
var minmajor, minminor, maxmajor, maxminor int
var err error
if minmajor, err = strconv.Atoi(string(qr.ProtocolVersion[0])); err != nil {
return nil, err
}
if minminor, err = strconv.Atoi(string(qr.ProtocolVersion[2])); err != nil {
return nil, err
}
if maxmajor, err = strconv.Atoi(string(qr.ProtocolMaxVersion[0])); err != nil {
return nil, err
}
if maxminor, err = strconv.Atoi(string(qr.ProtocolMaxVersion[2])); err != nil {
return nil, err
}
// Iterate supportedVersions in reverse sorted order (i.e. biggest major number first)
keys := make([]int, 0, len(supportedVersions))
for k := range supportedVersions {
......@@ -105,14 +88,14 @@ func calcVersion(qr *irma.Qr) (*irma.ProtocolVersion, error) {
sort.Sort(sort.Reverse(sort.IntSlice(keys)))
for _, major := range keys {
for _, minor := range supportedVersions[major] {
aboveMinimum := major > minmajor || (major == minmajor && minor >= minminor)
underMaximum := major < maxmajor || (major == maxmajor && minor <= maxminor)
aboveMinimum := major > qr.ProtocolVersion.Major || (major == qr.ProtocolVersion.Major && minor >= qr.ProtocolVersion.Minor)
underMaximum := major < qr.ProtocolMaxVersion.Major || (major == qr.ProtocolMaxVersion.Major && minor <= qr.ProtocolMaxVersion.Minor)
if aboveMinimum && underMaximum {
return irma.NewVersion(major, minor), nil
}
}
}
return nil, fmt.Errorf("No supported protocol version between %s and %s", qr.ProtocolVersion, qr.ProtocolMaxVersion)
return nil, fmt.Errorf("No supported protocol version between %s and %s", qr.ProtocolVersion.String(), qr.ProtocolMaxVersion.String())
}
// IsInteractive returns whether this session uses an API server or not.
......
......@@ -19,8 +19,8 @@ type Status string
// ProtocolVersion encodes the IRMA protocol version of an IRMA session.
type ProtocolVersion struct {
major int
minor int
Major int
Minor int
}
func NewVersion(major, minor int) *ProtocolVersion {
......@@ -28,7 +28,7 @@ func NewVersion(major, minor int) *ProtocolVersion {
}
func (v *ProtocolVersion) String() string {
return fmt.Sprintf("%d.%d", v.major, v.minor)
return fmt.Sprintf("%d.%d", v.Major, v.Minor)
}
func (v *ProtocolVersion) UnmarshalJSON(b []byte) (err error) {
......@@ -40,10 +40,10 @@ func (v *ProtocolVersion) UnmarshalJSON(b []byte) (err error) {
if len(parts) != 2 {
return errors.New("Invalid protocol version number: not of form x.y")
}
if v.major, err = strconv.Atoi(parts[0]); err != nil {
if v.Major, err = strconv.Atoi(parts[0]); err != nil {
return
}
v.minor, err = strconv.Atoi(parts[1])
v.Minor, err = strconv.Atoi(parts[1])
return
}
......@@ -53,10 +53,10 @@ func (v *ProtocolVersion) MarshalJSON() ([]byte, error) {
// Returns true if v is below the given version.
func (v *ProtocolVersion) Below(major, minor int) bool {
if v.major < major {
if v.Major < major {
return true
}
return v.major == major && v.minor < minor
return v.Major == major && v.Minor < minor
}
// GetMetadataVersion maps a chosen protocol version to a metadata version that
......@@ -106,9 +106,9 @@ type Qr struct {
// Server with which to perform the session
URL string `json:"u"`
// Session type (disclosing, signing, issuing)
Type Action `json:"irmaqr"`
ProtocolVersion string `json:"v"`
ProtocolMaxVersion string `json:"vmax"`
Type Action `json:"irmaqr"`
ProtocolVersion ProtocolVersion `json:"v"`
ProtocolMaxVersion ProtocolVersion `json:"vmax"`
}
// A SessionInfo is the first message in the IRMA protocol (i.e., GET on the server URL),
......
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