Browse Source

Fixes #3110 (#3136)

pull/3174/head
Franz Schmidt 9 years ago committed by 无闻
parent
commit
8b35c194ec
  1. 12
      models/error.go
  2. 3
      models/token.go
  3. 2
      modules/auth/auth.go
  4. 2
      routers/repo/http.go

12
models/error.go

@ -280,6 +280,18 @@ func (err ErrAccessTokenNotExist) Error() string {
return fmt.Sprintf("access token does not exist [sha: %s]", err.SHA) return fmt.Sprintf("access token does not exist [sha: %s]", err.SHA)
} }
type ErrAccessTokenEmpty struct {
}
func IsErrAccessTokenEmpty(err error) bool {
_, ok := err.(ErrAccessTokenEmpty)
return ok
}
func (err ErrAccessTokenEmpty) Error() string {
return fmt.Sprintf("access token is empty")
}
// ________ .__ __ .__ // ________ .__ __ .__
// \_____ \_______ _________ ____ |__|____________ _/ |_|__| ____ ____ // \_____ \_______ _________ ____ |__|____________ _/ |_|__| ____ ____
// / | \_ __ \/ ___\__ \ / \| \___ /\__ \\ __\ |/ _ \ / \ // / | \_ __ \/ ___\__ \ / \| \___ /\__ \\ __\ |/ _ \ / \

3
models/token.go

@ -56,6 +56,9 @@ func NewAccessToken(t *AccessToken) error {
// GetAccessTokenBySHA returns access token by given sha1. // GetAccessTokenBySHA returns access token by given sha1.
func GetAccessTokenBySHA(sha string) (*AccessToken, error) { func GetAccessTokenBySHA(sha string) (*AccessToken, error) {
if sha == "" {
return nil, ErrAccessTokenEmpty{}
}
t := &AccessToken{Sha1: sha} t := &AccessToken{Sha1: sha}
has, err := x.Get(t) has, err := x.Get(t)
if err != nil { if err != nil {

2
modules/auth/auth.go

@ -49,7 +49,7 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
if len(tokenSHA) > 0 { if len(tokenSHA) > 0 {
t, err := models.GetAccessTokenBySHA(tokenSHA) t, err := models.GetAccessTokenBySHA(tokenSHA)
if err != nil { if err != nil {
if models.IsErrAccessTokenNotExist(err) { if models.IsErrAccessTokenNotExist(err) || models.IsErrAccessTokenEmpty(err) {
log.Error(4, "GetAccessTokenBySHA: %v", err) log.Error(4, "GetAccessTokenBySHA: %v", err)
} }
return 0 return 0

2
routers/repo/http.go

@ -112,7 +112,7 @@ func HTTP(ctx *context.Context) {
// Assume username now is a token. // Assume username now is a token.
token, err := models.GetAccessTokenBySHA(authUsername) token, err := models.GetAccessTokenBySHA(authUsername)
if err != nil { if err != nil {
if models.IsErrAccessTokenNotExist(err) { if models.IsErrAccessTokenNotExist(err) || models.IsErrAccessTokenEmpty(err) {
ctx.HandleText(http.StatusUnauthorized, "invalid token") ctx.HandleText(http.StatusUnauthorized, "invalid token")
} else { } else {
ctx.Handle(http.StatusInternalServerError, "GetAccessTokenBySha", err) ctx.Handle(http.StatusInternalServerError, "GetAccessTokenBySha", err)

Loading…
Cancel
Save