Skip to content

Commit

Permalink
#50 - Added functionality to show both the algorithm and size in the …
Browse files Browse the repository at this point in the history
…same label into the key details component.

#58 - Added functionality to show the user ID into the key details component.
  • Loading branch information
piratax007 committed Aug 5, 2022
1 parent 14b58a3 commit 82f8e7c
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 87 deletions.
22 changes: 22 additions & 0 deletions api/algorithms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package api

type Algorithm interface {
HasKeySize() bool
Name() string
}

type algorithm struct {
hasKeySize bool
name string
}

func (a *algorithm) HasKeySize() bool {
return a.hasKeySize
}

func (a *algorithm) Name() string {
return a.name
}

var RSA Algorithm = &algorithm{hasKeySize: true, name: "RSA"}
var Ed25519 Algorithm = &algorithm{hasKeySize: false, name: "Ed25519"}
2 changes: 2 additions & 0 deletions api/key_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ type KeyEntry interface {
PrivateKeyLocations() []string
KeyType() KeyType
Size() int
Algorithm() Algorithm
}

type PublicKeyEntry interface {
KeyEntry
WithDigestContent(func([]byte) []byte) []byte
UserID() string
}

type PrivateKeyEntry interface {
Expand Down
59 changes: 48 additions & 11 deletions gui/definitions/interface/KeyDetails.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</packing>
</child>
<child>
<!-- n-columns=2 n-rows=6 -->
<!-- n-columns=2 n-rows=7 -->
<object class="GtkGrid" id="keyDetailsGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
Expand All @@ -48,6 +48,7 @@
<property name="halign">start</property>
<property name="ellipsize">start</property>
<property name="width-chars">20</property>
<property name="selectable">True</property>
<style>
<class name="path"/>
</style>
Expand Down Expand Up @@ -82,6 +83,7 @@
<property name="halign">start</property>
<property name="ellipsize">start</property>
<property name="width-chars">20</property>
<property name="selectable">True</property>
<style>
<class name="path"/>
</style>
Expand All @@ -101,6 +103,7 @@
<property name="halign">start</property>
<property name="ellipsize">end</property>
<property name="label" translatable="yes">(password protected)</property>
<property name="selectable">True</property>
<style>
<class name="passwordProtected"/>
</style>
Expand All @@ -114,13 +117,13 @@
<class name="privateKey"/>
</style>
<child>
<object class="GtkLabel" id="sizeLabel">
<object class="GtkLabel" id="securityPropertiesLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">Size:</property>
<property name="label" translatable="yes">Key algorithm:</property>
<style>
<class name="sizeLabel"/>
<class name="propertiesLabel"/>
</style>
</object>
<packing>
Expand All @@ -129,12 +132,13 @@
</packing>
</child>
<child>
<object class="GtkLabel" id="size">
<object class="GtkLabel" id="securityProperties">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="selectable">True</property>
<style>
<class name="size"/>
<class name="properties"/>
</style>
</object>
<packing>
Expand All @@ -143,7 +147,40 @@
</packing>
</child>
<style>
<class name="size"/>
<class name="properties"/>
</style>
<child>
<object class="GtkLabel" id="userIDLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="label" translatable="yes">User ID:</property>
<style>
<class name="userid"/>
</style>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="userID">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="selectable">True</property>
<style>
<class name="userid"/>
</style>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
</packing>
</child>
<style>
<class name="userid"/>
</style>
<child>
<object class="GtkLabel" id="sha1FingerprintLabel">
Expand All @@ -157,7 +194,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">4</property>
<property name="top-attach">5</property>
</packing>
</child>
<child>
Expand All @@ -174,7 +211,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
<property name="top-attach">5</property>
</packing>
</child>
<style>
Expand All @@ -192,7 +229,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">5</property>
<property name="top-attach">6</property>
</packing>
</child>
<child>
Expand All @@ -209,7 +246,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">5</property>
<property name="top-attach">6</property>
</packing>
</child>
<style>
Expand Down
4 changes: 4 additions & 0 deletions gui/definitions/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,8 @@ window {
.passwordProtected {
font-style: italic;
color: @theme_unfocused_fg_color;
}

.keyDetail grid label {
padding-right: 10px;
}
35 changes: 25 additions & 10 deletions gui/key_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/coyim/gotk3adapter/gtki"
"github.com/digitalautonomy/keymirror/api"
"strconv"
"strings"
)

Expand Down Expand Up @@ -144,24 +143,40 @@ func (kd *keyDetails) displayIsPasswordProtected() {
}
}

const sizeLabel = "sizeLabel"
const sizeValue = "size"
const sizeUnit = " bits"
const securityProperties = "securityProperties"

