aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorNiall Sheridan <nsheridan@gmail.com>2017-04-10 14:26:13 +0100
committerNiall Sheridan <nsheridan@gmail.com>2017-04-10 14:26:13 +0100
commit63123b959bc0322c80d41da12ef73abe3dc1ce45 (patch)
treea59f923f58d040001aee0b2b545fe5c1562b40f3 /server
parent8b63d26271a9250e262962c2998a36bae36f8d20 (diff)
override the json marshaller's time format
Diffstat (limited to 'server')
-rw-r--r--server/store/store.go15
-rw-r--r--server/store/store_test.go18
2 files changed, 33 insertions, 0 deletions
diff --git a/server/store/store.go b/server/store/store.go
index cf69225..f14e79f 100644
--- a/server/store/store.go
+++ b/server/store/store.go
@@ -1,6 +1,7 @@
package store
import (
+ "encoding/json"
"time"
"golang.org/x/crypto/ssh"
@@ -43,6 +44,20 @@ type CertRecord struct {
Raw string `json:"-" db:"raw_key"`
}
+func (c *CertRecord) MarshalJSON() ([]byte, error) {
+ type Alias CertRecord
+ f := "2006-01-02 15:04:05 -0700"
+ return json.Marshal(&struct {
+ *Alias
+ CreatedAt string `json:"created_at"`
+ Expires string `json:"expires"`
+ }{
+ Alias: (*Alias)(c),
+ CreatedAt: c.CreatedAt.Format(f),
+ Expires: c.Expires.Format(f),
+ })
+}
+
func parseTime(t uint64) time.Time {
return time.Unix(int64(t), 0)
}
diff --git a/server/store/store_test.go b/server/store/store_test.go
index cd58ccd..71a74c0 100644
--- a/server/store/store_test.go
+++ b/server/store/store_test.go
@@ -4,6 +4,7 @@ import (
"crypto/rand"
"crypto/rsa"
"database/sql"
+ "encoding/json"
"io/ioutil"
"os"
"os/user"
@@ -160,3 +161,20 @@ func TestSQLiteStore(t *testing.T) {
}
testStore(t, db)
}
+
+func TestMarshalCert(t *testing.T) {
+ a := assert.New(t)
+ c := &CertRecord{
+ KeyID: "id",
+ Principals: []string{"user"},
+ CreatedAt: time.Date(2017, time.April, 10, 13, 0, 0, 0, time.UTC),
+ Expires: time.Date(2017, time.April, 11, 10, 0, 0, 0, time.UTC),
+ Raw: "ABCDEF",
+ }
+ b, err := json.Marshal(c)
+ if err != nil {
+ t.Error(err)
+ }
+ want := `{"key_id":"id","principals":["user"],"revoked":false,"created_at":"2017-04-10 13:00:00 +0000","expires":"2017-04-11 10:00:00 +0000"}`
+ a.JSONEq(want, string(b))
+}