You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3 KiB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "encoding/json"
  7. "code.google.com/p/goauth2/oauth"
  8. "github.com/gogits/gogs/modules/log"
  9. "github.com/gogits/gogs/modules/oauth2"
  10. )
  11. // github && google && ...
  12. func SocialSignIn(tokens oauth2.Tokens) {
  13. transport := &oauth.Transport{}
  14. transport.Token = &oauth.Token{
  15. AccessToken: tokens.Access(),
  16. RefreshToken: tokens.Refresh(),
  17. Expiry: tokens.ExpiryTime(),
  18. Extra: tokens.ExtraData(),
  19. }
  20. // Github API refer: https://developer.github.com/v3/users/
  21. // FIXME: need to judge url
  22. type GithubUser struct {
  23. Id int `json:"id"`
  24. Name string `json:"login"`
  25. Email string `json:"email"`
  26. }
  27. // Make the request.
  28. scope := "https://api.github.com/user"
  29. r, err := transport.Client().Get(scope)
  30. if err != nil {
  31. log.Error("connect with github error: %s", err)
  32. // FIXME: handle error page
  33. return
  34. }
  35. defer r.Body.Close()
  36. user := &GithubUser{}
  37. err = json.NewDecoder(r.Body).Decode(user)
  38. if err != nil {
  39. log.Error("Get: %s", err)
  40. }
  41. log.Info("login: %s", user.Name)
  42. // FIXME: login here, user email to check auth, if not registe, then generate a uniq username
  43. }