[target]
-path=github.com/gogits/gogs
+path = github.com/gogits/gogs
[deps]
-github.com/codegangsta/cli=
-github.com/go-martini/martini=
-github.com/Unknwon/com=
-github.com/Unknwon/cae=
-github.com/Unknwon/goconfig=
-github.com/dchest/scrypt=
-github.com/nfnt/resize=
-github.com/lunny/xorm=
-github.com/go-sql-driver/mysql=
-github.com/lib/pq=
-github.com/gogits/logs=
-github.com/gogits/binding=
-github.com/gogits/git=
-github.com/gogits/gfm=
-github.com/gogits/cache=
-github.com/gogits/session=
-github.com/gogits/webdav=
-github.com/martini-contrib/oauth2=
-github.com/martini-contrib/sessions=
-code.google.com/p/goauth2=
+github.com/codegangsta/cli =
+github.com/go-martini/martini =
+github.com/Unknwon/com =
+github.com/Unknwon/cae =
+github.com/Unknwon/goconfig =
+github.com/dchest/scrypt =
+github.com/nfnt/resize =
+github.com/lunny/xorm =
+github.com/go-sql-driver/mysql =
+github.com/lib/pq =
+github.com/gogits/logs =
+github.com/gogits/binding =
+github.com/gogits/git =
+github.com/gogits/gfm =
+github.com/gogits/cache =
+github.com/gogits/session =
+github.com/gogits/webdav =
+code.google.com/p/goauth2 =
[res]
-include=templates|public|conf
+include = templates|public|conf
"code.google.com/p/goauth2/oauth"
"github.com/go-martini/martini"
- "github.com/martini-contrib/sessions"
+
+ "github.com/gogits/session"
+
+ "github.com/gogits/gogs/modules/middleware"
)
const (
Transport: http.DefaultTransport,
}
- return func(s sessions.Session, c martini.Context, w http.ResponseWriter, r *http.Request) {
- if r.Method == "GET" {
- switch r.URL.Path {
+ return func(c martini.Context, ctx *middleware.Context) {
+ if ctx.Req.Method == "GET" {
+ switch ctx.Req.URL.Path {
case PathLogin:
- login(transport, s, w, r)
+ login(transport, ctx)
case PathLogout:
- logout(transport, s, w, r)
+ logout(transport, ctx)
case PathCallback:
- handleOAuth2Callback(transport, s, w, r)
+ handleOAuth2Callback(transport, ctx)
}
}
- tk := unmarshallToken(s)
+ tk := unmarshallToken(ctx.Session)
if tk != nil {
// check if the access token is expired
if tk.IsExpired() && tk.Refresh() == "" {
- s.Delete(keyToken)
+ ctx.Session.Delete(keyToken)
tk = nil
}
}
// Sample usage:
// m.Get("/login-required", oauth2.LoginRequired, func() ... {})
var LoginRequired martini.Handler = func() martini.Handler {
- return func(s sessions.Session, c martini.Context, w http.ResponseWriter, r *http.Request) {
- token := unmarshallToken(s)
+ return func(c martini.Context, ctx *middleware.Context) {
+ token := unmarshallToken(ctx.Session)
if token == nil || token.IsExpired() {
- next := url.QueryEscape(r.URL.RequestURI())
- http.Redirect(w, r, PathLogin+"?next="+next, codeRedirect)
+ next := url.QueryEscape(ctx.Req.URL.RequestURI())
+ ctx.Redirect(PathLogin+"?next="+next, codeRedirect)
}
}
}()
-func login(t *oauth.Transport, s sessions.Session, w http.ResponseWriter, r *http.Request) {
- next := extractPath(r.URL.Query().Get(keyNextPage))
- if s.Get(keyToken) == nil {
+func login(t *oauth.Transport, ctx *middleware.Context) {
+ next := extractPath(ctx.Req.URL.Query().Get(keyNextPage))
+ if ctx.Session.Get(keyToken) == nil {
// User is not logged in.
- http.Redirect(w, r, t.Config.AuthCodeURL(next), codeRedirect)
+ ctx.Redirect(t.Config.AuthCodeURL(next), codeRedirect)
return
}
// No need to login, redirect to the next page.
- http.Redirect(w, r, next, codeRedirect)
+ ctx.Redirect(next, codeRedirect)
}
-func logout(t *oauth.Transport, s sessions.Session, w http.ResponseWriter, r *http.Request) {
- next := extractPath(r.URL.Query().Get(keyNextPage))
- s.Delete(keyToken)
- http.Redirect(w, r, next, codeRedirect)
+func logout(t *oauth.Transport, ctx *middleware.Context) {
+ next := extractPath(ctx.Req.URL.Query().Get(keyNextPage))
+ ctx.Session.Delete(keyToken)
+ ctx.Redirect(next, codeRedirect)
}
-func handleOAuth2Callback(t *oauth.Transport, s sessions.Session, w http.ResponseWriter, r *http.Request) {
- next := extractPath(r.URL.Query().Get("state"))
- code := r.URL.Query().Get("code")
+func handleOAuth2Callback(t *oauth.Transport, ctx *middleware.Context) {
+ next := extractPath(ctx.Req.URL.Query().Get("state"))
+ code := ctx.Req.URL.Query().Get("code")
tk, err := t.Exchange(code)
if err != nil {
// Pass the error message, or allow dev to provide its own
// error handler.
- http.Redirect(w, r, PathError, codeRedirect)
+ ctx.Redirect(PathError, codeRedirect)
return
}
// Store the credentials in the session.
val, _ := json.Marshal(tk)
- s.Set(keyToken, val)
- http.Redirect(w, r, next, codeRedirect)
+ ctx.Session.Set(keyToken, val)
+ ctx.Redirect(next, codeRedirect)
}
-func unmarshallToken(s sessions.Session) (t *token) {
+func unmarshallToken(s session.SessionStore) (t *token) {
if s.Get(keyToken) == nil {
return
}
"github.com/codegangsta/cli"
"github.com/go-martini/martini"
- // "github.com/martini-contrib/oauth2"
- // "github.com/martini-contrib/sessions"
"github.com/gogits/binding"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware"
+ "github.com/gogits/gogs/modules/oauth2"
"github.com/gogits/gogs/routers"
"github.com/gogits/gogs/routers/admin"
"github.com/gogits/gogs/routers/api/v1"
// Middlewares.
m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
-
- // scope := "https://api.github.com/user"
- // oauth2.PathCallback = "/oauth2callback"
- // m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
- // m.Use(oauth2.Github(&oauth2.Options{
- // ClientId: "09383403ff2dc16daaa1",
- // ClientSecret: "5f6e7101d30b77952aab22b75eadae17551ea6b5",
- // RedirectURL: base.AppUrl + oauth2.PathCallback,
- // Scopes: []string{scope},
- // }))
-
m.Use(middleware.InitContext())
+ scope := "https://api.github.com/user"
+ oauth2.PathCallback = "/oauth2callback"
+ m.Use(oauth2.Github(&oauth2.Options{
+ ClientId: "09383403ff2dc16daaa1",
+ ClientSecret: "5f6e7101d30b77952aab22b75eadae17551ea6b5",
+ RedirectURL: base.AppUrl + oauth2.PathCallback,
+ Scopes: []string{scope},
+ }))
+
reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
reqSignOut := middleware.Toggle(&middleware.ToggleOptions{SignOutRequire: true})