diff options
Diffstat (limited to 'routers')
-rw-r--r-- | routers/user/home.go | 41 | ||||
-rw-r--r-- | routers/user/profile.go | 15 |
2 files changed, 55 insertions, 1 deletions
diff --git a/routers/user/home.go b/routers/user/home.go index 740a9edc4e..8eedeb70bd 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -19,6 +19,8 @@ import ( "github.com/Unknwon/com" "github.com/Unknwon/paginater" + "github.com/keybase/go-crypto/openpgp" + "github.com/keybase/go-crypto/openpgp/armor" ) const ( @@ -384,6 +386,45 @@ func ShowSSHKeys(ctx *context.Context, uid int64) { ctx.PlainText(200, buf.Bytes()) } +// ShowGPGKeys output all the public GPG keys of user by uid +func ShowGPGKeys(ctx *context.Context, uid int64) { + keys, err := models.ListGPGKeys(uid) + if err != nil { + ctx.ServerError("ListGPGKeys", err) + return + } + entities := make([]*openpgp.Entity, 0) + failedEntitiesID := make([]string, 0) + for _, k := range keys { + e, err := models.GPGKeyToEntity(k) + if err != nil { + if models.IsErrGPGKeyImportNotExist(err) { + failedEntitiesID = append(failedEntitiesID, k.KeyID) + continue //Skip previous import without backup of imported armored key + } + ctx.ServerError("ShowGPGKeys", err) + return + } + entities = append(entities, e) + } + var buf bytes.Buffer + + headers := make(map[string]string) + if len(failedEntitiesID) > 0 { //If some key need re-import to be exported + headers["Note"] = fmt.Sprintf("The keys with the following IDs couldn't be exported and need to be reuploaded %s", strings.Join(failedEntitiesID, ", ")) + } + writer, _ := armor.Encode(&buf, "PGP PUBLIC KEY BLOCK", headers) + for _, e := range entities { + err = e.Serialize(writer) //TODO find why key are exported with a different cipherTypeByte as original (should not be blocking but strange) + if err != nil { + ctx.ServerError("ShowGPGKeys", err) + return + } + } + writer.Close() + ctx.PlainText(200, buf.Bytes()) +} + func showOrgProfile(ctx *context.Context) { ctx.SetParams(":org", ctx.Params(":username")) context.HandleOrgAssignment(ctx) diff --git a/routers/user/profile.go b/routers/user/profile.go index 03f88e256a..675c1dc3f4 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -59,9 +59,16 @@ func Profile(ctx *context.Context) { isShowKeys := false if strings.HasSuffix(uname, ".keys") { isShowKeys = true + uname = strings.TrimSuffix(uname, ".keys") } - ctxUser := GetUserByName(ctx, strings.TrimSuffix(uname, ".keys")) + isShowGPG := false + if strings.HasSuffix(uname, ".gpg") { + isShowGPG = true + uname = strings.TrimSuffix(uname, ".gpg") + } + + ctxUser := GetUserByName(ctx, uname) if ctx.Written() { return } @@ -72,6 +79,12 @@ func Profile(ctx *context.Context) { return } + // Show GPG keys. + if isShowGPG { + ShowGPGKeys(ctx, ctxUser.ID) + return + } + if ctxUser.IsOrganization() { showOrgProfile(ctx) return |