func (kd *keyDetails) displaySize() {
if pk, ok := kd.key.(api.KeyEntry); ok && pk.Size() > 0 {
label := kd.builder.get(sizeValue).(gtki.Label)
label.SetLabel(strconv.Itoa(pk.Size()) + sizeUnit)
func formatKeyAlgorithm(k api.KeyEntry) string {
algo := k.Algorithm()
if algo.HasKeySize() {
// TODO: this formatting probably needs to be i18n in the future
return fmt.Sprintf("%s (%d bits)", algo.Name(), k.Size())
}
return fmt.Sprintf("%s", algo.Name())
}

func (kd *keyDetails) displayAlgorithm() {
label := kd.builder.get(securityProperties).(gtki.Label)
label.SetLabel(formatKeyAlgorithm(kd.key))
}

const userIDLabel = "userIDLabel"
const userID = "userID"

func (kd *keyDetails) displayUserID() {
if pk, ok := kd.key.(api.PublicKeyEntry); ok {
label := kd.builder.get(userID).(gtki.Label)
label.SetLabel(pk.UserID())
} else {
kd.hideAll(sizeLabel, sizeValue)
kd.hideAll(userIDLabel, userID)
}
}

func (kd *keyDetails) display() {
kd.displayLocations(kd.key.PublicKeyLocations(), publicKeyPath, publicKeyPathLabel)
kd.displayLocations(kd.key.PrivateKeyLocations(), privateKeyPath, privateKeyPathLabel)
kd.displayIsPasswordProtected()
kd.displaySize()
kd.displayAlgorithm()
kd.displayUserID()
kd.displayFingerprint(sha1FingerprintLabel, sha1Fingerprint, returningSlice20(sha1.Sum))
kd.displayFingerprint(sha256FingerprintLabel, sha256Fingerprint, returningSlice32(sha256.Sum256))
kd.setClassForKeyDetails()
Expand Down
66 changes: 39 additions & 27 deletions gui/key_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,27 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe
labelPasswordProtected := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "passwordProtectedLabel").Return(labelPasswordProtected, nil).Once()

labelSize := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "sizeLabel").Return(labelSize, nil).Once()
labelSize.On("Hide").Return().Once()
valueSize := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "size").Return(valueSize, nil).Once()
valueSize.On("Hide").Return().Once()
textProperties := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "securityProperties").Return(textProperties, nil).Once()
textProperties.On("SetLabel", "Ed25519").Return().Once()

fingerprintSha1 := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "sha1Fingerprint").Return(fingerprintSha1, nil).Once()
fingerprintSha256 := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "sha256Fingerprint").Return(fingerprintSha256, nil).Once()

UserIDValue := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "userID").Return(UserIDValue, nil).Once()
UserIDValue.On("SetLabel", "").Return().Once()

keMock := &publicKeyEntryMock{}
keMock.On("WithDigestContent", mock.Anything).Return([]byte{0xAB, 0xCD, 0x10}).Once()
keMock.On("WithDigestContent", mock.Anything).Return([]byte{0xCC, 0x07, 0x00, 0xFF}).Once()
keMock.On("PublicKeyLocations").Return([]string{"/a/path/to/a/public/key"}).Once()
keMock.On("PrivateKeyLocations").Return(nil).Once()
keMock.On("KeyType").Return(api.PublicKeyType).Once()
keMock.On("Size").Return(0).Once()
keMock.On("Algorithm").Return(api.Ed25519).Once()
keMock.On("UserID").Return("").Once()
pathPublicKeyPath.On("SetLabel", "/a/path/to/a/public/key").Return().Once()
pathPublicKeyPath.On("SetTooltipText", "/a/path/to/a/public/key").Return().Once()
labelPrivateKeyPath.On("Hide").Return().Once()
Expand All @@ -78,11 +80,11 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe
pathPublicKeyPath.AssertExpectations(s.T())
labelPrivateKeyPath.AssertExpectations(s.T())
pathPrivateKey.AssertExpectations(s.T())
labelSize.AssertExpectations(s.T())
valueSize.AssertExpectations(s.T())
textProperties.AssertExpectations(s.T())
labelPasswordProtected.AssertExpectations(s.T())
fingerprintSha1.AssertExpectations(s.T())
fingerprintSha256.AssertExpectations(s.T())
UserIDValue.AssertExpectations(s.T())
scMock.AssertExpectations(s.T())
}

Expand All @@ -104,12 +106,14 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe
pathPublicKey := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "publicKeyPath").Return(pathPublicKey, nil).Once()

labelSize := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "sizeLabel").Return(labelSize, nil).Once()
labelSize.On("Hide").Return().Once()
valueSize := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "size").Return(valueSize, nil).Once()
valueSize.On("Hide").Return().Once()
textProperties := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "securityProperties").Return(textProperties, nil).Once()
textProperties.On("SetLabel", "Ed25519").Return().Once()

