在部署到App Engine时,http.Request中的字段未定义。

huangapple go评论87阅读模式
英文:

http.Request has undefined fields when deploying to appengine

问题

我正在开发一个网络应用程序,我依赖以下代码进行身份验证(我使用github.com/dgrijalva/jwt-go包):

func ValidateProtectedPage(protectedPage http.HandlerFunc) http.HandlerFunc {
	return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		// 如果没有设置Auth cookie,则返回404 not found
		cookie, err := req.Cookie("Auth")
		if err != nil {
			Forbidden(res)
			return
		}

		// 使用cookie返回一个Token
		token, err := jwt.ParseWithClaims(cookie.Value, &Claims{}, func(token *jwt.Token) (interface{}, error) {
			// 确保token的签名未被更改
			if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
				return nil, fmt.Errorf("Unexpected siging method")
			}
			return []byte(util.Secret), nil
		})

		if err != nil {
			Forbidden(res)
			return
		}

		// 获取token的声明并将其传递给原始请求
		if claims, ok := token.Claims.(*Claims); ok && token.Valid {
			ctx := context.WithValue(req.Context(), "Auth", *claims)
			protectedPage(res, req.WithContext(ctx))
		} else {
			Forbidden(res)
		}
	})
}

问题是,我试图将我的应用部署到appengine上,但是我遇到了以下错误:

planilla-go/controllers/auth.go:45: req.Context undefined (type *http.Request has no field or method Context)
planilla-go/controllers/auth.go:46: req.WithContext undefined (type *http.Request has no field or method WithContext)

根据我所了解,这是由于appengine的请求与go库中的请求不兼容导致的,但我还没有找到解决方法。

有没有办法包装或转换http.Request以使用appengine的请求呢?

英文:

I'm working on a web app and I depend on the following code for authentication (I'm using github.com/dgrijalva/jwt-go package):

func ValidateProtectedPage(protectedPage http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
	// If no Auth cookie is set then return a 404 not found
	cookie, err := req.Cookie("Auth")
	if err != nil {
		Forbidden(res)
		return
	}

	// Return a Token using the cookie
	token, err := jwt.ParseWithClaims(cookie.Value, &Claims{}, func(token *jwt.Token) (interface{}, error) {
		// Make sure token's signature wasn't changed
		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
			return nil, fmt.Errorf("Unexpected siging method")
		}
		return []byte(util.Secret), nil
	})

	if err != nil {
		Forbidden(res)
		return
	}

	// Grab the tokens claims and pass it into the original request
	if claims, ok := token.Claims.(*Claims); ok && token.Valid {
		ctx := context.WithValue(req.Context(), "Auth", *claims)
		protectedPage(res, req.WithContext(ctx))
	} else {
		Forbidden(res)
	}
})}

The issue is, that I'm trying to deploy my app to appengine, and I get the following errors:

planilla-go/controllers/auth.go:45: req.Context undefined (type *http.Request has no field or method Context)
planilla-go/controllers/auth.go:46: req.WithContext undefined (type *http.Request has no field or method WithContext)

From what I read this is due to incompatibilities between appengine's requests and those from go's library, but I couldn't find a workaround yet

Is there a way to wrap or convert the http.Request to use appengine's instead?

答案1

得分: 0

Appengine只支持Go 1.6。这有点令人困惑,因为它使我的应用程序无法使用,需要进行重大的兼容性改造,但这是当前平台的状态。

英文:

Appengine only supports Go 1.6. A bit baffling since it renders my app useless without a major compat overhaul, but that's the current state of the platform

huangapple
  • 本文由 发表于 2017年5月3日 00:33:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/43743025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定