tokarchuk/v1.17
Lunny Xiao 11 years ago
parent a92826a8fe
commit 9791e70da6
  1. 4
      models/publickey.go
  2. 70
      routers/repo/repo.go

@ -77,8 +77,8 @@ func init() {
// PublicKey represents a SSH key of user. // PublicKey represents a SSH key of user.
type PublicKey struct { type PublicKey struct {
Id int64 Id int64
OwnerId int64 `xorm:" index not null"` OwnerId int64 `xorm:"unique(s) index not null"`
Name string `xorm:" not null"` //UNIQUE(s) Name string `xorm:"unique(s) not null"` //UNIQUE(s)
Fingerprint string Fingerprint string
Content string `xorm:"TEXT not null"` Content string `xorm:"TEXT not null"`
Created time.Time `xorm:"created"` Created time.Time `xorm:"created"`

@ -261,7 +261,7 @@ func basicDecode(encoded string) (user string, name string, err error) {
} }
func authRequired(ctx *middleware.Context) { func authRequired(ctx *middleware.Context) {
ctx.ResponseWriter.Header().Set("WWW-Authenticate", `Basic realm="Gogs Auth"`) ctx.ResponseWriter.Header().Set("WWW-Authenticate", "Basic realm=\".\"")
ctx.Data["ErrorMsg"] = "no basic auth and digit auth" ctx.Data["ErrorMsg"] = "no basic auth and digit auth"
ctx.HTML(401, fmt.Sprintf("status/401")) ctx.HTML(401, fmt.Sprintf("status/401"))
} }
@ -273,6 +273,8 @@ func Http(ctx *middleware.Context, params martini.Params) {
reponame = reponame[:len(reponame)-4] reponame = reponame[:len(reponame)-4]
} }
//fmt.Println("req:", ctx.Req.Header)
repoUser, err := models.GetUserByName(username) repoUser, err := models.GetUserByName(username)
if err != nil { if err != nil {
ctx.Handle(500, "repo.GetUserByName", nil) ctx.Handle(500, "repo.GetUserByName", nil)
@ -297,43 +299,43 @@ func Http(ctx *middleware.Context, params martini.Params) {
// check basic auth // check basic auth
baHead := ctx.Req.Header.Get("Authorization") baHead := ctx.Req.Header.Get("Authorization")
if baHead != "" { if baHead == "" {
auths := strings.Fields(baHead) authRequired(ctx)
if len(auths) != 2 || auths[0] != "Basic" { return
ctx.Handle(401, "no basic auth and digit auth", nil) }
return
}
authUsername, passwd, err := basicDecode(auths[1])
if err != nil {
ctx.Handle(401, "no basic auth and digit auth", nil)
return
}
authUser, err := models.GetUserByName(authUsername) auths := strings.Fields(baHead)
if err != nil { if len(auths) != 2 || auths[0] != "Basic" {
ctx.Handle(401, "no basic auth and digit auth", nil) ctx.Handle(401, "no basic auth and digit auth", nil)
return return
} }
authUsername, passwd, err := basicDecode(auths[1])
if err != nil {
ctx.Handle(401, "no basic auth and digit auth", nil)
return
}
newUser := &models.User{Passwd: passwd} authUser, err := models.GetUserByName(authUsername)
newUser.EncodePasswd() if err != nil {
if authUser.Passwd != newUser.Passwd { ctx.Handle(401, "no basic auth and digit auth", nil)
ctx.Handle(401, "no basic auth and digit auth", nil) return
return }
}
var tp = models.AU_WRITABLE newUser := &models.User{Passwd: passwd}
if isPull { newUser.EncodePasswd()
tp = models.AU_READABLE if authUser.Passwd != newUser.Passwd {
} ctx.Handle(401, "no basic auth and digit auth", nil)
return
}
has, err := models.HasAccess(authUsername, username+"/"+reponame, tp) var tp = models.AU_WRITABLE
if err != nil || !has { if isPull {
ctx.Handle(401, "no basic auth and digit auth", nil) tp = models.AU_READABLE
return }
}
} else { has, err := models.HasAccess(authUsername, username+"/"+reponame, tp)
authRequired(ctx) if err != nil || !has {
ctx.Handle(401, "no basic auth and digit auth", nil)
return return
} }
} }

Loading…
Cancel
Save