Unverified Commit dfd0e987 authored by Sietse Ringers's avatar Sietse Ringers Committed by GitHub
Browse files

Merge pull request #13 from privacybydesign/david-pinchange

David pinchange
parents 4c9e9e88 3977ae30
......@@ -54,7 +54,6 @@ type Client struct {
// Other state
Preferences Preferences
Configuration *irma.Configuration
UnenrolledSchemeManagers []irma.SchemeManagerIdentifier
irmaConfigurationPath string
androidStoragePath string
handler ClientHandler
......@@ -80,10 +79,17 @@ type KeyshareHandler interface {
EnrollmentSuccess(manager irma.SchemeManagerIdentifier)
}
type ChangePinHandler interface {
ChangePinFailure(manager irma.SchemeManagerIdentifier, err error)
ChangePinSuccess(manager irma.SchemeManagerIdentifier)
ChangePinIncorrect(manager irma.SchemeManagerIdentifier)
}
// ClientHandler informs the user that the configuration or the list of attributes
// that this client uses has been updated.
type ClientHandler interface {
KeyshareHandler
ChangePinHandler
UpdateConfiguration(new *irma.IrmaIdentifierSet)
UpdateAttributes()
......@@ -174,8 +180,7 @@ func New(
cm.paillierKey(false)
}
cm.UnenrolledSchemeManagers = cm.unenrolledSchemeManagers()
if len(cm.UnenrolledSchemeManagers) > 1 {
if len(cm.UnenrolledSchemeManagers()) > 1 {
return nil, errors.New("Too many keyshare servers")
}
......@@ -673,16 +678,24 @@ func (client *Client) paillierKeyWorker(wait bool, ch chan bool) {
}
}
func (client *Client) unenrolledSchemeManagers() []irma.SchemeManagerIdentifier {
func (client *Client) genSchemeManagersList(enrolled bool) []irma.SchemeManagerIdentifier {
list := []irma.SchemeManagerIdentifier{}
for name, manager := range client.Configuration.SchemeManagers {
if _, contains := client.keyshareServers[name]; manager.Distributed() && !contains {
if _, contains := client.keyshareServers[name]; manager.Distributed() && contains == enrolled {
list = append(list, manager.Identifier())
}
}
return list
}
func (client *Client) UnenrolledSchemeManagers() []irma.SchemeManagerIdentifier {
return client.genSchemeManagersList(false)
}
func (client *Client) EnrolledSchemeManagers() []irma.SchemeManagerIdentifier {
return client.genSchemeManagersList(true)
}
// KeyshareEnroll attempts to enroll at the keyshare server of the specified scheme manager.
func (client *Client) KeyshareEnroll(manager irma.SchemeManagerIdentifier, email *string, pin string, lang string) {
go func() {
......@@ -738,6 +751,43 @@ func (client *Client) keyshareEnrollWorker(managerID irma.SchemeManagerIdentifie
return nil
}
func (client *Client) KeyshareChangePin(manager irma.SchemeManagerIdentifier, oldPin string, newPin string) {
go func() {
err := client.keyshareChangePinWorker(manager, oldPin, newPin)
if err != nil {
client.handler.ChangePinFailure(manager, err)
}
}()
}
func (client *Client) keyshareChangePinWorker(managerID irma.SchemeManagerIdentifier, oldPin string, newPin string) error {
kss, ok := client.keyshareServers[managerID]
if !ok {
return errors.New("Unknown keyshare server")
}
transport := irma.NewHTTPTransport(kss.URL)
message := keyshareChangepin{
Username: kss.Username,
OldPin: kss.HashedPin(oldPin),
NewPin: kss.HashedPin(newPin),
}
res := &keysharePinStatus{}
err := transport.Post("users/change/pin", res, message)
if err != nil {
return err
}
if res.Status != kssPinSuccess {
client.handler.ChangePinIncorrect(managerID)
} else {
client.handler.ChangePinSuccess(managerID)
}
return nil
}
// KeyshareRemove unenrolls the keyshare server of the specified scheme manager.
func (client *Client) KeyshareRemove(manager irma.SchemeManagerIdentifier) error {
if _, contains := client.keyshareServers[manager]; !contains {
......@@ -750,7 +800,6 @@ func (client *Client) KeyshareRemove(manager irma.SchemeManagerIdentifier) error
// KeyshareRemoveAll removes all keyshare server registrations.
func (client *Client) KeyshareRemoveAll() error {
client.keyshareServers = map[irma.SchemeManagerIdentifier]*keyshareServer{}
client.UnenrolledSchemeManagers = client.unenrolledSchemeManagers()
return client.storage.StoreKeyshareServers(client.keyshareServers)
}
......
......@@ -38,7 +38,6 @@ func (h *keyshareEnrollmentHandler) RequestPin(remainingAttempts int, callback P
func (h *keyshareEnrollmentHandler) Success(action irma.Action, result string) {
_ = h.client.storage.StoreKeyshareServers(h.client.keyshareServers) // TODO handle err?
h.client.UnenrolledSchemeManagers = h.client.unenrolledSchemeManagers()
h.client.handler.EnrollmentSuccess(h.kss.SchemeManagerIdentifier)
}
......
......@@ -5,6 +5,7 @@ import (
"math/big"
"os"
"testing"
"errors"
"github.com/mhe/gabi"
"github.com/privacybydesign/irmago"
......@@ -41,6 +42,27 @@ func (i *TestClientHandler) EnrollmentFailure(manager irma.SchemeManagerIdentifi
i.t.Fatal(err)
}
}
func (i *TestClientHandler) ChangePinSuccess(manager irma.SchemeManagerIdentifier) {
select {
case i.c <- nil: // nop
default: // nop
}
}
func (i *TestClientHandler) ChangePinFailure(manager irma.SchemeManagerIdentifier, err error) {
select {
case i.c <- err: //nop
default:
i.t.Fatal(err)
}
}
func (i *TestClientHandler) ChangePinIncorrect(manager irma.SchemeManagerIdentifier) {
err := errors.New("incorrect pin")
select {
case i.c <- err: //nop
default:
i.t.Fatal(err)
}
}
func parseStorage(t *testing.T) *Client {
require.NoError(t, fs.CopyDirectory("../testdata/teststorage", "../testdata/storage/test"))
......
......@@ -62,6 +62,12 @@ type keyshareEnrollment struct {
Language string `json:"language"`
}
type keyshareChangepin struct {
Username string `json:"id"`
OldPin string `json:"oldpin"`
NewPin string `json:"newpin"`
}
type keyshareAuthorization struct {
Status string `json:"status"`
Candidates []string `json:"candidates"`
......
......@@ -578,9 +578,6 @@ func (session *session) managerSession() {
}
// Update state and inform user of success
if manager.Distributed() {
session.client.UnenrolledSchemeManagers = session.client.unenrolledSchemeManagers()
}
session.client.handler.UpdateConfiguration(
&irma.IrmaIdentifierSet{
SchemeManagers: map[irma.SchemeManagerIdentifier]struct{}{manager.Identifier(): {}},
......
......@@ -8,11 +8,12 @@ import (
"testing"
"time"
"math/big"
"github.com/go-errors/errors"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/test"
"github.com/stretchr/testify/require"
"math/big"
)
type TestHandler struct {
......@@ -366,6 +367,16 @@ func keyshareSessions(t *testing.T, client *Client) {
sessionHelper(t, jwt, "signature", client)
}
// Test pinchange interaction
func TestKeyshareChangePin(t *testing.T) {
client := parseStorage(t)
require.NoError(t, client.keyshareChangePinWorker(irma.NewSchemeManagerIdentifier("test"), "12345", "54321"))
require.NoError(t, client.keyshareChangePinWorker(irma.NewSchemeManagerIdentifier("test"), "54321", "12345"))
test.ClearTestStorage(t)
}
// Enroll at a keyshare server and do an issuance, disclosure,
// and issuance session, also using irma-demo credentials deserialized from Android storage
func TestKeyshareEnrollmentAndSessions(t *testing.T) {
......
......@@ -212,7 +212,6 @@ func (client *Client) ParseAndroidStorage() (present bool, err error) {
return
}
}
client.UnenrolledSchemeManagers = client.unenrolledSchemeManagers()
if err = client.storage.StorePaillierKeys(client.paillierKeyCache); err != nil {
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