From 5180506678c9c4e7265eaee557d91992ec97fa3a Mon Sep 17 00:00:00 2001 From: skyblue Date: Wed, 2 Apr 2014 10:39:04 +0800 Subject: [PATCH] split into social.go --- routers/user/social.go | 49 ++++++++++++++++++++++++++++++++++++++++++ routers/user/user.go | 40 ---------------------------------- 2 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 routers/user/social.go diff --git a/routers/user/social.go b/routers/user/social.go new file mode 100644 index 000000000..2f711d58c --- /dev/null +++ b/routers/user/social.go @@ -0,0 +1,49 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +package user + +import ( + "encoding/json" + + "code.google.com/p/goauth2/oauth" + "github.com/gogits/gogs/modules/log" + "github.com/martini-contrib/oauth2" +) + +// 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 +} diff --git a/routers/user/user.go b/routers/user/user.go index 5890983ba..08930e22d 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -5,14 +5,11 @@ 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" @@ -77,43 +74,6 @@ 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"