From e7c8a3cb8d26da68b09f799585c03970cd243be1 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 6 Apr 2014 16:10:57 -0400 Subject: Add salt for every single user --- modules/base/tool.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'modules/base/tool.go') diff --git a/modules/base/tool.go b/modules/base/tool.go index 3946c4b5..f7d1bc2c 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -6,12 +6,14 @@ package base import ( "bytes" + "crypto/hmac" "crypto/md5" "crypto/rand" "crypto/sha1" "encoding/hex" "encoding/json" "fmt" + "hash" "math" "strconv" "strings" @@ -40,6 +42,44 @@ func GetRandomString(n int, alphabets ...byte) string { return string(bytes) } +// http://code.google.com/p/go/source/browse/pbkdf2/pbkdf2.go?repo=crypto +func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte { + prf := hmac.New(h, password) + hashLen := prf.Size() + numBlocks := (keyLen + hashLen - 1) / hashLen + + var buf [4]byte + dk := make([]byte, 0, numBlocks*hashLen) + U := make([]byte, hashLen) + for block := 1; block <= numBlocks; block++ { + // N.B.: || means concatenation, ^ means XOR + // for each block T_i = U_1 ^ U_2 ^ ... ^ U_iter + // U_1 = PRF(password, salt || uint(i)) + prf.Reset() + prf.Write(salt) + buf[0] = byte(block >> 24) + buf[1] = byte(block >> 16) + buf[2] = byte(block >> 8) + buf[3] = byte(block) + prf.Write(buf[:4]) + dk = prf.Sum(dk) + T := dk[len(dk)-hashLen:] + copy(U, T) + + // U_n = PRF(password, U_(n-1)) + for n := 2; n <= iter; n++ { + prf.Reset() + prf.Write(U) + U = U[:0] + U = prf.Sum(U) + for x := range U { + T[x] ^= U[x] + } + } + } + return dk[:keyLen] +} + // verify time limit code func VerifyTimeLimitCode(data string, minutes int, code string) bool { if len(code) <= 18 { -- cgit v1.2.3 From e2fe2209057b90e6c78a84b7c66c3395cf100e30 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 7 Apr 2014 00:47:19 -0400 Subject: Work on comment --- modules/base/markdown.go | 27 +++++++++++- modules/base/template.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++ modules/base/tool.go | 106 ----------------------------------------------- 3 files changed, 131 insertions(+), 108 deletions(-) (limited to 'modules/base/tool.go') diff --git a/modules/base/markdown.go b/modules/base/markdown.go index 962e1ae1..828f87de 100644 --- a/modules/base/markdown.go +++ b/modules/base/markdown.go @@ -6,9 +6,11 @@ package base import ( "bytes" + "fmt" "net/http" "path" "path/filepath" + "regexp" "strings" "github.com/gogits/gfm" @@ -87,7 +89,28 @@ func (options *CustomRender) Link(out *bytes.Buffer, link []byte, title []byte, options.Renderer.Link(out, link, title, content) } +var ( + mentionPattern = regexp.MustCompile(`@[0-9a-zA-Z_]{1,}`) + commitPattern = regexp.MustCompile(`[^>]http[s]{0,}.*commit/[0-9a-zA-Z]{1,}`) +) + +func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte { + ms := mentionPattern.FindAll(rawBytes, -1) + for _, m := range ms { + rawBytes = bytes.Replace(rawBytes, m, + []byte(fmt.Sprintf(`%s`, m[1:], m)), -1) + } + ms = commitPattern.FindAll(rawBytes, -1) + for _, m := range ms { + rawBytes = bytes.Replace(rawBytes, m, + []byte(fmt.Sprintf(`%s`, m, m)), -1) + } + return rawBytes +} + func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte { + body := RenderSpecialLink(rawBytes, urlPrefix) + fmt.Println(string(body)) htmlFlags := 0 // htmlFlags |= gfm.HTML_USE_XHTML // htmlFlags |= gfm.HTML_USE_SMARTYPANTS @@ -115,7 +138,7 @@ func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte { extensions |= gfm.EXTENSION_SPACE_HEADERS extensions |= gfm.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK - body := gfm.Markdown(rawBytes, renderer, extensions) - + body = gfm.Markdown(body, renderer, extensions) + fmt.Println(string(body)) return body } diff --git a/modules/base/template.go b/modules/base/template.go index 56b77a5d..6cd8ade6 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -5,7 +5,9 @@ package base import ( + "bytes" "container/list" + "encoding/json" "fmt" "html/template" "strings" @@ -85,3 +87,107 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "DiffLineTypeToStr": DiffLineTypeToStr, "ShortSha": ShortSha, } + +type Actioner interface { + GetOpType() int + GetActUserName() string + GetActEmail() string + GetRepoName() string + GetBranch() string + GetContent() string +} + +// ActionIcon accepts a int that represents action operation type +// and returns a icon class name. +func ActionIcon(opType int) string { + switch opType { + case 1: // Create repository. + return "plus-circle" + case 5: // Commit repository. + return "arrow-circle-o-right" + case 6: // Create issue. + return "exclamation-circle" + case 8: // Transfer repository. + return "share" + default: + return "invalid type" + } +} + +const ( + TPL_CREATE_REPO = `%s created repository %s` + TPL_COMMIT_REPO = `%s pushed to %s at %s%s` + TPL_COMMIT_REPO_LI = `
user-avatar %s %s
` + TPL_CREATE_ISSUE = `%s opened issue %s#%s +
user-avatar %s
` + TPL_TRANSFER_REPO = `%s transfered repository %s to %s` +) + +type PushCommit struct { + Sha1 string + Message string + AuthorEmail string + AuthorName string +} + +type PushCommits struct { + Len int + Commits []*PushCommit +} + +// ActionDesc accepts int that represents action operation type +// and returns the description. +func ActionDesc(act Actioner) string { + actUserName := act.GetActUserName() + email := act.GetActEmail() + repoName := act.GetRepoName() + repoLink := actUserName + "/" + repoName + branch := act.GetBranch() + content := act.GetContent() + switch act.GetOpType() { + case 1: // Create repository. + return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, repoLink, repoName) + case 5: // Commit repository. + var push *PushCommits + if err := json.Unmarshal([]byte(content), &push); err != nil { + return err.Error() + } + buf := bytes.NewBuffer([]byte("\n")) + for _, commit := range push.Commits { + buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, AvatarLink(commit.AuthorEmail), repoLink, commit.Sha1, commit.Sha1[:7], commit.Message) + "\n") + } + if push.Len > 3 { + buf.WriteString(fmt.Sprintf(`
%d other commits >>
`, actUserName, repoName, branch, push.Len)) + } + return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink, + buf.String()) + case 6: // Create issue. + infos := strings.SplitN(content, "|", 2) + return fmt.Sprintf(TPL_CREATE_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0], + AvatarLink(email), infos[1]) + case 8: // Transfer repository. + newRepoLink := content + "/" + repoName + return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink) + default: + return "invalid type" + } +} + +func DiffTypeToStr(diffType int) string { + diffTypes := map[int]string{ + 1: "add", 2: "modify", 3: "del", + } + return diffTypes[diffType] +} + +func DiffLineTypeToStr(diffType int) string { + switch diffType { + case 2: + return "add" + case 3: + return "del" + case 4: + return "tag" + } + return "same" +} diff --git a/modules/base/tool.go b/modules/base/tool.go index f7d1bc2c..0f06b3e0 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -5,13 +5,11 @@ package base import ( - "bytes" "crypto/hmac" "crypto/md5" "crypto/rand" "crypto/sha1" "encoding/hex" - "encoding/json" "fmt" "hash" "math" @@ -514,107 +512,3 @@ func (a argInt) Get(i int, args ...int) (r int) { } return } - -type Actioner interface { - GetOpType() int - GetActUserName() string - GetActEmail() string - GetRepoName() string - GetBranch() string - GetContent() string -} - -// ActionIcon accepts a int that represents action operation type -// and returns a icon class name. -func ActionIcon(opType int) string { - switch opType { - case 1: // Create repository. - return "plus-circle" - case 5: // Commit repository. - return "arrow-circle-o-right" - case 6: // Create issue. - return "exclamation-circle" - case 8: // Transfer repository. - return "share" - default: - return "invalid type" - } -} - -const ( - TPL_CREATE_REPO = `%s created repository %s` - TPL_COMMIT_REPO = `%s pushed to %s at %s%s` - TPL_COMMIT_REPO_LI = `
user-avatar %s %s
` - TPL_CREATE_ISSUE = `%s opened issue %s#%s -
user-avatar %s
` - TPL_TRANSFER_REPO = `%s transfered repository %s to %s` -) - -type PushCommit struct { - Sha1 string - Message string - AuthorEmail string - AuthorName string -} - -type PushCommits struct { - Len int - Commits []*PushCommit -} - -// ActionDesc accepts int that represents action operation type -// and returns the description. -func ActionDesc(act Actioner) string { - actUserName := act.GetActUserName() - email := act.GetActEmail() - repoName := act.GetRepoName() - repoLink := actUserName + "/" + repoName - branch := act.GetBranch() - content := act.GetContent() - switch act.GetOpType() { - case 1: // Create repository. - return fmt.Sprintf(TPL_CREATE_REPO, actUserName, actUserName, repoLink, repoName) - case 5: // Commit repository. - var push *PushCommits - if err := json.Unmarshal([]byte(content), &push); err != nil { - return err.Error() - } - buf := bytes.NewBuffer([]byte("\n")) - for _, commit := range push.Commits { - buf.WriteString(fmt.Sprintf(TPL_COMMIT_REPO_LI, AvatarLink(commit.AuthorEmail), repoLink, commit.Sha1, commit.Sha1[:7], commit.Message) + "\n") - } - if push.Len > 3 { - buf.WriteString(fmt.Sprintf(`
%d other commits >>
`, actUserName, repoName, branch, push.Len)) - } - return fmt.Sprintf(TPL_COMMIT_REPO, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink, - buf.String()) - case 6: // Create issue. - infos := strings.SplitN(content, "|", 2) - return fmt.Sprintf(TPL_CREATE_ISSUE, actUserName, actUserName, repoLink, infos[0], repoLink, infos[0], - AvatarLink(email), infos[1]) - case 8: // Transfer repository. - newRepoLink := content + "/" + repoName - return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink) - default: - return "invalid type" - } -} - -func DiffTypeToStr(diffType int) string { - diffTypes := map[int]string{ - 1: "add", 2: "modify", 3: "del", - } - return diffTypes[diffType] -} - -func DiffLineTypeToStr(diffType int) string { - switch diffType { - case 2: - return "add" - case 3: - return "del" - case 4: - return "tag" - } - return "same" -} -- cgit v1.2.3 From 9d983f27d69b72f42eeb833cb0f38e78e3819839 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 13 Apr 2014 03:14:43 -0400 Subject: go vet --- gogs.go | 4 ++-- models/models_sqlite.go | 4 ++-- models/update.go | 4 ++-- modules/base/tool.go | 1 - modules/mailer/mail.go | 2 +- modules/middleware/binding_test.go | 4 ++-- modules/oauth2/oauth2.go | 6 ------ 7 files changed, 9 insertions(+), 16 deletions(-) (limited to 'modules/base/tool.go') diff --git a/gogs.go b/gogs.go index 1e614f49..7a7d3ac8 100644 --- a/gogs.go +++ b/gogs.go @@ -1,3 +1,5 @@ +// +build go1.2 + // 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. @@ -14,8 +16,6 @@ import ( "github.com/gogits/gogs/modules/base" ) -// +build go1.2 - // Test that go1.2 tag above is included in builds. main.go refers to this definition. const go12tag = true diff --git a/models/models_sqlite.go b/models/models_sqlite.go index 1d80823f..c77e5ae5 100644 --- a/models/models_sqlite.go +++ b/models/models_sqlite.go @@ -1,9 +1,9 @@ +// +build sqlite + // 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. -// +build sqlite - package models import ( diff --git a/models/update.go b/models/update.go index 53591fa4..ba0e9793 100644 --- a/models/update.go +++ b/models/update.go @@ -39,12 +39,12 @@ func Update(refName, oldCommitId, newCommitId, userName, repoName string, userId if isNew { l, err = newCommit.CommitsBefore() if err != nil { - qlog.Fatalf("Find CommitsBefore erro:", err) + qlog.Fatalf("Find CommitsBefore erro: %v", err) } } else { l, err = newCommit.CommitsBeforeUntil(oldCommitId) if err != nil { - qlog.Fatalf("Find CommitsBeforeUntil erro:", err) + qlog.Fatalf("Find CommitsBeforeUntil erro: %v", err) return } } diff --git a/modules/base/tool.go b/modules/base/tool.go index 0f06b3e0..082c0392 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -284,7 +284,6 @@ func TimeSince(then time.Time) string { default: return fmt.Sprintf("%d years %s", diff/Year, lbl) } - return then.String() } const ( diff --git a/modules/mailer/mail.go b/modules/mailer/mail.go index d2bf1310..834f4a89 100644 --- a/modules/mailer/mail.go +++ b/modules/mailer/mail.go @@ -152,7 +152,7 @@ func SendIssueMentionMail(user, owner *models.User, repo *models.Repository, iss } issueLink := fmt.Sprintf("%s%s/%s/issues/%d", base.AppUrl, owner.Name, repo.Name, issue.Index) - body := fmt.Sprintf(`%s mentioned you.`) + body := fmt.Sprintf(`%s mentioned you.`, user.Name) subject := fmt.Sprintf("[%s] %s", repo.Name, issue.Name) content := fmt.Sprintf("%s
-
View it on Gogs.", body, issueLink) msg := NewMailMessageFrom(tos, user.Name, subject, content) diff --git a/modules/middleware/binding_test.go b/modules/middleware/binding_test.go index 654cef29..2a74e1a6 100644 --- a/modules/middleware/binding_test.go +++ b/modules/middleware/binding_test.go @@ -121,7 +121,7 @@ func handle(test testCase, t *testing.T, index int, post BlogPost, errors Errors if test.ok && errors.Count() > 0 { t.Errorf("%+v should be OK (0 errors), but had errors: %+v", test, errors) } else if !test.ok && errors.Count() == 0 { - t.Errorf("%+v should have errors, but was OK (0 errors): %+v", test) + t.Errorf("%+v should have errors, but was OK (0 errors)", test) } } @@ -132,7 +132,7 @@ func handleEmpty(test emptyPayloadTestCase, t *testing.T, index int, section Blo if test.ok && errors.Count() > 0 { t.Errorf("%+v should be OK (0 errors), but had errors: %+v", test, errors) } else if !test.ok && errors.Count() == 0 { - t.Errorf("%+v should have errors, but was OK (0 errors): %+v", test) + t.Errorf("%+v should have errors, but was OK (0 errors)", test) } } diff --git a/modules/oauth2/oauth2.go b/modules/oauth2/oauth2.go index 05ae4606..dcb6d0a4 100644 --- a/modules/oauth2/oauth2.go +++ b/modules/oauth2/oauth2.go @@ -9,7 +9,6 @@ package oauth2 import ( "encoding/json" - "fmt" "net/http" "net/url" "strings" @@ -95,11 +94,6 @@ func (t *token) ExpiryTime() time.Time { return t.Expiry } -// Formats tokens into string. -func (t *token) String() string { - return fmt.Sprintf("tokens: %v", t) -} - // Returns a new Google OAuth 2.0 backend endpoint. func Google(opts *Options) martini.Handler { opts.AuthUrl = "https://accounts.google.com/o/oauth2/auth" -- cgit v1.2.3 From b9a1d13c29c3a1dcebc5b34914a29efbafd374f5 Mon Sep 17 00:00:00 2001 From: Christopher Brickley Date: Mon, 14 Apr 2014 22:01:24 -0400 Subject: change gravatar image to match URL scheme --- models/user.go | 2 +- modules/base/tool.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'modules/base/tool.go') diff --git a/models/user.go b/models/user.go index ffee2dd9..ab43df7a 100644 --- a/models/user.go +++ b/models/user.go @@ -76,7 +76,7 @@ func (user *User) AvatarLink() string { if base.Service.EnableCacheAvatar { return "/avatar/" + user.Avatar } - return "http://1.gravatar.com/avatar/" + user.Avatar + return "//1.gravatar.com/avatar/" + user.Avatar } // NewGitSig generates and returns the signature of given user. diff --git a/modules/base/tool.go b/modules/base/tool.go index 082c0392..9b165b97 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -143,7 +143,7 @@ func AvatarLink(email string) string { if Service.EnableCacheAvatar { return "/avatar/" + EncodeMd5(email) } - return "http://1.gravatar.com/avatar/" + EncodeMd5(email) + return "//1.gravatar.com/avatar/" + EncodeMd5(email) } // Seconds-based time units -- cgit v1.2.3