]> source.dussan.org Git - gitea.git/commitdiff
mirror fix on oauth2
authorUnknwon <joe2010xtmf@163.com>
Sat, 29 Nov 2014 02:38:35 +0000 (21:38 -0500)
committerUnknwon <joe2010xtmf@163.com>
Sat, 29 Nov 2014 02:38:35 +0000 (21:38 -0500)
modules/social/social.go
routers/user/social.go
templates/user/auth/signin.tmpl

index 8406cd52cbc3a97117fd389078c848dc3579980b..bdafae69028d0db24174e3a97eaf76c5f988256c 100644 (file)
@@ -39,6 +39,8 @@ func NewOauthService() {
                return
        }
 
+       oauth2.AppSubUrl = setting.AppSubUrl
+
        setting.OauthService = &setting.Oauther{}
        setting.OauthService.OauthInfos = make(map[string]*setting.OauthInfo)
 
index 5d2f2027c47efec93a081352e47822a7913dfe3e..a8a94a5c45ffbb57685ddfe864b3043085c60331 100644 (file)
@@ -8,36 +8,28 @@ import (
        "encoding/json"
        "errors"
        "fmt"
-       "net/url"
        // "strings"
-       // "time"
+       "time"
 
        "github.com/macaron-contrib/oauth2"
 
-       // "github.com/gogits/gogs/models"
+       "github.com/gogits/gogs/models"
        "github.com/gogits/gogs/modules/log"
        "github.com/gogits/gogs/modules/middleware"
        "github.com/gogits/gogs/modules/setting"
        "github.com/gogits/gogs/modules/social"
 )
 
-func extractPath(next string) string {
-       n, err := url.Parse(next)
-       if err != nil {
-               return setting.AppSubUrl + "/"
-       }
-       return n.Path
-}
-
 func SocialSignIn(ctx *middleware.Context) {
        if setting.OauthService == nil {
                ctx.Handle(404, "OAuth2 service not enabled", nil)
                return
        }
 
+       next := setting.AppSubUrl + "/user/login"
        info := ctx.Session.Get(oauth2.KEY_TOKEN)
        if info == nil {
-               ctx.Redirect(setting.AppSubUrl + "/user/login")
+               ctx.Redirect(next)
                return
        }
 
@@ -61,39 +53,39 @@ func SocialSignIn(ctx *middleware.Context) {
        }
        log.Info("social.SocialSignIn(social login): %s", ui)
 
-       // oa, err := models.GetOauth2(ui.Identity)
-       // switch err {
-       // case nil:
-       //      ctx.Session.Set("uid", oa.User.Id)
-       //      ctx.Session.Set("uname", oa.User.Name)
-       // case models.ErrOauth2RecordNotExist:
-       //      raw, _ := json.Marshal(tk)
-       //      oa = &models.Oauth2{
-       //              Uid:      -1,
-       //              Type:     connect.Type(),
-       //              Identity: ui.Identity,
-       //              Token:    string(raw),
-       //      }
-       //      log.Trace("social.SocialSignIn(oa): %v", oa)
-       //      if err = models.AddOauth2(oa); err != nil {
-       //              log.Error(4, "social.SocialSignIn(add oauth2): %v", err) // 501
-       //              return
-       //      }
-       // case models.ErrOauth2NotAssociated:
-       //      next = setting.AppSubUrl + "/user/sign_up"
-       // default:
-       //      ctx.Handle(500, "social.SocialSignIn(GetOauth2)", err)
-       //      return
-       // }
+       oa, err := models.GetOauth2(ui.Identity)
+       switch err {
+       case nil:
+               ctx.Session.Set("uid", oa.User.Id)
+               ctx.Session.Set("uname", oa.User.Name)
+       case models.ErrOauth2RecordNotExist:
+               raw, _ := json.Marshal(tk)
+               oa = &models.Oauth2{
+                       Uid:      -1,
+                       Type:     connect.Type(),
+                       Identity: ui.Identity,
+                       Token:    string(raw),
+               }
+               log.Trace("social.SocialSignIn(oa): %v", oa)
+               if err = models.AddOauth2(oa); err != nil {
+                       log.Error(4, "social.SocialSignIn(add oauth2): %v", err) // 501
+                       return
+               }
+       case models.ErrOauth2NotAssociated:
+               next = setting.AppSubUrl + "/user/sign_up"
+       default:
+               ctx.Handle(500, "social.SocialSignIn(GetOauth2)", err)
+               return
+       }
 
-       // oa.Updated = time.Now()
-       // if err = models.UpdateOauth2(oa); err != nil {
-       //      log.Error(4, "UpdateOauth2: %v", err)
-       // }
+       oa.Updated = time.Now()
+       if err = models.UpdateOauth2(oa); err != nil {
+               log.Error(4, "UpdateOauth2: %v", err)
+       }
 
-       // ctx.Session.Set("socialId", oa.Id)
-       // ctx.Session.Set("socialName", ui.Name)
-       // ctx.Session.Set("socialEmail", ui.Email)
-       // log.Trace("social.SocialSignIn(social ID): %v", oa.Id)
-       // ctx.Redirect(next)
+       ctx.Session.Set("socialId", oa.Id)
+       ctx.Session.Set("socialName", ui.Name)
+       ctx.Session.Set("socialEmail", ui.Email)
+       log.Trace("social.SocialSignIn(social ID): %v", oa.Id)
+       ctx.Redirect(next)
 }
index 547480771490d7c7cd58fbad5033c7414b72b6c8..78d6febb80e4d3fd1d7ae79e60efbf588a9d8b46 100644 (file)
                 <button class="btn btn-green btn-large btn-radius">{{.i18n.Tr "sign_in"}}</button>&nbsp;&nbsp;&nbsp;&nbsp;
                 {{if not .IsSocialLogin}}<a href="{{AppSubUrl}}/user/forget_password">{{.i18n.Tr "auth.forget_password"}}</a>{{end}}
             </div>
-            {{if not .IsSocialLogin}}
             <div class="field">
                 <label></label>
                 <a href="{{AppSubUrl}}/user/sign_up">{{.i18n.Tr "auth.sign_up_now" | Str2html}}</a>
             </div>
-            {{if .OauthEnabled}}
+            {{if and (not .IsSocialLogin) .OauthEnabled}}
             <hr/>
             <div id="sign-social" class="text-center social-buttons">
                 {{template "ng/base/social" .}}
             </div>
             {{end}}
-            {{end}}
         </div>
     </form>
 </div>