labelUserID := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "userIDLabel").Return(labelUserID, nil).Once()
userIDValue := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "userID").Return(userIDValue, nil).Once()

labelSha1Fingerprint := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "sha1FingerprintLabel").Return(labelSha1Fingerprint, nil).Once()
Expand All @@ -125,12 +129,14 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe
keMock.On("PublicKeyLocations").Return(nil).Once()
keMock.On("PrivateKeyLocations").Return([]string{"/a/path/to/a/private/key"}).Once()
keMock.On("KeyType").Return(api.PrivateKeyType).Once()
keMock.On("Size").Return(0).Once()
keMock.On("Algorithm").Return(api.Ed25519).Once()
pathPrivateKey.On("SetLabel", "/a/path/to/a/private/key").Return().Once()
pathPrivateKey.On("SetTooltipText", "/a/path/to/a/private/key").Return().Once()
labelPasswordProtected.On("Hide").Return().Once()
labelPublicKeyPath.On("Hide").Return().Once()
pathPublicKey.On("Hide").Return().Once()
labelUserID.On("Hide").Return().Once()
userIDValue.On("Hide").Return().Once()
labelSha1Fingerprint.On("Hide").Return().Once()
fingerprintSha1.On("Hide").Return().Once()
labelSha256Fingerprint.On("Hide").Return().Once()
Expand All @@ -144,11 +150,12 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe
keyDetailsHolder.AssertExpectations(s.T())
keMock.AssertExpectations(s.T())
pathPrivateKey.AssertExpectations(s.T())
labelSize.AssertExpectations(s.T())
valueSize.AssertExpectations(s.T())
textProperties.AssertExpectations(s.T())
labelPasswordProtected.AssertExpectations(s.T())
labelPublicKeyPath.AssertExpectations(s.T())
pathPublicKey.AssertExpectations(s.T())
labelUserID.AssertExpectations(s.T())
userIDValue.AssertExpectations(s.T())
labelSha1Fingerprint.AssertExpectations(s.T())
fingerprintSha1.AssertExpectations(s.T())
labelSha256Fingerprint.AssertExpectations(s.T())
Expand Down Expand Up @@ -182,18 +189,20 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysBot
fingerprintSha256 := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "sha256Fingerprint").Return(fingerprintSha256, nil).Once()

labelSize := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "sizeLabel").Return(labelSize, nil).Once()
labelSize.On("Hide").Return().Once()
valueSize := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "size").Return(valueSize, nil).Once()
valueSize.On("Hide").Return().Once()
properties := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "securityProperties").Return(properties, nil).Once()
properties.On("SetLabel", "Ed25519").Return().Once()

labelUserID := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "userIDLabel").Return(labelUserID, nil).Once()
userIDValue := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "userID").Return(userIDValue, nil).Once()

keMock := &keyEntryMock{}
keMock.On("PublicKeyLocations").Return([]string{"/a/path/to/a/public/key"}).Once()
keMock.On("PrivateKeyLocations").Return([]string{"/a/path/to/a/private/key"}).Once()
keMock.On("KeyType").Return(api.PairKeyType).Once()
keMock.On("Size").Return(0).Once()
keMock.On("Algorithm").Return(api.Ed25519).Once()
pathPublicKey.On("SetLabel", "/a/path/to/a/public/key").Return().Once()
pathPublicKey.On("SetTooltipText", "/a/path/to/a/public/key").Return().Once()
pathPrivateKey.On("SetLabel", "/a/path/to/a/private/key").Return().Once()
Expand All @@ -203,6 +212,8 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysBot
fingerprintSha1.On("Hide").Return().Once()
labelFingerprintSha256.On("Hide").Return().Once()
fingerprintSha256.On("Hide").Return().Once()
labelUserID.On("Hide").Return().Once()
userIDValue.On("Hide").Return().Once()

scMock := expectClassToBeAdded(keyDetailsBoxMock, "keyPair")

Expand All @@ -213,8 +224,9 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysBot
keMock.AssertExpectations(s.T())
pathPublicKey.AssertExpectations(s.T())
pathPrivateKey.AssertExpectations(s.T())
labelSize.AssertExpectations(s.T())
valueSize.AssertExpectations(s.T())
properties.AssertExpectations(s.T())
labelUserID.AssertExpectations(s.T())
userIDValue.AssertExpectations(s.T())
labelPasswordProtected.AssertExpectations(s.T())
labelFingerprintSha1.AssertExpectations(s.T())
fingerprintSha1.AssertExpectations(s.T())
Expand Down
Loading

0 comments on commit 82f8e7c

Please sign in to comment.