From 5c1312f38e8968e068125a1b1dd59060f3f29bfe Mon Sep 17 00:00:00 2001 From: skyblue Date: Sat, 12 Apr 2014 09:42:09 +0800 Subject: [PATCH] clean oauth2 code --- models/oauth2.go | 6 +----- routers/user/social.go | 8 +++++--- routers/user/user.go | 6 +++--- templates/user/signin.tmpl | 14 +++++++++++--- templates/user/signup.tmpl | 4 +--- web.go | 12 ------------ 6 files changed, 21 insertions(+), 29 deletions(-) diff --git a/models/oauth2.go b/models/oauth2.go index e50d4039fd4..c5d58c07497 100644 --- a/models/oauth2.go +++ b/models/oauth2.go @@ -6,8 +6,6 @@ package models import ( "errors" - - "github.com/gogits/gogs/modules/log" ) // OT: Oauth2 Type @@ -20,7 +18,6 @@ const ( var ( ErrOauth2RecordNotExists = errors.New("not exists oauth2 record") ErrOauth2NotAssociatedWithUser = errors.New("not associated with user") - ErrOauth2NotExist = errors.New("not exist oauth2") ) type Oauth2 struct { @@ -61,12 +58,11 @@ func GetOauth2(identity string) (oa *Oauth2, err error) { func GetOauth2ById(id int64) (oa *Oauth2, err error) { oa = new(Oauth2) has, err := orm.Id(id).Get(oa) - log.Info("oa: %v", oa) if err != nil { return nil, err } if !has { - return nil, ErrOauth2NotExist + return nil, ErrOauth2RecordNotExists } return oa, nil } diff --git a/routers/user/social.go b/routers/user/social.go index 8568bc2af83..3e5f69beff4 100644 --- a/routers/user/social.go +++ b/routers/user/social.go @@ -17,7 +17,6 @@ import ( "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" - "github.com/gogits/gogs/modules/oauth2" ) type SocialConnector interface { @@ -77,7 +76,10 @@ func extractPath(next string) string { } // github && google && ... -func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { +func SocialSignIn(ctx *middleware.Context) { + //if base.OauthService != nil && base.OauthService.GitHub.Enabled { + //} + var socid int64 var ok bool next := extractPath(ctx.Query("next")) @@ -142,9 +144,9 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) { return } case models.ErrOauth2NotAssociatedWithUser: + ctx.Session.Set("socialId", oa.Id) ctx.Session.Set("socialName", soc.Name()) ctx.Session.Set("socialEmail", soc.Email()) - ctx.Session.Set("socialId", oa.Id) ctx.Redirect("/user/sign_up") return default: diff --git a/routers/user/user.go b/routers/user/user.go index 253ab7be002..8585267a644 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -82,7 +82,6 @@ func SignIn(ctx *middleware.Context) { ctx.Data["OauthGitHubEnabled"] = base.OauthService.GitHub.Enabled } - var user *models.User // Check auto-login. userName := ctx.GetCookie(base.CookieUserName) if len(userName) == 0 { @@ -91,7 +90,6 @@ func SignIn(ctx *middleware.Context) { } isSucceed := false - var err error defer func() { if !isSucceed { log.Trace("%s auto-login cookie cleared: %s", ctx.Req.RequestURI, userName) @@ -101,7 +99,7 @@ func SignIn(ctx *middleware.Context) { } }() - user, err = models.GetUserByName(userName) + user, err := models.GetUserByName(userName) if err != nil { ctx.HTML(500, "user/signin") return @@ -181,6 +179,8 @@ func SignOut(ctx *middleware.Context) { ctx.Session.Delete("userId") ctx.Session.Delete("userName") ctx.Session.Delete("socialId") + ctx.Session.Delete("socialName") + ctx.Session.Delete("socialEmail") ctx.SetCookie(base.CookieUserName, "", -1) ctx.SetCookie(base.CookieRememberName, "", -1) ctx.Redirect("/") diff --git a/templates/user/signin.tmpl b/templates/user/signin.tmpl index a06e3ca8a03..6ccad79e73f 100644 --- a/templates/user/signin.tmpl +++ b/templates/user/signin.tmpl @@ -3,7 +3,15 @@
{{.CsrfTokenHtml}} -

Log in

+

Log in + {{if .OauthEnabled}} + social login: + {{if .OauthGitHubEnabled}} + + {{end}} + + {{end}} +

{{template "base/alert" .}}
@@ -43,12 +51,12 @@
- {{if .OauthEnabled}} + {{template "base/footer" .}} diff --git a/templates/user/signup.tmpl b/templates/user/signup.tmpl index b12333a257a..6cf48a4d800 100644 --- a/templates/user/signup.tmpl +++ b/templates/user/signup.tmpl @@ -12,8 +12,6 @@

Sign Up

{{end}} {{template "base/alert" .}} - {{if .IsSocialLogin}} - {{end}}
@@ -23,7 +21,7 @@
- +
diff --git a/web.go b/web.go index b2be73d677f..e7b048bcd32 100644 --- a/web.go +++ b/web.go @@ -20,7 +20,6 @@ import ( "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" @@ -59,17 +58,6 @@ func runWeb(*cli.Context) { m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}})) m.Use(middleware.InitContext()) - if base.OauthService != nil { - if base.OauthService.GitHub.Enabled { - m.Use(oauth2.Github(&oauth2.Options{ - ClientId: base.OauthService.GitHub.ClientId, - ClientSecret: base.OauthService.GitHub.ClientSecret, - RedirectURL: base.AppUrl + oauth2.PathCallback[1:], - Scopes: []string{base.OauthService.GitHub.Scopes}, - })) - } - } - reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true}) ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView}) ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{