2019-12-19 16:48:04 +00:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-02 21:29:23 +00:00
|
|
|
"github.com/writeas/impart"
|
2019-12-27 18:35:48 +00:00
|
|
|
"github.com/writeas/nerds/store"
|
2019-12-19 16:48:04 +00:00
|
|
|
"github.com/writeas/writefreely/config"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MockOAuthDatastoreProvider struct {
|
|
|
|
DoDB func() OAuthDatastore
|
|
|
|
DoConfig func() *config.Config
|
|
|
|
DoSessionStore func() sessions.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockOAuthDatastore struct {
|
2020-04-20 22:18:23 +00:00
|
|
|
DoGenerateOAuthState func(context.Context, string, string, int64, string) (string, error)
|
|
|
|
DoValidateOAuthState func(context.Context, string) (string, string, int64, string, error)
|
2019-12-30 18:32:06 +00:00
|
|
|
DoGetIDForRemoteUser func(context.Context, string, string, string) (int64, error)
|
2019-12-19 16:48:04 +00:00
|
|
|
DoCreateUser func(*config.Config, *User, string) error
|
2019-12-30 18:32:06 +00:00
|
|
|
DoRecordRemoteUserID func(context.Context, int64, string, string, string, string) error
|
2020-01-03 16:31:38 +00:00
|
|
|
DoGetUserByID func(int64) (*User, error)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 20:15:47 +00:00
|
|
|
var _ OAuthDatastore = &MockOAuthDatastore{}
|
|
|
|
|
2019-12-19 16:48:04 +00:00
|
|
|
type StringReadCloser struct {
|
|
|
|
*strings.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (src *StringReadCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockHTTPClient struct {
|
|
|
|
DoDo func(req *http.Request) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) {
|
|
|
|
if m.DoDo != nil {
|
|
|
|
return m.DoDo(req)
|
|
|
|
}
|
|
|
|
return &http.Response{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockOAuthDatastoreProvider) SessionStore() sessions.Store {
|
|
|
|
if m.DoSessionStore != nil {
|
|
|
|
return m.DoSessionStore()
|
|
|
|
}
|
|
|
|
return sessions.NewCookieStore([]byte("secret-key"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockOAuthDatastoreProvider) DB() OAuthDatastore {
|
|
|
|
if m.DoDB != nil {
|
|
|
|
return m.DoDB()
|
|
|
|
}
|
|
|
|
return &MockOAuthDatastore{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockOAuthDatastoreProvider) Config() *config.Config {
|
|
|
|
if m.DoConfig != nil {
|
|
|
|
return m.DoConfig()
|
|
|
|
}
|
|
|
|
cfg := config.New()
|
|
|
|
cfg.UseSQLite(true)
|
2019-12-28 20:15:47 +00:00
|
|
|
cfg.WriteAsOauth = config.WriteAsOauthCfg{
|
|
|
|
ClientID: "development",
|
|
|
|
ClientSecret: "development",
|
|
|
|
AuthLocation: "https://write.as/oauth/login",
|
|
|
|
TokenLocation: "https://write.as/oauth/token",
|
|
|
|
InspectLocation: "https://write.as/oauth/inspect",
|
|
|
|
}
|
|
|
|
cfg.SlackOauth = config.SlackOauthCfg{
|
|
|
|
ClientID: "development",
|
|
|
|
ClientSecret: "development",
|
|
|
|
TeamID: "development",
|
|
|
|
}
|
2019-12-19 16:48:04 +00:00
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:18:23 +00:00
|
|
|
func (m *MockOAuthDatastore) ValidateOAuthState(ctx context.Context, state string) (string, string, int64, string, error) {
|
2019-12-19 16:48:04 +00:00
|
|
|
if m.DoValidateOAuthState != nil {
|
|
|
|
return m.DoValidateOAuthState(ctx, state)
|
|
|
|
}
|
2020-04-20 22:18:23 +00:00
|
|
|
return "", "", 0, "", nil
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 18:32:06 +00:00
|
|
|
func (m *MockOAuthDatastore) GetIDForRemoteUser(ctx context.Context, remoteUserID, provider, clientID string) (int64, error) {
|
2019-12-19 16:48:04 +00:00
|
|
|
if m.DoGetIDForRemoteUser != nil {
|
2019-12-30 18:32:06 +00:00
|
|
|
return m.DoGetIDForRemoteUser(ctx, remoteUserID, provider, clientID)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockOAuthDatastore) CreateUser(cfg *config.Config, u *User, username string) error {
|
|
|
|
if m.DoCreateUser != nil {
|
|
|
|
return m.DoCreateUser(cfg, u, username)
|
|
|
|
}
|
|
|
|
u.ID = 1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-30 18:32:06 +00:00
|
|
|
func (m *MockOAuthDatastore) RecordRemoteUserID(ctx context.Context, localUserID int64, remoteUserID, provider, clientID, accessToken string) error {
|
2019-12-19 16:48:04 +00:00
|
|
|
if m.DoRecordRemoteUserID != nil {
|
2019-12-30 18:32:06 +00:00
|
|
|
return m.DoRecordRemoteUserID(ctx, localUserID, remoteUserID, provider, clientID, accessToken)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-03 16:31:38 +00:00
|
|
|
func (m *MockOAuthDatastore) GetUserByID(userID int64) (*User, error) {
|
|
|
|
if m.DoGetUserByID != nil {
|
|
|
|
return m.DoGetUserByID(userID)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2020-04-21 11:31:23 +00:00
|
|
|
user := &User{}
|
2019-12-19 16:48:04 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:18:23 +00:00
|
|
|
func (m *MockOAuthDatastore) GenerateOAuthState(ctx context.Context, provider string, clientID string, attachUserID int64, inviteCode string) (string, error) {
|
2019-12-19 16:48:04 +00:00
|
|
|
if m.DoGenerateOAuthState != nil {
|
2020-04-20 22:18:23 +00:00
|
|
|
return m.DoGenerateOAuthState(ctx, provider, clientID, attachUserID, inviteCode)
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
2019-12-27 18:35:48 +00:00
|
|
|
return store.Generate62RandomString(14), nil
|
2019-12-19 16:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestViewOauthInit(t *testing.T) {
|
2019-12-23 19:30:32 +00:00
|
|
|
|
2019-12-19 16:48:04 +00:00
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
app := &MockOAuthDatastoreProvider{}
|
2019-12-23 19:30:32 +00:00
|
|
|
h := oauthHandler{
|
2020-01-03 16:31:38 +00:00
|
|
|
Config: app.Config(),
|
|
|
|
DB: app.DB(),
|
|
|
|
Store: app.SessionStore(),
|
2020-01-03 16:28:06 +00:00
|
|
|
EmailKey: []byte{0xd, 0xe, 0xc, 0xa, 0xf, 0xf, 0xb, 0xa, 0xd},
|
2020-01-02 21:29:23 +00:00
|
|
|
oauthClient: writeAsOauthClient{
|
2019-12-28 20:15:47 +00:00
|
|
|
ClientID: app.Config().WriteAsOauth.ClientID,
|
|
|
|
ClientSecret: app.Config().WriteAsOauth.ClientSecret,
|
|
|
|
ExchangeLocation: app.Config().WriteAsOauth.TokenLocation,
|
|
|
|
InspectLocation: app.Config().WriteAsOauth.InspectLocation,
|
|
|
|
AuthLocation: app.Config().WriteAsOauth.AuthLocation,
|
|
|
|
CallbackLocation: "http://localhost/oauth/callback",
|
|
|
|
HttpClient: nil,
|
|
|
|
},
|
2019-12-23 19:30:32 +00:00
|
|
|
}
|
2019-12-19 16:48:04 +00:00
|
|
|
req, err := http.NewRequest("GET", "/oauth/client", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr := httptest.NewRecorder()
|
2020-01-02 21:29:23 +00:00
|
|
|
err = h.viewOauthInit(nil, rr, req)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
httpErr, ok := err.(impart.HTTPError)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, httpErr.Status)
|
|
|
|
assert.NotEmpty(t, httpErr.Message)
|
|
|
|
locURI, err := url.Parse(httpErr.Message)
|
2019-12-19 16:48:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "/oauth/login", locURI.Path)
|
|
|
|
assert.Equal(t, "development", locURI.Query().Get("client_id"))
|
|
|
|
assert.Equal(t, "http://localhost/oauth/callback", locURI.Query().Get("redirect_uri"))
|
|
|
|
assert.Equal(t, "code", locURI.Query().Get("response_type"))
|
|
|
|
assert.NotEmpty(t, locURI.Query().Get("state"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("state failure", func(t *testing.T) {
|
|
|
|
app := &MockOAuthDatastoreProvider{
|
|
|
|
DoDB: func() OAuthDatastore {
|
|
|
|
return &MockOAuthDatastore{
|
2020-04-20 22:18:23 +00:00
|
|
|
DoGenerateOAuthState: func(ctx context.Context, provider, clientID string, attachUserID int64, inviteCode string) (string, error) {
|
2019-12-19 16:48:04 +00:00
|
|
|
return "", fmt.Errorf("pretend unable to write state error")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2019-12-23 19:30:32 +00:00
|
|
|
h := oauthHandler{
|
2020-01-03 16:31:38 +00:00
|
|
|
Config: app.Config(),
|
|
|
|
DB: app.DB(),
|
|
|
|
Store: app.SessionStore(),
|
2020-01-03 16:28:06 +00:00
|
|
|
EmailKey: []byte{0xd, 0xe, 0xc, 0xa, 0xf, 0xf, 0xb, 0xa, 0xd},
|
2020-01-02 21:29:23 +00:00
|
|
|
oauthClient: writeAsOauthClient{
|
2019-12-28 20:15:47 +00:00
|
|
|
ClientID: app.Config().WriteAsOauth.ClientID,
|
|
|
|
ClientSecret: app.Config().WriteAsOauth.ClientSecret,
|
|
|
|
ExchangeLocation: app.Config().WriteAsOauth.TokenLocation,
|
|
|
|
InspectLocation: app.Config().WriteAsOauth.InspectLocation,
|
|
|
|
AuthLocation: app.Config().WriteAsOauth.AuthLocation,
|
|
|
|
CallbackLocation: "http://localhost/oauth/callback",
|
|
|
|
HttpClient: nil,
|
|
|
|
},
|
2019-12-23 19:30:32 +00:00
|
|
|
}
|
2019-12-19 16:48:04 +00:00
|
|
|
req, err := http.NewRequest("GET", "/oauth/client", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr := httptest.NewRecorder()
|
2020-01-02 21:29:23 +00:00
|
|
|
err = h.viewOauthInit(nil, rr, req)
|
|
|
|
httpErr, ok := err.(impart.HTTPError)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NotEmpty(t, httpErr.Message)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, httpErr.Status)
|
|
|
|
assert.Equal(t, "could not prepare oauth redirect url", httpErr.Message)
|
2019-12-19 16:48:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestViewOauthCallback(t *testing.T) {
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
app := &MockOAuthDatastoreProvider{}
|
|
|
|
h := oauthHandler{
|
2020-01-03 16:31:38 +00:00
|
|
|
Config: app.Config(),
|
|
|
|
DB: app.DB(),
|
|
|
|
Store: app.SessionStore(),
|
2020-01-03 16:28:06 +00:00
|
|
|
EmailKey: []byte{0xd, 0xe, 0xc, 0xa, 0xf, 0xf, 0xb, 0xa, 0xd},
|
2019-12-28 20:15:47 +00:00
|
|
|
oauthClient: writeAsOauthClient{
|
|
|
|
ClientID: app.Config().WriteAsOauth.ClientID,
|
|
|
|
ClientSecret: app.Config().WriteAsOauth.ClientSecret,
|
|
|
|
ExchangeLocation: app.Config().WriteAsOauth.TokenLocation,
|
|
|
|
InspectLocation: app.Config().WriteAsOauth.InspectLocation,
|
|
|
|
AuthLocation: app.Config().WriteAsOauth.AuthLocation,
|
|
|
|
CallbackLocation: "http://localhost/oauth/callback",
|
|
|
|
HttpClient: &MockHTTPClient{
|
|
|
|
DoDo: func(req *http.Request) (*http.Response, error) {
|
|
|
|
switch req.URL.String() {
|
|
|
|
case "https://write.as/oauth/token":
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: &StringReadCloser{strings.NewReader(`{"access_token": "access_token", "expires_in": 1000, "refresh_token": "refresh_token", "token_type": "access"}`)},
|
|
|
|
}, nil
|
|
|
|
case "https://write.as/oauth/inspect":
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: &StringReadCloser{strings.NewReader(`{"client_id": "development", "user_id": "1", "expires_at": "2019-12-19T11:42:01Z", "username": "nick", "email": "nick@testing.write.as"}`)},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:48:04 +00:00
|
|
|
return &http.Response{
|
2019-12-28 20:15:47 +00:00
|
|
|
StatusCode: http.StatusNotFound,
|
2019-12-19 16:48:04 +00:00
|
|
|
}, nil
|
2019-12-28 20:15:47 +00:00
|
|
|
},
|
2019-12-19 16:48:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", "/oauth/callback", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rr := httptest.NewRecorder()
|
2020-09-02 13:33:16 +00:00
|
|
|
err = h.viewOauthCallback(&App{cfg: app.Config(), sessionStore: app.SessionStore()}, rr, req)
|
2019-12-19 16:48:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, rr.Code)
|
|
|
|
})
|
|
|
|
}
|