A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

463 righe
17 KiB

  1. /*
  2. * Copyright © 2019-2020 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "context"
  13. "encoding/json"
  14. "fmt"
  15. "io"
  16. "io/ioutil"
  17. "net/http"
  18. "net/url"
  19. "strings"
  20. "time"
  21. "github.com/gorilla/mux"
  22. "github.com/gorilla/sessions"
  23. "github.com/writeas/impart"
  24. "github.com/writeas/web-core/log"
  25. "github.com/writeas/writefreely/config"
  26. )
  27. // OAuthButtons holds display information for different OAuth providers we support.
  28. type OAuthButtons struct {
  29. SlackEnabled bool
  30. WriteAsEnabled bool
  31. GitLabEnabled bool
  32. GitLabDisplayName string
  33. GiteaEnabled bool
  34. GiteaDisplayName string
  35. GenericEnabled bool
  36. GenericDisplayName string
  37. }
  38. // NewOAuthButtons creates a new OAuthButtons struct based on our app configuration.
  39. func NewOAuthButtons(cfg *config.Config) *OAuthButtons {
  40. return &OAuthButtons{
  41. SlackEnabled: cfg.SlackOauth.ClientID != "",
  42. WriteAsEnabled: cfg.WriteAsOauth.ClientID != "",
  43. GitLabEnabled: cfg.GitlabOauth.ClientID != "",
  44. GitLabDisplayName: config.OrDefaultString(cfg.GitlabOauth.DisplayName, gitlabDisplayName),
  45. GiteaEnabled: cfg.GiteaOauth.ClientID != "",
  46. GiteaDisplayName: config.OrDefaultString(cfg.GiteaOauth.DisplayName, giteaDisplayName),
  47. GenericEnabled: cfg.GenericOauth.ClientID != "",
  48. GenericDisplayName: config.OrDefaultString(cfg.GenericOauth.DisplayName, genericOauthDisplayName),
  49. }
  50. }
  51. // TokenResponse contains data returned when a token is created either
  52. // through a code exchange or using a refresh token.
  53. type TokenResponse struct {
  54. AccessToken string `json:"access_token"`
  55. ExpiresIn int `json:"expires_in"`
  56. RefreshToken string `json:"refresh_token"`
  57. TokenType string `json:"token_type"`
  58. Error string `json:"error"`
  59. }
  60. // InspectResponse contains data returned when an access token is inspected.
  61. type InspectResponse struct {
  62. ClientID string `json:"client_id"`
  63. UserID string `json:"user_id"`
  64. ExpiresAt time.Time `json:"expires_at"`
  65. Username string `json:"username"`
  66. DisplayName string `json:"-"`
  67. Email string `json:"email"`
  68. Error string `json:"error"`
  69. }
  70. // tokenRequestMaxLen is the most bytes that we'll read from the /oauth/token
  71. // endpoint. One megabyte is plenty.
  72. const tokenRequestMaxLen = 1000000
  73. // infoRequestMaxLen is the most bytes that we'll read from the
  74. // /oauth/inspect endpoint.
  75. const infoRequestMaxLen = 1000000
  76. // OAuthDatastoreProvider provides a minimal interface of data store, config,
  77. // and session store for use with the oauth handlers.
  78. type OAuthDatastoreProvider interface {
  79. DB() OAuthDatastore
  80. Config() *config.Config
  81. SessionStore() sessions.Store
  82. }
  83. // OAuthDatastore provides a minimal interface of data store methods used in
  84. // oauth functionality.
  85. type OAuthDatastore interface {
  86. GetIDForRemoteUser(context.Context, string, string, string) (int64, error)
  87. RecordRemoteUserID(context.Context, int64, string, string, string, string) error
  88. ValidateOAuthState(context.Context, string) (string, string, int64, string, error)
  89. GenerateOAuthState(context.Context, string, string, int64, string) (string, error)
  90. CreateUser(*config.Config, *User, string) error
  91. GetUserByID(int64) (*User, error)
  92. }
  93. type HttpClient interface {
  94. Do(req *http.Request) (*http.Response, error)
  95. }
  96. type oauthClient interface {
  97. GetProvider() string
  98. GetClientID() string
  99. GetCallbackLocation() string
  100. buildLoginURL(state string) (string, error)
  101. exchangeOauthCode(ctx context.Context, code string) (*TokenResponse, error)
  102. inspectOauthAccessToken(ctx context.Context, accessToken string) (*InspectResponse, error)
  103. }
  104. type callbackProxyClient struct {
  105. server string
  106. callbackLocation string
  107. httpClient HttpClient
  108. }
  109. type oauthHandler struct {
  110. Config *config.Config
  111. DB OAuthDatastore
  112. Store sessions.Store
  113. EmailKey []byte
  114. oauthClient oauthClient
  115. callbackProxy *callbackProxyClient
  116. }
  117. func (h oauthHandler) viewOauthInit(app *App, w http.ResponseWriter, r *http.Request) error {
  118. ctx := r.Context()
  119. var attachUser int64
  120. if attach := r.URL.Query().Get("attach"); attach == "t" {
  121. user, _ := getUserAndSession(app, r)
  122. if user == nil {
  123. return impart.HTTPError{http.StatusInternalServerError, "cannot attach auth to user: user not found in session"}
  124. }
  125. attachUser = user.ID
  126. }
  127. state, err := h.DB.GenerateOAuthState(ctx, h.oauthClient.GetProvider(), h.oauthClient.GetClientID(), attachUser, r.FormValue("invite_code"))
  128. if err != nil {
  129. log.Error("viewOauthInit error: %s", err)
  130. return impart.HTTPError{http.StatusInternalServerError, "could not prepare oauth redirect url"}
  131. }
  132. if h.callbackProxy != nil {
  133. if err := h.callbackProxy.register(ctx, state); err != nil {
  134. log.Error("viewOauthInit error: %s", err)
  135. return impart.HTTPError{http.StatusInternalServerError, "could not register state server"}
  136. }
  137. }
  138. location, err := h.oauthClient.buildLoginURL(state)
  139. if err != nil {
  140. log.Error("viewOauthInit error: %s", err)
  141. return impart.HTTPError{http.StatusInternalServerError, "could not prepare oauth redirect url"}
  142. }
  143. return impart.HTTPError{http.StatusTemporaryRedirect, location}
  144. }
  145. func configureSlackOauth(parentHandler *Handler, r *mux.Router, app *App) {
  146. if app.Config().SlackOauth.ClientID != "" {
  147. callbackLocation := app.Config().App.Host + "/oauth/callback/slack"
  148. var stateRegisterClient *callbackProxyClient = nil
  149. if app.Config().SlackOauth.CallbackProxyAPI != "" {
  150. stateRegisterClient = &callbackProxyClient{
  151. server: app.Config().SlackOauth.CallbackProxyAPI,
  152. callbackLocation: app.Config().App.Host + "/oauth/callback/slack",
  153. httpClient: config.DefaultHTTPClient(),
  154. }
  155. callbackLocation = app.Config().SlackOauth.CallbackProxy
  156. }
  157. oauthClient := slackOauthClient{
  158. ClientID: app.Config().SlackOauth.ClientID,
  159. ClientSecret: app.Config().SlackOauth.ClientSecret,
  160. TeamID: app.Config().SlackOauth.TeamID,
  161. HttpClient: config.DefaultHTTPClient(),
  162. CallbackLocation: callbackLocation,
  163. }
  164. configureOauthRoutes(parentHandler, r, app, oauthClient, stateRegisterClient)
  165. }
  166. }
  167. func configureWriteAsOauth(parentHandler *Handler, r *mux.Router, app *App) {
  168. if app.Config().WriteAsOauth.ClientID != "" {
  169. callbackLocation := app.Config().App.Host + "/oauth/callback/write.as"
  170. var callbackProxy *callbackProxyClient = nil
  171. if app.Config().WriteAsOauth.CallbackProxy != "" {
  172. callbackProxy = &callbackProxyClient{
  173. server: app.Config().WriteAsOauth.CallbackProxyAPI,
  174. callbackLocation: app.Config().App.Host + "/oauth/callback/write.as",
  175. httpClient: config.DefaultHTTPClient(),
  176. }
  177. callbackLocation = app.Config().WriteAsOauth.CallbackProxy
  178. }
  179. oauthClient := writeAsOauthClient{
  180. ClientID: app.Config().WriteAsOauth.ClientID,
  181. ClientSecret: app.Config().WriteAsOauth.ClientSecret,
  182. ExchangeLocation: config.OrDefaultString(app.Config().WriteAsOauth.TokenLocation, writeAsExchangeLocation),
  183. InspectLocation: config.OrDefaultString(app.Config().WriteAsOauth.InspectLocation, writeAsIdentityLocation),
  184. AuthLocation: config.OrDefaultString(app.Config().WriteAsOauth.AuthLocation, writeAsAuthLocation),
  185. HttpClient: config.DefaultHTTPClient(),
  186. CallbackLocation: callbackLocation,
  187. }
  188. configureOauthRoutes(parentHandler, r, app, oauthClient, callbackProxy)
  189. }
  190. }
  191. func configureGitlabOauth(parentHandler *Handler, r *mux.Router, app *App) {
  192. if app.Config().GitlabOauth.ClientID != "" {
  193. callbackLocation := app.Config().App.Host + "/oauth/callback/gitlab"
  194. var callbackProxy *callbackProxyClient = nil
  195. if app.Config().GitlabOauth.CallbackProxy != "" {
  196. callbackProxy = &callbackProxyClient{
  197. server: app.Config().GitlabOauth.CallbackProxyAPI,
  198. callbackLocation: app.Config().App.Host + "/oauth/callback/gitlab",
  199. httpClient: config.DefaultHTTPClient(),
  200. }
  201. callbackLocation = app.Config().GitlabOauth.CallbackProxy
  202. }
  203. address := config.OrDefaultString(app.Config().GitlabOauth.Host, gitlabHost)
  204. oauthClient := gitlabOauthClient{
  205. ClientID: app.Config().GitlabOauth.ClientID,
  206. ClientSecret: app.Config().GitlabOauth.ClientSecret,
  207. ExchangeLocation: address + "/oauth/token",
  208. InspectLocation: address + "/api/v4/user",
  209. AuthLocation: address + "/oauth/authorize",
  210. HttpClient: config.DefaultHTTPClient(),
  211. CallbackLocation: callbackLocation,
  212. }
  213. configureOauthRoutes(parentHandler, r, app, oauthClient, callbackProxy)
  214. }
  215. }
  216. func configureGenericOauth(parentHandler *Handler, r *mux.Router, app *App) {
  217. if app.Config().GenericOauth.ClientID != "" {
  218. callbackLocation := app.Config().App.Host + "/oauth/callback/generic"
  219. var callbackProxy *callbackProxyClient = nil
  220. if app.Config().GenericOauth.CallbackProxy != "" {
  221. callbackProxy = &callbackProxyClient{
  222. server: app.Config().GenericOauth.CallbackProxyAPI,
  223. callbackLocation: app.Config().App.Host + "/oauth/callback/generic",
  224. httpClient: config.DefaultHTTPClient(),
  225. }
  226. callbackLocation = app.Config().GenericOauth.CallbackProxy
  227. }
  228. oauthClient := genericOauthClient{
  229. ClientID: app.Config().GenericOauth.ClientID,
  230. ClientSecret: app.Config().GenericOauth.ClientSecret,
  231. ExchangeLocation: app.Config().GenericOauth.Host + app.Config().GenericOauth.TokenEndpoint,
  232. InspectLocation: app.Config().GenericOauth.Host + app.Config().GenericOauth.InspectEndpoint,
  233. AuthLocation: app.Config().GenericOauth.Host + app.Config().GenericOauth.AuthEndpoint,
  234. HttpClient: config.DefaultHTTPClient(),
  235. CallbackLocation: callbackLocation,
  236. }
  237. configureOauthRoutes(parentHandler, r, app, oauthClient, callbackProxy)
  238. }
  239. }
  240. func configureGiteaOauth(parentHandler *Handler, r *mux.Router, app *App) {
  241. if app.Config().GiteaOauth.ClientID != "" {
  242. callbackLocation := app.Config().App.Host + "/oauth/callback/gitea"
  243. var callbackProxy *callbackProxyClient = nil
  244. if app.Config().GiteaOauth.CallbackProxy != "" {
  245. callbackProxy = &callbackProxyClient{
  246. server: app.Config().GiteaOauth.CallbackProxyAPI,
  247. callbackLocation: app.Config().App.Host + "/oauth/callback/gitea",
  248. httpClient: config.DefaultHTTPClient(),
  249. }
  250. callbackLocation = app.Config().GiteaOauth.CallbackProxy
  251. }
  252. oauthClient := giteaOauthClient{
  253. ClientID: app.Config().GiteaOauth.ClientID,
  254. ClientSecret: app.Config().GiteaOauth.ClientSecret,
  255. ExchangeLocation: app.Config().GiteaOauth.Host + "/login/oauth/access_token",
  256. InspectLocation: app.Config().GiteaOauth.Host + "/api/v1/user",
  257. AuthLocation: app.Config().GiteaOauth.Host + "/login/oauth/authorize",
  258. HttpClient: config.DefaultHTTPClient(),
  259. CallbackLocation: callbackLocation,
  260. }
  261. configureOauthRoutes(parentHandler, r, app, oauthClient, callbackProxy)
  262. }
  263. }
  264. func configureOauthRoutes(parentHandler *Handler, r *mux.Router, app *App, oauthClient oauthClient, callbackProxy *callbackProxyClient) {
  265. handler := &oauthHandler{
  266. Config: app.Config(),
  267. DB: app.DB(),
  268. Store: app.SessionStore(),
  269. oauthClient: oauthClient,
  270. EmailKey: app.keys.EmailKey,
  271. callbackProxy: callbackProxy,
  272. }
  273. r.HandleFunc("/oauth/"+oauthClient.GetProvider(), parentHandler.OAuth(handler.viewOauthInit)).Methods("GET")
  274. r.HandleFunc("/oauth/callback/"+oauthClient.GetProvider(), parentHandler.OAuth(handler.viewOauthCallback)).Methods("GET")
  275. r.HandleFunc("/oauth/signup", parentHandler.OAuth(handler.viewOauthSignup)).Methods("POST")
  276. }
  277. func (h oauthHandler) viewOauthCallback(app *App, w http.ResponseWriter, r *http.Request) error {
  278. ctx := r.Context()
  279. code := r.FormValue("code")
  280. state := r.FormValue("state")
  281. provider, clientID, attachUserID, inviteCode, err := h.DB.ValidateOAuthState(ctx, state)
  282. if err != nil {
  283. log.Error("Unable to ValidateOAuthState: %s", err)
  284. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  285. }
  286. tokenResponse, err := h.oauthClient.exchangeOauthCode(ctx, code)
  287. if err != nil {
  288. log.Error("Unable to exchangeOauthCode: %s", err)
  289. // TODO: show user friendly message if needed
  290. // TODO: show NO message for cases like user pressing "Cancel" on authorize step
  291. addSessionFlash(app, w, r, err.Error(), nil)
  292. if attachUserID > 0 {
  293. return impart.HTTPError{http.StatusFound, "/me/settings"}
  294. }
  295. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  296. }
  297. // Now that we have the access token, let's use it real quick to make sure
  298. // it really really works.
  299. tokenInfo, err := h.oauthClient.inspectOauthAccessToken(ctx, tokenResponse.AccessToken)
  300. if err != nil {
  301. log.Error("Unable to inspectOauthAccessToken: %s", err)
  302. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  303. }
  304. localUserID, err := h.DB.GetIDForRemoteUser(ctx, tokenInfo.UserID, provider, clientID)
  305. if err != nil {
  306. log.Error("Unable to GetIDForRemoteUser: %s", err)
  307. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  308. }
  309. if localUserID != -1 && attachUserID > 0 {
  310. if err = addSessionFlash(app, w, r, "This Slack account is already attached to another user.", nil); err != nil {
  311. return impart.HTTPError{Status: http.StatusInternalServerError, Message: err.Error()}
  312. }
  313. return impart.HTTPError{http.StatusFound, "/me/settings"}
  314. }
  315. if localUserID != -1 {
  316. // Existing user, so log in now
  317. user, err := h.DB.GetUserByID(localUserID)
  318. if err != nil {
  319. log.Error("Unable to GetUserByID %d: %s", localUserID, err)
  320. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  321. }
  322. if err = loginOrFail(h.Store, w, r, user); err != nil {
  323. log.Error("Unable to loginOrFail %d: %s", localUserID, err)
  324. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  325. }
  326. return nil
  327. }
  328. if attachUserID > 0 {
  329. log.Info("attaching to user %d", attachUserID)
  330. err = h.DB.RecordRemoteUserID(r.Context(), attachUserID, tokenInfo.UserID, provider, clientID, tokenResponse.AccessToken)
  331. if err != nil {
  332. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  333. }
  334. return impart.HTTPError{http.StatusFound, "/me/settings"}
  335. }
  336. // New user registration below.
  337. // First, verify that user is allowed to register
  338. if inviteCode != "" {
  339. // Verify invite code is valid
  340. i, err := app.db.GetUserInvite(inviteCode)
  341. if err != nil {
  342. return impart.HTTPError{http.StatusInternalServerError, err.Error()}
  343. }
  344. if !i.Active(app.db) {
  345. return impart.HTTPError{http.StatusNotFound, "Invite link has expired."}
  346. }
  347. } else if !app.cfg.App.OpenRegistration {
  348. addSessionFlash(app, w, r, ErrUserNotFound.Error(), nil)
  349. return impart.HTTPError{http.StatusFound, "/login"}
  350. }
  351. displayName := tokenInfo.DisplayName
  352. if len(displayName) == 0 {
  353. displayName = tokenInfo.Username
  354. }
  355. tp := &oauthSignupPageParams{
  356. AccessToken: tokenResponse.AccessToken,
  357. TokenUsername: tokenInfo.Username,
  358. TokenAlias: tokenInfo.DisplayName,
  359. TokenEmail: tokenInfo.Email,
  360. TokenRemoteUser: tokenInfo.UserID,
  361. Provider: provider,
  362. ClientID: clientID,
  363. InviteCode: inviteCode,
  364. }
  365. tp.TokenHash = tp.HashTokenParams(h.Config.Server.HashSeed)
  366. return h.showOauthSignupPage(app, w, r, tp, nil)
  367. }
  368. func (r *callbackProxyClient) register(ctx context.Context, state string) error {
  369. form := url.Values{}
  370. form.Add("state", state)
  371. form.Add("location", r.callbackLocation)
  372. req, err := http.NewRequestWithContext(ctx, "POST", r.server, strings.NewReader(form.Encode()))
  373. if err != nil {
  374. return err
  375. }
  376. req.Header.Set("User-Agent", ServerUserAgent(""))
  377. req.Header.Set("Accept", "application/json")
  378. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  379. resp, err := r.httpClient.Do(req)
  380. if err != nil {
  381. return err
  382. }
  383. if resp.StatusCode != http.StatusCreated {
  384. return fmt.Errorf("unable register state location: %d", resp.StatusCode)
  385. }
  386. return nil
  387. }
  388. func limitedJsonUnmarshal(body io.ReadCloser, n int, thing interface{}) error {
  389. lr := io.LimitReader(body, int64(n+1))
  390. data, err := ioutil.ReadAll(lr)
  391. if err != nil {
  392. return err
  393. }
  394. if len(data) == n+1 {
  395. return fmt.Errorf("content larger than max read allowance: %d", n)
  396. }
  397. return json.Unmarshal(data, thing)
  398. }
  399. func loginOrFail(store sessions.Store, w http.ResponseWriter, r *http.Request, user *User) error {
  400. // An error may be returned, but a valid session should always be returned.
  401. session, _ := store.Get(r, cookieName)
  402. session.Values[cookieUserVal] = user.Cookie()
  403. if err := session.Save(r, w); err != nil {
  404. fmt.Println("error saving session", err)
  405. return err
  406. }
  407. http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
  408. return nil
  409. }