From 9e9a7d50970f1424245d88169de82988fd57e112 Mon Sep 17 00:00:00 2001 From: Niall Sheridan Date: Thu, 5 Jan 2017 23:28:26 +0000 Subject: Move GetPublicKey to the shared `lib` package --- lib/const.go | 17 ----------------- lib/proto.go | 15 +++++++++++++++ lib/util.go | 10 ++++++++++ lib/util_test.go | 16 ++++++++++++++++ 4 files changed, 41 insertions(+), 17 deletions(-) delete mode 100644 lib/const.go create mode 100644 lib/proto.go create mode 100644 lib/util.go create mode 100644 lib/util_test.go (limited to 'lib') diff --git a/lib/const.go b/lib/const.go deleted file mode 100644 index 1ba2749..0000000 --- a/lib/const.go +++ /dev/null @@ -1,17 +0,0 @@ -package lib - -import "time" - -// SignRequest represents a signing request sent to the server. -type SignRequest struct { - Key string `json:"key"` - ValidUntil time.Time `json:"valid_until"` -} - -// SignResponse is sent by the server. -// `Status' is "ok" or "error". -// `Response' contains a signed certificate or an error message. -type SignResponse struct { - Status string `json:"status"` - Response string `json:"response"` -} diff --git a/lib/proto.go b/lib/proto.go new file mode 100644 index 0000000..f3d7115 --- /dev/null +++ b/lib/proto.go @@ -0,0 +1,15 @@ +package lib + +import "time" + +// SignRequest represents a signing request sent to the server. +type SignRequest struct { + Key string `json:"key"` + ValidUntil time.Time `json:"valid_until"` +} + +// SignResponse is sent by the server. +type SignResponse struct { + Status string `json:"status"` // Status will be "ok" or "error". + Response string `json:"response"` // Response will contain either the signed certificate or the error message. +} diff --git a/lib/util.go b/lib/util.go new file mode 100644 index 0000000..b1c7b87 --- /dev/null +++ b/lib/util.go @@ -0,0 +1,10 @@ +package lib + +import "golang.org/x/crypto/ssh" + +// GetPublicKey marshals a ssh certificate to a string. +func GetPublicKey(pub ssh.PublicKey) string { + marshaled := ssh.MarshalAuthorizedKey(pub) + // Strip trailing newline + return string(marshaled[:len(marshaled)-1]) +} diff --git a/lib/util_test.go b/lib/util_test.go new file mode 100644 index 0000000..9e89297 --- /dev/null +++ b/lib/util_test.go @@ -0,0 +1,16 @@ +package lib + +import ( + "testing" + + "github.com/nsheridan/cashier/testdata" + "golang.org/x/crypto/ssh" +) + +func TestGetPublicKey(t *testing.T) { + t.Parallel() + c, _, _, _, _ := ssh.ParseAuthorizedKey(testdata.Cert) + if GetPublicKey(c.(*ssh.Certificate)) != string(testdata.Cert) { + t.Fail() + } +} -- cgit v1.2.3