-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] GPG Verification (#425) #476
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2014 The Gogs Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/gogits/gogs/modules/log" | ||
) | ||
|
||
//TODO maybe refector with ssh-key ? | ||
//TODO database behind | ||
|
||
// PublicGPGKey represents a GPG key. | ||
type PublicGPGKey struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
OwnerID int64 `xorm:"INDEX NOT NULL"` | ||
Name string `xorm:"NOT NULL"` | ||
Fingerprint string `xorm:"NOT NULL"` | ||
Content string `xorm:"TEXT NOT NULL"` | ||
|
||
Created time.Time `xorm:"-"` | ||
} | ||
|
||
// ListPublicGPGKeys returns a list of public keys belongs to given user. | ||
func ListPublicGPGKeys(uid int64) ([]*PublicGPGKey, error) { | ||
keys := make([]*PublicGPGKey, 0, 5) | ||
return keys, x.Where("owner_id=?", uid).Find(&keys) | ||
} | ||
|
||
// GetPublicGPGKeyByID returns public key by given ID. | ||
func GetPublicGPGKeyByID(keyID int64) (*PublicGPGKey, error) { | ||
key := new(PublicGPGKey) | ||
has, err := x.Id(keyID).Get(key) | ||
if err != nil { | ||
return nil, err | ||
} else if !has { | ||
return nil, ErrKeyNotExist{keyID} | ||
} | ||
return key, nil | ||
} | ||
|
||
// CheckPublicGPGKeyString checks if the given public key string is a valid GPG key. | ||
// The function returns the actual public key line on success. | ||
func CheckPublicGPGKeyString(content string) (_ string, err error) { | ||
//TODO Implement | ||
return "", nil | ||
} | ||
|
||
// AddPublicGPGKey adds new public key to database. | ||
func AddPublicGPGKey(ownerID int64, name, content string) (*PublicKey, error) { | ||
log.Trace(content) | ||
//TODO Implement | ||
return nil, nil | ||
} | ||
|
||
// DeletePublicGPGKey deletes GPG key information in database. | ||
func DeletePublicGPGKey(doer *User, id int64) (err error) { | ||
//TODO Implement | ||
return nil | ||
} | ||
|
||
/* TODO | ||
// CheckCommitWithSign checks if author's signature of commit is corresponsind to a user. | ||
func CheckCommitWithSign(c *git.Commit) *User { | ||
u, err := GetUserByEmail(c.Author.Email) | ||
if err != nil { | ||
return nil | ||
} | ||
ks, err := ListPublicGPGKeys(u.ID) | ||
if err != nil { | ||
return nil | ||
} | ||
return u | ||
} | ||
|
||
// CheckCommitsWithSign checks if author's signature of commits are corresponding to users. | ||
func CheckCommitsWithSign(oldCommits *list.List) *list.List { | ||
var ( | ||
u *User | ||
emails = map[string]*User{} | ||
newCommits = list.New() | ||
e = oldCommits.Front() | ||
) | ||
for e != nil { | ||
c := e.Value.(*git.Commit) | ||
|
||
if v, ok := emails[c.Author.Email]; !ok { | ||
u, _ = GetUserByEmail(c.Author.Email) | ||
emails[c.Author.Email] = u | ||
} else { | ||
u = v | ||
} | ||
|
||
newCommits.PushBack(UserCommit{ | ||
User: u, | ||
Commit: c, | ||
}) | ||
e = e.Next() | ||
} | ||
return newCommits | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2015 The Gogs Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package user | ||
|
||
import ( | ||
api "github.com/gogits/go-gogs-client" | ||
|
||
"github.com/gogits/gogs/models" | ||
"github.com/gogits/gogs/modules/context" | ||
"github.com/gogits/gogs/modules/setting" | ||
"github.com/gogits/gogs/routers/api/v1/convert" | ||
"github.com/gogits/gogs/routers/api/v1/repo" | ||
) | ||
|
||
func composePublicGPGKeysAPILink() string { | ||
return setting.AppUrl + "api/v1/user/gpg_keys/" | ||
} | ||
|
||
func listPublicGPGKeys(ctx *context.APIContext, uid int64) { | ||
keys, err := models.ListPublicGPGKeys(uid) | ||
if err != nil { | ||
ctx.Error(500, "ListPublicGPGKeys", err) | ||
return | ||
} | ||
|
||
apiLink := composePublicGPGKeysAPILink() | ||
apiKeys := make([]*api.PublicKey, len(keys)) | ||
for i := range keys { | ||
apiKeys[i] = convert.ToPublicGPGKey(apiLink, keys[i]) | ||
} | ||
|
||
ctx.JSON(200, &apiKeys) | ||
} | ||
|
||
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-GPG-Keys#list-your-public-keys | ||
func ListMyPublicGPGKeys(ctx *context.APIContext) { | ||
listPublicGPGKeys(ctx, ctx.User.ID) | ||
} | ||
|
||
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-GPG-Keys#get-a-single-public-key | ||
func GetPublicGPGKey(ctx *context.APIContext) { | ||
key, err := models.GetPublicGPGKeyByID(ctx.ParamsInt64(":id")) | ||
if err != nil { | ||
if models.IsErrKeyNotExist(err) { | ||
ctx.Status(404) | ||
} else { | ||
ctx.Error(500, "GetPublicGPGKeyByID", err) | ||
} | ||
return | ||
} | ||
|
||
apiLink := composePublicGPGKeysAPILink() | ||
ctx.JSON(200, convert.ToPublicGPGKey(apiLink, key)) | ||
} | ||
|
||
// CreateUserPublicGPGKey creates new public GPG key to given user by ID. | ||
func CreateUserPublicGPGKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) { | ||
content, err := models.CheckPublicGPGKeyString(form.Key) | ||
if err != nil { | ||
repo.HandleCheckGPGKeyStringError(ctx, err) | ||
return | ||
} | ||
|
||
key, err := models.AddPublicGPGKey(uid, form.Title, content) | ||
if err != nil { | ||
repo.HandleAddGPGKeyError(ctx, err) | ||
return | ||
} | ||
apiLink := composePublicKeysAPILink() | ||
ctx.JSON(201, convert.ToPublicKey(apiLink, key)) | ||
} | ||
|
||
//TODO Update api | ||
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-GPG-Keys#create-a-public-key | ||
func CreatePublicGPGKey(ctx *context.APIContext, form api.CreateKeyOption) { | ||
CreateUserPublicGPGKey(ctx, form, ctx.User.ID) | ||
} | ||
|
||
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#delete-a-public-key | ||
func DeletePublicGPGKey(ctx *context.APIContext) { | ||
if err := models.DeletePublicGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { | ||
if models.IsErrGPGKeyAccessDenied(err) { | ||
ctx.Error(403, "", "You do not have access to this key") | ||
} else { | ||
ctx.Error(500, "DeletePublicGPGKey", err) | ||
} | ||
return | ||
} | ||
|
||
ctx.Status(204) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If every user only has one GPG key?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
humm? AFAIK this allows for a one-to-many setup, so a single user can have many keys.