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-19 16:48:04 +00:00
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"github.com/guregu/null/zero"
|
2019-12-27 18:35:48 +00:00
|
|
|
"github.com/writeas/nerds/store"
|
2019-12-19 16:48:04 +00:00
|
|
|
"github.com/writeas/web-core/auth"
|
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
"github.com/writeas/writefreely/config"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// InspectResponse contains data returned when an access token is inspected.
|
|
|
|
type InspectResponse struct {
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
UserID int64 `json:"user_id"`
|
|
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
GenerateOAuthState(context.Context) (string, error)
|
|
|
|
ValidateOAuthState(context.Context, string) error
|
|
|
|
GetIDForRemoteUser(context.Context, int64) (int64, error)
|
|
|
|
CreateUser(*config.Config, *User, string) error
|
|
|
|
RecordRemoteUserID(context.Context, int64, int64) error
|
|
|
|
GetUserForAuthByID(int64) (*User, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type HttpClient interface {
|
|
|
|
Do(req *http.Request) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type oauthHandler struct {
|
2019-12-23 19:30:32 +00:00
|
|
|
Config *config.Config
|
|
|
|
DB OAuthDatastore
|
|
|
|
Store sessions.Store
|
2019-12-19 16:48:04 +00:00
|
|
|
HttpClient HttpClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildAuthURL returns a URL used to initiate authentication.
|
2019-12-23 19:30:32 +00:00
|
|
|
func buildAuthURL(db OAuthDatastore, ctx context.Context, clientID, authLocation, callbackURL string) (string, error) {
|
|
|
|
state, err := db.GenerateOAuthState(ctx)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(authLocation)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
q := u.Query()
|
|
|
|
q.Set("client_id", clientID)
|
|
|
|
q.Set("redirect_uri", callbackURL)
|
|
|
|
q.Set("response_type", "code")
|
|
|
|
q.Set("state", state)
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
// app *App, w http.ResponseWriter, r *http.Request
|
|
|
|
func (h oauthHandler) viewOauthInit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
location, err := buildAuthURL(h.DB, r.Context(), h.Config.App.OAuthClientID, h.Config.App.OAuthProviderAuthLocation, h.Config.App.OAuthClientCallbackLocation)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-23 19:30:32 +00:00
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, "could not prepare oauth redirect url")
|
|
|
|
return
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
http.Redirect(w, r, location, http.StatusTemporaryRedirect)
|
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
func (h oauthHandler) viewOauthCallback(w http.ResponseWriter, r *http.Request) {
|
2019-12-19 16:48:04 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
code := r.FormValue("code")
|
|
|
|
state := r.FormValue("state")
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
err := h.DB.ValidateOAuthState(ctx, state)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-23 19:30:32 +00:00
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
tokenResponse, err := h.exchangeOauthCode(ctx, code)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-23 19:30:32 +00:00
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
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-23 19:30:32 +00:00
|
|
|
tokenInfo, err := h.inspectOauthAccessToken(ctx, tokenResponse.AccessToken)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-23 19:30:32 +00:00
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
localUserID, err := h.DB.GetIDForRemoteUser(ctx, tokenInfo.UserID)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-23 19:30:32 +00:00
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
fmt.Println("local user id", localUserID)
|
|
|
|
|
2019-12-19 16:48:04 +00:00
|
|
|
if localUserID == -1 {
|
|
|
|
// We don't have, nor do we want, the password from the origin, so we
|
|
|
|
//create a random string. If the user needs to set a password, they
|
|
|
|
//can do so through the settings page or through the password reset
|
|
|
|
//flow.
|
2019-12-27 18:35:48 +00:00
|
|
|
randPass := store.Generate62RandomString(14)
|
2019-12-19 16:48:04 +00:00
|
|
|
hashedPass, err := auth.HashPass([]byte(randPass))
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog.Println(err)
|
2019-12-23 19:30:32 +00:00
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, "unable to create password hash")
|
|
|
|
return
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
newUser := &User{
|
|
|
|
Username: tokenInfo.Username,
|
|
|
|
HashedPass: hashedPass,
|
|
|
|
HasPass: true,
|
|
|
|
Email: zero.NewString("", tokenInfo.Email != ""),
|
|
|
|
Created: time.Now().Truncate(time.Second).UTC(),
|
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
err = h.DB.CreateUser(h.Config, newUser, newUser.Username)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-23 19:30:32 +00:00
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
err = h.DB.RecordRemoteUserID(ctx, newUser.ID, tokenInfo.UserID)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-23 19:30:32 +00:00
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
if err := loginOrFail(h.Store, w, r, newUser); err != nil {
|
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
return
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
user, err := h.DB.GetUserForAuthByID(localUserID)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
2019-12-23 19:30:32 +00:00
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = loginOrFail(h.Store, w, r, user); err != nil {
|
|
|
|
failOAuthRequest(w, http.StatusInternalServerError, err.Error())
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
func (h oauthHandler) exchangeOauthCode(ctx context.Context, code string) (*TokenResponse, error) {
|
2019-12-19 16:48:04 +00:00
|
|
|
form := url.Values{}
|
|
|
|
form.Add("grant_type", "authorization_code")
|
2019-12-23 19:30:32 +00:00
|
|
|
form.Add("redirect_uri", h.Config.App.OAuthClientCallbackLocation)
|
2019-12-19 16:48:04 +00:00
|
|
|
form.Add("code", code)
|
2019-12-23 19:30:32 +00:00
|
|
|
req, err := http.NewRequest("POST", h.Config.App.OAuthProviderTokenLocation, strings.NewReader(form.Encode()))
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.WithContext(ctx)
|
|
|
|
req.Header.Set("User-Agent", "writefreely")
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2019-12-23 19:30:32 +00:00
|
|
|
req.SetBasicAuth(h.Config.App.OAuthClientID, h.Config.App.OAuthClientSecret)
|
2019-12-19 16:48:04 +00:00
|
|
|
|
|
|
|
resp, err := h.HttpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nick: I like using limited readers to reduce the risk of an endpoint
|
|
|
|
// being broken or compromised.
|
|
|
|
lr := io.LimitReader(resp.Body, tokenRequestMaxLen)
|
|
|
|
body, err := ioutil.ReadAll(lr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokenResponse TokenResponse
|
|
|
|
err = json.Unmarshal(body, &tokenResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tokenResponse, nil
|
|
|
|
}
|
|
|
|
|
2019-12-23 19:30:32 +00:00
|
|
|
func (h oauthHandler) inspectOauthAccessToken(ctx context.Context, accessToken string) (*InspectResponse, error) {
|
|
|
|
req, err := http.NewRequest("GET", h.Config.App.OAuthProviderInspectLocation, nil)
|
2019-12-19 16:48:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.WithContext(ctx)
|
|
|
|
req.Header.Set("User-Agent", "writefreely")
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
|
|
|
|
|
|
resp, err := h.HttpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nick: I like using limited readers to reduce the risk of an endpoint
|
|
|
|
// being broken or compromised.
|
|
|
|
lr := io.LimitReader(resp.Body, infoRequestMaxLen)
|
|
|
|
body, err := ioutil.ReadAll(lr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var inspectResponse InspectResponse
|
|
|
|
err = json.Unmarshal(body, &inspectResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &inspectResponse, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-12-23 19:30:32 +00:00
|
|
|
|
|
|
|
// failOAuthRequest is an HTTP handler helper that formats returned error
|
|
|
|
// messages.
|
|
|
|
func failOAuthRequest(w http.ResponseWriter, statusCode int, message string) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
err := json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
"error": message,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|