使用Google App Engine上的Datastore,可以将sessionauth与martini一起使用吗?

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

Is it possible to use sessionauth of martini with Datastore on google app engine?

问题

我尝试在Google App Engine上使用martinisessionauth示例,并希望将登录列表保存在Datastore中,但不知道如何处理appengine.Context。有人有经验吗?

谢谢。

更新:

// Auth example is an example application which requires a login
// to view a private link. The username is "testuser" and the password
// is "password". This will require GORP and an SQLite3 database.
package ahl

import (
	//"fmt"
	"github.com/go-martini/martini"
	"github.com/hnakamur/gaesessions"
	"github.com/martini-contrib/binding"
	"github.com/martini-contrib/render"
	"github.com/martini-contrib/sessionauth"
	"github.com/martini-contrib/sessions"
	"net/http"
)

//var namespace string = "ahl"

func init() {
	//store := sessions.NewCookieStore([]byte("secret123"))
	store := gaesessions.NewDatastoreStore("", gaesessions.DefaultNonPersistentSessionDuration)

	m := martini.Classic()
	m.Use(render.Renderer())

	// Default our store to use Session cookies, so we don't leave logged in
	// users roaming around
	//store.Options(sessions.Options{
	//	MaxAge: 0,
	//})
	m.Use(sessions.Sessions("my_session", store))
	m.Use(sessionauth.SessionUser(GenerateAnonymousUser))
	sessionauth.RedirectUrl = "/new-login"
	sessionauth.RedirectParam = "new-next"

	m.Get("/", func(r render.Render) {
		r.HTML(200, "index", nil)
	})

	m.Get("/new-login", func(r render.Render) {
		r.HTML(200, "login", nil)
	})

	m.Post("/new-login", binding.Bind(MyUserModel{}), func(session sessions.Session, postedUser MyUserModel, r render.Render, req *http.Request) {
		// You should verify credentials against a database or some other mechanism at this point.
		// Then you can authenticate this session.
		//user := MyUserModel{}
		user := MyUserModel{1, "testuser", "password", false}
		//err := dbmap.SelectOne(&user, "SELECT * FROM users WHERE username = $1 and password = $2", postedUser.Username, postedUser.Password)
		//if err != nil {
		//	r.Redirect(sessionauth.RedirectUrl)
		//	return
		//} else {
		err := sessionauth.AuthenticateSession(session, &user)
		if err != nil {
			r.JSON(500, err)
		}

		params := req.URL.Query()
		redirect := params.Get(sessionauth.RedirectParam)
		r.Redirect(redirect)
		return
		//}
	})

	m.Get("/private", sessionauth.LoginRequired, func(r render.Render, user sessionauth.User) {
		r.HTML(200, "private", user.(*MyUserModel))
	})

	m.Get("/logout", sessionauth.LoginRequired, func(session sessions.Session, user sessionauth.User, r render.Render) {
		sessionauth.Logout(session, user)
		r.Redirect("/")
	})

	http.Handle("/", m)
}
英文:

I tried to use the example of sessionauth of martini on google app engine and want to save login list in Datastore, but did not know how to deal with appengine.Context. Does anyone has the experience?

Thank you.

Update:

// Auth example is an example application which requires a login
// to view a private link. The username is "testuser" and the password
// is "password". This will require GORP and an SQLite3 database.
package ahl
import (
//"fmt"
"github.com/go-martini/martini"
"github.com/hnakamur/gaesessions"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
"github.com/martini-contrib/sessionauth"
"github.com/martini-contrib/sessions"
"net/http"
)
//var namespace string = "ahl"
func init() {
//store := sessions.NewCookieStore([]byte("secret123"))
store := gaesessions.NewDatastoreStore("", gaesessions.DefaultNonPersistentSessionDuration)
m := martini.Classic()
m.Use(render.Renderer())
// Default our store to use Session cookies, so we don't leave logged in
// users roaming around
//store.Options(sessions.Options{
//	MaxAge: 0,
//})
m.Use(sessions.Sessions("my_session", store))
m.Use(sessionauth.SessionUser(GenerateAnonymousUser))
sessionauth.RedirectUrl = "/new-login"
sessionauth.RedirectParam = "new-next"
m.Get("/", func(r render.Render) {
r.HTML(200, "index", nil)
})
m.Get("/new-login", func(r render.Render) {
r.HTML(200, "login", nil)
})
m.Post("/new-login", binding.Bind(MyUserModel{}), func(session sessions.Session, postedUser MyUserModel, r render.Render, req *http.Request) {
// You should verify credentials against a database or some other mechanism at this point.
// Then you can authenticate this session.
//user := MyUserModel{}
user := MyUserModel{1, "testuser", "password", false}
//err := dbmap.SelectOne(&user, "SELECT * FROM users WHERE username = $1 and password = $2", postedUser.Username, postedUser.Password)
//if err != nil {
//	r.Redirect(sessionauth.RedirectUrl)
//	return
//} else {
err := sessionauth.AuthenticateSession(session, &user)
if err != nil {
r.JSON(500, err)
}
params := req.URL.Query()
redirect := params.Get(sessionauth.RedirectParam)
r.Redirect(redirect)
return
//}
})
m.Get("/private", sessionauth.LoginRequired, func(r render.Render, user sessionauth.User) {
r.HTML(200, "private", user.(*MyUserModel))
})
m.Get("/logout", sessionauth.LoginRequired, func(session sessions.Session, user sessionauth.User, r render.Render) {
sessionauth.Logout(session, user)
r.Redirect("/")
})
http.Handle("/", m)
}

答案1

得分: 1

是的,这应该是可能的。sessionauth包要求你传递一个*sessions.Store,而有一个gaesessions包可以替代默认的cookie/file存储:https://github.com/hnakamur/gaesessions

sessionauth包有一个完整的示例(https://github.com/martini-contrib/sessionauth/blob/master/example/auth_example.go)-只需将sessions.NewCookieStore替换为gaesessions.NewDatastoreStore

英文:

Yes, it should be possible. The sessionauth package requires you to pass it a *sessions.Store, and there is a gaesessions package that can replace the default cookie/file stores: https://github.com/hnakamur/gaesessions

The sessionauth package has a full example (https://github.com/martini-contrib/sessionauth/blob/master/example/auth_example.go) - just replace sessions.NewCookieStore with gaesessions.NewDatastoreStore.

huangapple
  • 本文由 发表于 2014年6月3日 17:18:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/24011381.html
匿名

发表评论

匿名网友

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

确定