From 272c27c8f20b20889571ed640538c6564beb62ed Mon Sep 17 00:00:00 2001 From: skyblue Date: Wed, 2 Apr 2014 08:14:56 +0800 Subject: [PATCH] add github social login, first step --- routers/user/user.go | 40 ++++++++++++++++++++++++++++++++++++++++ web.go | 13 +++++++++++++ 2 files changed, 53 insertions(+) diff --git a/routers/user/user.go b/routers/user/user.go index 08930e22df..5890983ba1 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -5,11 +5,14 @@ package user import ( + "encoding/json" "fmt" "net/url" "strings" + "code.google.com/p/goauth2/oauth" "github.com/go-martini/martini" + "github.com/martini-contrib/oauth2" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" @@ -74,6 +77,43 @@ func Profile(ctx *middleware.Context, params martini.Params) { ctx.HTML(200, "user/profile") } +// github && google && ... +func SocialSignIn(tokens oauth2.Tokens) { + transport := &oauth.Transport{} + transport.Token = &oauth.Token{ + AccessToken: tokens.Access(), + RefreshToken: tokens.Refresh(), + Expiry: tokens.ExpiryTime(), + Extra: tokens.ExtraData(), + } + + // Github API refer: https://developer.github.com/v3/users/ + // FIXME: need to judge url + type GithubUser struct { + Id int `json:"id"` + Name string `json:"login"` + Email string `json:"email"` + } + + // Make the request. + scope := "https://api.github.com/user" + r, err := transport.Client().Get(scope) + if err != nil { + log.Error("connect with github error: %s", err) + // FIXME: handle error page + return + } + defer r.Body.Close() + + user := &GithubUser{} + err = json.NewDecoder(r.Body).Decode(user) + if err != nil { + log.Error("Get: %s", err) + } + log.Info("login: %s", user.Name) + // FIXME: login here, user email to check auth, if not registe, then generate a uniq username +} + func SignIn(ctx *middleware.Context, form auth.LogInForm) { ctx.Data["Title"] = "Log In" diff --git a/web.go b/web.go index cdec9232a9..48d80b4fed 100644 --- a/web.go +++ b/web.go @@ -11,6 +11,8 @@ import ( "github.com/codegangsta/cli" "github.com/go-martini/martini" + "github.com/martini-contrib/oauth2" + "github.com/martini-contrib/sessions" "github.com/gogits/binding" @@ -58,6 +60,16 @@ func runWeb(*cli.Context) { // 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()) reqSignIn := middleware.Toggle(&middleware.ToggleOptions{SignInRequire: true}) @@ -80,6 +92,7 @@ func runWeb(*cli.Context) { m.Get("/avatar/:hash", avt.ServeHTTP) m.Group("/user", func(r martini.Router) { + r.Any("/login/github", user.SocialSignIn) r.Any("/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn) r.Any("/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp) }, reqSignOut)