summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorUnknwon <joe2010xtmf@163.com>2014-11-12 06:48:50 -0500
committerUnknwon <joe2010xtmf@163.com>2014-11-12 06:48:50 -0500
commit8c9338a5377c60c84cdee1f5781b3de5933bb3b0 (patch)
tree57de36743bf8b9c8eaa0ff51172180b834354c4d /modules
parent21b9d5fa1f4014b30619d221f5d665509d373147 (diff)
downloadgitea-8c9338a5377c60c84cdee1f5781b3de5933bb3b0.tar.gz
gitea-8c9338a5377c60c84cdee1f5781b3de5933bb3b0.zip
add personal access token panel #12
Diffstat (limited to 'modules')
-rw-r--r--modules/auth/auth.go28
-rw-r--r--modules/auth/publickey_form.go19
-rw-r--r--modules/auth/user_form.go17
-rw-r--r--modules/base/tool.go7
-rw-r--r--modules/middleware/context.go2
5 files changed, 48 insertions, 25 deletions
diff --git a/modules/auth/auth.go b/modules/auth/auth.go
index b9266d6895..1a7606a790 100644
--- a/modules/auth/auth.go
+++ b/modules/auth/auth.go
@@ -20,7 +20,7 @@ import (
)
// SignedInId returns the id of signed in user.
-func SignedInId(header http.Header, sess session.Store) int64 {
+func SignedInId(req *http.Request, sess session.Store) int64 {
if !models.HasEngine {
return 0
}
@@ -38,20 +38,38 @@ func SignedInId(header http.Header, sess session.Store) int64 {
}
return id
}
+
+ // API calls also need to check access token.
+ if strings.HasPrefix(req.URL.Path, "/api/") {
+ auHead := req.Header.Get("Authorization")
+ if len(auHead) > 0 {
+ auths := strings.Fields(auHead)
+ if len(auths) == 2 && auths[0] == "token" {
+ t, err := models.GetAccessTokenBySha(auths[1])
+ if err != nil {
+ if err != models.ErrAccessTokenNotExist {
+ log.Error(4, "GetAccessTokenBySha: %v", err)
+ }
+ return 0
+ }
+ return t.Uid
+ }
+ }
+ }
return 0
}
// SignedInUser returns the user object of signed user.
-func SignedInUser(header http.Header, sess session.Store) *models.User {
+func SignedInUser(req *http.Request, sess session.Store) *models.User {
if !models.HasEngine {
return nil
}
- uid := SignedInId(header, sess)
+ uid := SignedInId(req, sess)
if uid <= 0 {
if setting.Service.EnableReverseProxyAuth {
- webAuthUser := header.Get(setting.ReverseProxyAuthUser)
+ webAuthUser := req.Header.Get(setting.ReverseProxyAuthUser)
if len(webAuthUser) > 0 {
u, err := models.GetUserByName(webAuthUser)
if err != nil {
@@ -65,7 +83,7 @@ func SignedInUser(header http.Header, sess session.Store) *models.User {
}
// Check with basic auth.
- baHead := header.Get("Authorization")
+ baHead := req.Header.Get("Authorization")
if len(baHead) > 0 {
auths := strings.Fields(baHead)
if len(auths) == 2 && auths[0] == "Basic" {
diff --git a/modules/auth/publickey_form.go b/modules/auth/publickey_form.go
deleted file mode 100644
index 5a1d44c04b..0000000000
--- a/modules/auth/publickey_form.go
+++ /dev/null
@@ -1,19 +0,0 @@
-// 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 auth
-
-import (
- "github.com/Unknwon/macaron"
- "github.com/macaron-contrib/binding"
-)
-
-type AddSSHKeyForm struct {
- SSHTitle string `form:"title" binding:"Required"`
- Content string `form:"content" binding:"Required"`
-}
-
-func (f *AddSSHKeyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
- return validate(errs, ctx.Data, f, ctx.Locale)
-}
diff --git a/modules/auth/user_form.go b/modules/auth/user_form.go
index 72bdd4589f..6046a8d1ee 100644
--- a/modules/auth/user_form.go
+++ b/modules/auth/user_form.go
@@ -95,3 +95,20 @@ type ChangePasswordForm struct {
func (f *ChangePasswordForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
return validate(errs, ctx.Data, f, ctx.Locale)
}
+
+type AddSSHKeyForm struct {
+ SSHTitle string `form:"title" binding:"Required"`
+ Content string `form:"content" binding:"Required"`
+}
+
+func (f *AddSSHKeyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
+ return validate(errs, ctx.Data, f, ctx.Locale)
+}
+
+type NewAccessTokenForm struct {
+ Name string `form:"name" binding:"Required"`
+}
+
+func (f *NewAccessTokenForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
+ return validate(errs, ctx.Data, f, ctx.Locale)
+}
diff --git a/modules/base/tool.go b/modules/base/tool.go
index 4d3e1c7bfd..50f073a525 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -33,6 +33,13 @@ func EncodeMd5(str string) string {
return hex.EncodeToString(m.Sum(nil))
}
+// Encode string to sha1 hex value.
+func EncodeSha1(str string) string {
+ h := sha1.New()
+ h.Write([]byte(str))
+ return hex.EncodeToString(h.Sum(nil))
+}
+
func BasicAuthDecode(encoded string) (user string, name string, err error) {
var s []byte
s, err = base64.StdEncoding.DecodeString(encoded)
diff --git a/modules/middleware/context.go b/modules/middleware/context.go
index 58b4f7497b..cbc0b0cf3c 100644
--- a/modules/middleware/context.go
+++ b/modules/middleware/context.go
@@ -172,7 +172,7 @@ func Contexter() macaron.Handler {
ctx.Data["PageStartTime"] = time.Now()
// Get user from session if logined.
- ctx.User = auth.SignedInUser(ctx.Req.Header, ctx.Session)
+ ctx.User = auth.SignedInUser(ctx.Req.Request, ctx.Session)
if ctx.User != nil {
ctx.IsSigned = true