fix oauth2

This commit is contained in:
Unknwon
2014-11-28 21:20:13 -05:00
parent 9adc8e9d3f
commit d6132aaa88
10 changed files with 185 additions and 251 deletions

View File

@ -21,6 +21,7 @@ github.com/macaron-contrib/cache = commit:0bb9e6c9ef
github.com/macaron-contrib/captcha = commit:3567dc48b8
github.com/macaron-contrib/csrf = commit:422b79675c
github.com/macaron-contrib/i18n =
github.com/macaron-contrib/oauth2 =
github.com/macaron-contrib/session = commit:f00d48fd4f
github.com/macaron-contrib/toolbox = commit:57127bcc89
github.com/mattn/go-sqlite3 = commit:a80c27ba33

View File

@ -21,6 +21,7 @@ import (
"github.com/macaron-contrib/captcha"
"github.com/macaron-contrib/csrf"
"github.com/macaron-contrib/i18n"
"github.com/macaron-contrib/oauth2"
"github.com/macaron-contrib/session"
"github.com/macaron-contrib/toolbox"
@ -140,6 +141,11 @@ func newMacaron() *macaron.Macaron {
},
},
}))
// OAuth 2.
for _, info := range setting.OauthService.OauthInfos {
m.Use(oauth2.NewOAuth2Provider(info.Options, info.AuthUrl, info.TokenUrl))
}
m.Use(middleware.Contexter())
return m
}
@ -213,7 +219,7 @@ func runWeb(*cli.Context) {
m.Group("/user", func() {
m.Get("/login", user.SignIn)
m.Post("/login", bindIgnErr(auth.SignInForm{}), user.SignInPost)
m.Get("/login/:name", user.SocialSignIn)
m.Get("/info/:name", user.SocialSignIn)
m.Get("/sign_up", user.SignUp)
m.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost)
m.Get("/reset_password", user.ResetPasswd)
@ -406,7 +412,7 @@ func runWeb(*cli.Context) {
m.Get("/issues2/", repo.Issues2)
m.Get("/pulls2/", repo.PullRequest2)
m.Get("/labels2/", repo.Labels2)
m.Get("/milestone2/",repo.Milestones2)
m.Get("/milestone2/", repo.Milestones2)
m.Group("", func() {
m.Get("/src/*", repo.Home)

View File

@ -125,13 +125,10 @@ TOKEN_URL = https://accounts.google.com/o/oauth2/token
ENABLED = false
CLIENT_ID =
CLIENT_SECRET =
SCOPES = all
SCOPES = get_user_info
; QQ 互联
; AUTH_URL = https://graph.qq.com/oauth2.0/authorize
; TOKEN_URL = https://graph.qq.com/oauth2.0/token
; Tencent weibo
AUTH_URL = https://open.t.qq.com/cgi-bin/oauth2/authorize
TOKEN_URL = https://open.t.qq.com/cgi-bin/oauth2/access_token
AUTH_URL = https://graph.qq.com/oauth2.0/authorize
TOKEN_URL = https://graph.qq.com/oauth2.0/token
[oauth.weibo]
ENABLED = false

View File

@ -1,3 +1,5 @@
# This file lists all individuals having contributed content to the translation.
# This file lists all PUBLIC individuals having contributed content to the translation.
# Order of name is meaningless.
Thomas Fanninger <gogs.thomas@fanninger.at>
Thomas Fanninger <gogs.thomas@fanninger.at>
Łukasz Jan Niemier <lukasz@niemier.pl>

View File

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting"
)
const APP_VER = "0.5.8.1125 Beta"
const APP_VER = "0.5.8.1128 Beta"
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -17,6 +17,7 @@ import (
"github.com/Unknwon/com"
"github.com/Unknwon/goconfig"
"github.com/macaron-contrib/oauth2"
"github.com/macaron-contrib/session"
"github.com/gogits/gogs/modules/log"
@ -434,9 +435,8 @@ type Mailer struct {
}
type OauthInfo struct {
ClientId, ClientSecret string
Scopes string
AuthUrl, TokenUrl string
oauth2.Options
AuthUrl, TokenUrl string
}
// Oauther represents oauth service.

File diff suppressed because it is too large Load Diff

View File

@ -9,10 +9,12 @@ import (
"errors"
"fmt"
"net/url"
"strings"
"time"
// "strings"
// "time"
"github.com/gogits/gogs/models"
"github.com/macaron-contrib/oauth2"
// "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware"
"github.com/gogits/gogs/modules/setting"
@ -29,79 +31,69 @@ func extractPath(next string) string {
func SocialSignIn(ctx *middleware.Context) {
if setting.OauthService == nil {
ctx.Handle(404, "social.SocialSignIn(oauth service not enabled)", nil)
ctx.Handle(404, "OAuth2 service not enabled", nil)
return
}
info := ctx.Session.Get(oauth2.KEY_TOKEN)
if info == nil {
ctx.Redirect(setting.AppSubUrl + "/user/login")
return
}
next := extractPath(ctx.Query("next"))
name := ctx.Params(":name")
connect, ok := social.SocialMap[name]
if !ok {
ctx.Handle(404, "social.SocialSignIn(social login not enabled)", errors.New(name))
return
}
appUrl := strings.TrimSuffix(setting.AppUrl, "/")
if name == "weibo" {
appUrl = strings.Replace(appUrl, "localhost", "127.0.0.1", 1)
}
code := ctx.Query("code")
if code == "" {
// redirect to social login page
connect.SetRedirectUrl(appUrl + ctx.Req.URL.Path)
ctx.Redirect(connect.AuthCodeURL(next))
ctx.Handle(404, "social login not enabled", errors.New(name))
return
}
// handle call back
tk, err := connect.Exchange(code)
if err != nil {
ctx.Handle(500, "social.SocialSignIn(Exchange)", err)
tk := new(oauth2.Token)
if err := json.Unmarshal(info.([]byte), tk); err != nil {
ctx.Handle(500, "Unmarshal token", err)
return
}
next = extractPath(ctx.Query("state"))
log.Trace("social.SocialSignIn(Got token)")
ui, err := connect.UserInfo(tk, ctx.Req.URL)
if err != nil {
ctx.Handle(500, fmt.Sprintf("social.SocialSignIn(get info from %s)", name), err)
ctx.Handle(500, fmt.Sprintf("UserInfo(%s)", name), err)
return
}
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)
}

View File

@ -1 +1 @@
0.5.8.1125 Beta
0.5.8.1128 Beta

View File

@ -1,4 +1,4 @@
{{if .OauthService.GitHub}}<a class="btn github" href="{{AppSubUrl}}/user/login/github?next=/user/sign_up"><i class="fa fa-github"></i>GitHub</a>{{end}}
{{if .OauthService.Google}}<a class="btn google" href="{{AppSubUrl}}/user/login/google?next=/user/sign_up"><i class="fa fa-google"></i>Google +</a>{{end}}
{{if .OauthService.Weibo}}<a class="btn weibo" href="{{AppSubUrl}}/user/login/weibo?next=/user/sign_up"><i class="fa fa-weibo"></i>新浪微博</a>{{end}}
{{if .OauthService.Tencent}}<a class="btn qq" href="{{AppSubUrl}}/user/login/qq?next=/user/sign_up"><i class="fa fa-qq"></i>腾讯 QQ&nbsp;</a>{{end}}
{{if .OauthService.GitHub}}<a class="btn github" href="{{AppSubUrl}}/user/login/oauth2/github?next={{AppSubUrl}}/user/info/github"><i class="fa fa-github"></i>GitHub</a>{{end}}
{{if .OauthService.Google}}<a class="btn google" href="{{AppSubUrl}}/user/login/oauth2/google?next={{AppSubUrl}}/user/info/google"><i class="fa fa-google"></i>Google +</a>{{end}}
{{if .OauthService.Weibo}}<a class="btn weibo" href="{{AppSubUrl}}/user/login/oauth2/weibo?next={{AppSubUrl}}/user/info/weibo"><i class="fa fa-weibo"></i>新浪微博</a>{{end}}
{{if .OauthService.Tencent}}<a class="btn qq" href="{{AppSubUrl}}/user/login/oauth2/qq?next={{AppSubUrl}}/user/info/qq"><i class="fa fa-qq"></i>腾讯 QQ&nbsp;</a>{{end}}