您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

oauth2.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. // Copyright 2014 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. // Package oauth2 contains Martini handlers to provide
  6. // user login via an OAuth 2.0 backend.
  7. package oauth2
  8. import (
  9. "encoding/json"
  10. "fmt"
  11. "net/http"
  12. "net/url"
  13. "strings"
  14. "time"
  15. "code.google.com/p/goauth2/oauth"
  16. "github.com/go-martini/martini"
  17. "github.com/gogits/session"
  18. "github.com/gogits/gogs/modules/log"
  19. "github.com/gogits/gogs/modules/middleware"
  20. )
  21. const (
  22. keyToken = "oauth2_token"
  23. keyNextPage = "next"
  24. )
  25. var (
  26. // Path to handle OAuth 2.0 logins.
  27. PathLogin = "/login"
  28. // Path to handle OAuth 2.0 logouts.
  29. PathLogout = "/logout"
  30. // Path to handle callback from OAuth 2.0 backend
  31. // to exchange credentials.
  32. PathCallback = "/oauth2callback"
  33. // Path to handle error cases.
  34. PathError = "/oauth2error"
  35. )
  36. // Represents OAuth2 backend options.
  37. type Options struct {
  38. ClientId string
  39. ClientSecret string
  40. RedirectURL string
  41. Scopes []string
  42. AuthUrl string
  43. TokenUrl string
  44. }
  45. // Represents a container that contains
  46. // user's OAuth 2.0 access and refresh tokens.
  47. type Tokens interface {
  48. Access() string
  49. Refresh() string
  50. IsExpired() bool
  51. ExpiryTime() time.Time
  52. ExtraData() map[string]string
  53. }
  54. type token struct {
  55. oauth.Token
  56. }
  57. func (t *token) ExtraData() map[string]string {
  58. return t.Extra
  59. }
  60. // Returns the access token.
  61. func (t *token) Access() string {
  62. return t.AccessToken
  63. }
  64. // Returns the refresh token.
  65. func (t *token) Refresh() string {
  66. return t.RefreshToken
  67. }
  68. // Returns whether the access token is
  69. // expired or not.
  70. func (t *token) IsExpired() bool {
  71. if t == nil {
  72. return true
  73. }
  74. return t.Expired()
  75. }
  76. // Returns the expiry time of the user's
  77. // access token.
  78. func (t *token) ExpiryTime() time.Time {
  79. return t.Expiry
  80. }
  81. // Formats tokens into string.
  82. func (t *token) String() string {
  83. return fmt.Sprintf("tokens: %v", t)
  84. }
  85. // Returns a new Google OAuth 2.0 backend endpoint.
  86. func Google(opts *Options) martini.Handler {
  87. opts.AuthUrl = "https://accounts.google.com/o/oauth2/auth"
  88. opts.TokenUrl = "https://accounts.google.com/o/oauth2/token"
  89. return NewOAuth2Provider(opts)
  90. }
  91. // Returns a new Github OAuth 2.0 backend endpoint.
  92. func Github(opts *Options) martini.Handler {
  93. opts.AuthUrl = "https://github.com/login/oauth/authorize"
  94. opts.TokenUrl = "https://github.com/login/oauth/access_token"
  95. return NewOAuth2Provider(opts)
  96. }
  97. func Facebook(opts *Options) martini.Handler {
  98. opts.AuthUrl = "https://www.facebook.com/dialog/oauth"
  99. opts.TokenUrl = "https://graph.facebook.com/oauth/access_token"
  100. return NewOAuth2Provider(opts)
  101. }
  102. // Returns a generic OAuth 2.0 backend endpoint.
  103. func NewOAuth2Provider(opts *Options) martini.Handler {
  104. config := &oauth.Config{
  105. ClientId: opts.ClientId,
  106. ClientSecret: opts.ClientSecret,
  107. RedirectURL: opts.RedirectURL,
  108. Scope: strings.Join(opts.Scopes, " "),
  109. AuthURL: opts.AuthUrl,
  110. TokenURL: opts.TokenUrl,
  111. }
  112. transport := &oauth.Transport{
  113. Config: config,
  114. Transport: http.DefaultTransport,
  115. }
  116. return func(c martini.Context, ctx *middleware.Context) {
  117. if ctx.Req.Method == "GET" {
  118. switch ctx.Req.URL.Path {
  119. case PathLogin:
  120. login(transport, ctx)
  121. case PathLogout:
  122. logout(transport, ctx)
  123. case PathCallback:
  124. handleOAuth2Callback(transport, ctx)
  125. }
  126. }
  127. tk := unmarshallToken(ctx.Session)
  128. if tk != nil {
  129. // check if the access token is expired
  130. if tk.IsExpired() && tk.Refresh() == "" {
  131. ctx.Session.Delete(keyToken)
  132. tk = nil
  133. }
  134. }
  135. // Inject tokens.
  136. c.MapTo(tk, (*Tokens)(nil))
  137. }
  138. }
  139. // Handler that redirects user to the login page
  140. // if user is not logged in.
  141. // Sample usage:
  142. // m.Get("/login-required", oauth2.LoginRequired, func() ... {})
  143. var LoginRequired martini.Handler = func() martini.Handler {
  144. return func(c martini.Context, ctx *middleware.Context) {
  145. token := unmarshallToken(ctx.Session)
  146. if token == nil || token.IsExpired() {
  147. next := url.QueryEscape(ctx.Req.URL.RequestURI())
  148. ctx.Redirect(PathLogin + "?next=" + next)
  149. return
  150. }
  151. }
  152. }()
  153. func login(t *oauth.Transport, ctx *middleware.Context) {
  154. next := extractPath(ctx.Query(keyNextPage))
  155. if ctx.Session.Get(keyToken) == nil {
  156. // User is not logged in.
  157. ctx.Redirect(t.Config.AuthCodeURL(next))
  158. return
  159. }
  160. // No need to login, redirect to the next page.
  161. ctx.Redirect(next)
  162. }
  163. func logout(t *oauth.Transport, ctx *middleware.Context) {
  164. next := extractPath(ctx.Query(keyNextPage))
  165. ctx.Session.Delete(keyToken)
  166. ctx.Redirect(next)
  167. }
  168. func handleOAuth2Callback(t *oauth.Transport, ctx *middleware.Context) {
  169. if errMsg := ctx.Query("error_description"); len(errMsg) > 0 {
  170. log.Error("oauth2.handleOAuth2Callback: %s", errMsg)
  171. return
  172. }
  173. next := extractPath(ctx.Query("state"))
  174. code := ctx.Query("code")
  175. tk, err := t.Exchange(code)
  176. if err != nil {
  177. // Pass the error message, or allow dev to provide its own
  178. // error handler.
  179. log.Error("oauth2.handleOAuth2Callback(token.Exchange): %v", err)
  180. // ctx.Redirect(PathError)
  181. return
  182. }
  183. // Store the credentials in the session.
  184. val, _ := json.Marshal(tk)
  185. ctx.Session.Set(keyToken, val)
  186. ctx.Redirect(next)
  187. }
  188. func unmarshallToken(s session.SessionStore) (t *token) {
  189. if s.Get(keyToken) == nil {
  190. return
  191. }
  192. data := s.Get(keyToken).([]byte)
  193. var tk oauth.Token
  194. json.Unmarshal(data, &tk)
  195. return &token{tk}
  196. }
  197. func extractPath(next string) string {
  198. n, err := url.Parse(next)
  199. if err != nil {
  200. return "/"
  201. }
  202. return n.Path
  203. }