diff options
author | skyblue <ssx205@gmail.com> | 2014-04-02 08:14:56 +0800 |
---|---|---|
committer | skyblue <ssx205@gmail.com> | 2014-04-02 08:14:56 +0800 |
commit | 272c27c8f20b20889571ed640538c6564beb62ed (patch) | |
tree | 91e9b85f986a24be6eb4098a39dcd10a0b08fd1e | |
parent | 67bd2daa022fd2402d7aece4a7b54249fdb2f0f0 (diff) | |
download | gitea-272c27c8f20b20889571ed640538c6564beb62ed.tar.gz gitea-272c27c8f20b20889571ed640538c6564beb62ed.zip |
add github social login, first step
-rw-r--r-- | routers/user/user.go | 40 | ||||
-rw-r--r-- | web.go | 13 |
2 files changed, 53 insertions, 0 deletions
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" @@ -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) |