clean oauth2 code

This commit is contained in:
skyblue 2014-04-12 09:42:09 +08:00
parent 4c6e0e9499
commit 5c1312f38e
6 changed files with 21 additions and 29 deletions

View File

@ -6,8 +6,6 @@ package models
import ( import (
"errors" "errors"
"github.com/gogits/gogs/modules/log"
) )
// OT: Oauth2 Type // OT: Oauth2 Type
@ -20,7 +18,6 @@ const (
var ( var (
ErrOauth2RecordNotExists = errors.New("not exists oauth2 record") ErrOauth2RecordNotExists = errors.New("not exists oauth2 record")
ErrOauth2NotAssociatedWithUser = errors.New("not associated with user") ErrOauth2NotAssociatedWithUser = errors.New("not associated with user")
ErrOauth2NotExist = errors.New("not exist oauth2")
) )
type Oauth2 struct { type Oauth2 struct {
@ -61,12 +58,11 @@ func GetOauth2(identity string) (oa *Oauth2, err error) {
func GetOauth2ById(id int64) (oa *Oauth2, err error) { func GetOauth2ById(id int64) (oa *Oauth2, err error) {
oa = new(Oauth2) oa = new(Oauth2)
has, err := orm.Id(id).Get(oa) has, err := orm.Id(id).Get(oa)
log.Info("oa: %v", oa)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !has { if !has {
return nil, ErrOauth2NotExist return nil, ErrOauth2RecordNotExists
} }
return oa, nil return oa, nil
} }

View File

@ -17,7 +17,6 @@ import (
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/oauth2"
) )
type SocialConnector interface { type SocialConnector interface {
@ -77,7 +76,10 @@ func extractPath(next string) string {
} }
// github && google && ... // 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 socid int64
var ok bool var ok bool
next := extractPath(ctx.Query("next")) next := extractPath(ctx.Query("next"))
@ -142,9 +144,9 @@ func SocialSignIn(ctx *middleware.Context, tokens oauth2.Tokens) {
return return
} }
case models.ErrOauth2NotAssociatedWithUser: case models.ErrOauth2NotAssociatedWithUser:
ctx.Session.Set("socialId", oa.Id)
ctx.Session.Set("socialName", soc.Name()) ctx.Session.Set("socialName", soc.Name())
ctx.Session.Set("socialEmail", soc.Email()) ctx.Session.Set("socialEmail", soc.Email())
ctx.Session.Set("socialId", oa.Id)
ctx.Redirect("/user/sign_up") ctx.Redirect("/user/sign_up")
return return
default: default:

View File

@ -82,7 +82,6 @@ func SignIn(ctx *middleware.Context) {
ctx.Data["OauthGitHubEnabled"] = base.OauthService.GitHub.Enabled ctx.Data["OauthGitHubEnabled"] = base.OauthService.GitHub.Enabled
} }
var user *models.User
// Check auto-login. // Check auto-login.
userName := ctx.GetCookie(base.CookieUserName) userName := ctx.GetCookie(base.CookieUserName)
if len(userName) == 0 { if len(userName) == 0 {
@ -91,7 +90,6 @@ func SignIn(ctx *middleware.Context) {
} }
isSucceed := false isSucceed := false
var err error
defer func() { defer func() {
if !isSucceed { if !isSucceed {
log.Trace("%s auto-login cookie cleared: %s", ctx.Req.RequestURI, userName) 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 { if err != nil {
ctx.HTML(500, "user/signin") ctx.HTML(500, "user/signin")
return return
@ -181,6 +179,8 @@ func SignOut(ctx *middleware.Context) {
ctx.Session.Delete("userId") ctx.Session.Delete("userId")
ctx.Session.Delete("userName") ctx.Session.Delete("userName")
ctx.Session.Delete("socialId") ctx.Session.Delete("socialId")
ctx.Session.Delete("socialName")
ctx.Session.Delete("socialEmail")
ctx.SetCookie(base.CookieUserName, "", -1) ctx.SetCookie(base.CookieUserName, "", -1)
ctx.SetCookie(base.CookieRememberName, "", -1) ctx.SetCookie(base.CookieRememberName, "", -1)
ctx.Redirect("/") ctx.Redirect("/")

View File

@ -3,7 +3,15 @@
<div class="container" id="body" data-page="user-signin"> <div class="container" id="body" data-page="user-signin">
<form action="/user/login" method="post" class="form-horizontal card" id="login-card"> <form action="/user/login" method="post" class="form-horizontal card" id="login-card">
{{.CsrfTokenHtml}} {{.CsrfTokenHtml}}
<h3>Log in</h3> <h3>Log in
{{if .OauthEnabled}}
<small class="pull-right">social login:
{{if .OauthGitHubEnabled}}
<a href="/user/login/github?next=/user/sign_up"><i class="fa fa-github-square fa-2x"></i></a>
{{end}}
</small>
{{end}}
</h3>
{{template "base/alert" .}} {{template "base/alert" .}}
<div class="form-group {{if .Err_UserName}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_UserName}}has-error has-feedback{{end}}">
<label class="col-md-4 control-label">Username: </label> <label class="col-md-4 control-label">Username: </label>
@ -43,12 +51,12 @@
</div> </div>
</div> </div>
{{if .OauthEnabled}} <!-- {{if .OauthEnabled}}
<div class="form-group text-center" id="social-login"> <div class="form-group text-center" id="social-login">
<h4>Log In with Social Accounts</h4> <h4>Log In with Social Accounts</h4>
{{if .OauthGitHubEnabled}}<a href="/user/login/github?next=/user/sign_up"><i class="fa fa-github-square fa-3x"></i></a>{{end}} {{if .OauthGitHubEnabled}}<a href="/user/login/github?next=/user/sign_up"><i class="fa fa-github-square fa-3x"></i></a>{{end}}
</div> </div>
{{end}} {{end}} -->
</form> </form>
</div> </div>
{{template "base/footer" .}} {{template "base/footer" .}}

View File

@ -12,8 +12,6 @@
<h3>Sign Up</h3> <h3>Sign Up</h3>
{{end}} {{end}}
{{template "base/alert" .}} {{template "base/alert" .}}
{{if .IsSocialLogin}}
{{end}}
<div class="form-group {{if .Err_UserName}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_UserName}}has-error has-feedback{{end}}">
<label class="col-md-4 control-label">Username: </label> <label class="col-md-4 control-label">Username: </label>
<div class="col-md-6"> <div class="col-md-6">
@ -23,7 +21,7 @@
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}">
<label class="col-md-4 control-label">Email: </label> <label class="col-md-4 control-label">Email: </label>
<div class="col-md-6"> <div class="col-md-6">
<input name="email" class="form-control" placeholder="Type your e-mail address" value="{{.email}}{{.socialEmail}}" required="required" title="Email is not valid"> <input name="email" class="form-control" placeholder="Type your e-mail address" value="{{.email}}" required="required" title="Email is not valid">
</div> </div>
</div> </div>

12
web.go
View File

@ -20,7 +20,6 @@ import (
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/oauth2"
"github.com/gogits/gogs/routers" "github.com/gogits/gogs/routers"
"github.com/gogits/gogs/routers/admin" "github.com/gogits/gogs/routers/admin"
"github.com/gogits/gogs/routers/api/v1" "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.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
m.Use(middleware.InitContext()) 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}) reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true})
ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView}) ignSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: base.Service.RequireSignInView})
ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{ ignSignInAndCsrf := middleware.Toggle(&middleware.ToggleOptions{