2019-12-19 16:48:04 +00:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2019-12-23 19:30:32 +00:00
|
|
|
"fmt"
|
2019-12-27 18:40:11 +00:00
|
|
|
"github.com/gorilla/mux"
|
2019-12-19 16:48:04 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2019-12-30 23:14:01 +00:00
|
|
|
"github.com/writeas/impart"
|
2019-12-19 16:48:04 +00:00
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
"github.com/writeas/writefreely/config"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-01-07 20:22:25 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2019-12-19 16:48:04 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TokenResponse contains data returned when a token is created either
|
|
|
|
// through a code exchange or using a refresh token.
|
|
|
|
type TokenResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
2019-12-30 23:25:24 +00:00
|
|
|
Error string `json:"error"`
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InspectResponse contains data returned when an access token is inspected.
|
|
|
|
type InspectResponse struct {
|
2020-01-02 20:55:28 +00:00
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
DisplayName string `json:"-"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Error string `json:"error"`
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tokenRequestMaxLen is the most bytes that we'll read from the /oauth/token
|
|
|
|
// endpoint. One megabyte is plenty.
|
|
|
|
const tokenRequestMaxLen = 1000000
|
|
|
|
|
|
|
|
// infoRequestMaxLen is the most bytes that we'll read from the
|
|
|
|
// /oauth/inspect endpoint.
|
|
|
|
const infoRequestMaxLen = 1000000
|
|
|
|
|
|
|
|
// OAuthDatastoreProvider provides a minimal interface of data store, config,
|
|
|
|
// and session store for use with the oauth handlers.
|
|
|
|
type OAuthDatastoreProvider interface {
|
|
|
|
DB() OAuthDatastore
|
|
|
|
Config() *config.Config
|
|
|
|
SessionStore() sessions.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
// OAuthDatastore provides a minimal interface of data store methods used in
|
|
|
|
// oauth functionality.
|
|
|
|
type OAuthDatastore interface {
|
2019-12-30 18:32:06 +00:00
|
|
|
GetIDForRemoteUser(context.Context, string, string, string) (int64, error)
|
|
|
|
RecordRemoteUserID(context.Context, int64, string, string, string, string) error
|
2020-01-15 18:16:59 +00:00
|
|
|
ValidateOAuthState(context.Context, string) (string, string, int64, error)
|
|
|
|
GenerateOAuthState(context.Context, string, string, int64) (string, error)
|
2019-12-27 18:40:11 +00:00
|
|
|
|
2019-12-19 16:48:04 +00:00
|
|
|
CreateUser(*config.Config, *User, string) error
|
2020-01-03 16:31:38 +00:00
|
|
|
GetUserByID(int64) (*User, error)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HttpClient interface {
|
|
|
|
Do(req *http.Request) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:15:47 +00:00
|
|
|
type oauthClient interface {
|
|
|
|
GetProvider() string
|
|
|
|
GetClientID() string
|
2020-01-07 20:22:25 +00:00
|
|
|
GetCallbackLocation() string
|
2019-12-28 20:15:47 +00:00
|
|
|
buildLoginURL(state string) (string, error)
|
|
|
|
exchangeOauthCode(ctx context.Context, code string) (*TokenResponse, error)
|
|
|
|
inspectOauthAccessToken(ctx context.Context, accessToken string) (*InspectResponse, error)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 02:47:23 +00:00
|
|
|
type callbackProxyClient struct {
|
|
|
|
server string
|
2020-01-07 20:22:25 +00:00
|
|
|
callbackLocation string
|
|
|
|
httpClient HttpClient
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:48:04 +00:00
|
|
|
type oauthHandler struct {
|
2020-01-08 02:47:23 +00:00
|
|
|
Config *config.Config
|
|
|
|
DB OAuthDatastore
|
|
|
|
Store sessions.Store
|
|
|
|
EmailKey []byte
|
|
|
|
oauthClient oauthClient
|
|
|
|
callbackProxy *callbackProxyClient
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 21:19:26 +00:00
|
|
|
func (h oauthHandler) viewOauthInit(app *App, w http.ResponseWriter, r *http.Request) error {
|
2019-12-28 20:15:47 +00:00
|
|
|
ctx := r.Context()
|
2020-01-15 18:16:59 +00:00
|
|
|
|
|
|
|
var attachUser int64
|
|
|
|
if attach := r.URL.Query().Get("attach"); attach == "t" {
|
|
|
|
user, _ := getUserAndSession(app, r)
|
|
|
|
if user == nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, "cannot attach auth to user: user not found in session"}
|
|
|
|
}
|
|
|
|
attachUser = user.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := h.DB.GenerateOAuthState(ctx, h.oauthClient.GetProvider(), h.oauthClient.GetClientID(), attachUser)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2020-01-15 18:16:59 +00:00
|
|
|
log.Error("viewOauthInit error: %s", err)
|
2020-01-02 21:19:26 +00:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, "could not prepare oauth redirect url"}
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2020-01-07 20:22:25 +00:00
|
|
|
|
2020-01-08 02:47:23 +00:00
|
|
|
if h.callbackProxy != nil {
|
|
|
|
if err := h.callbackProxy.register(ctx, state); err != nil {
|
2020-01-15 18:16:59 +00:00
|
|
|
log.Error("viewOauthInit error: %s", err)
|
2020-01-08 02:47:23 +00:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, "could not register state server"}
|
2020-01-07 20:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:15:47 +00:00
|
|
|
location, err := h.oauthClient.buildLoginURL(state)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2020-01-15 18:16:59 +00:00
|
|
|
log.Error("viewOauthInit error: %s", err)
|
2020-01-02 21:19:26 +00:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, "could not prepare oauth redirect url"}
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2020-01-02 21:19:26 +00:00
|
|
|
return impart.HTTPError{http.StatusTemporaryRedirect, location}
|
2019-12-27 18:40:11 +00:00
|
|
|
}
|
2019-12-19 16:48:04 +00:00
|
|
|
|
2020-01-02 21:19:26 +00:00
|
|
|
func configureSlackOauth(parentHandler *Handler, r *mux.Router, app *App) {
|
2019-12-28 20:15:47 +00:00
|
|
|
if app.Config().SlackOauth.ClientID != "" {
|
2020-01-10 21:16:43 +00:00
|
|
|
callbackLocation := app.Config().App.Host + "/oauth/callback/slack"
|
2020-01-08 02:47:23 +00:00
|
|
|
|
|
|
|
var stateRegisterClient *callbackProxyClient = nil
|
|
|
|
if app.Config().SlackOauth.CallbackProxyAPI != "" {
|
|
|
|
stateRegisterClient = &callbackProxyClient{
|
|
|
|
server: app.Config().SlackOauth.CallbackProxyAPI,
|
2020-01-10 21:16:43 +00:00
|
|
|
callbackLocation: app.Config().App.Host + "/oauth/callback/slack",
|
2020-01-07 20:22:25 +00:00
|
|
|
httpClient: config.DefaultHTTPClient(),
|
|
|
|
}
|
2020-01-08 02:47:23 +00:00
|
|
|
callbackLocation = app.Config().SlackOauth.CallbackProxy
|
2020-01-07 20:22:25 +00:00
|
|
|
}
|
2019-12-28 20:15:47 +00:00
|
|
|
oauthClient := slackOauthClient{
|
2020-01-08 02:47:23 +00:00
|
|
|
ClientID: app.Config().SlackOauth.ClientID,
|
|
|
|
ClientSecret: app.Config().SlackOauth.ClientSecret,
|
|
|
|
TeamID: app.Config().SlackOauth.TeamID,
|
|
|
|
HttpClient: config.DefaultHTTPClient(),
|
|
|
|
CallbackLocation: callbackLocation,
|
2019-12-27 18:40:11 +00:00
|
|
|
}
|
2020-01-07 20:22:25 +00:00
|
|
|
configureOauthRoutes(parentHandler, r, app, oauthClient, stateRegisterClient)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2019-12-28 20:15:47 +00:00
|
|
|
}
|
2019-12-19 16:48:04 +00:00
|
|
|
|
2020-01-02 21:19:26 +00:00
|
|
|
func configureWriteAsOauth(parentHandler *Handler, r *mux.Router, app *App) {
|
2019-12-28 20:15:47 +00:00
|
|
|
if app.Config().WriteAsOauth.ClientID != "" {
|
2020-01-10 21:16:43 +00:00
|
|
|
callbackLocation := app.Config().App.Host + "/oauth/callback/write.as"
|
2020-01-08 02:47:23 +00:00
|
|
|
|
|
|
|
var callbackProxy *callbackProxyClient = nil
|
|
|
|
if app.Config().WriteAsOauth.CallbackProxy != "" {
|
|
|
|
callbackProxy = &callbackProxyClient{
|
|
|
|
server: app.Config().WriteAsOauth.CallbackProxyAPI,
|
2020-01-10 21:16:43 +00:00
|
|
|
callbackLocation: app.Config().App.Host + "/oauth/callback/write.as",
|
2020-01-07 20:22:25 +00:00
|
|
|
httpClient: config.DefaultHTTPClient(),
|
|
|
|
}
|
2020-03-11 10:51:12 +00:00
|
|
|
callbackLocation = app.Config().WriteAsOauth.CallbackProxy
|
2020-01-07 20:22:25 +00:00
|
|
|
}
|
2020-01-08 02:47:23 +00:00
|
|
|
|
2019-12-28 20:15:47 +00:00
|
|
|
oauthClient := writeAsOauthClient{
|
|
|
|
ClientID: app.Config().WriteAsOauth.ClientID,
|
|
|
|
ClientSecret: app.Config().WriteAsOauth.ClientSecret,
|
2020-01-02 20:50:54 +00:00
|
|
|
ExchangeLocation: config.OrDefaultString(app.Config().WriteAsOauth.TokenLocation, writeAsExchangeLocation),
|
|
|
|
InspectLocation: config.OrDefaultString(app.Config().WriteAsOauth.InspectLocation, writeAsIdentityLocation),
|
|
|
|
AuthLocation: config.OrDefaultString(app.Config().WriteAsOauth.AuthLocation, writeAsAuthLocation),
|
|
|
|
HttpClient: config.DefaultHTTPClient(),
|
2020-01-08 02:47:23 +00:00
|
|
|
CallbackLocation: callbackLocation,
|
2019-12-28 20:15:47 +00:00
|
|
|
}
|
2020-01-08 02:47:23 +00:00
|
|
|
configureOauthRoutes(parentHandler, r, app, oauthClient, callbackProxy)
|
2019-12-28 20:15:47 +00:00
|
|
|
}
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 10:51:12 +00:00
|
|
|
func configureGitlabOauth(parentHandler *Handler, r *mux.Router, app *App) {
|
|
|
|
if app.Config().GitlabOauth.ClientID != "" {
|
|
|
|
callbackLocation := app.Config().App.Host + "/oauth/callback/gitlab"
|
|
|
|
|
|
|
|
var callbackProxy *callbackProxyClient = nil
|
|
|
|
if app.Config().GitlabOauth.CallbackProxy != "" {
|
|
|
|
callbackProxy = &callbackProxyClient{
|
|
|
|
server: app.Config().GitlabOauth.CallbackProxyAPI,
|
|
|
|
callbackLocation: app.Config().App.Host + "/oauth/callback/gitlab",
|
|
|
|
httpClient: config.DefaultHTTPClient(),
|
|
|
|
}
|
|
|
|
callbackLocation = app.Config().GitlabOauth.CallbackProxy
|
|
|
|
}
|
|
|
|
|
2020-03-18 20:17:06 +00:00
|
|
|
address := config.OrDefaultString(app.Config().GitlabOauth.Host, gitlabHost)
|
2020-03-11 10:51:12 +00:00
|
|
|
oauthClient := gitlabOauthClient{
|
|
|
|
ClientID: app.Config().GitlabOauth.ClientID,
|
|
|
|
ClientSecret: app.Config().GitlabOauth.ClientSecret,
|
2020-03-16 08:00:04 +00:00
|
|
|
ExchangeLocation: address + "/oauth/token",
|
|
|
|
InspectLocation: address + "/api/v4/user",
|
|
|
|
AuthLocation: address + "/oauth/authorize",
|
2020-03-11 10:51:12 +00:00
|
|
|
HttpClient: config.DefaultHTTPClient(),
|
|
|
|
CallbackLocation: callbackLocation,
|
|
|
|
}
|
|
|
|
configureOauthRoutes(parentHandler, r, app, oauthClient, callbackProxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 02:47:23 +00:00
|
|
|
func configureOauthRoutes(parentHandler *Handler, r *mux.Router, app *App, oauthClient oauthClient, callbackProxy *callbackProxyClient) {
|
2019-12-28 20:15:47 +00:00
|
|
|
handler := &oauthHandler{
|
2020-01-08 02:47:23 +00:00
|
|
|
Config: app.Config(),
|
|
|
|
DB: app.DB(),
|
|
|
|
Store: app.SessionStore(),
|
|
|
|
oauthClient: oauthClient,
|
|
|
|
EmailKey: app.keys.EmailKey,
|
|
|
|
callbackProxy: callbackProxy,
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2020-01-02 21:19:26 +00:00
|
|
|
r.HandleFunc("/oauth/"+oauthClient.GetProvider(), parentHandler.OAuth(handler.viewOauthInit)).Methods("GET")
|
2020-01-10 21:16:43 +00:00
|
|
|
r.HandleFunc("/oauth/callback/"+oauthClient.GetProvider(), parentHandler.OAuth(handler.viewOauthCallback)).Methods("GET")
|
2020-01-03 18:50:21 +00:00
|
|
|
r.HandleFunc("/oauth/signup", parentHandler.OAuth(handler.viewOauthSignup)).Methods("POST")
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 23:14:01 +00:00
|
|
|
func (h oauthHandler) viewOauthCallback(app *App, w http.ResponseWriter, r *http.Request) error {
|
2019-12-19 16:48:04 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
code := r.FormValue("code")
|
|
|
|
state := r.FormValue("state")
|
|
|
|
|
2020-01-15 18:16:59 +00:00
|
|
|
provider, clientID, attachUserID, err := h.DB.ValidateOAuthState(ctx, state)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-30 23:23:45 +00:00
|
|
|
log.Error("Unable to ValidateOAuthState: %s", err)
|
2019-12-30 23:14:01 +00:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, err.Error()}
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 20:15:47 +00:00
|
|
|
tokenResponse, err := h.oauthClient.exchangeOauthCode(ctx, code)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-30 23:23:45 +00:00
|
|
|
log.Error("Unable to exchangeOauthCode: %s", err)
|
2019-12-30 23:14:01 +00:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, err.Error()}
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have the access token, let's use it real quick to make sur
|
|
|
|
// it really really works.
|
2019-12-28 20:15:47 +00:00
|
|
|
tokenInfo, err := h.oauthClient.inspectOauthAccessToken(ctx, tokenResponse.AccessToken)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-30 23:23:45 +00:00
|
|
|
log.Error("Unable to inspectOauthAccessToken: %s", err)
|
2019-12-30 23:14:01 +00:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, err.Error()}
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 18:32:06 +00:00
|
|
|
localUserID, err := h.DB.GetIDForRemoteUser(ctx, tokenInfo.UserID, provider, clientID)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-30 23:23:45 +00:00
|
|
|
log.Error("Unable to GetIDForRemoteUser: %s", err)
|
2019-12-30 23:14:01 +00:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, err.Error()}
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 18:50:21 +00:00
|
|
|
if localUserID != -1 {
|
|
|
|
user, err := h.DB.GetUserByID(localUserID)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2020-01-03 18:50:21 +00:00
|
|
|
log.Error("Unable to GetUserByID %d: %s", localUserID, err)
|
2019-12-30 23:14:01 +00:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, err.Error()}
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2020-01-03 18:50:21 +00:00
|
|
|
if err = loginOrFail(h.Store, w, r, user); err != nil {
|
|
|
|
log.Error("Unable to loginOrFail %d: %s", localUserID, err)
|
2019-12-30 23:14:01 +00:00
|
|
|
return impart.HTTPError{http.StatusInternalServerError, err.Error()}
|
2019-12-23 19:30:32 +00:00
|
|
|
}
|
2019-12-30 23:14:01 +00:00
|
|
|
return nil
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2020-01-15 18:16:59 +00:00
|
|
|
if attachUserID > 0 {
|
|
|
|
log.Info("attaching to user %d", attachUserID)
|
|
|
|
err = h.DB.RecordRemoteUserID(r.Context(), attachUserID, tokenInfo.UserID, provider, clientID, tokenResponse.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
return impart.HTTPError{http.StatusInternalServerError, err.Error()}
|
|
|
|
}
|
|
|
|
return impart.HTTPError{http.StatusFound, "/me/settings"}
|
|
|
|
}
|
2019-12-19 16:48:04 +00:00
|
|
|
|
2020-01-14 15:24:48 +00:00
|
|
|
displayName := tokenInfo.DisplayName
|
|
|
|
if len(displayName) == 0 {
|
|
|
|
displayName = tokenInfo.Username
|
|
|
|
}
|
|
|
|
|
2020-01-03 18:50:21 +00:00
|
|
|
tp := &oauthSignupPageParams{
|
|
|
|
AccessToken: tokenResponse.AccessToken,
|
|
|
|
TokenUsername: tokenInfo.Username,
|
|
|
|
TokenAlias: tokenInfo.DisplayName,
|
|
|
|
TokenEmail: tokenInfo.Email,
|
|
|
|
TokenRemoteUser: tokenInfo.UserID,
|
|
|
|
Provider: provider,
|
|
|
|
ClientID: clientID,
|
2019-12-23 19:30:32 +00:00
|
|
|
}
|
2020-01-03 18:50:21 +00:00
|
|
|
tp.TokenHash = tp.HashTokenParams(h.Config.Server.HashSeed)
|
|
|
|
|
|
|
|
return h.showOauthSignupPage(app, w, r, tp, nil)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 02:47:23 +00:00
|
|
|
func (r *callbackProxyClient) register(ctx context.Context, state string) error {
|
2020-01-07 20:22:25 +00:00
|
|
|
form := url.Values{}
|
|
|
|
form.Add("state", state)
|
|
|
|
form.Add("location", r.callbackLocation)
|
2020-01-08 02:47:23 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", r.server, strings.NewReader(form.Encode()))
|
2020-01-07 20:22:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", "writefreely")
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
resp, err := r.httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
2020-01-08 02:47:23 +00:00
|
|
|
return fmt.Errorf("unable register state location: %d", resp.StatusCode)
|
2020-01-07 20:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:15:47 +00:00
|
|
|
func limitedJsonUnmarshal(body io.ReadCloser, n int, thing interface{}) error {
|
|
|
|
lr := io.LimitReader(body, int64(n+1))
|
|
|
|
data, err := ioutil.ReadAll(lr)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-28 20:15:47 +00:00
|
|
|
return err
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2019-12-28 20:15:47 +00:00
|
|
|
if len(data) == n+1 {
|
|
|
|
return fmt.Errorf("content larger than max read allowance: %d", n)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2019-12-28 20:15:47 +00:00
|
|
|
return json.Unmarshal(data, thing)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
func loginOrFail(store sessions.Store, w http.ResponseWriter, r *http.Request, user *User) error {
|
|
|
|
// An error may be returned, but a valid session should always be returned.
|
|
|
|
session, _ := store.Get(r, cookieName)
|
2019-12-19 16:48:04 +00:00
|
|
|
session.Values[cookieUserVal] = user.Cookie()
|
2019-12-23 19:30:32 +00:00
|
|
|
if err := session.Save(r, w); err != nil {
|
|
|
|
fmt.Println("error saving session", err)
|
2019-12-19 16:48:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
|
|
return nil
|
|
|
|
}
|